]> git.ipfire.org Git - thirdparty/cups.git/blame - filter/image-pnm.c
Load cups into easysw/current.
[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 *
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 * _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
70 lineptr = fgets(line, sizeof(line), fp);
71 lineptr ++;
72
73 format = atoi(lineptr);
74 while (isdigit(*lineptr & 255))
75 lineptr ++;
76
77 while (lineptr != NULL && img->xsize == 0)
78 {
79 if (*lineptr == '\0' || *lineptr == '#')
80 lineptr = fgets(line, sizeof(line), fp);
81 else if (isdigit(*lineptr & 255))
82 {
83 img->xsize = atoi(lineptr);
84 while (isdigit(*lineptr & 255))
85 lineptr ++;
86 }
87 else
88 lineptr ++;
89 }
90
91 while (lineptr != NULL && img->ysize == 0)
92 {
93 if (*lineptr == '\0' || *lineptr == '#')
94 lineptr = fgets(line, sizeof(line), fp);
95 else if (isdigit(*lineptr & 255))
96 {
97 img->ysize = atoi(lineptr);
98 while (isdigit(*lineptr & 255))
99 lineptr ++;
100 }
101 else
102 lineptr ++;
103 }
104
105 if (format != 1 && format != 4)
106 {
107 maxval = 0;
108
109 while (lineptr != NULL && maxval == 0)
110 {
111 if (*lineptr == '\0' || *lineptr == '#')
112 lineptr = fgets(line, sizeof(line), fp);
113 else if (isdigit(*lineptr & 255))
114 {
115 maxval = atoi(lineptr);
116 while (isdigit(*lineptr & 255))
117 lineptr ++;
118 }
119 else
120 lineptr ++;
121 }
122 }
123 else
124 maxval = 1;
125
126 if (img->xsize == 0 || img->xsize > CUPS_IMAGE_MAX_WIDTH ||
127 img->ysize == 0 || img->ysize > CUPS_IMAGE_MAX_HEIGHT)
128 {
c0e1af83 129 fprintf(stderr, "DEBUG: Bad PNM dimensions %dx%d!\n",
ef416fc2 130 img->xsize, img->ysize);
131 fclose(fp);
132 return (1);
133 }
134
135 if (maxval == 0)
136 {
c0e1af83 137 fprintf(stderr, "DEBUG: Bad PNM max value %d!\n", maxval);
ef416fc2 138 fclose(fp);
139 return (1);
140 }
141
142 if (format == 1 || format == 2 || format == 4 || format == 5)
143 img->colorspace = secondary;
144 else
145 img->colorspace = (primary == CUPS_IMAGE_RGB_CMYK) ? CUPS_IMAGE_RGB : primary;
146
147 cupsImageSetMaxTiles(img, 0);
148
149 bpp = cupsImageGetDepth(img);
150 in = malloc(img->xsize * 3);
151 out = malloc(img->xsize * bpp);
152
153 /*
154 * Read the image file...
155 */
156
157 for (y = 0; y < img->ysize; y ++)
158 {
159 switch (format)
160 {
161 case 1 :
162 case 2 :
163 for (x = img->xsize, inptr = in; x > 0; x --, inptr ++)
164 if (fscanf(fp, "%d", &val) == 1)
165 *inptr = 255 * val / maxval;
166 break;
167
168 case 3 :
169 for (x = img->xsize, inptr = in; x > 0; x --, inptr += 3)
170 {
171 if (fscanf(fp, "%d", &val) == 1)
172 inptr[0] = 255 * val / maxval;
173 if (fscanf(fp, "%d", &val) == 1)
174 inptr[1] = 255 * val / maxval;
175 if (fscanf(fp, "%d", &val) == 1)
176 inptr[2] = 255 * val / maxval;
177 }
178 break;
179
180 case 4 :
181 fread(out, (img->xsize + 7) / 8, 1, fp);
182 for (x = img->xsize, inptr = in, outptr = out, bit = 128;
183 x > 0;
184 x --, inptr ++)
185 {
186 if (*outptr & bit)
187 *inptr = 255;
188 else
189 *inptr = 0;
190
191 if (bit > 1)
192 bit >>= 1;
193 else
194 {
195 bit = 128;
b86bc4cf 196 outptr ++;
ef416fc2 197 }
198 }
199 break;
200
201 case 5 :
202 fread(in, img->xsize, 1, fp);
203 break;
204
205 case 6 :
206 fread(in, img->xsize, 3, fp);
207 break;
208 }
209
210 switch (format)
211 {
212 case 1 :
213 case 2 :
214 case 4 :
215 case 5 :
216 if (img->colorspace == CUPS_IMAGE_WHITE)
217 {
218 if (lut)
219 cupsImageLut(in, img->xsize, lut);
220
221 _cupsImagePutRow(img, 0, y, img->xsize, in);
222 }
223 else
224 {
225 switch (img->colorspace)
226 {
227 default :
228 break;
229
230 case CUPS_IMAGE_RGB :
231 cupsImageWhiteToRGB(in, out, img->xsize);
232 break;
233 case CUPS_IMAGE_BLACK :
234 cupsImageWhiteToBlack(in, out, img->xsize);
235 break;
236 case CUPS_IMAGE_CMY :
237 cupsImageWhiteToCMY(in, out, img->xsize);
238 break;
239 case CUPS_IMAGE_CMYK :
240 cupsImageWhiteToCMYK(in, out, img->xsize);
241 break;
242 }
243
244 if (lut)
245 cupsImageLut(out, img->xsize * bpp, lut);
246
247 _cupsImagePutRow(img, 0, y, img->xsize, out);
248 }
249 break;
250
251 default :
252 if ((saturation != 100 || hue != 0) && bpp > 1)
253 cupsImageRGBAdjust(in, img->xsize, saturation, hue);
254
f301802f 255 switch (img->colorspace)
ef416fc2 256 {
f301802f 257 default :
258 break;
259
260 case CUPS_IMAGE_WHITE :
261 cupsImageRGBToWhite(in, out, img->xsize);
262 break;
263 case CUPS_IMAGE_RGB :
264 cupsImageRGBToRGB(in, out, img->xsize);
265 break;
266 case CUPS_IMAGE_BLACK :
267 cupsImageRGBToBlack(in, out, img->xsize);
268 break;
269 case CUPS_IMAGE_CMY :
270 cupsImageRGBToCMY(in, out, img->xsize);
271 break;
272 case CUPS_IMAGE_CMYK :
273 cupsImageRGBToCMYK(in, out, img->xsize);
274 break;
ef416fc2 275 }
ef416fc2 276
f301802f 277 if (lut)
278 cupsImageLut(out, img->xsize * bpp, lut);
ef416fc2 279
f301802f 280 _cupsImagePutRow(img, 0, y, img->xsize, out);
ef416fc2 281 break;
282 }
283 }
284
285 free(in);
286 free(out);
287
288 fclose(fp);
289
290 return (0);
291}
292
293
294/*
bc44d920 295 * End of "$Id: image-pnm.c 6649 2007-07-11 21:46:42Z mike $".
ef416fc2 296 */