%D \module
%D   [       file=grph-raw,
%D        version=2006.08.26, % overhaul of 1997.03.31
%D          title=\CONTEXT\ Graphic Macros,
%D       subtitle=Raw Bitmaps,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{ConTeXt Graphic Macros / Raw Bitmaps}

%D \startluacode
%D     function document.TestBitmap(nx,ny)
%D         local random = math.random
%D         local maxbit = 2^24
%D         for i=1,nx do
%D             for i=1,ny do
%D                 context("%06x",random(0,maxbit))
%D             end
%D         end
%D     end
%D \stopluacode
%D
%D \def\TestBitmap#1#2{\ctxlua{document.TestBitmap(#1,#2)}}
%D
%D \blank
%D
%D \startMPcode
%D     draw textext("\bitmapimage[x=100,y=100]{\TestBitmap{100}{100}}") xsized 10cm ;
%D \stopMPcode
%D
%D \blank
%D
%D \startMPcode
%D     draw textext("\bitmapimage[x=200,y=50]{\TestBitmap{50}{200}}") xsized 10cm ;
%D \stopMPcode

\registerctxluafile{grph-raw}{autosuffix}

\unprotect

\permanent\protected\def\bitmapimage[#S#1]#2%
  {\hbox\bgroup
     \getdummyparameters[\c!color=rgb,\c!width=,\c!height=,\c!x=,\c!y=,#1]%
     \clf_bitmapimage
       data        {#2}%
       colorspace  {\directdummyparameter\c!color}%
       %
       \ifchkdimexpr\directdummyparameter\c!width \or width       \lastchkdimension \fi
       \ifchkdimexpr\directdummyparameter\c!height\or height      \lastchkdimension \fi
       \ifchknumexpr\directdummyparameter\c!x     \or xresolution \lastchknumber    \fi
       \ifchknumexpr\directdummyparameter\c!y     \or yresolution \lastchknumber    \fi
     \relax
   \egroup}

\permanent\protected\def\startbitmapimage[#S#1]#2\stopbitmapimage % not that efficient but seldom used
  {\bitmapimage[#1]{#2}}

\permanent\protected\lettonothing\stopbitmapimage

%D Built in:
%D
%D \starttyping
%D \startTEXpage
%D   \qrcode[width=8cm,buffer=foo]
%D   \qrcode[width=8cm,text={a test text},,color=darkgray]
%D \stopTEXpage
%D \stoptyping
%D
%D \starttyping
%D \startTEXpage
%D     \startluacode
%D         context(figures.qrcode("this also works","darkgray",tex.sp("4cm")))
%D     \stopluacode
%D \stopTEXpage
%D \stoptyping

\permanent\protected\def\qrcode[#1]%
  {\bgroup
     \getdummyparameters
       [\c!text=,\c!buffer=,\c!width=4cm,#1]%
     \ifempty{\dummyparameter\c!buffer}%
        \clf_qrcodestring{\dummyparameter\c!text}%
     \else
        \clf_qrcodebuffer{\dummyparameter\c!buffer}%
     \fi
     {\dummyparameter\c!color}%
     \dummyparameter\c!width
     \relax
   \egroup}

\protect \endinput
