]> git.ipfire.org Git - thirdparty/cups.git/blame - filter/image-jpeg.c
Load cups into easysw/current.
[thirdparty/cups.git] / filter / image-jpeg.c
CommitLineData
ef416fc2 1/*
f7deaa1a 2 * "$Id: image-jpeg.c 5508 2006-05-11 11:41:16Z mike $"
ef416fc2 3 *
4 * JPEG image routines for the Common UNIX Printing System (CUPS).
5 *
f301802f 6 * Copyright 1993-2006 by Easy Software Products.
ef416fc2 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
45int /* 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) ||
ef416fc2 198 (img->colorspace == CUPS_IMAGE_CMYK && cinfo.out_color_space == JCS_CMYK))
199 {
200#ifdef DEBUG
201 int i, j;
202 cups_ib_t *ptr;
203
204
205 fputs("DEBUG: Direct Data...\n", stderr);
206
207 fputs("DEBUG:", stderr);
208
209 for (i = 0, ptr = in; i < img->xsize; i ++)
210 {
211 putc(' ', stderr);
212 for (j = 0; j < cinfo.output_components; j ++, ptr ++)
213 fprintf(stderr, "%02X", *ptr & 255);
214 }
215
216 putc('\n', stderr);
217#endif /* DEBUG */
218
219 if (lut)
220 cupsImageLut(in, img->xsize * cupsImageGetDepth(img), lut);
221
222 _cupsImagePutRow(img, 0, cinfo.output_scanline - 1, img->xsize, in);
223 }
224 else if (cinfo.out_color_space == JCS_GRAYSCALE)
225 {
226 switch (img->colorspace)
227 {
228 default :
229 break;
230
231 case CUPS_IMAGE_BLACK :
232 cupsImageWhiteToBlack(in, out, img->xsize);
233 break;
234 case CUPS_IMAGE_RGB :
235 cupsImageWhiteToRGB(in, out, img->xsize);
236 break;
237 case CUPS_IMAGE_CMY :
238 cupsImageWhiteToCMY(in, out, img->xsize);
239 break;
240 case CUPS_IMAGE_CMYK :
241 cupsImageWhiteToCMYK(in, out, img->xsize);
242 break;
243 }
244
245 if (lut)
246 cupsImageLut(out, img->xsize * cupsImageGetDepth(img), lut);
247
248 _cupsImagePutRow(img, 0, cinfo.output_scanline - 1, img->xsize, out);
249 }
250 else if (cinfo.out_color_space == JCS_RGB)
251 {
252 switch (img->colorspace)
253 {
254 default :
255 break;
256
f301802f 257 case CUPS_IMAGE_RGB :
258 cupsImageRGBToRGB(in, out, img->xsize);
259 break;
ef416fc2 260 case CUPS_IMAGE_WHITE :
261 cupsImageRGBToWhite(in, out, img->xsize);
262 break;
263 case CUPS_IMAGE_BLACK :
264 cupsImageRGBToBlack(in, out, img->xsize);
265 break;
266 case CUPS_IMAGE_CMY :
267 cupsImageRGBToCMY(in, out, img->xsize);
268 break;
269 case CUPS_IMAGE_CMYK :
270 cupsImageRGBToCMYK(in, out, img->xsize);
271 break;
272 }
273
274 if (lut)
275 cupsImageLut(out, img->xsize * cupsImageGetDepth(img), lut);
276
277 _cupsImagePutRow(img, 0, cinfo.output_scanline - 1, img->xsize, out);
278 }
279 else /* JCS_CMYK */
280 {
281 fputs("DEBUG: JCS_CMYK\n", stderr);
282
283 switch (img->colorspace)
284 {
285 default :
286 break;
287
288 case CUPS_IMAGE_WHITE :
289 cupsImageCMYKToWhite(in, out, img->xsize);
290 break;
291 case CUPS_IMAGE_BLACK :
292 cupsImageCMYKToBlack(in, out, img->xsize);
293 break;
294 case CUPS_IMAGE_CMY :
295 cupsImageCMYKToCMY(in, out, img->xsize);
296 break;
297 case CUPS_IMAGE_RGB :
298 cupsImageCMYKToRGB(in, out, img->xsize);
299 break;
300 }
301
302 if (lut)
303 cupsImageLut(out, img->xsize * cupsImageGetDepth(img), lut);
304
305 _cupsImagePutRow(img, 0, cinfo.output_scanline - 1, img->xsize, out);
306 }
307 }
308
309 free(in);
310 free(out);
311
312 jpeg_finish_decompress(&cinfo);
313 jpeg_destroy_decompress(&cinfo);
314
315 fclose(fp);
316
317 return (0);
318}
319#endif /* HAVE_LIBJPEG */
320
321
322/*
f7deaa1a 323 * End of "$Id: image-jpeg.c 5508 2006-05-11 11:41:16Z mike $".
ef416fc2 324 */