Matthew Savoie <savoie@fsl.noaa.gov> writes:
> Hi, I'm having a slight problem writing Color JPEGS in IDL. I looked at the
> FAQ, but it doesn't have any direct examples.
> Here's a sample program that I ran to generate gif and jpeg output. The
> problem is that the only reasonable jpeg I can generate is when I don't use
> the TRUE keyword, but that only seems to generate grey images.
> Does anyone out there know what I'm doing wrong? Can you send me an example
> that works?
> [ some pretty reasonable code snipped here]
Here is an example that works. Given a 2D image variable,
it creates a color JPEG image. The important details are that I load
the color table and get the color vectors in the Z-buffer. This makes
sure I have vectors that are 256 elements in length.
To make a color JPEG image, you need a 3D image array. (Another
way of saying this is that you need a true-color image.) What I
do to obtain this, is use the red, green, and blue color vectors
as color lookup tables (e.g., image3d(0,*,*) = r(image)). This
gives me a red, green, and blue representation of the original
image.
To show you what this looks like (on an 8-bit display), I use
the COLOR_QUAN function to obtain an 8-bit image and a color
table to load.
I think the code is pretty self-explanatory, but let me know if you
have further questions.
Yours,
David
*******************************************
PRO MAKE_JPEG, image
ON_ERROR, 1
; Check data parameter. Define default if necessary.
IF N_PARAMS() EQ 0 THEN image = BYTSCL(DIST(300, 300))
s = SIZE(image)
IF s(0) NE 2 THEN MESSAGE, 'Must pass 2D image data.'
; Go into Z-Buffer for 256 colors.
oldDevice = !D.NAME
SET_PLOT, 'Z'
; Load colors and obtain the color vectors.
LOADCT, 13
TVLCT, r, g, b, /GET
; Back to current device.
SET_PLOT, oldDevice
; Create a pixel interleaved image array.
image3d = BYTARR(3, s(1), s(2))
; Make red, green, and blue images from the lookup vectors.
image3d(0, *, *) = r(image)
image3d(1, *, *) = g(image)
image3d(2, *, *) = b(image)
; Write the color JPEG image.
WRITE_JPEG, 'image.jpg', image3d, TRUE=1, QUALITY=100
; To see this on an 8-bit display.
WINDOW, XSIZE=s(1), YSIZE=s(2), /FREE
TV, COLOR_QUAN(image3d, 1, red, green, blue)
red = CONGRID(red, !D. N_COLORS)
green = CONGRID(green, !D. N_COLORS)
blue = CONGRID(blue, !D. N_COLORS)
TVLCT, red, green, blue
END
--
David Fanning, Ph.D.
Phone: 970-221-0438
Fax: 970-221-4728
E-Mail: davidf@fortnet.org
|