]> git.ipfire.org Git - thirdparty/cups.git/blob - filter/image-sgi.c
Load cups into easysw/current.
[thirdparty/cups.git] / filter / image-sgi.c
1 /*
2 * "$Id: image-sgi.c 6649 2007-07-11 21:46:42Z mike $"
3 *
4 * SGI image file routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007 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 * _cupsImageReadSGI() - Read a SGI image file.
20 */
21
22 /*
23 * Include necessary headers...
24 */
25
26 #include "image-private.h"
27 #include "image-sgi.h"
28
29
30 /*
31 * '_cupsImageReadSGI()' - Read a SGI image file.
32 */
33
34 int /* O - Read status */
35 _cupsImageReadSGI(
36 cups_image_t *img, /* IO - cupsImage */
37 FILE *fp, /* I - cupsImage file */
38 cups_icspace_t primary, /* I - Primary choice for colorspace */
39 cups_icspace_t secondary, /* I - Secondary choice for colorspace */
40 int saturation, /* I - Color saturation (%) */
41 int hue, /* I - Color hue (degrees) */
42 const cups_ib_t *lut) /* I - Lookup table for gamma/brightness */
43 {
44 int i, y; /* Looping vars */
45 int bpp; /* Bytes per pixel */
46 sgi_t *sgip; /* SGI image file */
47 cups_ib_t *in, /* Input pixels */
48 *inptr, /* Current input pixel */
49 *out; /* Output pixels */
50 unsigned short *rows[4], /* Row pointers for image data */
51 *red,
52 *green,
53 *blue,
54 *gray,
55 *alpha;
56
57
58 /*
59 * Setup the SGI file...
60 */
61
62 sgip = sgiOpenFile(fp, SGI_READ, 0, 0, 0, 0, 0);
63
64 /*
65 * Get the image dimensions and load the output image...
66 */
67
68 /*
69 * Check the image dimensions; since xsize and ysize are unsigned shorts,
70 * just check if they are 0 since they can't exceed CUPS_IMAGE_MAX_WIDTH or
71 * CUPS_IMAGE_MAX_HEIGHT...
72 */
73
74 if (sgip->xsize == 0 || sgip->ysize == 0 ||
75 sgip->zsize == 0 || sgip->zsize > 4)
76 {
77 fprintf(stderr, "DEBUG: Bad SGI image dimensions %ux%ux%u!\n",
78 sgip->xsize, sgip->ysize, sgip->zsize);
79 sgiClose(sgip);
80 fclose(fp);
81 return (1);
82 }
83
84 if (sgip->zsize < 3)
85 img->colorspace = secondary;
86 else
87 img->colorspace = (primary == CUPS_IMAGE_RGB_CMYK) ? CUPS_IMAGE_RGB : primary;
88
89 img->xsize = sgip->xsize;
90 img->ysize = sgip->ysize;
91
92 cupsImageSetMaxTiles(img, 0);
93
94 bpp = cupsImageGetDepth(img);
95 in = malloc(img->xsize * sgip->zsize);
96 out = malloc(img->xsize * bpp);
97
98 rows[0] = calloc(img->xsize * sgip->zsize, sizeof(unsigned short));
99 for (i = 1; i < sgip->zsize; i ++)
100 rows[i] = rows[0] + i * img->xsize;
101
102 /*
103 * Read the SGI image file...
104 */
105
106 for (y = 0; y < img->ysize; y ++)
107 {
108 for (i = 0; i < sgip->zsize; i ++)
109 sgiGetRow(sgip, rows[i], img->ysize - 1 - y, i);
110
111 switch (sgip->zsize)
112 {
113 case 1 :
114 if (sgip->bpp == 1)
115 for (i = img->xsize - 1, gray = rows[0], inptr = in;
116 i >= 0;
117 i --)
118 {
119 *inptr++ = *gray++;
120 }
121 else
122 for (i = img->xsize - 1, gray = rows[0], inptr = in;
123 i >= 0;
124 i --)
125 {
126 *inptr++ = (*gray++) / 256 + 128;
127 }
128 break;
129 case 2 :
130 if (sgip->bpp == 1)
131 for (i = img->xsize - 1, gray = rows[0], alpha = rows[1], inptr = in;
132 i >= 0;
133 i --)
134 {
135 *inptr++ = (*gray++) * (*alpha++) / 255;
136 }
137 else
138 for (i = img->xsize - 1, gray = rows[0], alpha = rows[1], inptr = in;
139 i >= 0;
140 i --)
141 {
142 *inptr++ = ((*gray++) / 256 + 128) * (*alpha++) / 32767;
143 }
144 break;
145 case 3 :
146 if (sgip->bpp == 1)
147 for (i = img->xsize - 1, red = rows[0], green = rows[1],
148 blue = rows[2], inptr = in;
149 i >= 0;
150 i --)
151 {
152 *inptr++ = *red++;
153 *inptr++ = *green++;
154 *inptr++ = *blue++;
155 }
156 else
157 for (i = img->xsize - 1, red = rows[0], green = rows[1],
158 blue = rows[2], inptr = in;
159 i >= 0;
160 i --)
161 {
162 *inptr++ = (*red++) / 256 + 128;
163 *inptr++ = (*green++) / 256 + 128;
164 *inptr++ = (*blue++) / 256 + 128;
165 }
166 break;
167 case 4 :
168 if (sgip->bpp == 1)
169 for (i = img->xsize - 1, red = rows[0], green = rows[1],
170 blue = rows[2], alpha = rows[3], inptr = in;
171 i >= 0;
172 i --)
173 {
174 *inptr++ = (*red++) * (*alpha) / 255;
175 *inptr++ = (*green++) * (*alpha) / 255;
176 *inptr++ = (*blue++) * (*alpha++) / 255;
177 }
178 else
179 for (i = img->xsize - 1, red = rows[0], green = rows[1],
180 blue = rows[2], alpha = rows[3], inptr = in;
181 i >= 0;
182 i --)
183 {
184 *inptr++ = ((*red++) / 256 + 128) * (*alpha) / 32767;
185 *inptr++ = ((*green++) / 256 + 128) * (*alpha) / 32767;
186 *inptr++ = ((*blue++) / 256 + 128) * (*alpha++) / 32767;
187 }
188 break;
189 }
190
191 if (sgip->zsize < 3)
192 {
193 if (img->colorspace == CUPS_IMAGE_WHITE)
194 {
195 if (lut)
196 cupsImageLut(in, img->xsize, lut);
197
198 _cupsImagePutRow(img, 0, y, img->xsize, in);
199 }
200 else
201 {
202 switch (img->colorspace)
203 {
204 default :
205 break;
206
207 case CUPS_IMAGE_RGB :
208 case CUPS_IMAGE_RGB_CMYK :
209 cupsImageWhiteToRGB(in, out, img->xsize);
210 break;
211 case CUPS_IMAGE_BLACK :
212 cupsImageWhiteToBlack(in, out, img->xsize);
213 break;
214 case CUPS_IMAGE_CMY :
215 cupsImageWhiteToCMY(in, out, img->xsize);
216 break;
217 case CUPS_IMAGE_CMYK :
218 cupsImageWhiteToCMYK(in, out, img->xsize);
219 break;
220 }
221
222 if (lut)
223 cupsImageLut(out, img->xsize * bpp, lut);
224
225 _cupsImagePutRow(img, 0, y, img->xsize, out);
226 }
227 }
228 else
229 {
230 if ((saturation != 100 || hue != 0) && bpp > 1)
231 cupsImageRGBAdjust(in, img->xsize, saturation, hue);
232
233 switch (img->colorspace)
234 {
235 default :
236 break;
237
238 case CUPS_IMAGE_WHITE :
239 cupsImageRGBToWhite(in, out, img->xsize);
240 break;
241 case CUPS_IMAGE_RGB :
242 cupsImageRGBToRGB(in, out, img->xsize);
243 break;
244 case CUPS_IMAGE_BLACK :
245 cupsImageRGBToBlack(in, out, img->xsize);
246 break;
247 case CUPS_IMAGE_CMY :
248 cupsImageRGBToCMY(in, out, img->xsize);
249 break;
250 case CUPS_IMAGE_CMYK :
251 cupsImageRGBToCMYK(in, out, img->xsize);
252 break;
253 }
254
255 if (lut)
256 cupsImageLut(out, img->xsize * bpp, lut);
257
258 _cupsImagePutRow(img, 0, y, img->xsize, out);
259 }
260 }
261
262 free(in);
263 free(out);
264 free(rows[0]);
265
266 sgiClose(sgip);
267
268 return (0);
269 }
270
271
272 /*
273 * End of "$Id: image-sgi.c 6649 2007-07-11 21:46:42Z mike $".
274 */