]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/image-pnm.c
2 * "$Id: image-pnm.c 7374 2008-03-08 01:13:26Z mike $"
4 * Portable Any Map file routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 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 if ((lineptr
= fgets(line
, sizeof(line
), fp
)) == NULL
)
72 fputs("DEBUG: Bad PNM header!\n", stderr
);
79 format
= atoi(lineptr
);
80 while (isdigit(*lineptr
& 255))
83 while (lineptr
!= NULL
&& img
->xsize
== 0)
85 if (*lineptr
== '\0' || *lineptr
== '#')
86 lineptr
= fgets(line
, sizeof(line
), fp
);
87 else if (isdigit(*lineptr
& 255))
89 img
->xsize
= atoi(lineptr
);
90 while (isdigit(*lineptr
& 255))
97 while (lineptr
!= NULL
&& img
->ysize
== 0)
99 if (*lineptr
== '\0' || *lineptr
== '#')
100 lineptr
= fgets(line
, sizeof(line
), fp
);
101 else if (isdigit(*lineptr
& 255))
103 img
->ysize
= atoi(lineptr
);
104 while (isdigit(*lineptr
& 255))
111 if (format
!= 1 && format
!= 4)
115 while (lineptr
!= NULL
&& maxval
== 0)
117 if (*lineptr
== '\0' || *lineptr
== '#')
118 lineptr
= fgets(line
, sizeof(line
), fp
);
119 else if (isdigit(*lineptr
& 255))
121 maxval
= atoi(lineptr
);
122 while (isdigit(*lineptr
& 255))
132 if (img
->xsize
== 0 || img
->xsize
> CUPS_IMAGE_MAX_WIDTH
||
133 img
->ysize
== 0 || img
->ysize
> CUPS_IMAGE_MAX_HEIGHT
)
135 fprintf(stderr
, "DEBUG: Bad PNM dimensions %dx%d!\n",
136 img
->xsize
, img
->ysize
);
143 fprintf(stderr
, "DEBUG: Bad PNM max value %d!\n", maxval
);
148 if (format
== 1 || format
== 2 || format
== 4 || format
== 5)
149 img
->colorspace
= secondary
;
151 img
->colorspace
= (primary
== CUPS_IMAGE_RGB_CMYK
) ? CUPS_IMAGE_RGB
: primary
;
153 cupsImageSetMaxTiles(img
, 0);
155 bpp
= cupsImageGetDepth(img
);
157 if ((in
= malloc(img
->xsize
* 3)) == NULL
)
159 fputs("DEBUG: Unable to allocate memory!\n", stderr
);
164 if ((out
= malloc(img
->xsize
* bpp
)) == NULL
)
166 fputs("DEBUG: Unable to allocate memory!\n", stderr
);
173 * Read the image file...
176 for (y
= 0; y
< img
->ysize
; y
++)
181 for (x
= img
->xsize
, inptr
= in
; x
> 0; x
--, inptr
++)
182 if (fscanf(fp
, "%d", &val
) == 1)
183 *inptr
= val
? 0 : 255;
187 for (x
= img
->xsize
, inptr
= in
; x
> 0; x
--, inptr
++)
188 if (fscanf(fp
, "%d", &val
) == 1)
189 *inptr
= 255 * val
/ maxval
;
193 for (x
= img
->xsize
, inptr
= in
; x
> 0; x
--, inptr
+= 3)
195 if (fscanf(fp
, "%d", &val
) == 1)
196 inptr
[0] = 255 * val
/ maxval
;
197 if (fscanf(fp
, "%d", &val
) == 1)
198 inptr
[1] = 255 * val
/ maxval
;
199 if (fscanf(fp
, "%d", &val
) == 1)
200 inptr
[2] = 255 * val
/ maxval
;
205 fread(out
, (img
->xsize
+ 7) / 8, 1, fp
);
206 for (x
= img
->xsize
, inptr
= in
, outptr
= out
, bit
= 128;
226 fread(in
, img
->xsize
, 1, fp
);
230 fread(in
, img
->xsize
, 3, fp
);
240 if (img
->colorspace
== CUPS_IMAGE_WHITE
)
243 cupsImageLut(in
, img
->xsize
, lut
);
245 _cupsImagePutRow(img
, 0, y
, img
->xsize
, in
);
249 switch (img
->colorspace
)
254 case CUPS_IMAGE_RGB
:
255 cupsImageWhiteToRGB(in
, out
, img
->xsize
);
257 case CUPS_IMAGE_BLACK
:
258 cupsImageWhiteToBlack(in
, out
, img
->xsize
);
260 case CUPS_IMAGE_CMY
:
261 cupsImageWhiteToCMY(in
, out
, img
->xsize
);
263 case CUPS_IMAGE_CMYK
:
264 cupsImageWhiteToCMYK(in
, out
, img
->xsize
);
269 cupsImageLut(out
, img
->xsize
* bpp
, lut
);
271 _cupsImagePutRow(img
, 0, y
, img
->xsize
, out
);
276 if ((saturation
!= 100 || hue
!= 0) && bpp
> 1)
277 cupsImageRGBAdjust(in
, img
->xsize
, saturation
, hue
);
279 switch (img
->colorspace
)
284 case CUPS_IMAGE_WHITE
:
285 cupsImageRGBToWhite(in
, out
, img
->xsize
);
287 case CUPS_IMAGE_RGB
:
288 cupsImageRGBToRGB(in
, out
, img
->xsize
);
290 case CUPS_IMAGE_BLACK
:
291 cupsImageRGBToBlack(in
, out
, img
->xsize
);
293 case CUPS_IMAGE_CMY
:
294 cupsImageRGBToCMY(in
, out
, img
->xsize
);
296 case CUPS_IMAGE_CMYK
:
297 cupsImageRGBToCMYK(in
, out
, img
->xsize
);
302 cupsImageLut(out
, img
->xsize
* bpp
, lut
);
304 _cupsImagePutRow(img
, 0, y
, img
->xsize
, out
);
319 * End of "$Id: image-pnm.c 7374 2008-03-08 01:13:26Z mike $".