]>
Commit | Line | Data |
---|---|---|
516ba4c9 | 1 | /* |
71fe22b7 | 2 | * "$Id: image-pnm.c,v 1.6 2000/01/04 13:45:45 mike Exp $" |
516ba4c9 | 3 | * |
ed19bd98 | 4 | * Portable Any Map file routines for the Common UNIX Printing System (CUPS). |
516ba4c9 | 5 | * |
71fe22b7 | 6 | * Copyright 1993-2000 by Easy Software Products. |
516ba4c9 | 7 | * |
ed19bd98 | 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: | |
516ba4c9 | 14 | * |
ed19bd98 | 15 | * Attn: CUPS Licensing Information |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636-3111 USA | |
516ba4c9 | 19 | * |
ed19bd98 | 20 | * Voice: (301) 373-9603 |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
68749fc8 | 23 | * |
ed19bd98 | 24 | * Contents: |
516ba4c9 | 25 | * |
6de9968b | 26 | * ImageReadPNM() - Read a PNM image file. |
516ba4c9 | 27 | */ |
28 | ||
29 | /* | |
30 | * Include necessary headers... | |
31 | */ | |
32 | ||
33 | #include "image.h" | |
34 | #include <ctype.h> | |
35 | ||
6de9968b | 36 | |
37 | /* | |
38 | * 'ImageReadPNM()' - Read a PNM image file. | |
39 | */ | |
40 | ||
081ab09a | 41 | int /* O - Read status */ |
42 | ImageReadPNM(image_t *img, /* IO - Image */ | |
43 | FILE *fp, /* I - Image file */ | |
44 | int primary, /* I - Primary choice for colorspace */ | |
45 | int secondary, /* I - Secondary choice for colorspace */ | |
46 | int saturation, /* I - Color saturation (%) */ | |
47 | int hue, /* I - Color hue (degrees) */ | |
48 | const ib_t *lut) /* I - Lookup table for gamma/brightness */ | |
516ba4c9 | 49 | { |
50 | int x, y; /* Looping vars */ | |
51 | int bpp; /* Bytes per pixel */ | |
52 | ib_t *in, /* Input pixels */ | |
53 | *inptr, /* Current input pixel */ | |
54 | *out, /* Output pixels */ | |
55 | *outptr, /* Current output pixel */ | |
56 | bit; /* Bit in input line */ | |
57 | char line[255], /* Input line */ | |
58 | *lineptr; /* Pointer in line */ | |
59 | int format, /* Format of PNM file */ | |
60 | val, /* Pixel value */ | |
61 | maxval; /* Maximum pixel value */ | |
62 | ||
63 | ||
64 | /* | |
65 | * Read the file header in the format: | |
66 | * | |
67 | * Pformat | |
68 | * # comment1 | |
69 | * # comment2 | |
70 | * ... | |
71 | * # commentN | |
72 | * width | |
73 | * height | |
74 | * max sample | |
75 | */ | |
76 | ||
77 | lineptr = fgets(line, sizeof(line), fp); | |
78 | lineptr ++; | |
79 | ||
80 | format = atoi(lineptr); | |
81 | while (isdigit(*lineptr)) | |
82 | lineptr ++; | |
83 | ||
84 | while (lineptr != NULL && img->xsize == 0) | |
85 | { | |
86 | if (*lineptr == '\0' || *lineptr == '#') | |
87 | lineptr = fgets(line, sizeof(line), fp); | |
88 | else if (isdigit(*lineptr)) | |
89 | { | |
90 | img->xsize = atoi(lineptr); | |
91 | while (isdigit(*lineptr)) | |
92 | lineptr ++; | |
93 | } | |
94 | else | |
95 | lineptr ++; | |
6de9968b | 96 | } |
516ba4c9 | 97 | |
98 | while (lineptr != NULL && img->ysize == 0) | |
99 | { | |
100 | if (*lineptr == '\0' || *lineptr == '#') | |
101 | lineptr = fgets(line, sizeof(line), fp); | |
102 | else if (isdigit(*lineptr)) | |
103 | { | |
104 | img->ysize = atoi(lineptr); | |
105 | while (isdigit(*lineptr)) | |
106 | lineptr ++; | |
107 | } | |
108 | else | |
109 | lineptr ++; | |
6de9968b | 110 | } |
516ba4c9 | 111 | |
112 | if (format != 1 && format != 4) | |
113 | { | |
114 | maxval = 0; | |
115 | ||
116 | while (lineptr != NULL && maxval == 0) | |
117 | { | |
118 | if (*lineptr == '\0' || *lineptr == '#') | |
119 | lineptr = fgets(line, sizeof(line), fp); | |
120 | else if (isdigit(*lineptr)) | |
121 | { | |
122 | maxval = atoi(lineptr); | |
123 | while (isdigit(*lineptr)) | |
124 | lineptr ++; | |
125 | } | |
126 | else | |
127 | lineptr ++; | |
6de9968b | 128 | } |
516ba4c9 | 129 | } |
130 | else | |
131 | maxval = 1; | |
132 | ||
133 | if (format == 1 || format == 2 || format == 4 || format == 5) | |
134 | img->colorspace = secondary; | |
135 | else | |
136 | img->colorspace = primary; | |
137 | ||
138 | ImageSetMaxTiles(img, 0); | |
139 | ||
140 | bpp = ImageGetDepth(img); | |
141 | in = malloc(img->xsize * 3); | |
142 | out = malloc(img->xsize * bpp); | |
143 | ||
144 | /* | |
145 | * Read the image file... | |
146 | */ | |
147 | ||
148 | for (y = 0; y < img->ysize; y ++) | |
149 | { | |
150 | switch (format) | |
151 | { | |
152 | case 1 : | |
153 | case 2 : | |
154 | for (x = img->xsize, inptr = in; x > 0; x --, inptr ++) | |
155 | if (fscanf(fp, "%d", &val) == 1) | |
156 | *inptr = 255 * val / maxval; | |
157 | break; | |
158 | ||
159 | case 3 : | |
160 | for (x = img->xsize, inptr = in; x > 0; x --, inptr += 3) | |
161 | { | |
162 | if (fscanf(fp, "%d", &val) == 1) | |
163 | inptr[0] = 255 * val / maxval; | |
164 | if (fscanf(fp, "%d", &val) == 1) | |
165 | inptr[1] = 255 * val / maxval; | |
166 | if (fscanf(fp, "%d", &val) == 1) | |
167 | inptr[2] = 255 * val / maxval; | |
6de9968b | 168 | } |
516ba4c9 | 169 | break; |
170 | ||
171 | case 4 : | |
172 | fread(out, (img->xsize + 7) / 8, 1, fp); | |
173 | for (x = img->xsize, inptr = in, outptr = out, bit = 128; | |
174 | x > 0; | |
175 | x --, inptr ++) | |
176 | { | |
177 | if (*outptr & bit) | |
178 | *inptr = 255; | |
179 | else | |
180 | *inptr = 0; | |
181 | ||
182 | if (bit > 1) | |
183 | bit >>= 1; | |
184 | else | |
185 | { | |
186 | bit = 128; | |
187 | inptr ++; | |
6de9968b | 188 | } |
189 | } | |
516ba4c9 | 190 | break; |
191 | ||
192 | case 5 : | |
193 | fread(in, img->xsize, 1, fp); | |
194 | break; | |
195 | ||
196 | case 6 : | |
197 | fread(in, img->xsize, 3, fp); | |
198 | break; | |
6de9968b | 199 | } |
516ba4c9 | 200 | |
201 | switch (format) | |
202 | { | |
203 | case 1 : | |
204 | case 2 : | |
205 | case 4 : | |
206 | case 5 : | |
207 | if (img->colorspace == IMAGE_WHITE) | |
6de9968b | 208 | { |
209 | if (lut) | |
210 | ImageLut(in, img->xsize, lut); | |
211 | ||
516ba4c9 | 212 | ImagePutRow(img, 0, y, img->xsize, in); |
6de9968b | 213 | } |
516ba4c9 | 214 | else |
215 | { | |
216 | switch (img->colorspace) | |
217 | { | |
218 | case IMAGE_RGB : | |
219 | ImageWhiteToRGB(in, out, img->xsize); | |
220 | break; | |
221 | case IMAGE_BLACK : | |
222 | ImageWhiteToBlack(in, out, img->xsize); | |
223 | break; | |
224 | case IMAGE_CMY : | |
225 | ImageWhiteToCMY(in, out, img->xsize); | |
226 | break; | |
227 | case IMAGE_CMYK : | |
228 | ImageWhiteToCMYK(in, out, img->xsize); | |
229 | break; | |
6de9968b | 230 | } |
231 | ||
232 | if (lut) | |
233 | ImageLut(out, img->xsize * bpp, lut); | |
516ba4c9 | 234 | |
235 | ImagePutRow(img, 0, y, img->xsize, out); | |
6de9968b | 236 | } |
516ba4c9 | 237 | break; |
238 | ||
239 | default : | |
240 | if ((saturation != 100 || hue != 0) && bpp > 1) | |
241 | ImageRGBAdjust(in, img->xsize, saturation, hue); | |
242 | ||
243 | if (img->colorspace == IMAGE_RGB) | |
6de9968b | 244 | { |
245 | if (lut) | |
246 | ImageLut(in, img->xsize * 3, lut); | |
247 | ||
516ba4c9 | 248 | ImagePutRow(img, 0, y, img->xsize, in); |
6de9968b | 249 | } |
516ba4c9 | 250 | else |
251 | { | |
252 | switch (img->colorspace) | |
253 | { | |
254 | case IMAGE_WHITE : | |
255 | ImageRGBToWhite(in, out, img->xsize); | |
256 | break; | |
257 | case IMAGE_BLACK : | |
258 | ImageRGBToBlack(in, out, img->xsize); | |
259 | break; | |
260 | case IMAGE_CMY : | |
261 | ImageRGBToCMY(in, out, img->xsize); | |
262 | break; | |
263 | case IMAGE_CMYK : | |
264 | ImageRGBToCMYK(in, out, img->xsize); | |
265 | break; | |
6de9968b | 266 | } |
267 | ||
268 | if (lut) | |
269 | ImageLut(out, img->xsize * bpp, lut); | |
516ba4c9 | 270 | |
271 | ImagePutRow(img, 0, y, img->xsize, out); | |
6de9968b | 272 | } |
516ba4c9 | 273 | break; |
6de9968b | 274 | } |
275 | } | |
516ba4c9 | 276 | |
277 | free(in); | |
278 | free(out); | |
279 | ||
280 | fclose(fp); | |
281 | ||
282 | return (0); | |
283 | } | |
284 | ||
285 | ||
286 | /* | |
71fe22b7 | 287 | * End of "$Id: image-pnm.c,v 1.6 2000/01/04 13:45:45 mike Exp $". |
516ba4c9 | 288 | */ |