2 * "$Id: image-jpeg.c,v 1.17 2002/12/17 18:59:26 swdev Exp $"
4 * JPEG image routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1993-2003 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636-3111 USA
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * ImageReadJPEG() - Read a JPEG image file.
32 * Include necessary headers...
38 # include <jpeglib.h> /* JPEG/JFIF image definitions */
42 * 'ImageReadJPEG()' - Read a JPEG image file.
45 int /* O - Read status */
46 ImageReadJPEG(image_t
*img
, /* IO - Image */
47 FILE *fp
, /* I - Image file */
48 int primary
, /* I - Primary choice for colorspace */
49 int secondary
, /* I - Secondary choice for colorspace */
50 int saturation
, /* I - Color saturation (%) */
51 int hue
, /* I - Color hue (degrees) */
52 const ib_t
*lut
) /* I - Lookup table for gamma/brightness */
54 struct jpeg_decompress_struct cinfo
; /* Decompressor info */
55 struct jpeg_error_mgr jerr
; /* Error handler info */
56 ib_t
*in
, /* Input pixels */
57 *out
; /* Output pixels */
59 /* Photoshop JPEG header */
60 int psjpeg
; /* Non-zero if Photoshop JPEG */
61 static const char *cspaces
[] =
62 { /* JPEG colorspaces... */
73 * Read the first 16 bytes to determine if this is a Photoshop JPEG file...
76 fread(header
, sizeof(header
), 1, fp
);
79 psjpeg
= memcmp(header
+ 6, "Photoshop ", 10) == 0;
82 * Read the JPEG header...
85 cinfo
.err
= jpeg_std_error(&jerr
);
86 jpeg_create_decompress(&cinfo
);
87 jpeg_stdio_src(&cinfo
, fp
);
88 jpeg_read_header(&cinfo
, 1);
90 cinfo
.quantize_colors
= 0;
92 fprintf(stderr
, "DEBUG: num_components = %d\n", cinfo
.num_components
);
93 fprintf(stderr
, "DEBUG: jpeg_color_space = %s\n",
94 cspaces
[cinfo
.jpeg_color_space
]);
96 if (cinfo
.num_components
== 1)
98 fputs("DEBUG: Converting image to grayscale...\n", stderr
);
100 cinfo
.out_color_space
= JCS_GRAYSCALE
;
101 cinfo
.out_color_components
= 1;
102 cinfo
.output_components
= 1;
104 img
->colorspace
= secondary
;
106 else if (cinfo
.num_components
== 4)
108 fputs("DEBUG: Converting image to CMYK...\n", stderr
);
110 cinfo
.out_color_space
= JCS_CMYK
;
111 cinfo
.out_color_components
= 4;
112 cinfo
.output_components
= 4;
114 img
->colorspace
= (primary
== IMAGE_RGB_CMYK
) ? IMAGE_CMYK
: primary
;
118 fputs("DEBUG: Converting image to RGB...\n", stderr
);
120 cinfo
.out_color_space
= JCS_RGB
;
121 cinfo
.out_color_components
= 3;
122 cinfo
.output_components
= 3;
124 img
->colorspace
= (primary
== IMAGE_RGB_CMYK
) ? IMAGE_RGB
: primary
;
127 jpeg_calc_output_dimensions(&cinfo
);
129 if (cinfo
.output_width
<= 0 || cinfo
.output_width
> IMAGE_MAX_WIDTH
||
130 cinfo
.output_height
<= 0 || cinfo
.output_height
> IMAGE_MAX_HEIGHT
)
132 fprintf(stderr
, "ERROR: Bad JPEG dimensions %dx%d!\n",
133 cinfo
.output_width
, cinfo
.output_height
);
135 jpeg_destroy_decompress(&cinfo
);
141 img
->xsize
= cinfo
.output_width
;
142 img
->ysize
= cinfo
.output_height
;
144 if (cinfo
.X_density
> 0 && cinfo
.Y_density
> 0 && cinfo
.density_unit
> 0)
146 if (cinfo
.density_unit
== 1)
148 img
->xppi
= cinfo
.X_density
;
149 img
->yppi
= cinfo
.Y_density
;
153 img
->xppi
= (int)((float)cinfo
.X_density
* 2.54);
154 img
->yppi
= (int)((float)cinfo
.Y_density
* 2.54);
157 if (img
->xppi
== 0 || img
->yppi
== 0)
159 fprintf(stderr
, "ERROR: Bad JPEG image resolution %dx%d PPI.\n",
160 img
->xppi
, img
->yppi
);
161 img
->xppi
= img
->yppi
= 128;
165 fprintf(stderr
, "DEBUG: JPEG image %dx%dx%d, %dx%d PPI\n",
166 img
->xsize
, img
->ysize
, cinfo
.output_components
,
167 img
->xppi
, img
->yppi
);
169 ImageSetMaxTiles(img
, 0);
171 in
= malloc(img
->xsize
* cinfo
.output_components
);
172 out
= malloc(img
->xsize
* ImageGetDepth(img
));
174 jpeg_start_decompress(&cinfo
);
176 while (cinfo
.output_scanline
< cinfo
.output_height
)
178 jpeg_read_scanlines(&cinfo
, (JSAMPROW
*)&in
, (JDIMENSION
)1);
180 if (psjpeg
&& cinfo
.output_components
== 4)
183 * Invert CMYK data from Photoshop...
186 ib_t
*ptr
; /* Pointer into buffer */
187 int i
; /* Looping var */
190 for (ptr
= in
, i
= img
->xsize
* 4; i
> 0; i
--, ptr
++)
194 if ((saturation
!= 100 || hue
!= 0) && cinfo
.output_components
== 3)
195 ImageRGBAdjust(in
, img
->xsize
, saturation
, hue
);
197 if ((img
->colorspace
== IMAGE_WHITE
&& cinfo
.out_color_space
== JCS_GRAYSCALE
) ||
198 (img
->colorspace
== IMAGE_RGB
&& cinfo
.out_color_space
== JCS_RGB
) ||
199 (img
->colorspace
== IMAGE_CMYK
&& cinfo
.out_color_space
== JCS_CMYK
))
206 fputs("DEBUG: Direct Data...\n", stderr
);
208 fputs("DEBUG:", stderr
);
210 for (i
= 0, ptr
= in
; i
< img
->xsize
; i
++)
213 for (j
= 0; j
< cinfo
.output_components
; j
++, ptr
++)
214 fprintf(stderr
, "%02X", *ptr
& 255);
221 ImageLut(in
, img
->xsize
* ImageGetDepth(img
), lut
);
223 ImagePutRow(img
, 0, cinfo
.output_scanline
- 1, img
->xsize
, in
);
225 else if (cinfo
.out_color_space
== JCS_GRAYSCALE
)
227 switch (img
->colorspace
)
230 ImageWhiteToBlack(in
, out
, img
->xsize
);
233 ImageWhiteToRGB(in
, out
, img
->xsize
);
236 ImageWhiteToCMY(in
, out
, img
->xsize
);
239 ImageWhiteToCMYK(in
, out
, img
->xsize
);
244 ImageLut(out
, img
->xsize
* ImageGetDepth(img
), lut
);
246 ImagePutRow(img
, 0, cinfo
.output_scanline
- 1, img
->xsize
, out
);
248 else if (cinfo
.out_color_space
== JCS_RGB
)
250 switch (img
->colorspace
)
253 ImageRGBToWhite(in
, out
, img
->xsize
);
256 ImageRGBToBlack(in
, out
, img
->xsize
);
259 ImageRGBToCMY(in
, out
, img
->xsize
);
262 ImageRGBToCMYK(in
, out
, img
->xsize
);
267 ImageLut(out
, img
->xsize
* ImageGetDepth(img
), lut
);
269 ImagePutRow(img
, 0, cinfo
.output_scanline
- 1, img
->xsize
, out
);
273 fputs("DEBUG: JCS_CMYK\n", stderr
);
275 switch (img
->colorspace
)
278 ImageCMYKToWhite(in
, out
, img
->xsize
);
281 ImageCMYKToBlack(in
, out
, img
->xsize
);
284 ImageCMYKToCMY(in
, out
, img
->xsize
);
287 ImageCMYKToRGB(in
, out
, img
->xsize
);
292 ImageLut(out
, img
->xsize
* ImageGetDepth(img
), lut
);
294 ImagePutRow(img
, 0, cinfo
.output_scanline
- 1, img
->xsize
, out
);
301 jpeg_finish_decompress(&cinfo
);
302 jpeg_destroy_decompress(&cinfo
);
310 #endif /* HAVE_LIBJPEG */
314 * End of "$Id: image-jpeg.c,v 1.17 2002/12/17 18:59:26 swdev Exp $".