]> git.ipfire.org Git - thirdparty/cups.git/blob - filter/image-jpeg.c
Load cups into easysw/current.
[thirdparty/cups.git] / filter / image-jpeg.c
1 /*
2 * "$Id: image-jpeg.c 4741 2005-10-02 04:25:52Z mike $"
3 *
4 * JPEG image routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1993-2005 by Easy Software Products.
7 *
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
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * This file is subject to the Apple OS-Developed Software exception.
25 *
26 * Contents:
27 *
28 * _cupsImageReadJPEG() - Read a JPEG image file.
29 */
30
31 /*
32 * Include necessary headers...
33 */
34
35 #include "image-private.h"
36
37 #ifdef HAVE_LIBJPEG
38 # include <jpeglib.h> /* JPEG/JFIF image definitions */
39
40
41 /*
42 * '_cupsImageReadJPEG()' - Read a JPEG image file.
43 */
44
45 int /* O - Read status */
46 _cupsImageReadJPEG(
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 */
54 {
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... */
63 "JCS_UNKNOWN",
64 "JCS_GRAYSCALE",
65 "JCS_RGB",
66 "JCS_YCbCr",
67 "JCS_CMYK",
68 "JCS_YCCK"
69 };
70
71
72 /*
73 * Read the first 16 bytes to determine if this is a Photoshop JPEG file...
74 */
75
76 fread(header, sizeof(header), 1, fp);
77 rewind(fp);
78
79 psjpeg = memcmp(header + 6, "Photoshop ", 10) == 0;
80
81 /*
82 * Read the JPEG header...
83 */
84
85 cinfo.err = jpeg_std_error(&jerr);
86 jpeg_create_decompress(&cinfo);
87 jpeg_stdio_src(&cinfo, fp);
88 jpeg_read_header(&cinfo, 1);
89
90 cinfo.quantize_colors = 0;
91
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]);
95
96 if (cinfo.num_components == 1)
97 {
98 fputs("DEBUG: Converting image to grayscale...\n", stderr);
99
100 cinfo.out_color_space = JCS_GRAYSCALE;
101 cinfo.out_color_components = 1;
102 cinfo.output_components = 1;
103
104 img->colorspace = secondary;
105 }
106 else if (cinfo.num_components == 4)
107 {
108 fputs("DEBUG: Converting image to CMYK...\n", stderr);
109
110 cinfo.out_color_space = JCS_CMYK;
111 cinfo.out_color_components = 4;
112 cinfo.output_components = 4;
113
114 img->colorspace = (primary == CUPS_IMAGE_RGB_CMYK) ? CUPS_IMAGE_CMYK : primary;
115 }
116 else
117 {
118 fputs("DEBUG: Converting image to RGB...\n", stderr);
119
120 cinfo.out_color_space = JCS_RGB;
121 cinfo.out_color_components = 3;
122 cinfo.output_components = 3;
123
124 img->colorspace = (primary == CUPS_IMAGE_RGB_CMYK) ? CUPS_IMAGE_RGB : primary;
125 }
126
127 jpeg_calc_output_dimensions(&cinfo);
128
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)
131 {
132 fprintf(stderr, "ERROR: Bad JPEG dimensions %dx%d!\n",
133 cinfo.output_width, cinfo.output_height);
134
135 jpeg_destroy_decompress(&cinfo);
136
137 fclose(fp);
138 return (1);
139 }
140
141 img->xsize = cinfo.output_width;
142 img->ysize = cinfo.output_height;
143
144 if (cinfo.X_density > 0 && cinfo.Y_density > 0 && cinfo.density_unit > 0)
145 {
146 if (cinfo.density_unit == 1)
147 {
148 img->xppi = cinfo.X_density;
149 img->yppi = cinfo.Y_density;
150 }
151 else
152 {
153 img->xppi = (int)((float)cinfo.X_density * 2.54);
154 img->yppi = (int)((float)cinfo.Y_density * 2.54);
155 }
156
157 if (img->xppi == 0 || img->yppi == 0)
158 {
159 fprintf(stderr, "ERROR: Bad JPEG image resolution %dx%d PPI.\n",
160 img->xppi, img->yppi);
161 img->xppi = img->yppi = 128;
162 }
163 }
164
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);
168
169 cupsImageSetMaxTiles(img, 0);
170
171 in = malloc(img->xsize * cinfo.output_components);
172 out = malloc(img->xsize * cupsImageGetDepth(img));
173
174 jpeg_start_decompress(&cinfo);
175
176 while (cinfo.output_scanline < cinfo.output_height)
177 {
178 jpeg_read_scanlines(&cinfo, (JSAMPROW *)&in, (JDIMENSION)1);
179
180 if (psjpeg && cinfo.output_components == 4)
181 {
182 /*
183 * Invert CMYK data from Photoshop...
184 */
185
186 cups_ib_t *ptr; /* Pointer into buffer */
187 int i; /* Looping var */
188
189
190 for (ptr = in, i = img->xsize * 4; i > 0; i --, ptr ++)
191 *ptr = 255 - *ptr;
192 }
193
194 if ((saturation != 100 || hue != 0) && cinfo.output_components == 3)
195 cupsImageRGBAdjust(in, img->xsize, saturation, hue);
196
197 if ((img->colorspace == CUPS_IMAGE_WHITE && cinfo.out_color_space == JCS_GRAYSCALE) ||
198 (img->colorspace == CUPS_IMAGE_RGB && cinfo.out_color_space == JCS_RGB) ||
199 (img->colorspace == CUPS_IMAGE_CMYK && cinfo.out_color_space == JCS_CMYK))
200 {
201 #ifdef DEBUG
202 int i, j;
203 cups_ib_t *ptr;
204
205
206 fputs("DEBUG: Direct Data...\n", stderr);
207
208 fputs("DEBUG:", stderr);
209
210 for (i = 0, ptr = in; i < img->xsize; i ++)
211 {
212 putc(' ', stderr);
213 for (j = 0; j < cinfo.output_components; j ++, ptr ++)
214 fprintf(stderr, "%02X", *ptr & 255);
215 }
216
217 putc('\n', stderr);
218 #endif /* DEBUG */
219
220 if (lut)
221 cupsImageLut(in, img->xsize * cupsImageGetDepth(img), lut);
222
223 _cupsImagePutRow(img, 0, cinfo.output_scanline - 1, img->xsize, in);
224 }
225 else if (cinfo.out_color_space == JCS_GRAYSCALE)
226 {
227 switch (img->colorspace)
228 {
229 default :
230 break;
231
232 case CUPS_IMAGE_BLACK :
233 cupsImageWhiteToBlack(in, out, img->xsize);
234 break;
235 case CUPS_IMAGE_RGB :
236 cupsImageWhiteToRGB(in, out, img->xsize);
237 break;
238 case CUPS_IMAGE_CMY :
239 cupsImageWhiteToCMY(in, out, img->xsize);
240 break;
241 case CUPS_IMAGE_CMYK :
242 cupsImageWhiteToCMYK(in, out, img->xsize);
243 break;
244 }
245
246 if (lut)
247 cupsImageLut(out, img->xsize * cupsImageGetDepth(img), lut);
248
249 _cupsImagePutRow(img, 0, cinfo.output_scanline - 1, img->xsize, out);
250 }
251 else if (cinfo.out_color_space == JCS_RGB)
252 {
253 switch (img->colorspace)
254 {
255 default :
256 break;
257
258 case CUPS_IMAGE_WHITE :
259 cupsImageRGBToWhite(in, out, img->xsize);
260 break;
261 case CUPS_IMAGE_BLACK :
262 cupsImageRGBToBlack(in, out, img->xsize);
263 break;
264 case CUPS_IMAGE_CMY :
265 cupsImageRGBToCMY(in, out, img->xsize);
266 break;
267 case CUPS_IMAGE_CMYK :
268 cupsImageRGBToCMYK(in, out, img->xsize);
269 break;
270 }
271
272 if (lut)
273 cupsImageLut(out, img->xsize * cupsImageGetDepth(img), lut);
274
275 _cupsImagePutRow(img, 0, cinfo.output_scanline - 1, img->xsize, out);
276 }
277 else /* JCS_CMYK */
278 {
279 fputs("DEBUG: JCS_CMYK\n", stderr);
280
281 switch (img->colorspace)
282 {
283 default :
284 break;
285
286 case CUPS_IMAGE_WHITE :
287 cupsImageCMYKToWhite(in, out, img->xsize);
288 break;
289 case CUPS_IMAGE_BLACK :
290 cupsImageCMYKToBlack(in, out, img->xsize);
291 break;
292 case CUPS_IMAGE_CMY :
293 cupsImageCMYKToCMY(in, out, img->xsize);
294 break;
295 case CUPS_IMAGE_RGB :
296 cupsImageCMYKToRGB(in, out, img->xsize);
297 break;
298 }
299
300 if (lut)
301 cupsImageLut(out, img->xsize * cupsImageGetDepth(img), lut);
302
303 _cupsImagePutRow(img, 0, cinfo.output_scanline - 1, img->xsize, out);
304 }
305 }
306
307 free(in);
308 free(out);
309
310 jpeg_finish_decompress(&cinfo);
311 jpeg_destroy_decompress(&cinfo);
312
313 fclose(fp);
314
315 return (0);
316 }
317 #endif /* HAVE_LIBJPEG */
318
319
320 /*
321 * End of "$Id: image-jpeg.c 4741 2005-10-02 04:25:52Z mike $".
322 */