Revert manifest to default one
[external/cups.git] / filter / image-sgi.c
1 /*
2  * "$Id: image-sgi.c 9771 2011-05-12 05:21:56Z mike $"
3  *
4  *   SGI image file 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  *   _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     return (1);
81   }
82
83   if (sgip->zsize < 3)
84     img->colorspace = secondary;
85   else
86     img->colorspace = (primary == CUPS_IMAGE_RGB_CMYK) ? CUPS_IMAGE_RGB : primary;
87
88   img->xsize = sgip->xsize;
89   img->ysize = sgip->ysize;
90
91   cupsImageSetMaxTiles(img, 0);
92
93   bpp = cupsImageGetDepth(img);
94
95   if ((in = malloc(img->xsize * sgip->zsize)) == NULL)
96   {
97     fputs("DEBUG: Unable to allocate memory!\n", stderr);
98     sgiClose(sgip);
99     return (1);
100   }
101
102   if ((out = malloc(img->xsize * bpp)) == NULL)
103   {
104     fputs("DEBUG: Unable to allocate memory!\n", stderr);
105     sgiClose(sgip);
106     free(in);
107     return (1);
108   }
109
110   if ((rows[0] = calloc(img->xsize * sgip->zsize,
111                         sizeof(unsigned short))) == NULL)
112   {
113     fputs("DEBUG: Unable to allocate memory!\n", stderr);
114     sgiClose(sgip);
115     free(in);
116     free(out);
117     return (1);
118   }
119
120   for (i = 1; i < sgip->zsize; i ++)
121     rows[i] = rows[0] + i * img->xsize;
122
123  /*
124   * Read the SGI image file...
125   */
126
127   for (y = 0; y < img->ysize; y ++)
128   {
129     for (i = 0; i < sgip->zsize; i ++)
130       sgiGetRow(sgip, rows[i], img->ysize - 1 - y, i);
131
132     switch (sgip->zsize)
133     {
134       case 1 :
135           if (sgip->bpp == 1)
136             for (i = img->xsize - 1, gray = rows[0], inptr = in;
137                  i >= 0;
138                  i --)
139             {
140               *inptr++ = *gray++;
141             }
142           else
143             for (i = img->xsize - 1, gray = rows[0], inptr = in;
144                  i >= 0;
145                  i --)
146             {
147               *inptr++ = (*gray++) / 256 + 128;
148             }
149           break;
150       case 2 :
151           if (sgip->bpp == 1)
152             for (i = img->xsize - 1, gray = rows[0], alpha = rows[1], inptr = in;
153                  i >= 0;
154                  i --)
155             {
156               *inptr++ = (*gray++) * (*alpha++) / 255;
157             }
158           else
159             for (i = img->xsize - 1, gray = rows[0], alpha = rows[1], inptr = in;
160                  i >= 0;
161                  i --)
162             {
163               *inptr++ = ((*gray++) / 256 + 128) * (*alpha++) / 32767;
164             }
165           break;
166       case 3 :
167           if (sgip->bpp == 1)
168             for (i = img->xsize - 1, red = rows[0], green = rows[1],
169                      blue = rows[2], inptr = in;
170                  i >= 0;
171                  i --)
172             {
173               *inptr++ = *red++;
174               *inptr++ = *green++;
175               *inptr++ = *blue++;
176             }
177           else
178             for (i = img->xsize - 1, red = rows[0], green = rows[1],
179                      blue = rows[2], inptr = in;
180                  i >= 0;
181                  i --)
182             {
183               *inptr++ = (*red++) / 256 + 128;
184               *inptr++ = (*green++) / 256 + 128;
185               *inptr++ = (*blue++) / 256 + 128;
186             }
187           break;
188       case 4 :
189           if (sgip->bpp == 1)
190             for (i = img->xsize - 1, red = rows[0], green = rows[1],
191                      blue = rows[2], alpha = rows[3], inptr = in;
192                  i >= 0;
193                  i --)
194             {
195               *inptr++ = (*red++) * (*alpha) / 255;
196               *inptr++ = (*green++) * (*alpha) / 255;
197               *inptr++ = (*blue++) * (*alpha++) / 255;
198             }
199           else
200             for (i = img->xsize - 1, red = rows[0], green = rows[1],
201                      blue = rows[2], alpha = rows[3], inptr = in;
202                  i >= 0;
203                  i --)
204             {
205               *inptr++ = ((*red++) / 256 + 128) * (*alpha) / 32767;
206               *inptr++ = ((*green++) / 256 + 128) * (*alpha) / 32767;
207               *inptr++ = ((*blue++) / 256 + 128) * (*alpha++) / 32767;
208             }
209           break;
210     }
211
212     if (sgip->zsize < 3)
213     {
214       if (img->colorspace == CUPS_IMAGE_WHITE)
215       {
216         if (lut)
217           cupsImageLut(in, img->xsize, lut);
218
219         _cupsImagePutRow(img, 0, y, img->xsize, in);
220       }
221       else
222       {
223         switch (img->colorspace)
224         {
225           default :
226               break;
227
228           case CUPS_IMAGE_RGB :
229           case CUPS_IMAGE_RGB_CMYK :
230               cupsImageWhiteToRGB(in, out, img->xsize);
231               break;
232           case CUPS_IMAGE_BLACK :
233               cupsImageWhiteToBlack(in, out, img->xsize);
234               break;
235           case CUPS_IMAGE_CMY :
236               cupsImageWhiteToCMY(in, out, img->xsize);
237               break;
238           case CUPS_IMAGE_CMYK :
239               cupsImageWhiteToCMYK(in, out, img->xsize);
240               break;
241         }
242
243         if (lut)
244           cupsImageLut(out, img->xsize * bpp, lut);
245
246         _cupsImagePutRow(img, 0, y, img->xsize, out);
247       }
248     }
249     else
250     {
251       if ((saturation != 100 || hue != 0) && bpp > 1)
252         cupsImageRGBAdjust(in, img->xsize, saturation, hue);
253
254       switch (img->colorspace)
255       {
256         default :
257             break;
258
259         case CUPS_IMAGE_WHITE :
260             cupsImageRGBToWhite(in, out, img->xsize);
261             break;
262         case CUPS_IMAGE_RGB :
263             cupsImageRGBToRGB(in, out, img->xsize);
264             break;
265         case CUPS_IMAGE_BLACK :
266             cupsImageRGBToBlack(in, out, img->xsize);
267             break;
268         case CUPS_IMAGE_CMY :
269             cupsImageRGBToCMY(in, out, img->xsize);
270             break;
271         case CUPS_IMAGE_CMYK :
272             cupsImageRGBToCMYK(in, out, img->xsize);
273             break;
274       }
275
276       if (lut)
277         cupsImageLut(out, img->xsize * bpp, lut);
278
279       _cupsImagePutRow(img, 0, y, img->xsize, out);
280     }
281   }
282
283   free(in);
284   free(out);
285   free(rows[0]);
286
287   sgiClose(sgip);
288
289   return (0);
290 }
291
292
293 /*
294  * End of "$Id: image-sgi.c 9771 2011-05-12 05:21:56Z mike $".
295  */