]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/image-pnm.c
1f87829d3e3fbf57b5b5f158a63aa89e616a3782
2 * "$Id: image-pnm.c 6649 2007-07-11 21:46:42Z mike $"
4 * Portable Any Map file routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1993-2007 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
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/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * _cupsImageReadPNM() - Read a PNM image file.
23 * Include necessary headers...
26 #include "image-private.h"
30 * '_cupsImageReadPNM()' - Read a PNM image file.
33 int /* O - Read status */
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 */
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 */
58 * Read the file header in the format:
70 lineptr
= fgets(line
, sizeof(line
), fp
);
73 format
= atoi(lineptr
);
74 while (isdigit(*lineptr
& 255))
77 while (lineptr
!= NULL
&& img
->xsize
== 0)
79 if (*lineptr
== '\0' || *lineptr
== '#')
80 lineptr
= fgets(line
, sizeof(line
), fp
);
81 else if (isdigit(*lineptr
& 255))
83 img
->xsize
= atoi(lineptr
);
84 while (isdigit(*lineptr
& 255))
91 while (lineptr
!= NULL
&& img
->ysize
== 0)
93 if (*lineptr
== '\0' || *lineptr
== '#')
94 lineptr
= fgets(line
, sizeof(line
), fp
);
95 else if (isdigit(*lineptr
& 255))
97 img
->ysize
= atoi(lineptr
);
98 while (isdigit(*lineptr
& 255))
105 if (format
!= 1 && format
!= 4)
109 while (lineptr
!= NULL
&& maxval
== 0)
111 if (*lineptr
== '\0' || *lineptr
== '#')
112 lineptr
= fgets(line
, sizeof(line
), fp
);
113 else if (isdigit(*lineptr
& 255))
115 maxval
= atoi(lineptr
);
116 while (isdigit(*lineptr
& 255))
126 if (img
->xsize
== 0 || img
->xsize
> CUPS_IMAGE_MAX_WIDTH
||
127 img
->ysize
== 0 || img
->ysize
> CUPS_IMAGE_MAX_HEIGHT
)
129 fprintf(stderr
, "DEBUG: Bad PNM dimensions %dx%d!\n",
130 img
->xsize
, img
->ysize
);
137 fprintf(stderr
, "DEBUG: Bad PNM max value %d!\n", maxval
);
142 if (format
== 1 || format
== 2 || format
== 4 || format
== 5)
143 img
->colorspace
= secondary
;
145 img
->colorspace
= (primary
== CUPS_IMAGE_RGB_CMYK
) ? CUPS_IMAGE_RGB
: primary
;
147 cupsImageSetMaxTiles(img
, 0);
149 bpp
= cupsImageGetDepth(img
);
150 in
= malloc(img
->xsize
* 3);
151 out
= malloc(img
->xsize
* bpp
);
154 * Read the image file...
157 for (y
= 0; y
< img
->ysize
; y
++)
163 for (x
= img
->xsize
, inptr
= in
; x
> 0; x
--, inptr
++)
164 if (fscanf(fp
, "%d", &val
) == 1)
165 *inptr
= 255 * val
/ maxval
;
169 for (x
= img
->xsize
, inptr
= in
; x
> 0; x
--, inptr
+= 3)
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
;
181 fread(out
, (img
->xsize
+ 7) / 8, 1, fp
);
182 for (x
= img
->xsize
, inptr
= in
, outptr
= out
, bit
= 128;
202 fread(in
, img
->xsize
, 1, fp
);
206 fread(in
, img
->xsize
, 3, fp
);
216 if (img
->colorspace
== CUPS_IMAGE_WHITE
)
219 cupsImageLut(in
, img
->xsize
, lut
);
221 _cupsImagePutRow(img
, 0, y
, img
->xsize
, in
);
225 switch (img
->colorspace
)
230 case CUPS_IMAGE_RGB
:
231 cupsImageWhiteToRGB(in
, out
, img
->xsize
);
233 case CUPS_IMAGE_BLACK
:
234 cupsImageWhiteToBlack(in
, out
, img
->xsize
);
236 case CUPS_IMAGE_CMY
:
237 cupsImageWhiteToCMY(in
, out
, img
->xsize
);
239 case CUPS_IMAGE_CMYK
:
240 cupsImageWhiteToCMYK(in
, out
, img
->xsize
);
245 cupsImageLut(out
, img
->xsize
* bpp
, lut
);
247 _cupsImagePutRow(img
, 0, y
, img
->xsize
, out
);
252 if ((saturation
!= 100 || hue
!= 0) && bpp
> 1)
253 cupsImageRGBAdjust(in
, img
->xsize
, saturation
, hue
);
255 switch (img
->colorspace
)
260 case CUPS_IMAGE_WHITE
:
261 cupsImageRGBToWhite(in
, out
, img
->xsize
);
263 case CUPS_IMAGE_RGB
:
264 cupsImageRGBToRGB(in
, out
, img
->xsize
);
266 case CUPS_IMAGE_BLACK
:
267 cupsImageRGBToBlack(in
, out
, img
->xsize
);
269 case CUPS_IMAGE_CMY
:
270 cupsImageRGBToCMY(in
, out
, img
->xsize
);
272 case CUPS_IMAGE_CMYK
:
273 cupsImageRGBToCMYK(in
, out
, img
->xsize
);
278 cupsImageLut(out
, img
->xsize
* bpp
, lut
);
280 _cupsImagePutRow(img
, 0, y
, img
->xsize
, out
);
295 * End of "$Id: image-pnm.c 6649 2007-07-11 21:46:42Z mike $".