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