]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/image-pnm.c
2 * "$Id: image-pnm.c,v 1.6 2000/01/04 13:45:45 mike Exp $"
4 * Portable Any Map file routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1993-2000 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-3111 USA
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * ImageReadPNM() - Read a PNM image file.
30 * Include necessary headers...
38 * 'ImageReadPNM()' - Read a PNM image file.
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 */
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 */
65 * Read the file header in the format:
77 lineptr
= fgets(line
, sizeof(line
), fp
);
80 format
= atoi(lineptr
);
81 while (isdigit(*lineptr
))
84 while (lineptr
!= NULL
&& img
->xsize
== 0)
86 if (*lineptr
== '\0' || *lineptr
== '#')
87 lineptr
= fgets(line
, sizeof(line
), fp
);
88 else if (isdigit(*lineptr
))
90 img
->xsize
= atoi(lineptr
);
91 while (isdigit(*lineptr
))
98 while (lineptr
!= NULL
&& img
->ysize
== 0)
100 if (*lineptr
== '\0' || *lineptr
== '#')
101 lineptr
= fgets(line
, sizeof(line
), fp
);
102 else if (isdigit(*lineptr
))
104 img
->ysize
= atoi(lineptr
);
105 while (isdigit(*lineptr
))
112 if (format
!= 1 && format
!= 4)
116 while (lineptr
!= NULL
&& maxval
== 0)
118 if (*lineptr
== '\0' || *lineptr
== '#')
119 lineptr
= fgets(line
, sizeof(line
), fp
);
120 else if (isdigit(*lineptr
))
122 maxval
= atoi(lineptr
);
123 while (isdigit(*lineptr
))
133 if (format
== 1 || format
== 2 || format
== 4 || format
== 5)
134 img
->colorspace
= secondary
;
136 img
->colorspace
= primary
;
138 ImageSetMaxTiles(img
, 0);
140 bpp
= ImageGetDepth(img
);
141 in
= malloc(img
->xsize
* 3);
142 out
= malloc(img
->xsize
* bpp
);
145 * Read the image file...
148 for (y
= 0; y
< img
->ysize
; y
++)
154 for (x
= img
->xsize
, inptr
= in
; x
> 0; x
--, inptr
++)
155 if (fscanf(fp
, "%d", &val
) == 1)
156 *inptr
= 255 * val
/ maxval
;
160 for (x
= img
->xsize
, inptr
= in
; x
> 0; x
--, inptr
+= 3)
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
;
172 fread(out
, (img
->xsize
+ 7) / 8, 1, fp
);
173 for (x
= img
->xsize
, inptr
= in
, outptr
= out
, bit
= 128;
193 fread(in
, img
->xsize
, 1, fp
);
197 fread(in
, img
->xsize
, 3, fp
);
207 if (img
->colorspace
== IMAGE_WHITE
)
210 ImageLut(in
, img
->xsize
, lut
);
212 ImagePutRow(img
, 0, y
, img
->xsize
, in
);
216 switch (img
->colorspace
)
219 ImageWhiteToRGB(in
, out
, img
->xsize
);
222 ImageWhiteToBlack(in
, out
, img
->xsize
);
225 ImageWhiteToCMY(in
, out
, img
->xsize
);
228 ImageWhiteToCMYK(in
, out
, img
->xsize
);
233 ImageLut(out
, img
->xsize
* bpp
, lut
);
235 ImagePutRow(img
, 0, y
, img
->xsize
, out
);
240 if ((saturation
!= 100 || hue
!= 0) && bpp
> 1)
241 ImageRGBAdjust(in
, img
->xsize
, saturation
, hue
);
243 if (img
->colorspace
== IMAGE_RGB
)
246 ImageLut(in
, img
->xsize
* 3, lut
);
248 ImagePutRow(img
, 0, y
, img
->xsize
, in
);
252 switch (img
->colorspace
)
255 ImageRGBToWhite(in
, out
, img
->xsize
);
258 ImageRGBToBlack(in
, out
, img
->xsize
);
261 ImageRGBToCMY(in
, out
, img
->xsize
);
264 ImageRGBToCMYK(in
, out
, img
->xsize
);
269 ImageLut(out
, img
->xsize
* bpp
, lut
);
271 ImagePutRow(img
, 0, y
, img
->xsize
, out
);
287 * End of "$Id: image-pnm.c,v 1.6 2000/01/04 13:45:45 mike Exp $".