xref: /aosp_15_r20/external/libjpeg-turbo/rdcolmap.c (revision dfc6aa5c1cfd4bc4e2018dc74aa96e29ee49c6da)
1*dfc6aa5cSAndroid Build Coastguard Worker /*
2*dfc6aa5cSAndroid Build Coastguard Worker  * rdcolmap.c
3*dfc6aa5cSAndroid Build Coastguard Worker  *
4*dfc6aa5cSAndroid Build Coastguard Worker  * Copyright (C) 1994-1996, Thomas G. Lane.
5*dfc6aa5cSAndroid Build Coastguard Worker  * This file is part of the Independent JPEG Group's software.
6*dfc6aa5cSAndroid Build Coastguard Worker  * For conditions of distribution and use, see the accompanying README.ijg
7*dfc6aa5cSAndroid Build Coastguard Worker  * file.
8*dfc6aa5cSAndroid Build Coastguard Worker  *
9*dfc6aa5cSAndroid Build Coastguard Worker  * This file implements djpeg's "-map file" switch.  It reads a source image
10*dfc6aa5cSAndroid Build Coastguard Worker  * and constructs a colormap to be supplied to the JPEG decompressor.
11*dfc6aa5cSAndroid Build Coastguard Worker  *
12*dfc6aa5cSAndroid Build Coastguard Worker  * Currently, these file formats are supported for the map file:
13*dfc6aa5cSAndroid Build Coastguard Worker  *   GIF: the contents of the GIF's global colormap are used.
14*dfc6aa5cSAndroid Build Coastguard Worker  *   PPM (either text or raw flavor): the entire file is read and
15*dfc6aa5cSAndroid Build Coastguard Worker  *      each unique pixel value is entered in the map.
16*dfc6aa5cSAndroid Build Coastguard Worker  * Note that reading a large PPM file will be horrendously slow.
17*dfc6aa5cSAndroid Build Coastguard Worker  * Typically, a PPM-format map file should contain just one pixel
18*dfc6aa5cSAndroid Build Coastguard Worker  * of each desired color.  Such a file can be extracted from an
19*dfc6aa5cSAndroid Build Coastguard Worker  * ordinary image PPM file with ppmtomap(1).
20*dfc6aa5cSAndroid Build Coastguard Worker  *
21*dfc6aa5cSAndroid Build Coastguard Worker  * Rescaling a PPM that has a maxval unequal to MAXJSAMPLE is not
22*dfc6aa5cSAndroid Build Coastguard Worker  * currently implemented.
23*dfc6aa5cSAndroid Build Coastguard Worker  */
24*dfc6aa5cSAndroid Build Coastguard Worker 
25*dfc6aa5cSAndroid Build Coastguard Worker #include "cdjpeg.h"             /* Common decls for cjpeg/djpeg applications */
26*dfc6aa5cSAndroid Build Coastguard Worker 
27*dfc6aa5cSAndroid Build Coastguard Worker #ifdef QUANT_2PASS_SUPPORTED    /* otherwise can't quantize to supplied map */
28*dfc6aa5cSAndroid Build Coastguard Worker 
29*dfc6aa5cSAndroid Build Coastguard Worker /* Portions of this code are based on the PBMPLUS library, which is:
30*dfc6aa5cSAndroid Build Coastguard Worker **
31*dfc6aa5cSAndroid Build Coastguard Worker ** Copyright (C) 1988 by Jef Poskanzer.
32*dfc6aa5cSAndroid Build Coastguard Worker **
33*dfc6aa5cSAndroid Build Coastguard Worker ** Permission to use, copy, modify, and distribute this software and its
34*dfc6aa5cSAndroid Build Coastguard Worker ** documentation for any purpose and without fee is hereby granted, provided
35*dfc6aa5cSAndroid Build Coastguard Worker ** that the above copyright notice appear in all copies and that both that
36*dfc6aa5cSAndroid Build Coastguard Worker ** copyright notice and this permission notice appear in supporting
37*dfc6aa5cSAndroid Build Coastguard Worker ** documentation.  This software is provided "as is" without express or
38*dfc6aa5cSAndroid Build Coastguard Worker ** implied warranty.
39*dfc6aa5cSAndroid Build Coastguard Worker */
40*dfc6aa5cSAndroid Build Coastguard Worker 
41*dfc6aa5cSAndroid Build Coastguard Worker 
42*dfc6aa5cSAndroid Build Coastguard Worker /*
43*dfc6aa5cSAndroid Build Coastguard Worker  * Add a (potentially) new color to the color map.
44*dfc6aa5cSAndroid Build Coastguard Worker  */
45*dfc6aa5cSAndroid Build Coastguard Worker 
46*dfc6aa5cSAndroid Build Coastguard Worker LOCAL(void)
add_map_entry(j_decompress_ptr cinfo,int R,int G,int B)47*dfc6aa5cSAndroid Build Coastguard Worker add_map_entry(j_decompress_ptr cinfo, int R, int G, int B)
48*dfc6aa5cSAndroid Build Coastguard Worker {
49*dfc6aa5cSAndroid Build Coastguard Worker   JSAMPROW colormap0 = cinfo->colormap[0];
50*dfc6aa5cSAndroid Build Coastguard Worker   JSAMPROW colormap1 = cinfo->colormap[1];
51*dfc6aa5cSAndroid Build Coastguard Worker   JSAMPROW colormap2 = cinfo->colormap[2];
52*dfc6aa5cSAndroid Build Coastguard Worker   int ncolors = cinfo->actual_number_of_colors;
53*dfc6aa5cSAndroid Build Coastguard Worker   int index;
54*dfc6aa5cSAndroid Build Coastguard Worker 
55*dfc6aa5cSAndroid Build Coastguard Worker   /* Check for duplicate color. */
56*dfc6aa5cSAndroid Build Coastguard Worker   for (index = 0; index < ncolors; index++) {
57*dfc6aa5cSAndroid Build Coastguard Worker     if (colormap0[index] == R && colormap1[index] == G &&
58*dfc6aa5cSAndroid Build Coastguard Worker         colormap2[index] == B)
59*dfc6aa5cSAndroid Build Coastguard Worker       return;                   /* color is already in map */
60*dfc6aa5cSAndroid Build Coastguard Worker   }
61*dfc6aa5cSAndroid Build Coastguard Worker 
62*dfc6aa5cSAndroid Build Coastguard Worker   /* Check for map overflow. */
63*dfc6aa5cSAndroid Build Coastguard Worker   if (ncolors >= (MAXJSAMPLE + 1))
64*dfc6aa5cSAndroid Build Coastguard Worker     ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, (MAXJSAMPLE + 1));
65*dfc6aa5cSAndroid Build Coastguard Worker 
66*dfc6aa5cSAndroid Build Coastguard Worker   /* OK, add color to map. */
67*dfc6aa5cSAndroid Build Coastguard Worker   colormap0[ncolors] = (JSAMPLE)R;
68*dfc6aa5cSAndroid Build Coastguard Worker   colormap1[ncolors] = (JSAMPLE)G;
69*dfc6aa5cSAndroid Build Coastguard Worker   colormap2[ncolors] = (JSAMPLE)B;
70*dfc6aa5cSAndroid Build Coastguard Worker   cinfo->actual_number_of_colors++;
71*dfc6aa5cSAndroid Build Coastguard Worker }
72*dfc6aa5cSAndroid Build Coastguard Worker 
73*dfc6aa5cSAndroid Build Coastguard Worker 
74*dfc6aa5cSAndroid Build Coastguard Worker /*
75*dfc6aa5cSAndroid Build Coastguard Worker  * Extract color map from a GIF file.
76*dfc6aa5cSAndroid Build Coastguard Worker  */
77*dfc6aa5cSAndroid Build Coastguard Worker 
78*dfc6aa5cSAndroid Build Coastguard Worker LOCAL(void)
read_gif_map(j_decompress_ptr cinfo,FILE * infile)79*dfc6aa5cSAndroid Build Coastguard Worker read_gif_map(j_decompress_ptr cinfo, FILE *infile)
80*dfc6aa5cSAndroid Build Coastguard Worker {
81*dfc6aa5cSAndroid Build Coastguard Worker   int header[13];
82*dfc6aa5cSAndroid Build Coastguard Worker   int i, colormaplen;
83*dfc6aa5cSAndroid Build Coastguard Worker   int R, G, B;
84*dfc6aa5cSAndroid Build Coastguard Worker 
85*dfc6aa5cSAndroid Build Coastguard Worker   /* Initial 'G' has already been read by read_color_map */
86*dfc6aa5cSAndroid Build Coastguard Worker   /* Read the rest of the GIF header and logical screen descriptor */
87*dfc6aa5cSAndroid Build Coastguard Worker   for (i = 1; i < 13; i++) {
88*dfc6aa5cSAndroid Build Coastguard Worker     if ((header[i] = getc(infile)) == EOF)
89*dfc6aa5cSAndroid Build Coastguard Worker       ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
90*dfc6aa5cSAndroid Build Coastguard Worker   }
91*dfc6aa5cSAndroid Build Coastguard Worker 
92*dfc6aa5cSAndroid Build Coastguard Worker   /* Verify GIF Header */
93*dfc6aa5cSAndroid Build Coastguard Worker   if (header[1] != 'I' || header[2] != 'F')
94*dfc6aa5cSAndroid Build Coastguard Worker     ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
95*dfc6aa5cSAndroid Build Coastguard Worker 
96*dfc6aa5cSAndroid Build Coastguard Worker   /* There must be a global color map. */
97*dfc6aa5cSAndroid Build Coastguard Worker   if ((header[10] & 0x80) == 0)
98*dfc6aa5cSAndroid Build Coastguard Worker     ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
99*dfc6aa5cSAndroid Build Coastguard Worker 
100*dfc6aa5cSAndroid Build Coastguard Worker   /* OK, fetch it. */
101*dfc6aa5cSAndroid Build Coastguard Worker   colormaplen = 2 << (header[10] & 0x07);
102*dfc6aa5cSAndroid Build Coastguard Worker 
103*dfc6aa5cSAndroid Build Coastguard Worker   for (i = 0; i < colormaplen; i++) {
104*dfc6aa5cSAndroid Build Coastguard Worker     R = getc(infile);
105*dfc6aa5cSAndroid Build Coastguard Worker     G = getc(infile);
106*dfc6aa5cSAndroid Build Coastguard Worker     B = getc(infile);
107*dfc6aa5cSAndroid Build Coastguard Worker     if (R == EOF || G == EOF || B == EOF)
108*dfc6aa5cSAndroid Build Coastguard Worker       ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
109*dfc6aa5cSAndroid Build Coastguard Worker     add_map_entry(cinfo,
110*dfc6aa5cSAndroid Build Coastguard Worker                   R << (BITS_IN_JSAMPLE - 8),
111*dfc6aa5cSAndroid Build Coastguard Worker                   G << (BITS_IN_JSAMPLE - 8),
112*dfc6aa5cSAndroid Build Coastguard Worker                   B << (BITS_IN_JSAMPLE - 8));
113*dfc6aa5cSAndroid Build Coastguard Worker   }
114*dfc6aa5cSAndroid Build Coastguard Worker }
115*dfc6aa5cSAndroid Build Coastguard Worker 
116*dfc6aa5cSAndroid Build Coastguard Worker 
117*dfc6aa5cSAndroid Build Coastguard Worker /* Support routines for reading PPM */
118*dfc6aa5cSAndroid Build Coastguard Worker 
119*dfc6aa5cSAndroid Build Coastguard Worker 
120*dfc6aa5cSAndroid Build Coastguard Worker LOCAL(int)
pbm_getc(FILE * infile)121*dfc6aa5cSAndroid Build Coastguard Worker pbm_getc(FILE *infile)
122*dfc6aa5cSAndroid Build Coastguard Worker /* Read next char, skipping over any comments */
123*dfc6aa5cSAndroid Build Coastguard Worker /* A comment/newline sequence is returned as a newline */
124*dfc6aa5cSAndroid Build Coastguard Worker {
125*dfc6aa5cSAndroid Build Coastguard Worker   register int ch;
126*dfc6aa5cSAndroid Build Coastguard Worker 
127*dfc6aa5cSAndroid Build Coastguard Worker   ch = getc(infile);
128*dfc6aa5cSAndroid Build Coastguard Worker   if (ch == '#') {
129*dfc6aa5cSAndroid Build Coastguard Worker     do {
130*dfc6aa5cSAndroid Build Coastguard Worker       ch = getc(infile);
131*dfc6aa5cSAndroid Build Coastguard Worker     } while (ch != '\n' && ch != EOF);
132*dfc6aa5cSAndroid Build Coastguard Worker   }
133*dfc6aa5cSAndroid Build Coastguard Worker   return ch;
134*dfc6aa5cSAndroid Build Coastguard Worker }
135*dfc6aa5cSAndroid Build Coastguard Worker 
136*dfc6aa5cSAndroid Build Coastguard Worker 
137*dfc6aa5cSAndroid Build Coastguard Worker LOCAL(unsigned int)
read_pbm_integer(j_decompress_ptr cinfo,FILE * infile)138*dfc6aa5cSAndroid Build Coastguard Worker read_pbm_integer(j_decompress_ptr cinfo, FILE *infile)
139*dfc6aa5cSAndroid Build Coastguard Worker /* Read an unsigned decimal integer from the PPM file */
140*dfc6aa5cSAndroid Build Coastguard Worker /* Swallows one trailing character after the integer */
141*dfc6aa5cSAndroid Build Coastguard Worker /* Note that on a 16-bit-int machine, only values up to 64k can be read. */
142*dfc6aa5cSAndroid Build Coastguard Worker /* This should not be a problem in practice. */
143*dfc6aa5cSAndroid Build Coastguard Worker {
144*dfc6aa5cSAndroid Build Coastguard Worker   register int ch;
145*dfc6aa5cSAndroid Build Coastguard Worker   register unsigned int val;
146*dfc6aa5cSAndroid Build Coastguard Worker 
147*dfc6aa5cSAndroid Build Coastguard Worker   /* Skip any leading whitespace */
148*dfc6aa5cSAndroid Build Coastguard Worker   do {
149*dfc6aa5cSAndroid Build Coastguard Worker     ch = pbm_getc(infile);
150*dfc6aa5cSAndroid Build Coastguard Worker     if (ch == EOF)
151*dfc6aa5cSAndroid Build Coastguard Worker       ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
152*dfc6aa5cSAndroid Build Coastguard Worker   } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
153*dfc6aa5cSAndroid Build Coastguard Worker 
154*dfc6aa5cSAndroid Build Coastguard Worker   if (ch < '0' || ch > '9')
155*dfc6aa5cSAndroid Build Coastguard Worker     ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
156*dfc6aa5cSAndroid Build Coastguard Worker 
157*dfc6aa5cSAndroid Build Coastguard Worker   val = ch - '0';
158*dfc6aa5cSAndroid Build Coastguard Worker   while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
159*dfc6aa5cSAndroid Build Coastguard Worker     val *= 10;
160*dfc6aa5cSAndroid Build Coastguard Worker     val += ch - '0';
161*dfc6aa5cSAndroid Build Coastguard Worker   }
162*dfc6aa5cSAndroid Build Coastguard Worker   return val;
163*dfc6aa5cSAndroid Build Coastguard Worker }
164*dfc6aa5cSAndroid Build Coastguard Worker 
165*dfc6aa5cSAndroid Build Coastguard Worker 
166*dfc6aa5cSAndroid Build Coastguard Worker /*
167*dfc6aa5cSAndroid Build Coastguard Worker  * Extract color map from a PPM file.
168*dfc6aa5cSAndroid Build Coastguard Worker  */
169*dfc6aa5cSAndroid Build Coastguard Worker 
170*dfc6aa5cSAndroid Build Coastguard Worker LOCAL(void)
read_ppm_map(j_decompress_ptr cinfo,FILE * infile)171*dfc6aa5cSAndroid Build Coastguard Worker read_ppm_map(j_decompress_ptr cinfo, FILE *infile)
172*dfc6aa5cSAndroid Build Coastguard Worker {
173*dfc6aa5cSAndroid Build Coastguard Worker   int c;
174*dfc6aa5cSAndroid Build Coastguard Worker   unsigned int w, h, maxval, row, col;
175*dfc6aa5cSAndroid Build Coastguard Worker   int R, G, B;
176*dfc6aa5cSAndroid Build Coastguard Worker 
177*dfc6aa5cSAndroid Build Coastguard Worker   /* Initial 'P' has already been read by read_color_map */
178*dfc6aa5cSAndroid Build Coastguard Worker   c = getc(infile);             /* save format discriminator for a sec */
179*dfc6aa5cSAndroid Build Coastguard Worker 
180*dfc6aa5cSAndroid Build Coastguard Worker   /* while we fetch the remaining header info */
181*dfc6aa5cSAndroid Build Coastguard Worker   w = read_pbm_integer(cinfo, infile);
182*dfc6aa5cSAndroid Build Coastguard Worker   h = read_pbm_integer(cinfo, infile);
183*dfc6aa5cSAndroid Build Coastguard Worker   maxval = read_pbm_integer(cinfo, infile);
184*dfc6aa5cSAndroid Build Coastguard Worker 
185*dfc6aa5cSAndroid Build Coastguard Worker   if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
186*dfc6aa5cSAndroid Build Coastguard Worker     ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
187*dfc6aa5cSAndroid Build Coastguard Worker 
188*dfc6aa5cSAndroid Build Coastguard Worker   /* For now, we don't support rescaling from an unusual maxval. */
189*dfc6aa5cSAndroid Build Coastguard Worker   if (maxval != (unsigned int)MAXJSAMPLE)
190*dfc6aa5cSAndroid Build Coastguard Worker     ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
191*dfc6aa5cSAndroid Build Coastguard Worker 
192*dfc6aa5cSAndroid Build Coastguard Worker   switch (c) {
193*dfc6aa5cSAndroid Build Coastguard Worker   case '3':                     /* it's a text-format PPM file */
194*dfc6aa5cSAndroid Build Coastguard Worker     for (row = 0; row < h; row++) {
195*dfc6aa5cSAndroid Build Coastguard Worker       for (col = 0; col < w; col++) {
196*dfc6aa5cSAndroid Build Coastguard Worker         R = read_pbm_integer(cinfo, infile);
197*dfc6aa5cSAndroid Build Coastguard Worker         G = read_pbm_integer(cinfo, infile);
198*dfc6aa5cSAndroid Build Coastguard Worker         B = read_pbm_integer(cinfo, infile);
199*dfc6aa5cSAndroid Build Coastguard Worker         add_map_entry(cinfo, R, G, B);
200*dfc6aa5cSAndroid Build Coastguard Worker       }
201*dfc6aa5cSAndroid Build Coastguard Worker     }
202*dfc6aa5cSAndroid Build Coastguard Worker     break;
203*dfc6aa5cSAndroid Build Coastguard Worker 
204*dfc6aa5cSAndroid Build Coastguard Worker   case '6':                     /* it's a raw-format PPM file */
205*dfc6aa5cSAndroid Build Coastguard Worker     for (row = 0; row < h; row++) {
206*dfc6aa5cSAndroid Build Coastguard Worker       for (col = 0; col < w; col++) {
207*dfc6aa5cSAndroid Build Coastguard Worker         R = getc(infile);
208*dfc6aa5cSAndroid Build Coastguard Worker         G = getc(infile);
209*dfc6aa5cSAndroid Build Coastguard Worker         B = getc(infile);
210*dfc6aa5cSAndroid Build Coastguard Worker         if (R == EOF || G == EOF || B == EOF)
211*dfc6aa5cSAndroid Build Coastguard Worker           ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
212*dfc6aa5cSAndroid Build Coastguard Worker         add_map_entry(cinfo, R, G, B);
213*dfc6aa5cSAndroid Build Coastguard Worker       }
214*dfc6aa5cSAndroid Build Coastguard Worker     }
215*dfc6aa5cSAndroid Build Coastguard Worker     break;
216*dfc6aa5cSAndroid Build Coastguard Worker 
217*dfc6aa5cSAndroid Build Coastguard Worker   default:
218*dfc6aa5cSAndroid Build Coastguard Worker     ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
219*dfc6aa5cSAndroid Build Coastguard Worker     break;
220*dfc6aa5cSAndroid Build Coastguard Worker   }
221*dfc6aa5cSAndroid Build Coastguard Worker }
222*dfc6aa5cSAndroid Build Coastguard Worker 
223*dfc6aa5cSAndroid Build Coastguard Worker 
224*dfc6aa5cSAndroid Build Coastguard Worker /*
225*dfc6aa5cSAndroid Build Coastguard Worker  * Main entry point from djpeg.c.
226*dfc6aa5cSAndroid Build Coastguard Worker  *  Input: opened input file (from file name argument on command line).
227*dfc6aa5cSAndroid Build Coastguard Worker  *  Output: colormap and actual_number_of_colors fields are set in cinfo.
228*dfc6aa5cSAndroid Build Coastguard Worker  */
229*dfc6aa5cSAndroid Build Coastguard Worker 
230*dfc6aa5cSAndroid Build Coastguard Worker GLOBAL(void)
read_color_map(j_decompress_ptr cinfo,FILE * infile)231*dfc6aa5cSAndroid Build Coastguard Worker read_color_map(j_decompress_ptr cinfo, FILE *infile)
232*dfc6aa5cSAndroid Build Coastguard Worker {
233*dfc6aa5cSAndroid Build Coastguard Worker   /* Allocate space for a color map of maximum supported size. */
234*dfc6aa5cSAndroid Build Coastguard Worker   cinfo->colormap = (*cinfo->mem->alloc_sarray)
235*dfc6aa5cSAndroid Build Coastguard Worker     ((j_common_ptr)cinfo, JPOOL_IMAGE,
236*dfc6aa5cSAndroid Build Coastguard Worker      (JDIMENSION)(MAXJSAMPLE + 1), (JDIMENSION)3);
237*dfc6aa5cSAndroid Build Coastguard Worker   cinfo->actual_number_of_colors = 0; /* initialize map to empty */
238*dfc6aa5cSAndroid Build Coastguard Worker 
239*dfc6aa5cSAndroid Build Coastguard Worker   /* Read first byte to determine file format */
240*dfc6aa5cSAndroid Build Coastguard Worker   switch (getc(infile)) {
241*dfc6aa5cSAndroid Build Coastguard Worker   case 'G':
242*dfc6aa5cSAndroid Build Coastguard Worker     read_gif_map(cinfo, infile);
243*dfc6aa5cSAndroid Build Coastguard Worker     break;
244*dfc6aa5cSAndroid Build Coastguard Worker   case 'P':
245*dfc6aa5cSAndroid Build Coastguard Worker     read_ppm_map(cinfo, infile);
246*dfc6aa5cSAndroid Build Coastguard Worker     break;
247*dfc6aa5cSAndroid Build Coastguard Worker   default:
248*dfc6aa5cSAndroid Build Coastguard Worker     ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
249*dfc6aa5cSAndroid Build Coastguard Worker     break;
250*dfc6aa5cSAndroid Build Coastguard Worker   }
251*dfc6aa5cSAndroid Build Coastguard Worker }
252*dfc6aa5cSAndroid Build Coastguard Worker 
253*dfc6aa5cSAndroid Build Coastguard Worker #endif /* QUANT_2PASS_SUPPORTED */
254