[Tex/LaTex] Create bitmap within TeX file

barcodesbitmapluatexpdftexpixelated

I'd like to create a bitmap (black and white) from within LuaTeX (or pdfTeX) and rescale it afterwards. For example (a 4×4 bitmap) could be made by

\makebitmap{0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0}

I don't really care about the interface. More important for me is the overall approach.

I would like to use it in LuaTeX, thus img.stream could be useful for me. But a PDFTeX approach with \pdf... commands would be fine, too.

Important restrictions: I must not create any intermediate file and it must work offline.

To make things clear: I am only interested in how to generate a bitmap graphics (png, bmp, whatever) within pdf/LuaTeX.

Background: I've developed a qrcode encoding lua library and I'd like make a LuaLaTeX package for it. I see three possible approaches:

  1. Use pdf instructions (0 0 10 10 re f for example)
  2. Use \rule commands to make the black parts
  3. Create a bitmap "file" and scale it.

I still experiment about the best approach.

I have tried with pdf instructions (item 1 above), and the result is ugly in some pdf viewer applications:

sample qrcode

The black boxes are divided by some small white lines in some zoom settings. I want to avoid them. That is why I want to create bitmaps instead of pdf instructions.

Best Answer

For this you need to create and add a PDF object (using \pdfobj I guess; see the pdftex manual) into the PDF which holds the (binary?) representation of the image and then reference this object where you like the image being shown. This is what \includegraphics does for PDF output.

Accroding to he PDF reference (31MB!), section 3.3.6 JBIG2Decode Filter the JBIG2 format was especially created for monochrome images like this. The best thing here is that it object stream can be given in ASCII, avoiding the need to handle binary material in TeX.

The following example is given there. I don't think there should be an issue adding this to a PDF using \pdfobj. You "only" need to produce the JBIG2 encoding of your image, which should be possible using Lua.

5 0 obj
<< /Type /XObject
/Subtype /Image
/Width 52
/Height 66
/ColorSpace /DeviceGray
/BitsPerComponent 1
/Length 224
/Filter [ /ASCIIHexDecode /JBIG2Decode ]
/DecodeParms [ null << /JBIG2Globals 6 0 R >> ]
>>
stream
000000013000010000001300000034000000420000000000
00000040000000000002062000010000001e000000340000
004200000000000000000200100000000231db51ce51ffac >
endstream
endobj
6 0 obj
<< /Length 126
/Filter /ASCIIHexDecode
>>
stream
0000000000010000000032000003fffdff02fefefe000000
01000000012ae225aea9a5a538b4d9999c5c8e56ef0f872
7f2b53d4e37ef795cc5506dffac >
endstream
endobj

I will try to create a LaTeX example file showing the principle.

So far I only figured out how to create the objects, but not how to display the image in the document.

\documentclass{article}

\pdfcompresslevel=0
\pdfobjcompresslevel=0

\begin{document}

\leavevmode\fbox{%

\pdfobj stream attr 
{
/Filter /ASCIIHexDecode
} {
0000000000010000000032000003fffdff02fefefe000000
01000000012ae225aea9a5a538b4d9999c5c8e56ef0f872
7f2b53d4e37ef795cc5506dffac
}%
%\pdfrefobj\pdflastobj
\pdfobj stream attr {
    /Type /XObject
/Subtype /Image
/Width 52
/Height 66
/ColorSpace /DeviceGray
/BitsPerComponent 1
%/Length 224
/Filter [ /ASCIIHexDecode /JBIG2Decode ]
/DecodeParms [ null << /JBIG2Globals \the\pdflastobj\space 0 R >> ]
} {
000000013000010000001300000034000000420000000000
00000040000000000002062000010000001e000000340000
004200000000000000000200100000000231db51ce51ffac
}%
\pdfrefobj\pdflastobj
}

\end{document}
Related Question