4 * Copyright (C) 1994-1996, Thomas G. Lane.
5 * This file is part of the Independent JPEG Group's software.
6 * For conditions of distribution and use, see the accompanying README.ijg
9 * This file implements djpeg's "-map file" switch. It reads a source image
10 * and constructs a colormap to be supplied to the JPEG decompressor.
12 * Currently, these file formats are supported for the map file:
13 * GIF: the contents of the GIF's global colormap are used.
14 * PPM (either text or raw flavor): the entire file is read and
15 * each unique pixel value is entered in the map.
16 * Note that reading a large PPM file will be horrendously slow.
17 * Typically, a PPM-format map file should contain just one pixel
18 * of each desired color. Such a file can be extracted from an
19 * ordinary image PPM file with ppmtomap(1).
21 * Rescaling a PPM that has a maxval unequal to MAXJSAMPLE is not
22 * currently implemented.
25 #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
27 #ifdef QUANT_2PASS_SUPPORTED /* otherwise can't quantize to supplied map */
29 /* Portions of this code are based on the PBMPLUS library, which is:
31 ** Copyright (C) 1988 by Jef Poskanzer.
33 ** Permission to use, copy, modify, and distribute this software and its
34 ** documentation for any purpose and without fee is hereby granted, provided
35 ** that the above copyright notice appear in all copies and that both that
36 ** copyright notice and this permission notice appear in supporting
37 ** documentation. This software is provided "as is" without express or
43 * Add a (potentially) new color to the color map.
47 add_map_entry(j_decompress_ptr cinfo, int R, int G, int B)
49 JSAMPROW colormap0 = cinfo->colormap[0];
50 JSAMPROW colormap1 = cinfo->colormap[1];
51 JSAMPROW colormap2 = cinfo->colormap[2];
52 int ncolors = cinfo->actual_number_of_colors;
55 /* Check for duplicate color. */
56 for (index = 0; index < ncolors; index++) {
57 if (GETJSAMPLE(colormap0[index]) == R &&
58 GETJSAMPLE(colormap1[index]) == G &&
59 GETJSAMPLE(colormap2[index]) == B)
60 return; /* color is already in map */
63 /* Check for map overflow. */
64 if (ncolors >= (MAXJSAMPLE + 1))
65 ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, (MAXJSAMPLE + 1));
67 /* OK, add color to map. */
68 colormap0[ncolors] = (JSAMPLE)R;
69 colormap1[ncolors] = (JSAMPLE)G;
70 colormap2[ncolors] = (JSAMPLE)B;
71 cinfo->actual_number_of_colors++;
76 * Extract color map from a GIF file.
80 read_gif_map(j_decompress_ptr cinfo, FILE *infile)
86 /* Initial 'G' has already been read by read_color_map */
87 /* Read the rest of the GIF header and logical screen descriptor */
88 for (i = 1; i < 13; i++) {
89 if ((header[i] = getc(infile)) == EOF)
90 ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
93 /* Verify GIF Header */
94 if (header[1] != 'I' || header[2] != 'F')
95 ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
97 /* There must be a global color map. */
98 if ((header[10] & 0x80) == 0)
99 ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
102 colormaplen = 2 << (header[10] & 0x07);
104 for (i = 0; i < colormaplen; i++) {
108 if (R == EOF || G == EOF || B == EOF)
109 ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
111 R << (BITS_IN_JSAMPLE - 8),
112 G << (BITS_IN_JSAMPLE - 8),
113 B << (BITS_IN_JSAMPLE - 8));
118 /* Support routines for reading PPM */
122 pbm_getc(FILE *infile)
123 /* Read next char, skipping over any comments */
124 /* A comment/newline sequence is returned as a newline */
132 } while (ch != '\n' && ch != EOF);
139 read_pbm_integer(j_decompress_ptr cinfo, FILE *infile)
140 /* Read an unsigned decimal integer from the PPM file */
141 /* Swallows one trailing character after the integer */
142 /* Note that on a 16-bit-int machine, only values up to 64k can be read. */
143 /* This should not be a problem in practice. */
146 register unsigned int val;
148 /* Skip any leading whitespace */
150 ch = pbm_getc(infile);
152 ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
153 } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
155 if (ch < '0' || ch > '9')
156 ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
159 while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
168 * Extract color map from a PPM file.
172 read_ppm_map(j_decompress_ptr cinfo, FILE *infile)
175 unsigned int w, h, maxval, row, col;
178 /* Initial 'P' has already been read by read_color_map */
179 c = getc(infile); /* save format discriminator for a sec */
181 /* while we fetch the remaining header info */
182 w = read_pbm_integer(cinfo, infile);
183 h = read_pbm_integer(cinfo, infile);
184 maxval = read_pbm_integer(cinfo, infile);
186 if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
187 ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
189 /* For now, we don't support rescaling from an unusual maxval. */
190 if (maxval != (unsigned int)MAXJSAMPLE)
191 ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
194 case '3': /* it's a text-format PPM file */
195 for (row = 0; row < h; row++) {
196 for (col = 0; col < w; col++) {
197 R = read_pbm_integer(cinfo, infile);
198 G = read_pbm_integer(cinfo, infile);
199 B = read_pbm_integer(cinfo, infile);
200 add_map_entry(cinfo, R, G, B);
205 case '6': /* it's a raw-format PPM file */
206 for (row = 0; row < h; row++) {
207 for (col = 0; col < w; col++) {
211 if (R == EOF || G == EOF || B == EOF)
212 ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
213 add_map_entry(cinfo, R, G, B);
219 ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
226 * Main entry point from djpeg.c.
227 * Input: opened input file (from file name argument on command line).
228 * Output: colormap and actual_number_of_colors fields are set in cinfo.
232 read_color_map(j_decompress_ptr cinfo, FILE *infile)
234 /* Allocate space for a color map of maximum supported size. */
235 cinfo->colormap = (*cinfo->mem->alloc_sarray)
236 ((j_common_ptr)cinfo, JPOOL_IMAGE,
237 (JDIMENSION)(MAXJSAMPLE + 1), (JDIMENSION)3);
238 cinfo->actual_number_of_colors = 0; /* initialize map to empty */
240 /* Read first byte to determine file format */
241 switch (getc(infile)) {
243 read_gif_map(cinfo, infile);
246 read_ppm_map(cinfo, infile);
249 ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
254 #endif /* QUANT_2PASS_SUPPORTED */