]> git.ipfire.org Git - thirdparty/cups.git/blame - filter/image-pix.c
Load cups into easysw/current.
[thirdparty/cups.git] / filter / image-pix.c
CommitLineData
ef416fc2 1/*
f7faf1f5 2 * "$Id: image-pix.c 5509 2006-05-11 11:41:36Z mike $"
ef416fc2 3 *
4 * Alias PIX image 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 * _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
43static short read_short(FILE *fp);
44
45
46/*
47 * '_cupsImageReadPIX()' - Read a PIX image file.
48 */
49
50int /* 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 {
f301802f 162 ptr = in;
ef416fc2 163
164 for (x = img->xsize; x > 0; x --, count --)
165 {
166 if (count == 0)
167 {
168 count = getc(fp);
169 b = getc(fp);
170 g = getc(fp);
171 r = getc(fp);
172 }
173
174 *ptr++ = r;
175 *ptr++ = g;
176 *ptr++ = b;
177 }
178
f301802f 179 if (saturation != 100 || hue != 0)
180 cupsImageRGBAdjust(in, img->xsize, saturation, hue);
ef416fc2 181
f301802f 182 switch (img->colorspace)
183 {
184 default :
185 break;
186
187 case CUPS_IMAGE_WHITE :
188 cupsImageRGBToWhite(in, out, img->xsize);
189 break;
190 case CUPS_IMAGE_RGB :
191 cupsImageRGBToWhite(in, out, img->xsize);
192 break;
193 case CUPS_IMAGE_BLACK :
194 cupsImageRGBToBlack(in, out, img->xsize);
195 break;
196 case CUPS_IMAGE_CMY :
197 cupsImageRGBToCMY(in, out, img->xsize);
198 break;
199 case CUPS_IMAGE_CMYK :
200 cupsImageRGBToCMYK(in, out, img->xsize);
201 break;
ef416fc2 202 }
203
204 if (lut)
205 cupsImageLut(out, img->xsize * bpp, lut);
206
207 _cupsImagePutRow(img, 0, y, img->xsize, out);
208 }
209 }
210
211 fclose(fp);
212 free(in);
213 free(out);
214
215 return (0);
216}
217
218
219/*
220 * 'read_short()' - Read a 16-bit integer.
221 */
222
223static short /* O - Value from file */
224read_short(FILE *fp) /* I - File to read from */
225{
226 int ch; /* Character from file */
227
228
229 ch = getc(fp);
230 return ((ch << 8) | getc(fp));
231}
232
233
234/*
f7faf1f5 235 * End of "$Id: image-pix.c 5509 2006-05-11 11:41:36Z mike $".
ef416fc2 236 */