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