2 * "$Id: image-jpeg.c 6649 2007-07-11 21:46:42Z 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 char header
[16]; /* Photoshop JPEG header */
51 int psjpeg
; /* Non-zero if Photoshop JPEG */
52 static const char *cspaces
[] =
53 { /* JPEG colorspaces... */
64 * Read the first 16 bytes to determine if this is a Photoshop JPEG file...
67 fread(header
, sizeof(header
), 1, fp
);
70 psjpeg
= memcmp(header
+ 6, "Photoshop ", 10) == 0;
73 * Read the JPEG header...
76 cinfo
.err
= jpeg_std_error(&jerr
);
77 jpeg_create_decompress(&cinfo
);
78 jpeg_stdio_src(&cinfo
, fp
);
79 jpeg_read_header(&cinfo
, 1);
81 cinfo
.quantize_colors
= 0;
83 fprintf(stderr
, "DEBUG: num_components = %d\n", cinfo
.num_components
);
84 fprintf(stderr
, "DEBUG: jpeg_color_space = %s\n",
85 cspaces
[cinfo
.jpeg_color_space
]);
87 if (cinfo
.num_components
== 1)
89 fputs("DEBUG: Converting image to grayscale...\n", stderr
);
91 cinfo
.out_color_space
= JCS_GRAYSCALE
;
92 cinfo
.out_color_components
= 1;
93 cinfo
.output_components
= 1;
95 img
->colorspace
= secondary
;
97 else if (cinfo
.num_components
== 4)
99 fputs("DEBUG: Converting image to CMYK...\n", stderr
);
101 cinfo
.out_color_space
= JCS_CMYK
;
102 cinfo
.out_color_components
= 4;
103 cinfo
.output_components
= 4;
105 img
->colorspace
= (primary
== CUPS_IMAGE_RGB_CMYK
) ? CUPS_IMAGE_CMYK
: primary
;
109 fputs("DEBUG: Converting image to RGB...\n", stderr
);
111 cinfo
.out_color_space
= JCS_RGB
;
112 cinfo
.out_color_components
= 3;
113 cinfo
.output_components
= 3;
115 img
->colorspace
= (primary
== CUPS_IMAGE_RGB_CMYK
) ? CUPS_IMAGE_RGB
: primary
;
118 jpeg_calc_output_dimensions(&cinfo
);
120 if (cinfo
.output_width
<= 0 || cinfo
.output_width
> CUPS_IMAGE_MAX_WIDTH
||
121 cinfo
.output_height
<= 0 || cinfo
.output_height
> CUPS_IMAGE_MAX_HEIGHT
)
123 fprintf(stderr
, "DEBUG: Bad JPEG dimensions %dx%d!\n",
124 cinfo
.output_width
, cinfo
.output_height
);
126 jpeg_destroy_decompress(&cinfo
);
132 img
->xsize
= cinfo
.output_width
;
133 img
->ysize
= cinfo
.output_height
;
135 if (cinfo
.X_density
> 0 && cinfo
.Y_density
> 0 && cinfo
.density_unit
> 0)
137 if (cinfo
.density_unit
== 1)
139 img
->xppi
= cinfo
.X_density
;
140 img
->yppi
= cinfo
.Y_density
;
144 img
->xppi
= (int)((float)cinfo
.X_density
* 2.54);
145 img
->yppi
= (int)((float)cinfo
.Y_density
* 2.54);
148 if (img
->xppi
== 0 || img
->yppi
== 0)
150 fprintf(stderr
, "DEBUG: Bad JPEG image resolution %dx%d PPI.\n",
151 img
->xppi
, img
->yppi
);
152 img
->xppi
= img
->yppi
= 128;
156 fprintf(stderr
, "DEBUG: JPEG image %dx%dx%d, %dx%d PPI\n",
157 img
->xsize
, img
->ysize
, cinfo
.output_components
,
158 img
->xppi
, img
->yppi
);
160 cupsImageSetMaxTiles(img
, 0);
162 in
= malloc(img
->xsize
* cinfo
.output_components
);
163 out
= malloc(img
->xsize
* cupsImageGetDepth(img
));
165 jpeg_start_decompress(&cinfo
);
167 while (cinfo
.output_scanline
< cinfo
.output_height
)
169 jpeg_read_scanlines(&cinfo
, (JSAMPROW
*)&in
, (JDIMENSION
)1);
171 if (psjpeg
&& cinfo
.output_components
== 4)
174 * Invert CMYK data from Photoshop...
177 cups_ib_t
*ptr
; /* Pointer into buffer */
178 int i
; /* Looping var */
181 for (ptr
= in
, i
= img
->xsize
* 4; i
> 0; i
--, ptr
++)
185 if ((saturation
!= 100 || hue
!= 0) && cinfo
.output_components
== 3)
186 cupsImageRGBAdjust(in
, img
->xsize
, saturation
, hue
);
188 if ((img
->colorspace
== CUPS_IMAGE_WHITE
&& cinfo
.out_color_space
== JCS_GRAYSCALE
) ||
189 (img
->colorspace
== CUPS_IMAGE_CMYK
&& cinfo
.out_color_space
== JCS_CMYK
))
196 fputs("DEBUG: Direct Data...\n", stderr
);
198 fputs("DEBUG:", stderr
);
200 for (i
= 0, ptr
= in
; i
< img
->xsize
; i
++)
203 for (j
= 0; j
< cinfo
.output_components
; j
++, ptr
++)
204 fprintf(stderr
, "%02X", *ptr
& 255);
211 cupsImageLut(in
, img
->xsize
* cupsImageGetDepth(img
), lut
);
213 _cupsImagePutRow(img
, 0, cinfo
.output_scanline
- 1, img
->xsize
, in
);
215 else if (cinfo
.out_color_space
== JCS_GRAYSCALE
)
217 switch (img
->colorspace
)
222 case CUPS_IMAGE_BLACK
:
223 cupsImageWhiteToBlack(in
, out
, img
->xsize
);
225 case CUPS_IMAGE_RGB
:
226 cupsImageWhiteToRGB(in
, out
, img
->xsize
);
228 case CUPS_IMAGE_CMY
:
229 cupsImageWhiteToCMY(in
, out
, img
->xsize
);
231 case CUPS_IMAGE_CMYK
:
232 cupsImageWhiteToCMYK(in
, out
, img
->xsize
);
237 cupsImageLut(out
, img
->xsize
* cupsImageGetDepth(img
), lut
);
239 _cupsImagePutRow(img
, 0, cinfo
.output_scanline
- 1, img
->xsize
, out
);
241 else if (cinfo
.out_color_space
== JCS_RGB
)
243 switch (img
->colorspace
)
248 case CUPS_IMAGE_RGB
:
249 cupsImageRGBToRGB(in
, out
, img
->xsize
);
251 case CUPS_IMAGE_WHITE
:
252 cupsImageRGBToWhite(in
, out
, img
->xsize
);
254 case CUPS_IMAGE_BLACK
:
255 cupsImageRGBToBlack(in
, out
, img
->xsize
);
257 case CUPS_IMAGE_CMY
:
258 cupsImageRGBToCMY(in
, out
, img
->xsize
);
260 case CUPS_IMAGE_CMYK
:
261 cupsImageRGBToCMYK(in
, out
, img
->xsize
);
266 cupsImageLut(out
, img
->xsize
* cupsImageGetDepth(img
), lut
);
268 _cupsImagePutRow(img
, 0, cinfo
.output_scanline
- 1, img
->xsize
, out
);
272 fputs("DEBUG: JCS_CMYK\n", stderr
);
274 switch (img
->colorspace
)
279 case CUPS_IMAGE_WHITE
:
280 cupsImageCMYKToWhite(in
, out
, img
->xsize
);
282 case CUPS_IMAGE_BLACK
:
283 cupsImageCMYKToBlack(in
, out
, img
->xsize
);
285 case CUPS_IMAGE_CMY
:
286 cupsImageCMYKToCMY(in
, out
, img
->xsize
);
288 case CUPS_IMAGE_RGB
:
289 cupsImageCMYKToRGB(in
, out
, img
->xsize
);
294 cupsImageLut(out
, img
->xsize
* cupsImageGetDepth(img
), lut
);
296 _cupsImagePutRow(img
, 0, cinfo
.output_scanline
- 1, img
->xsize
, out
);
303 jpeg_finish_decompress(&cinfo
);
304 jpeg_destroy_decompress(&cinfo
);
310 #endif /* HAVE_LIBJPEG */
314 * End of "$Id: image-jpeg.c 6649 2007-07-11 21:46:42Z mike $".