00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "cdjpeg.h"
00025
00026 #ifdef QUANT_2PASS_SUPPORTED
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 LOCAL(void)
00046 add_map_entry (j_decompress_ptr cinfo, int R, int G, int B)
00047 {
00048 JSAMPROW colormap0 = cinfo->colormap[0];
00049 JSAMPROW colormap1 = cinfo->colormap[1];
00050 JSAMPROW colormap2 = cinfo->colormap[2];
00051 int ncolors = cinfo->actual_number_of_colors;
00052 int index;
00053
00054
00055 for (index = 0; index < ncolors; index++) {
00056 if (GETJSAMPLE(colormap0[index]) == R &&
00057 GETJSAMPLE(colormap1[index]) == G &&
00058 GETJSAMPLE(colormap2[index]) == B)
00059 return;
00060 }
00061
00062
00063 if (ncolors >= (MAXJSAMPLE+1))
00064 ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, (MAXJSAMPLE+1));
00065
00066
00067 colormap0[ncolors] = (JSAMPLE) R;
00068 colormap1[ncolors] = (JSAMPLE) G;
00069 colormap2[ncolors] = (JSAMPLE) B;
00070 cinfo->actual_number_of_colors++;
00071 }
00072
00073
00074
00075
00076
00077
00078 LOCAL(void)
00079 read_gif_map (j_decompress_ptr cinfo, FILE * infile)
00080 {
00081 int header[13];
00082 int i, colormaplen;
00083 int R, G, B;
00084
00085
00086
00087 for (i = 1; i < 13; i++) {
00088 if ((header[i] = getc(infile)) == EOF)
00089 ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
00090 }
00091
00092
00093 if (header[1] != 'I' || header[2] != 'F')
00094 ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
00095
00096
00097 if ((header[10] & 0x80) == 0)
00098 ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
00099
00100
00101 colormaplen = 2 << (header[10] & 0x07);
00102
00103 for (i = 0; i < colormaplen; i++) {
00104 R = getc(infile);
00105 G = getc(infile);
00106 B = getc(infile);
00107 if (R == EOF || G == EOF || B == EOF)
00108 ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
00109 add_map_entry(cinfo,
00110 R << (BITS_IN_JSAMPLE-8),
00111 G << (BITS_IN_JSAMPLE-8),
00112 B << (BITS_IN_JSAMPLE-8));
00113 }
00114 }
00115
00116
00117
00118
00119
00120 LOCAL(int)
00121 pbm_getc (FILE * infile)
00122
00123
00124 {
00125 register int ch;
00126
00127 ch = getc(infile);
00128 if (ch == '#') {
00129 do {
00130 ch = getc(infile);
00131 } while (ch != '\n' && ch != EOF);
00132 }
00133 return ch;
00134 }
00135
00136
00137 LOCAL(unsigned int)
00138 read_pbm_integer (j_decompress_ptr cinfo, FILE * infile)
00139
00140
00141
00142
00143 {
00144 register int ch;
00145 register unsigned int val;
00146
00147
00148 do {
00149 ch = pbm_getc(infile);
00150 if (ch == EOF)
00151 ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
00152 } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
00153
00154 if (ch < '0' || ch > '9')
00155 ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
00156
00157 val = ch - '0';
00158 while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
00159 val *= 10;
00160 val += ch - '0';
00161 }
00162 return val;
00163 }
00164
00165
00166
00167
00168
00169
00170 LOCAL(void)
00171 read_ppm_map (j_decompress_ptr cinfo, FILE * infile)
00172 {
00173 int c;
00174 unsigned int w, h, maxval, row, col;
00175 int R, G, B;
00176
00177
00178 c = getc(infile);
00179
00180
00181 w = read_pbm_integer(cinfo, infile);
00182 h = read_pbm_integer(cinfo, infile);
00183 maxval = read_pbm_integer(cinfo, infile);
00184
00185 if (w <= 0 || h <= 0 || maxval <= 0)
00186 ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
00187
00188
00189 if (maxval != (unsigned int) MAXJSAMPLE)
00190 ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
00191
00192 switch (c) {
00193 case '3':
00194 for (row = 0; row < h; row++) {
00195 for (col = 0; col < w; col++) {
00196 R = read_pbm_integer(cinfo, infile);
00197 G = read_pbm_integer(cinfo, infile);
00198 B = read_pbm_integer(cinfo, infile);
00199 add_map_entry(cinfo, R, G, B);
00200 }
00201 }
00202 break;
00203
00204 case '6':
00205 for (row = 0; row < h; row++) {
00206 for (col = 0; col < w; col++) {
00207 R = getc(infile);
00208 G = getc(infile);
00209 B = getc(infile);
00210 if (R == EOF || G == EOF || B == EOF)
00211 ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
00212 add_map_entry(cinfo, R, G, B);
00213 }
00214 }
00215 break;
00216
00217 default:
00218 ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
00219 break;
00220 }
00221 }
00222
00223
00224
00225
00226
00227
00228
00229
00230 GLOBAL(void)
00231 read_color_map (j_decompress_ptr cinfo, FILE * infile)
00232 {
00233
00234 cinfo->colormap = (*cinfo->mem->alloc_sarray)
00235 ((j_common_ptr) cinfo, JPOOL_IMAGE,
00236 (JDIMENSION) (MAXJSAMPLE+1), (JDIMENSION) 3);
00237 cinfo->actual_number_of_colors = 0;
00238
00239
00240 switch (getc(infile)) {
00241 case 'G':
00242 read_gif_map(cinfo, infile);
00243 break;
00244 case 'P':
00245 read_ppm_map(cinfo, infile);
00246 break;
00247 default:
00248 ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
00249 break;
00250 }
00251 }
00252
00253 #endif