Graphics, Figures & Tables ⇒ Hardcoded RGB images?
Hardcoded RGB images?
I'm writing a program which generates LaTeX output as a ".tex" output file. Apart from text and math, the program also generates RGB raster images, which need to be displayed in the output document as figures.
I know how to insert an EPS image into a LaTeX file, but this would involve to also create an EPS file for each generated image, and I'd like to avoid that.
Is there any way of outputting an array of color pixels, hardcoded in LaTeX? I'm using the PSTricks package, but I didn't find any graphics primitive suitable for an array of pixels.
Is there any other package supporting arrays of pixels?
Thanks!
NEW: TikZ book now 40% off at Amazon.com for a short time.
And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p
- Johannes_B
- Site Moderator
- Posts: 4182
- Joined: Thu Nov 01, 2012 4:08 pm
Re: Hardcoded RGB images?
-
- Posts: 34
- Joined: Sun Oct 16, 2011 5:56 pm
Hardcoded RGB images?
Without "hardcoded in LaTeX", I'd say you should generate PNG images and use pdflatex as outlined by Johannes_B.
But if you really want to hardcode an image in LaTeX, you could make use of pgfplots.
An image is actually a "matrix of color pixels", and matrizes can be visualized as surface plots. That what pgfplots can do: you have a matrix, each point in the matrix has a color, and pgfplots visualizes+interpolates colors in-between. You would write your "matrix of color pixels" in the form
X Y COLOR
where X and Y are mandatory and COLOR is an RGB triple or (optionally) a named LaTeX color, or (optionally) some other color space which will be converted to RGB.
Here is an example:
Code: Select all
\documentclass{standalone}\usepackage{pgfplots}\usepgfplotslibrary{patchplots}\pgfplotsset{compat=1.9}\begin{document}\begin{tikzpicture}\begin{axis}[% perhaps we want to see the axis?%hide axis,%% we want% 0% 1% 2% 3% 4% rather than% 4% 3% 2% 1% 0% -> reverse y dir:y dir=reverse,%% make sure that unit Y = unit X:axis equal image,]\addplot[% characterize the input matrix (could be detected% automatically if we introduced empty lines after each row):%% "row" = Ymesh/rows=5,%% configure output: we want to interpolate between pixels:surf,shader=interp,
In this case, I used a data table, coded inline into LaTeX. The table has 4*5 lines (plus one header line). The header line is "X Y COLOR\\". The "\\" has been configured as line terminator which allows us to concatenate the lines in matrix form. A line of the form "0 1 color=red\\" means X=0, Y=1 with red color. The syntax "color=red" consists of a Pgfplots keyword "color" followed by a LaTeX (xcolor) name "red". A line of the form "1 4 1,0,1\\" means X=1, Y=4, and the RGB color defined by 100%, 0%, 100%.
The rest is configuration of the axis. Uncomment "hide axis" to eliminate the axis (but I suppose it shows how to interprete my listing).
** DISCLAIMER **
A am author of the package pgfplots.
** REMARK **
It might be that there is a similar solution in pstricks, you may want to wait for a pstricks answer before you accept mine.
Re: Hardcoded RGB images?
And now I need to also be able to create figures from photos, which are stored in standard image files (where "standard" means tiff, jpeg, tga, png, bmp, etc...). The first problem is that I cannot use pdflatex because I really need pstricks. The second problem is that if I go the EPS way, this needs an intermediate step (converting to EPS) which introduces undesired side effects (more processing time, need to check if the EPS files are synced to the original ones or need to be regenerated, and extra disk usage because EPS files tend to be huge).
A third problem if I go the EPS way is that I don't know how to create a pspicture whose size is exactly the same size of the EPS it encloses. I mean, my way of creating image figures would be: "insert this image file so that its width is 8cm, the height keeps aspect ratio, and its then enclosed in a pspicture of exactly the same size, so that a proper caption can be added". I know how to include the graphic with a given width, but I don't know how to tell the pspicture environment to resize to fit it exactly.
If I could avoid the EPS way, my program can read most image files (except EPS), so it can know the pixel size (width and height) of the included image, and it can then generate a pspicture with the proper size. But if the image is EPS, my program cannot read it nor guess its pixel size, so the pspicture must be able to automatically resize to its content.
Finally I thought what I posted here: why not read the image file from my program (thus avoiding the conversion to EPS and its undesired side effects), so it would be able to generate the pspicture of the right size, and then saving the pixels in a brute force way in the generated LaTeX?
But this final thought is beginning to scare me a little... maybe the PDF generated from this LaTeX won't have the images in the same way as normal PDF files, so maybe they won't display in a optimal way/performance on some viewers...
I think I need to keep thinking...