[Tex/LaTex] Embedding images with an encoding algorithm

embeddinggraphics

Friends, sometimes I had to create a self-contained PHP script. That means the PHP script could have any files within it due to some encoding/decoding algorithm, like Base64. For example, this code is a Base64 encoding of this tiny image: Brazil

Of course, in this particular case, PHP can handle that huge encoded string because it has a bundled base64_decode() function.

I was wondering if this idea is possible to achieve somehow in TeX/LaTeX. I need to point out that I'm just curious on this subject, not that I want to embed whatever in code. Besides, as you noted, the encoded string is huge, so that'd surely make people cry blood.

One possible solution I see is if somebody implement an encoding/decoding algorithm in TeX/LaTeX, like \base64_decode, so it would be possible to use something like this: \includegraphics{\base64_decode{\myhugeunreadablestring}}. Sorry for the acid trip ticket, but I'm really curious on how this insane code would work. =)

Best Answer

AFAIK it is not possible to make TeX write binary content, so even if you would implement a base64 decode function you couldn't write it to an external file or e.g. into a \pdfobj (PDF object) directly. However, it would be possible if you have the text based base64 code in an environment (e.g. filecontents, see also the filecontents package which extends this environment) which writes it to a file where a command line tool turns it to the binary image which is then included as normal. You could call the command line tool using \write18 if -shell-escape is active or use an external script or Makefile to do this.

Related Question