2 * "$Id: image-jpeg.c 5508 2006-05-11 11:41:16Z mike $"
4 * JPEG image routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1993-2006 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 USA
20 * Voice: (301) 373-9600
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 * _cupsImageReadJPEG() - Read a JPEG image file.
32 * Include necessary headers...
35 #include "image-private.h"
38 # include <jpeglib.h> /* JPEG/JFIF image definitions */
42 * '_cupsImageReadJPEG()' - Read a JPEG image file.
45 int /* O - Read status */
47 cups_image_t
*img
, /* IO - cupsImage */
48 FILE *fp
, /* I - cupsImage file */
49 cups_icspace_t primary
, /* I - Primary choice for colorspace */
50 cups_icspace_t secondary
, /* I - Secondary choice for colorspace */
51 int saturation
, /* I - Color saturation (%) */
52 int hue
, /* I - Color hue (degrees) */
53 const cups_ib_t
*lut
) /* I - Lookup table for gamma/brightness */
55 struct jpeg_decompress_struct cinfo
; /* Decompressor info */
56 struct jpeg_error_mgr jerr
; /* Error handler info */
57 cups_ib_t
*in
, /* Input pixels */
58 *out
; /* Output pixels */
59 char header
[16]; /* 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
== CUPS_IMAGE_RGB_CMYK
) ? CUPS_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
== CUPS_IMAGE_RGB_CMYK
) ? CUPS_IMAGE_RGB
: primary
;
127 jpeg_calc_output_dimensions(&cinfo
);
129 if (cinfo
.output_width
<= 0 || cinfo
.output_width
> CUPS_IMAGE_MAX_WIDTH
||
130 cinfo
.output_height
<= 0 || cinfo
.output_height
> CUPS_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 cupsImageSetMaxTiles(img
, 0);
171 in
= malloc(img
->xsize
* cinfo
.output_components
);
172 out
= malloc(img
->xsize
* cupsImageGetDepth(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 cups_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 cupsImageRGBAdjust(in
, img
->xsize
, saturation
, hue
);
197 if ((img
->colorspace
== CUPS_IMAGE_WHITE
&& cinfo
.out_color_space
== JCS_GRAYSCALE
) ||
198 (img
->colorspace
== CUPS_IMAGE_CMYK
&& cinfo
.out_color_space
== JCS_CMYK
))
205 fputs("DEBUG: Direct Data...\n", stderr
);
207 fputs("DEBUG:", stderr
);
209 for (i
= 0, ptr
= in
; i
< img
->xsize
; i
++)
212 for (j
= 0; j
< cinfo
.output_components
; j
++, ptr
++)
213 fprintf(stderr
, "%02X", *ptr
& 255);
220 cupsImageLut(in
, img
->xsize
* cupsImageGetDepth(img
), lut
);
222 _cupsImagePutRow(img
, 0, cinfo
.output_scanline
- 1, img
->xsize
, in
);
224 else if (cinfo
.out_color_space
== JCS_GRAYSCALE
)
226 switch (img
->colorspace
)
231 case CUPS_IMAGE_BLACK
:
232 cupsImageWhiteToBlack(in
, out
, img
->xsize
);
234 case CUPS_IMAGE_RGB
:
235 cupsImageWhiteToRGB(in
, out
, img
->xsize
);
237 case CUPS_IMAGE_CMY
:
238 cupsImageWhiteToCMY(in
, out
, img
->xsize
);
240 case CUPS_IMAGE_CMYK
:
241 cupsImageWhiteToCMYK(in
, out
, img
->xsize
);
246 cupsImageLut(out
, img
->xsize
* cupsImageGetDepth(img
), lut
);
248 _cupsImagePutRow(img
, 0, cinfo
.output_scanline
- 1, img
->xsize
, out
);
250 else if (cinfo
.out_color_space
== JCS_RGB
)
252 switch (img
->colorspace
)
257 case CUPS_IMAGE_RGB
:
258 cupsImageRGBToRGB(in
, out
, img
->xsize
);
260 case CUPS_IMAGE_WHITE
:
261 cupsImageRGBToWhite(in
, out
, img
->xsize
);
263 case CUPS_IMAGE_BLACK
:
264 cupsImageRGBToBlack(in
, out
, img
->xsize
);
266 case CUPS_IMAGE_CMY
:
267 cupsImageRGBToCMY(in
, out
, img
->xsize
);
269 case CUPS_IMAGE_CMYK
:
270 cupsImageRGBToCMYK(in
, out
, img
->xsize
);
275 cupsImageLut(out
, img
->xsize
* cupsImageGetDepth(img
), lut
);
277 _cupsImagePutRow(img
, 0, cinfo
.output_scanline
- 1, img
->xsize
, out
);
281 fputs("DEBUG: JCS_CMYK\n", stderr
);
283 switch (img
->colorspace
)
288 case CUPS_IMAGE_WHITE
:
289 cupsImageCMYKToWhite(in
, out
, img
->xsize
);
291 case CUPS_IMAGE_BLACK
:
292 cupsImageCMYKToBlack(in
, out
, img
->xsize
);
294 case CUPS_IMAGE_CMY
:
295 cupsImageCMYKToCMY(in
, out
, img
->xsize
);
297 case CUPS_IMAGE_RGB
:
298 cupsImageCMYKToRGB(in
, out
, img
->xsize
);
303 cupsImageLut(out
, img
->xsize
* cupsImageGetDepth(img
), lut
);
305 _cupsImagePutRow(img
, 0, cinfo
.output_scanline
- 1, img
->xsize
, out
);
312 jpeg_finish_decompress(&cinfo
);
313 jpeg_destroy_decompress(&cinfo
);
319 #endif /* HAVE_LIBJPEG */
323 * End of "$Id: image-jpeg.c 5508 2006-05-11 11:41:16Z mike $".