8 * Copyright (C) 1994-1996, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
12 * This file implements djpeg's "-map file" switch. It reads a source image
13 * and constructs a colormap to be supplied to the JPEG decompressor.
15 * Currently, these file formats are supported for the map file:
16 * GIF: the contents of the GIF's global colormap are used.
17 * PPM (either text or raw flavor): the entire file is read and
18 * each unique pixel value is entered in the map.
19 * Note that reading a large PPM file will be horrendously slow.
20 * Typically, a PPM-format map file should contain just one pixel
21 * of each desired color. Such a file can be extracted from an
22 * ordinary image PPM file with ppmtomap(1).
24 * Rescaling a PPM that has a maxval unequal to MAXJSAMPLE is not
25 * currently implemented.
28 #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
30 #ifdef QUANT_2PASS_SUPPORTED /* otherwise can't quantize to supplied map */
32 /* Portions of this code are based on the PBMPLUS library, which is:
34 ** Copyright (C) 1988 by Jef Poskanzer.
36 ** Permission to use, copy, modify, and distribute this software and its
37 ** documentation for any purpose and without fee is hereby granted, provided
38 ** that the above copyright notice appear in all copies and that both that
39 ** copyright notice and this permission notice appear in supporting
40 ** documentation. This software is provided "as is" without express or
46 * Add a (potentially) new color to the color map.
50 add_map_entry (j_decompress_ptr cinfo
, int R
, int G
, int B
)
52 JSAMPROW colormap0
= cinfo
->colormap
[0];
53 JSAMPROW colormap1
= cinfo
->colormap
[1];
54 JSAMPROW colormap2
= cinfo
->colormap
[2];
55 int ncolors
= cinfo
->actual_number_of_colors
;
58 /* Check for duplicate color. */
59 for (index
= 0; index
< ncolors
; index
++) {
60 if (GETJSAMPLE(colormap0
[index
]) == R
&&
61 GETJSAMPLE(colormap1
[index
]) == G
&&
62 GETJSAMPLE(colormap2
[index
]) == B
)
63 return; /* color is already in map */
66 /* Check for map overflow. */
67 if (ncolors
>= (MAXJSAMPLE
+1))
68 ERREXIT1(cinfo
, JERR_QUANT_MANY_COLORS
, (MAXJSAMPLE
+1));
70 /* OK, add color to map. */
71 colormap0
[ncolors
] = (JSAMPLE
) R
;
72 colormap1
[ncolors
] = (JSAMPLE
) G
;
73 colormap2
[ncolors
] = (JSAMPLE
) B
;
74 cinfo
->actual_number_of_colors
++;
79 * Extract color map from a GIF file.
83 read_gif_map (j_decompress_ptr cinfo
, FILE * infile
)
89 /* Initial 'G' has already been read by read_color_map */
90 /* Read the rest of the GIF header and logical screen descriptor */
91 for (i
= 1; i
< 13; i
++) {
92 if ((header
[i
] = getc(infile
)) == EOF
)
93 ERREXIT(cinfo
, JERR_BAD_CMAP_FILE
);
96 /* Verify GIF Header */
97 if (header
[1] != 'I' || header
[2] != 'F')
98 ERREXIT(cinfo
, JERR_BAD_CMAP_FILE
);
100 /* There must be a global color map. */
101 if ((header
[10] & 0x80) == 0)
102 ERREXIT(cinfo
, JERR_BAD_CMAP_FILE
);
105 colormaplen
= 2 << (header
[10] & 0x07);
107 for (i
= 0; i
< colormaplen
; i
++) {
111 if (R
== EOF
|| G
== EOF
|| B
== EOF
)
112 ERREXIT(cinfo
, JERR_BAD_CMAP_FILE
);
114 R
<< (BITS_IN_JSAMPLE
-8),
115 G
<< (BITS_IN_JSAMPLE
-8),
116 B
<< (BITS_IN_JSAMPLE
-8));
121 /* Support routines for reading PPM */
125 pbm_getc (FILE * infile
)
126 /* Read next char, skipping over any comments */
127 /* A comment/newline sequence is returned as a newline */
135 } while (ch
!= '\n' && ch
!= EOF
);
142 read_pbm_integer (j_decompress_ptr cinfo
, FILE * infile
)
143 /* Read an unsigned decimal integer from the PPM file */
144 /* Swallows one trailing character after the integer */
145 /* Note that on a 16-bit-int machine, only values up to 64k can be read. */
146 /* This should not be a problem in practice. */
149 register unsigned int val
;
151 /* Skip any leading whitespace */
153 ch
= pbm_getc(infile
);
155 ERREXIT(cinfo
, JERR_BAD_CMAP_FILE
);
156 } while (ch
== ' ' || ch
== '\t' || ch
== '\n' || ch
== '\r');
158 if (ch
< '0' || ch
> '9')
159 ERREXIT(cinfo
, JERR_BAD_CMAP_FILE
);
162 while ((ch
= pbm_getc(infile
)) >= '0' && ch
<= '9') {
171 * Extract color map from a PPM file.
175 read_ppm_map (j_decompress_ptr cinfo
, FILE * infile
)
178 unsigned int w
, h
, maxval
, row
, col
;
181 /* Initial 'P' has already been read by read_color_map */
182 c
= getc(infile
); /* save format discriminator for a sec */
184 /* while we fetch the remaining header info */
185 w
= read_pbm_integer(cinfo
, infile
);
186 h
= read_pbm_integer(cinfo
, infile
);
187 maxval
= read_pbm_integer(cinfo
, infile
);
189 if (w
<= 0 || h
<= 0 || maxval
<= 0) /* error check */
190 ERREXIT(cinfo
, JERR_BAD_CMAP_FILE
);
192 /* For now, we don't support rescaling from an unusual maxval. */
193 if (maxval
!= (unsigned int) MAXJSAMPLE
)
194 ERREXIT(cinfo
, JERR_BAD_CMAP_FILE
);
197 case '3': /* it's a text-format PPM file */
198 for (row
= 0; row
< h
; row
++) {
199 for (col
= 0; col
< w
; col
++) {
200 R
= read_pbm_integer(cinfo
, infile
);
201 G
= read_pbm_integer(cinfo
, infile
);
202 B
= read_pbm_integer(cinfo
, infile
);
203 add_map_entry(cinfo
, R
, G
, B
);
208 case '6': /* it's a raw-format PPM file */
209 for (row
= 0; row
< h
; row
++) {
210 for (col
= 0; col
< w
; col
++) {
214 if (R
== EOF
|| G
== EOF
|| B
== EOF
)
215 ERREXIT(cinfo
, JERR_BAD_CMAP_FILE
);
216 add_map_entry(cinfo
, R
, G
, B
);
222 ERREXIT(cinfo
, JERR_BAD_CMAP_FILE
);
229 * Main entry point from djpeg.c.
230 * Input: opened input file (from file name argument on command line).
231 * Output: colormap and actual_number_of_colors fields are set in cinfo.
235 read_color_map (j_decompress_ptr cinfo
, FILE * infile
)
237 /* Allocate space for a color map of maximum supported size. */
238 cinfo
->colormap
= (*cinfo
->mem
->alloc_sarray
)
239 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
240 (JDIMENSION
) (MAXJSAMPLE
+1), (JDIMENSION
) 3);
241 cinfo
->actual_number_of_colors
= 0; /* initialize map to empty */
243 /* Read first byte to determine file format */
244 switch (getc(infile
)) {
246 read_gif_map(cinfo
, infile
);
249 read_ppm_map(cinfo
, infile
);
252 ERREXIT(cinfo
, JERR_BAD_CMAP_FILE
);
257 #endif /* QUANT_2PASS_SUPPORTED */