]> git.ipfire.org Git - thirdparty/cups.git/blame - filter/image-pnm.c
Import CUPS 1.4svn-r7226.
[thirdparty/cups.git] / filter / image-pnm.c
CommitLineData
ef416fc2 1/*
bc44d920 2 * "$Id: image-pnm.c 6649 2007-07-11 21:46:42Z mike $"
ef416fc2 3 *
4 * Portable Any Map file routines for the Common UNIX Printing System (CUPS).
5 *
91c84a35 6 * Copyright 2007-2008 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 * _cupsImageReadPNM() - Read a PNM image file.
20 */
21
22/*
23 * Include necessary headers...
24 */
25
26#include "image-private.h"
27
28
29/*
30 * '_cupsImageReadPNM()' - Read a PNM image file.
31 */
32
33int /* O - Read status */
34_cupsImageReadPNM(
35 cups_image_t *img, /* IO - cupsImage */
36 FILE *fp, /* I - cupsImage file */
37 cups_icspace_t primary, /* I - Primary choice for colorspace */
38 cups_icspace_t secondary, /* I - Secondary choice for colorspace */
39 int saturation, /* I - Color saturation (%) */
40 int hue, /* I - Color hue (degrees) */
41 const cups_ib_t *lut) /* I - Lookup table for gamma/brightness */
42{
43 int x, y; /* Looping vars */
44 int bpp; /* Bytes per pixel */
45 cups_ib_t *in, /* Input pixels */
46 *inptr, /* Current input pixel */
47 *out, /* Output pixels */
48 *outptr, /* Current output pixel */
49 bit; /* Bit in input line */
50 char line[255], /* Input line */
51 *lineptr; /* Pointer in line */
52 int format, /* Format of PNM file */
53 val, /* Pixel value */
54 maxval; /* Maximum pixel value */
55
56
57 /*
58 * Read the file header in the format:
59 *
60 * Pformat
61 * # comment1
62 * # comment2
63 * ...
64 * # commentN
65 * width
66 * height
67 * max sample
68 */
69
91c84a35
MS
70 if ((lineptr = fgets(line, sizeof(line), fp)) == NULL)
71 {
72 fputs("DEBUG: Bad PNM header!\n", stderr);
73 fclose(fp);
74 return (1);
75 }
76
ef416fc2 77 lineptr ++;
78
79 format = atoi(lineptr);
80 while (isdigit(*lineptr & 255))
81 lineptr ++;
82
83 while (lineptr != NULL && img->xsize == 0)
84 {
85 if (*lineptr == '\0' || *lineptr == '#')
86 lineptr = fgets(line, sizeof(line), fp);
87 else if (isdigit(*lineptr & 255))
88 {
89 img->xsize = atoi(lineptr);
90 while (isdigit(*lineptr & 255))
91 lineptr ++;
92 }
93 else
94 lineptr ++;
95 }
96
97 while (lineptr != NULL && img->ysize == 0)
98 {
99 if (*lineptr == '\0' || *lineptr == '#')
100 lineptr = fgets(line, sizeof(line), fp);
101 else if (isdigit(*lineptr & 255))
102 {
103 img->ysize = atoi(lineptr);
104 while (isdigit(*lineptr & 255))
105 lineptr ++;
106 }
107 else
108 lineptr ++;
109 }
110
111 if (format != 1 && format != 4)
112 {
113 maxval = 0;
114
115 while (lineptr != NULL && maxval == 0)
116 {
117 if (*lineptr == '\0' || *lineptr == '#')
118 lineptr = fgets(line, sizeof(line), fp);
119 else if (isdigit(*lineptr & 255))
120 {
121 maxval = atoi(lineptr);
122 while (isdigit(*lineptr & 255))
123 lineptr ++;
124 }
125 else
126 lineptr ++;
127 }
128 }
129 else
130 maxval = 1;
131
132 if (img->xsize == 0 || img->xsize > CUPS_IMAGE_MAX_WIDTH ||
133 img->ysize == 0 || img->ysize > CUPS_IMAGE_MAX_HEIGHT)
134 {
c0e1af83 135 fprintf(stderr, "DEBUG: Bad PNM dimensions %dx%d!\n",
ef416fc2 136 img->xsize, img->ysize);
137 fclose(fp);
138 return (1);
139 }
140
141 if (maxval == 0)
142 {
c0e1af83 143 fprintf(stderr, "DEBUG: Bad PNM max value %d!\n", maxval);
ef416fc2 144 fclose(fp);
145 return (1);
146 }
147
148 if (format == 1 || format == 2 || format == 4 || format == 5)
149 img->colorspace = secondary;
150 else
151 img->colorspace = (primary == CUPS_IMAGE_RGB_CMYK) ? CUPS_IMAGE_RGB : primary;
152
153 cupsImageSetMaxTiles(img, 0);
154
155 bpp = cupsImageGetDepth(img);
91c84a35
MS
156
157 if ((in = malloc(img->xsize * 3)) == NULL)
158 {
159 fputs("DEBUG: Unable to allocate memory!\n", stderr);
160 fclose(fp);
161 return (1);
162 }
163
164 if ((out = malloc(img->xsize * bpp)) == NULL)
165 {
166 fputs("DEBUG: Unable to allocate memory!\n", stderr);
167 fclose(fp);
168 free(in);
169 return (1);
170 }
ef416fc2 171
172 /*
173 * Read the image file...
174 */
175
176 for (y = 0; y < img->ysize; y ++)
177 {
178 switch (format)
179 {
180 case 1 :
181 case 2 :
182 for (x = img->xsize, inptr = in; x > 0; x --, inptr ++)
183 if (fscanf(fp, "%d", &val) == 1)
184 *inptr = 255 * val / maxval;
185 break;
186
187 case 3 :
188 for (x = img->xsize, inptr = in; x > 0; x --, inptr += 3)
189 {
190 if (fscanf(fp, "%d", &val) == 1)
191 inptr[0] = 255 * val / maxval;
192 if (fscanf(fp, "%d", &val) == 1)
193 inptr[1] = 255 * val / maxval;
194 if (fscanf(fp, "%d", &val) == 1)
195 inptr[2] = 255 * val / maxval;
196 }
197 break;
198
199 case 4 :
200 fread(out, (img->xsize + 7) / 8, 1, fp);
201 for (x = img->xsize, inptr = in, outptr = out, bit = 128;
202 x > 0;
203 x --, inptr ++)
204 {
205 if (*outptr & bit)
206 *inptr = 255;
207 else
208 *inptr = 0;
209
210 if (bit > 1)
211 bit >>= 1;
212 else
213 {
214 bit = 128;
b86bc4cf 215 outptr ++;
ef416fc2 216 }
217 }
218 break;
219
220 case 5 :
221 fread(in, img->xsize, 1, fp);
222 break;
223
224 case 6 :
225 fread(in, img->xsize, 3, fp);
226 break;
227 }
228
229 switch (format)
230 {
231 case 1 :
232 case 2 :
233 case 4 :
234 case 5 :
235 if (img->colorspace == CUPS_IMAGE_WHITE)
236 {
237 if (lut)
238 cupsImageLut(in, img->xsize, lut);
239
240 _cupsImagePutRow(img, 0, y, img->xsize, in);
241 }
242 else
243 {
244 switch (img->colorspace)
245 {
246 default :
247 break;
248
249 case CUPS_IMAGE_RGB :
250 cupsImageWhiteToRGB(in, out, img->xsize);
251 break;
252 case CUPS_IMAGE_BLACK :
253 cupsImageWhiteToBlack(in, out, img->xsize);
254 break;
255 case CUPS_IMAGE_CMY :
256 cupsImageWhiteToCMY(in, out, img->xsize);
257 break;
258 case CUPS_IMAGE_CMYK :
259 cupsImageWhiteToCMYK(in, out, img->xsize);
260 break;
261 }
262
263 if (lut)
264 cupsImageLut(out, img->xsize * bpp, lut);
265
266 _cupsImagePutRow(img, 0, y, img->xsize, out);
267 }
268 break;
269
270 default :
271 if ((saturation != 100 || hue != 0) && bpp > 1)
272 cupsImageRGBAdjust(in, img->xsize, saturation, hue);
273
f301802f 274 switch (img->colorspace)
ef416fc2 275 {
f301802f 276 default :
277 break;
278
279 case CUPS_IMAGE_WHITE :
280 cupsImageRGBToWhite(in, out, img->xsize);
281 break;
282 case CUPS_IMAGE_RGB :
283 cupsImageRGBToRGB(in, out, img->xsize);
284 break;
285 case CUPS_IMAGE_BLACK :
286 cupsImageRGBToBlack(in, out, img->xsize);
287 break;
288 case CUPS_IMAGE_CMY :
289 cupsImageRGBToCMY(in, out, img->xsize);
290 break;
291 case CUPS_IMAGE_CMYK :
292 cupsImageRGBToCMYK(in, out, img->xsize);
293 break;
ef416fc2 294 }
ef416fc2 295
f301802f 296 if (lut)
297 cupsImageLut(out, img->xsize * bpp, lut);
ef416fc2 298
f301802f 299 _cupsImagePutRow(img, 0, y, img->xsize, out);
ef416fc2 300 break;
301 }
302 }
303
304 free(in);
305 free(out);
306
307 fclose(fp);
308
309 return (0);
310}
311
312
313/*
bc44d920 314 * End of "$Id: image-pnm.c 6649 2007-07-11 21:46:42Z mike $".
ef416fc2 315 */