]> git.ipfire.org Git - thirdparty/cups.git/blob - filter/image-jpeg.c
Change the end copyright for Easy Software Products files to 2003.
[thirdparty/cups.git] / filter / image-jpeg.c
1 /*
2 * "$Id: image-jpeg.c,v 1.17 2002/12/17 18:59:26 swdev Exp $"
3 *
4 * JPEG image routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1993-2003 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-3111 USA
19 *
20 * Voice: (301) 373-9603
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 * ImageReadJPEG() - Read a JPEG image file.
29 */
30
31 /*
32 * Include necessary headers...
33 */
34
35 #include "image.h"
36
37 #ifdef HAVE_LIBJPEG
38 # include <jpeglib.h> /* JPEG/JFIF image definitions */
39
40
41 /*
42 * 'ImageReadJPEG()' - Read a JPEG image file.
43 */
44
45 int /* O - Read status */
46 ImageReadJPEG(image_t *img, /* IO - Image */
47 FILE *fp, /* I - Image file */
48 int primary, /* I - Primary choice for colorspace */
49 int secondary, /* I - Secondary choice for colorspace */
50 int saturation, /* I - Color saturation (%) */
51 int hue, /* I - Color hue (degrees) */
52 const ib_t *lut) /* I - Lookup table for gamma/brightness */
53 {
54 struct jpeg_decompress_struct cinfo; /* Decompressor info */
55 struct jpeg_error_mgr jerr; /* Error handler info */
56 ib_t *in, /* Input pixels */
57 *out; /* Output pixels */
58 char header[16];
59 /* 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 == IMAGE_RGB_CMYK) ? 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 == IMAGE_RGB_CMYK) ? IMAGE_RGB : primary;
125 }
126
127 jpeg_calc_output_dimensions(&cinfo);
128
129 if (cinfo.output_width <= 0 || cinfo.output_width > IMAGE_MAX_WIDTH ||
130 cinfo.output_height <= 0 || cinfo.output_height > 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 ImageSetMaxTiles(img, 0);
170
171 in = malloc(img->xsize * cinfo.output_components);
172 out = malloc(img->xsize * ImageGetDepth(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 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 ImageRGBAdjust(in, img->xsize, saturation, hue);
196
197 if ((img->colorspace == IMAGE_WHITE && cinfo.out_color_space == JCS_GRAYSCALE) ||
198 (img->colorspace == IMAGE_RGB && cinfo.out_color_space == JCS_RGB) ||
199 (img->colorspace == IMAGE_CMYK && cinfo.out_color_space == JCS_CMYK))
200 {
201 #ifdef DEBUG
202 int i, j;
203 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 ImageLut(in, img->xsize * ImageGetDepth(img), lut);
222
223 ImagePutRow(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 case IMAGE_BLACK :
230 ImageWhiteToBlack(in, out, img->xsize);
231 break;
232 case IMAGE_RGB :
233 ImageWhiteToRGB(in, out, img->xsize);
234 break;
235 case IMAGE_CMY :
236 ImageWhiteToCMY(in, out, img->xsize);
237 break;
238 case IMAGE_CMYK :
239 ImageWhiteToCMYK(in, out, img->xsize);
240 break;
241 }
242
243 if (lut)
244 ImageLut(out, img->xsize * ImageGetDepth(img), lut);
245
246 ImagePutRow(img, 0, cinfo.output_scanline - 1, img->xsize, out);
247 }
248 else if (cinfo.out_color_space == JCS_RGB)
249 {
250 switch (img->colorspace)
251 {
252 case IMAGE_WHITE :
253 ImageRGBToWhite(in, out, img->xsize);
254 break;
255 case IMAGE_BLACK :
256 ImageRGBToBlack(in, out, img->xsize);
257 break;
258 case IMAGE_CMY :
259 ImageRGBToCMY(in, out, img->xsize);
260 break;
261 case IMAGE_CMYK :
262 ImageRGBToCMYK(in, out, img->xsize);
263 break;
264 }
265
266 if (lut)
267 ImageLut(out, img->xsize * ImageGetDepth(img), lut);
268
269 ImagePutRow(img, 0, cinfo.output_scanline - 1, img->xsize, out);
270 }
271 else /* JCS_CMYK */
272 {
273 fputs("DEBUG: JCS_CMYK\n", stderr);
274
275 switch (img->colorspace)
276 {
277 case IMAGE_WHITE :
278 ImageCMYKToWhite(in, out, img->xsize);
279 break;
280 case IMAGE_BLACK :
281 ImageCMYKToBlack(in, out, img->xsize);
282 break;
283 case IMAGE_CMY :
284 ImageCMYKToCMY(in, out, img->xsize);
285 break;
286 case IMAGE_RGB :
287 ImageCMYKToRGB(in, out, img->xsize);
288 break;
289 }
290
291 if (lut)
292 ImageLut(out, img->xsize * ImageGetDepth(img), lut);
293
294 ImagePutRow(img, 0, cinfo.output_scanline - 1, img->xsize, out);
295 }
296 }
297
298 free(in);
299 free(out);
300
301 jpeg_finish_decompress(&cinfo);
302 jpeg_destroy_decompress(&cinfo);
303
304 fclose(fp);
305
306 return (0);
307 }
308
309
310 #endif /* HAVE_LIBJPEG */
311
312
313 /*
314 * End of "$Id: image-jpeg.c,v 1.17 2002/12/17 18:59:26 swdev Exp $".
315 */