]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/image-pnm.c
2 * "$Id: image-pnm.c 5509 2006-05-11 11:41:36Z mike $"
4 * Portable Any Map file routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1993-2006 by Easy Software Products.
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
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * _cupsImageReadPNM() - Read a PNM image file.
32 * Include necessary headers...
35 #include "image-private.h"
39 * '_cupsImageReadPNM()' - Read a PNM image file.
42 int /* O - Read status */
44 cups_image_t
*img
, /* IO - cupsImage */
45 FILE *fp
, /* I - cupsImage file */
46 cups_icspace_t primary
, /* I - Primary choice for colorspace */
47 cups_icspace_t secondary
, /* I - Secondary choice for colorspace */
48 int saturation
, /* I - Color saturation (%) */
49 int hue
, /* I - Color hue (degrees) */
50 const cups_ib_t
*lut
) /* I - Lookup table for gamma/brightness */
52 int x
, y
; /* Looping vars */
53 int bpp
; /* Bytes per pixel */
54 cups_ib_t
*in
, /* Input pixels */
55 *inptr
, /* Current input pixel */
56 *out
, /* Output pixels */
57 *outptr
, /* Current output pixel */
58 bit
; /* Bit in input line */
59 char line
[255], /* Input line */
60 *lineptr
; /* Pointer in line */
61 int format
, /* Format of PNM file */
62 val
, /* Pixel value */
63 maxval
; /* Maximum pixel value */
67 * Read the file header in the format:
79 lineptr
= fgets(line
, sizeof(line
), fp
);
82 format
= atoi(lineptr
);
83 while (isdigit(*lineptr
& 255))
86 while (lineptr
!= NULL
&& img
->xsize
== 0)
88 if (*lineptr
== '\0' || *lineptr
== '#')
89 lineptr
= fgets(line
, sizeof(line
), fp
);
90 else if (isdigit(*lineptr
& 255))
92 img
->xsize
= atoi(lineptr
);
93 while (isdigit(*lineptr
& 255))
100 while (lineptr
!= NULL
&& img
->ysize
== 0)
102 if (*lineptr
== '\0' || *lineptr
== '#')
103 lineptr
= fgets(line
, sizeof(line
), fp
);
104 else if (isdigit(*lineptr
& 255))
106 img
->ysize
= atoi(lineptr
);
107 while (isdigit(*lineptr
& 255))
114 if (format
!= 1 && format
!= 4)
118 while (lineptr
!= NULL
&& maxval
== 0)
120 if (*lineptr
== '\0' || *lineptr
== '#')
121 lineptr
= fgets(line
, sizeof(line
), fp
);
122 else if (isdigit(*lineptr
& 255))
124 maxval
= atoi(lineptr
);
125 while (isdigit(*lineptr
& 255))
135 if (img
->xsize
== 0 || img
->xsize
> CUPS_IMAGE_MAX_WIDTH
||
136 img
->ysize
== 0 || img
->ysize
> CUPS_IMAGE_MAX_HEIGHT
)
138 fprintf(stderr
, "ERROR: Bad PNM dimensions %dx%d!\n",
139 img
->xsize
, img
->ysize
);
146 fprintf(stderr
, "ERROR: Bad PNM max value %d!\n", maxval
);
151 if (format
== 1 || format
== 2 || format
== 4 || format
== 5)
152 img
->colorspace
= secondary
;
154 img
->colorspace
= (primary
== CUPS_IMAGE_RGB_CMYK
) ? CUPS_IMAGE_RGB
: primary
;
156 cupsImageSetMaxTiles(img
, 0);
158 bpp
= cupsImageGetDepth(img
);
159 in
= malloc(img
->xsize
* 3);
160 out
= malloc(img
->xsize
* bpp
);
163 * Read the image file...
166 for (y
= 0; y
< img
->ysize
; y
++)
172 for (x
= img
->xsize
, inptr
= in
; x
> 0; x
--, inptr
++)
173 if (fscanf(fp
, "%d", &val
) == 1)
174 *inptr
= 255 * val
/ maxval
;
178 for (x
= img
->xsize
, inptr
= in
; x
> 0; x
--, inptr
+= 3)
180 if (fscanf(fp
, "%d", &val
) == 1)
181 inptr
[0] = 255 * val
/ maxval
;
182 if (fscanf(fp
, "%d", &val
) == 1)
183 inptr
[1] = 255 * val
/ maxval
;
184 if (fscanf(fp
, "%d", &val
) == 1)
185 inptr
[2] = 255 * val
/ maxval
;
190 fread(out
, (img
->xsize
+ 7) / 8, 1, fp
);
191 for (x
= img
->xsize
, inptr
= in
, outptr
= out
, bit
= 128;
211 fread(in
, img
->xsize
, 1, fp
);
215 fread(in
, img
->xsize
, 3, fp
);
225 if (img
->colorspace
== CUPS_IMAGE_WHITE
)
228 cupsImageLut(in
, img
->xsize
, lut
);
230 _cupsImagePutRow(img
, 0, y
, img
->xsize
, in
);
234 switch (img
->colorspace
)
239 case CUPS_IMAGE_RGB
:
240 cupsImageWhiteToRGB(in
, out
, img
->xsize
);
242 case CUPS_IMAGE_BLACK
:
243 cupsImageWhiteToBlack(in
, out
, img
->xsize
);
245 case CUPS_IMAGE_CMY
:
246 cupsImageWhiteToCMY(in
, out
, img
->xsize
);
248 case CUPS_IMAGE_CMYK
:
249 cupsImageWhiteToCMYK(in
, out
, img
->xsize
);
254 cupsImageLut(out
, img
->xsize
* bpp
, lut
);
256 _cupsImagePutRow(img
, 0, y
, img
->xsize
, out
);
261 if ((saturation
!= 100 || hue
!= 0) && bpp
> 1)
262 cupsImageRGBAdjust(in
, img
->xsize
, saturation
, hue
);
264 switch (img
->colorspace
)
269 case CUPS_IMAGE_WHITE
:
270 cupsImageRGBToWhite(in
, out
, img
->xsize
);
272 case CUPS_IMAGE_RGB
:
273 cupsImageRGBToRGB(in
, out
, img
->xsize
);
275 case CUPS_IMAGE_BLACK
:
276 cupsImageRGBToBlack(in
, out
, img
->xsize
);
278 case CUPS_IMAGE_CMY
:
279 cupsImageRGBToCMY(in
, out
, img
->xsize
);
281 case CUPS_IMAGE_CMYK
:
282 cupsImageRGBToCMYK(in
, out
, img
->xsize
);
287 cupsImageLut(out
, img
->xsize
* bpp
, lut
);
289 _cupsImagePutRow(img
, 0, y
, img
->xsize
, out
);
304 * End of "$Id: image-pnm.c 5509 2006-05-11 11:41:36Z mike $".