]> git.ipfire.org Git - thirdparty/cups.git/blob - filter/image-photocd.c
Load cups into easysw/current.
[thirdparty/cups.git] / filter / image-photocd.c
1 /*
2 * "$Id: image-photocd.c 6649 2007-07-11 21:46:42Z mike $"
3 *
4 * PhotoCD routines for the Common UNIX Printing System (CUPS).
5 *
6 * PhotoCD support is currently limited to the 768x512 base image, which
7 * is only YCC encoded. Support for the higher resolution images will
8 * require a lot of extra code...
9 *
10 * Copyright 2007 by Apple Inc.
11 * Copyright 1993-2006 by Easy Software Products.
12 *
13 * These coded instructions, statements, and computer programs are the
14 * property of Apple Inc. and are protected by Federal copyright
15 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
16 * which should have been included with this file. If this file is
17 * file is missing or damaged, see the license at "http://www.cups.org/".
18 *
19 * This file is subject to the Apple OS-Developed Software exception.
20 *
21 * Contents:
22 *
23 * _cupsImageReadPhotoCD() - Read a PhotoCD image file.
24 */
25
26 /*
27 * Include necessary headers...
28 */
29
30 #include "image-private.h"
31
32
33 /*
34 * '_cupsImageReadPhotoCD()' - Read a PhotoCD image file.
35 */
36
37 int /* O - Read status */
38 _cupsImageReadPhotoCD(
39 cups_image_t *img, /* IO - cupsImage */
40 FILE *fp, /* I - cupsImage file */
41 cups_icspace_t primary, /* I - Primary choice for colorspace */
42 cups_icspace_t secondary, /* I - Secondary choice for colorspace */
43 int saturation, /* I - Color saturation (%) */
44 int hue, /* I - Color hue (degrees) */
45 const cups_ib_t *lut) /* I - Lookup table for gamma/brightness */
46 {
47 int x, y; /* Looping vars */
48 int xdir, /* X direction */
49 xstart; /* X starting point */
50 int bpp; /* Bytes per pixel */
51 int pass; /* Pass number */
52 int rotation; /* 0 for 768x512, 1 for 512x768 */
53 int temp, /* Adjusted luminance */
54 temp2, /* Red, green, and blue values */
55 cb, cr; /* Adjusted chroma values */
56 cups_ib_t *in, /* Input (YCC) pixels */
57 *iy, /* Luminance */
58 *icb, /* Blue chroma */
59 *icr, /* Red chroma */
60 *rgb, /* RGB */
61 *rgbptr, /* Pointer into RGB data */
62 *out; /* Output pixels */
63
64
65 (void)secondary;
66
67 /*
68 * Get the image orientation...
69 */
70
71 fseek(fp, 72, SEEK_SET);
72 rotation = (getc(fp) & 63) != 8;
73
74 /*
75 * Seek to the start of the base image...
76 */
77
78 fseek(fp, 0x30000, SEEK_SET);
79
80 /*
81 * Allocate and initialize...
82 */
83
84 img->colorspace = (primary == CUPS_IMAGE_RGB_CMYK) ? CUPS_IMAGE_RGB : primary;
85 img->xppi = 128;
86 img->yppi = 128;
87
88 if (rotation)
89 {
90 img->xsize = 512;
91 img->ysize = 768;
92 }
93 else
94 {
95 img->xsize = 768;
96 img->ysize = 512;
97 }
98
99 cupsImageSetMaxTiles(img, 0);
100
101 bpp = cupsImageGetDepth(img);
102 in = malloc(768 * 3);
103 out = malloc(768 * bpp);
104
105 if (bpp > 1)
106 rgb = malloc(768 * 3);
107 else
108 rgb = NULL;
109
110 if (rotation)
111 {
112 xstart = 767 * bpp;
113 xdir = -2 * bpp;
114 }
115 else
116 {
117 xstart = 0;
118 xdir = 0;
119 }
120
121 /*
122 * Read the image file...
123 */
124
125 for (y = 0; y < 512; y += 2)
126 {
127 /*
128 * Grab the next two scanlines:
129 *
130 * YYYYYYYYYYYYYYY...
131 * YYYYYYYYYYYYYYY...
132 * CbCbCb...CrCrCr...
133 */
134
135 if (fread(in, 1, 768 * 3, fp) < (768 * 3))
136 {
137 /*
138 * Couldn't read a row of data - return an error!
139 */
140
141 free(in);
142 free(out);
143
144 return (-1);
145 }
146
147 /*
148 * Process the two scanlines...
149 */
150
151 for (pass = 0, iy = in; pass < 2; pass ++)
152 {
153 if (bpp == 1)
154 {
155 /*
156 * Just extract the luminance channel from the line and put it
157 * in the image...
158 */
159
160 if (primary == CUPS_IMAGE_BLACK)
161 {
162 if (rotation)
163 {
164 for (rgbptr = out + xstart, x = 0; x < 768; x ++)
165 *rgbptr-- = 255 - *iy++;
166
167 if (lut)
168 cupsImageLut(out, 768, lut);
169
170 _cupsImagePutCol(img, 511 - y - pass, 0, 768, out);
171 }
172 else
173 {
174 cupsImageWhiteToBlack(iy, out, 768);
175
176 if (lut)
177 cupsImageLut(out, 768, lut);
178
179 _cupsImagePutRow(img, 0, y + pass, 768, out);
180 iy += 768;
181 }
182 }
183 else if (rotation)
184 {
185 for (rgbptr = out + xstart, x = 0; x < 768; x ++)
186 *rgbptr-- = 255 - *iy++;
187
188 if (lut)
189 cupsImageLut(out, 768, lut);
190
191 _cupsImagePutCol(img, 511 - y - pass, 0, 768, out);
192 }
193 else
194 {
195 if (lut)
196 cupsImageLut(iy, 768, lut);
197
198 _cupsImagePutRow(img, 0, y + pass, 768, iy);
199 iy += 768;
200 }
201 }
202 else
203 {
204 /*
205 * Convert YCbCr to RGB... While every pixel gets a luminance
206 * value, adjacent pixels share chroma information.
207 */
208
209 cb = cr = 0.0f;
210
211 for (x = 0, rgbptr = rgb + xstart, icb = in + 1536, icr = in + 1920;
212 x < 768;
213 x ++, iy ++, rgbptr += xdir)
214 {
215 if (!(x & 1))
216 {
217 cb = (float)(*icb - 156);
218 cr = (float)(*icr - 137);
219 }
220
221 temp = 92241 * (*iy);
222
223 temp2 = (temp + 86706 * cr) / 65536;
224 if (temp2 < 0)
225 *rgbptr++ = 0;
226 else if (temp2 > 255)
227 *rgbptr++ = 255;
228 else
229 *rgbptr++ = temp2;
230
231 temp2 = (temp - 25914 * cb - 44166 * cr) / 65536;
232 if (temp2 < 0)
233 *rgbptr++ = 0;
234 else if (temp2 > 255)
235 *rgbptr++ = 255;
236 else
237 *rgbptr++ = temp2;
238
239 temp2 = (temp + 133434 * cb) / 65536;
240 if (temp2 < 0)
241 *rgbptr++ = 0;
242 else if (temp2 > 255)
243 *rgbptr++ = 255;
244 else
245 *rgbptr++ = temp2;
246
247 if (x & 1)
248 {
249 icb ++;
250 icr ++;
251 }
252 }
253
254 /*
255 * Adjust the hue and saturation if needed...
256 */
257
258 if (saturation != 100 || hue != 0)
259 cupsImageRGBAdjust(rgb, 768, saturation, hue);
260
261 /*
262 * Then convert the RGB data to the appropriate colorspace and
263 * put it in the image...
264 */
265
266 switch (img->colorspace)
267 {
268 default :
269 break;
270
271 case CUPS_IMAGE_RGB :
272 cupsImageRGBToRGB(rgb, out, 768);
273 break;
274 case CUPS_IMAGE_CMY :
275 cupsImageRGBToCMY(rgb, out, 768);
276 break;
277 case CUPS_IMAGE_CMYK :
278 cupsImageRGBToCMYK(rgb, out, 768);
279 break;
280 }
281
282 if (lut)
283 cupsImageLut(out, 768 * bpp, lut);
284
285 if (rotation)
286 _cupsImagePutCol(img, 511 - y - pass, 0, 768, out);
287 else
288 _cupsImagePutRow(img, 0, y + pass, 768, out);
289 }
290 }
291 }
292
293 /*
294 * Free memory and return...
295 */
296
297 free(in);
298 free(out);
299 if (bpp > 1)
300 free(rgb);
301
302 return (0);
303 }
304
305
306 /*
307 * End of "$Id: image-photocd.c 6649 2007-07-11 21:46:42Z mike $".
308 */