Graphics, Figures & TablesHardcoded RGB images?

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
sonny
Posts: 13
Joined: Wed Mar 25, 2009 11:14 pm

Hardcoded RGB images?

Post by sonny »

Hi,

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!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Re: Hardcoded RGB images?

Post by Johannes_B »

pdfLaTeX can work with png, jpg and pdf-files. If you rely on pstricks, using XeLaTeX can be very helpful.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
feuersaenger
Posts: 34
Joined: Sun Oct 16, 2011 5:56 pm

Hardcoded RGB images?

Post by feuersaenger »

You say that you have an "array of color pixels, hardcoded in LaTeX"?

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" = Y
		mesh/rows=5,
		%
		% configure output: we want to interpolate between pixels:
		surf,shader=interp,
		% patch type=bilinear has high quality, but needs more space in the PDF and more time in the viewer.
		% patch type=rectangle (default) has low quality but is highly
		% compact. Works well for many samples.
		patch type=bilinear,
		%
		% ... and the colors are provided *explicitly* in the data
		% stream:
		mesh/color input=explicit,
		]
	table[row sep=\\,meta index=2] {
	X Y color\\
	0 0 color=red\\		 1 0 color=yellow\\ 2 0 color=green\\ 	3 0 color=red\\
	0 1 color=red\\		 1 1 color=red\\ 	2 1 color=red\\ 	3 1 color=red\\
	0 2 color=yellow\\	 1 2 color=red\\	2 2 color=black\\ 	3 2 color=red\\
	0 3 color=red\\		 1 3 1,1,1\\ 		2 3 0,0,1\\ 		3 3 1,0,0\\
	0 4 color=brown\\	 1 4 1,0,1\\ 		2 4 0,1,1\\		 	3 4 1,0,0\\
	};
\end{axis}
\end{tikzpicture}

\end{document}
P.png
P.png (29.88 KiB) Viewed 4648 times
Clearly, this 4x5 pixel graphics is unsuitable to illustrate your most-recent photograph. But it shows the basic idea.

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.
sonny
Posts: 13
Joined: Wed Mar 25, 2009 11:14 pm

Re: Hardcoded RGB images?

Post by sonny »

Thanks a lot. Your extension can be a solution, but I think I'm looking in a bad direction. My problem is that my program is a LaTeX generator, and then I use latex instead of pdflatex because the generated LaTeX has drawings made of pstricks primitives inside pspicture environments (each figure with a caption so a proper figure index can be generated in the table of contents).

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...
sonny
Posts: 13
Joined: Wed Mar 25, 2009 11:14 pm

Re: Hardcoded RGB images?

Post by sonny »

I think I've a good solution. The fact that I draw all figures with pstricks made me think I need a pspicture for everything, which isn't true. I just found images are better used with the standard figure environment, so you don't need to size it, just do an includegraphic inside a figure environment, and that's all. This solves everything for me, except the need for the conversion to EPS, but I think I can live with it.
Post Reply