2 * "$Id: image-jpeg.c 7355 2008-02-28 20:49:40Z mike $"
4 * JPEG image routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1993-2007 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * _cupsImageReadJPEG() - Read a JPEG image file.
23 * Include necessary headers...
26 #include "image-private.h"
29 # include <jpeglib.h> /* JPEG/JFIF image definitions */
33 * '_cupsImageReadJPEG()' - Read a JPEG image file.
36 int /* O - Read status */
38 cups_image_t
*img
, /* IO - cupsImage */
39 FILE *fp
, /* I - cupsImage file */
40 cups_icspace_t primary
, /* I - Primary choice for colorspace */
41 cups_icspace_t secondary
, /* I - Secondary choice for colorspace */
42 int saturation
, /* I - Color saturation (%) */
43 int hue
, /* I - Color hue (degrees) */
44 const cups_ib_t
*lut
) /* I - Lookup table for gamma/brightness */
46 struct jpeg_decompress_struct cinfo
; /* Decompressor info */
47 struct jpeg_error_mgr jerr
; /* Error handler info */
48 cups_ib_t
*in
, /* Input pixels */
49 *out
; /* Output pixels */
50 jpeg_saved_marker_ptr marker
; /* Pointer to marker data */
51 int psjpeg
= 0; /* Non-zero if Photoshop CMYK JPEG */
52 static const char *cspaces
[] =
53 { /* JPEG colorspaces... */
64 * Read the JPEG header...
67 cinfo
.err
= jpeg_std_error(&jerr
);
68 jpeg_create_decompress(&cinfo
);
69 jpeg_save_markers(&cinfo
, JPEG_APP0
+ 14, 0xffff); /* Adobe JPEG */
70 jpeg_stdio_src(&cinfo
, fp
);
71 jpeg_read_header(&cinfo
, 1);
74 * Parse any Adobe APPE data embedded in the JPEG file. Since Adobe doesn't
75 * bother following standards, we have to invert the CMYK JPEG data written by
79 for (marker
= cinfo
.marker_list
; marker
; marker
= marker
->next
)
80 if (marker
->marker
== (JPEG_APP0
+ 14) && marker
->data_length
>= 12 &&
81 !memcmp(marker
->data
, "Adobe", 5) && marker
->data
[11] == 2)
83 fputs("DEBUG: Adobe CMYK JPEG detected (inverting color values)\n",
88 cinfo
.quantize_colors
= 0;
90 fprintf(stderr
, "DEBUG: num_components = %d\n", cinfo
.num_components
);
91 fprintf(stderr
, "DEBUG: jpeg_color_space = %s\n",
92 cspaces
[cinfo
.jpeg_color_space
]);
94 if (cinfo
.num_components
== 1)
96 fputs("DEBUG: Converting image to grayscale...\n", stderr
);
98 cinfo
.out_color_space
= JCS_GRAYSCALE
;
99 cinfo
.out_color_components
= 1;
100 cinfo
.output_components
= 1;
102 img
->colorspace
= secondary
;
104 else if (cinfo
.num_components
== 4)
106 fputs("DEBUG: Converting image to CMYK...\n", stderr
);
108 cinfo
.out_color_space
= JCS_CMYK
;
109 cinfo
.out_color_components
= 4;
110 cinfo
.output_components
= 4;
112 img
->colorspace
= (primary
== CUPS_IMAGE_RGB_CMYK
) ? CUPS_IMAGE_CMYK
: primary
;
116 fputs("DEBUG: Converting image to RGB...\n", stderr
);
118 cinfo
.out_color_space
= JCS_RGB
;
119 cinfo
.out_color_components
= 3;
120 cinfo
.output_components
= 3;
122 img
->colorspace
= (primary
== CUPS_IMAGE_RGB_CMYK
) ? CUPS_IMAGE_RGB
: primary
;
125 jpeg_calc_output_dimensions(&cinfo
);
127 if (cinfo
.output_width
<= 0 || cinfo
.output_width
> CUPS_IMAGE_MAX_WIDTH
||
128 cinfo
.output_height
<= 0 || cinfo
.output_height
> CUPS_IMAGE_MAX_HEIGHT
)
130 fprintf(stderr
, "DEBUG: Bad JPEG dimensions %dx%d!\n",
131 cinfo
.output_width
, cinfo
.output_height
);
133 jpeg_destroy_decompress(&cinfo
);
139 img
->xsize
= cinfo
.output_width
;
140 img
->ysize
= cinfo
.output_height
;
142 if (cinfo
.X_density
> 0 && cinfo
.Y_density
> 0 && cinfo
.density_unit
> 0)
144 if (cinfo
.density_unit
== 1)
146 img
->xppi
= cinfo
.X_density
;
147 img
->yppi
= cinfo
.Y_density
;
151 img
->xppi
= (int)((float)cinfo
.X_density
* 2.54);
152 img
->yppi
= (int)((float)cinfo
.Y_density
* 2.54);
155 if (img
->xppi
== 0 || img
->yppi
== 0)
157 fprintf(stderr
, "DEBUG: Bad JPEG image resolution %dx%d PPI.\n",
158 img
->xppi
, img
->yppi
);
159 img
->xppi
= img
->yppi
= 128;
163 fprintf(stderr
, "DEBUG: JPEG image %dx%dx%d, %dx%d PPI\n",
164 img
->xsize
, img
->ysize
, cinfo
.output_components
,
165 img
->xppi
, img
->yppi
);
167 cupsImageSetMaxTiles(img
, 0);
169 in
= malloc(img
->xsize
* cinfo
.output_components
);
170 out
= malloc(img
->xsize
* cupsImageGetDepth(img
));
172 jpeg_start_decompress(&cinfo
);
174 while (cinfo
.output_scanline
< cinfo
.output_height
)
176 jpeg_read_scanlines(&cinfo
, (JSAMPROW
*)&in
, (JDIMENSION
)1);
178 if (psjpeg
&& cinfo
.output_components
== 4)
181 * Invert CMYK data from Photoshop...
184 cups_ib_t
*ptr
; /* Pointer into buffer */
185 int i
; /* Looping var */
188 for (ptr
= in
, i
= img
->xsize
* 4; i
> 0; i
--, ptr
++)
192 if ((saturation
!= 100 || hue
!= 0) && cinfo
.output_components
== 3)
193 cupsImageRGBAdjust(in
, img
->xsize
, saturation
, hue
);
195 if ((img
->colorspace
== CUPS_IMAGE_WHITE
&& cinfo
.out_color_space
== JCS_GRAYSCALE
) ||
196 (img
->colorspace
== CUPS_IMAGE_CMYK
&& cinfo
.out_color_space
== JCS_CMYK
))
203 fputs("DEBUG: Direct Data...\n", stderr
);
205 fputs("DEBUG:", stderr
);
207 for (i
= 0, ptr
= in
; i
< img
->xsize
; i
++)
210 for (j
= 0; j
< cinfo
.output_components
; j
++, ptr
++)
211 fprintf(stderr
, "%02X", *ptr
& 255);
218 cupsImageLut(in
, img
->xsize
* cupsImageGetDepth(img
), lut
);
220 _cupsImagePutRow(img
, 0, cinfo
.output_scanline
- 1, img
->xsize
, in
);
222 else if (cinfo
.out_color_space
== JCS_GRAYSCALE
)
224 switch (img
->colorspace
)
229 case CUPS_IMAGE_BLACK
:
230 cupsImageWhiteToBlack(in
, out
, img
->xsize
);
232 case CUPS_IMAGE_RGB
:
233 cupsImageWhiteToRGB(in
, out
, img
->xsize
);
235 case CUPS_IMAGE_CMY
:
236 cupsImageWhiteToCMY(in
, out
, img
->xsize
);
238 case CUPS_IMAGE_CMYK
:
239 cupsImageWhiteToCMYK(in
, out
, img
->xsize
);
244 cupsImageLut(out
, img
->xsize
* cupsImageGetDepth(img
), lut
);
246 _cupsImagePutRow(img
, 0, cinfo
.output_scanline
- 1, img
->xsize
, out
);
248 else if (cinfo
.out_color_space
== JCS_RGB
)
250 switch (img
->colorspace
)
255 case CUPS_IMAGE_RGB
:
256 cupsImageRGBToRGB(in
, out
, img
->xsize
);
258 case CUPS_IMAGE_WHITE
:
259 cupsImageRGBToWhite(in
, out
, img
->xsize
);
261 case CUPS_IMAGE_BLACK
:
262 cupsImageRGBToBlack(in
, out
, img
->xsize
);
264 case CUPS_IMAGE_CMY
:
265 cupsImageRGBToCMY(in
, out
, img
->xsize
);
267 case CUPS_IMAGE_CMYK
:
268 cupsImageRGBToCMYK(in
, out
, img
->xsize
);
273 cupsImageLut(out
, img
->xsize
* cupsImageGetDepth(img
), lut
);
275 _cupsImagePutRow(img
, 0, cinfo
.output_scanline
- 1, img
->xsize
, out
);
279 fputs("DEBUG: JCS_CMYK\n", stderr
);
281 switch (img
->colorspace
)
286 case CUPS_IMAGE_WHITE
:
287 cupsImageCMYKToWhite(in
, out
, img
->xsize
);
289 case CUPS_IMAGE_BLACK
:
290 cupsImageCMYKToBlack(in
, out
, img
->xsize
);
292 case CUPS_IMAGE_CMY
:
293 cupsImageCMYKToCMY(in
, out
, img
->xsize
);
295 case CUPS_IMAGE_RGB
:
296 cupsImageCMYKToRGB(in
, out
, img
->xsize
);
301 cupsImageLut(out
, img
->xsize
* cupsImageGetDepth(img
), lut
);
303 _cupsImagePutRow(img
, 0, cinfo
.output_scanline
- 1, img
->xsize
, out
);
310 jpeg_finish_decompress(&cinfo
);
311 jpeg_destroy_decompress(&cinfo
);
317 #endif /* HAVE_LIBJPEG */
321 * End of "$Id: image-jpeg.c 7355 2008-02-28 20:49:40Z mike $".