]> git.ipfire.org Git - thirdparty/cups.git/blob - filter/image-pix.c
Merge changes from CUPS 1.5.1-r9875.
[thirdparty/cups.git] / filter / image-pix.c
1 /*
2 * "$Id: image-pix.c 7221 2008-01-16 22:20:08Z mike $"
3 *
4 * Alias PIX image routines for CUPS.
5 *
6 * Copyright 2007-2011 by Apple Inc.
7 * Copyright 1993-2007 by Easy Software Products.
8 *
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 *
17 * Contents:
18 *
19 * _cupsImageReadPIX() - Read a PIX image file.
20 * read_short() - Read a 16-bit integer.
21 */
22
23 /*
24 * Include necessary headers...
25 */
26
27 #include "image-private.h"
28
29
30 /*
31 * Local functions...
32 */
33
34 static short read_short(FILE *fp);
35
36
37 /*
38 * '_cupsImageReadPIX()' - Read a PIX image file.
39 */
40
41 int /* O - Read status */
42 _cupsImageReadPIX(
43 cups_image_t *img, /* IO - cupsImage */
44 FILE *fp, /* I - cupsImage file */
45 cups_icspace_t primary, /* I - Primary choice for colorspace */
46 cups_icspace_t secondary, /* I - Secondary choice for colorspace */
47 int saturation, /* I - Color saturation (%) */
48 int hue, /* I - Color hue (degrees) */
49 const cups_ib_t *lut) /* I - Lookup table for gamma/brightness */
50 {
51 short width, /* Width of image */
52 height, /* Height of image */
53 depth; /* Depth of image (bits) */
54 int count, /* Repetition count */
55 bpp, /* Bytes per pixel */
56 x, y; /* Looping vars */
57 cups_ib_t r, g, b; /* Red, green/gray, blue values */
58 cups_ib_t *in, /* Input pixels */
59 *out, /* Output pixels */
60 *ptr; /* Pointer into pixels */
61
62
63 /*
64 * Get the image dimensions and setup the image...
65 */
66
67 width = read_short(fp);
68 height = read_short(fp);
69 read_short(fp);
70 read_short(fp);
71 depth = read_short(fp);
72
73 /*
74 * Check the dimensions of the image. Since the short values used for the
75 * width and height cannot exceed CUPS_IMAGE_MAX_WIDTH or
76 * CUPS_IMAGE_MAX_HEIGHT, we just need to verify they are positive integers.
77 */
78
79 if (width <= 0 || height <= 0 ||
80 (depth != 8 && depth != 24))
81 {
82 fprintf(stderr, "DEBUG: Bad PIX image dimensions %dx%dx%d\n",
83 width, height, depth);
84 fclose(fp);
85 return (1);
86 }
87
88 if (depth == 8)
89 img->colorspace = secondary;
90 else
91 img->colorspace = (primary == CUPS_IMAGE_RGB_CMYK) ? CUPS_IMAGE_RGB : primary;
92
93 img->xsize = width;
94 img->ysize = height;
95
96 cupsImageSetMaxTiles(img, 0);
97
98 bpp = cupsImageGetDepth(img);
99
100 if ((in = malloc(img->xsize * (depth / 8))) == NULL)
101 {
102 fputs("DEBUG: Unable to allocate memory!\n", stderr);
103 fclose(fp);
104 return (1);
105 }
106
107 if ((out = malloc(img->xsize * bpp)) == NULL)
108 {
109 fputs("DEBUG: Unable to allocate memory!\n", stderr);
110 fclose(fp);
111 free(in);
112 return (1);
113 }
114
115 /*
116 * Read the image data...
117 */
118
119 if (depth == 8)
120 {
121 for (count = 0, y = 0, g = 0; y < img->ysize; y ++)
122 {
123 if (img->colorspace == CUPS_IMAGE_WHITE)
124 ptr = out;
125 else
126 ptr = in;
127
128 for (x = img->xsize; x > 0; x --, count --)
129 {
130 if (count == 0)
131 {
132 count = getc(fp);
133 g = getc(fp);
134 }
135
136 *ptr++ = g;
137 }
138
139 if (img->colorspace != CUPS_IMAGE_WHITE)
140 switch (img->colorspace)
141 {
142 default :
143 cupsImageWhiteToRGB(in, out, img->xsize);
144 break;
145 case CUPS_IMAGE_BLACK :
146 cupsImageWhiteToBlack(in, out, img->xsize);
147 break;
148 case CUPS_IMAGE_CMY :
149 cupsImageWhiteToCMY(in, out, img->xsize);
150 break;
151 case CUPS_IMAGE_CMYK :
152 cupsImageWhiteToCMYK(in, out, img->xsize);
153 break;
154 }
155
156 if (lut)
157 cupsImageLut(out, img->xsize * bpp, lut);
158
159 _cupsImagePutRow(img, 0, y, img->xsize, out);
160 }
161 }
162 else
163 {
164 for (count = 0, y = 0, r = 0, g = 0, b = 0; y < img->ysize; y ++)
165 {
166 ptr = in;
167
168 for (x = img->xsize; x > 0; x --, count --)
169 {
170 if (count == 0)
171 {
172 count = getc(fp);
173 b = getc(fp);
174 g = getc(fp);
175 r = getc(fp);
176 }
177
178 *ptr++ = r;
179 *ptr++ = g;
180 *ptr++ = b;
181 }
182
183 if (saturation != 100 || hue != 0)
184 cupsImageRGBAdjust(in, img->xsize, saturation, hue);
185
186 switch (img->colorspace)
187 {
188 default :
189 break;
190
191 case CUPS_IMAGE_WHITE :
192 cupsImageRGBToWhite(in, out, img->xsize);
193 break;
194 case CUPS_IMAGE_RGB :
195 cupsImageRGBToWhite(in, out, img->xsize);
196 break;
197 case CUPS_IMAGE_BLACK :
198 cupsImageRGBToBlack(in, out, img->xsize);
199 break;
200 case CUPS_IMAGE_CMY :
201 cupsImageRGBToCMY(in, out, img->xsize);
202 break;
203 case CUPS_IMAGE_CMYK :
204 cupsImageRGBToCMYK(in, out, img->xsize);
205 break;
206 }
207
208 if (lut)
209 cupsImageLut(out, img->xsize * bpp, lut);
210
211 _cupsImagePutRow(img, 0, y, img->xsize, out);
212 }
213 }
214
215 fclose(fp);
216 free(in);
217 free(out);
218
219 return (0);
220 }
221
222
223 /*
224 * 'read_short()' - Read a 16-bit integer.
225 */
226
227 static short /* O - Value from file */
228 read_short(FILE *fp) /* I - File to read from */
229 {
230 int ch; /* Character from file */
231
232
233 ch = getc(fp);
234 return ((ch << 8) | getc(fp));
235 }
236
237
238 /*
239 * End of "$Id: image-pix.c 7221 2008-01-16 22:20:08Z mike $".
240 */