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