Back to index

plt-scheme  4.2.1
rdcolmap.c
Go to the documentation of this file.
00001 /*
00002  * rdcolmap.c
00003  *
00004  * Copyright (C) 1994-1996, Thomas G. Lane.
00005  * This file is part of the Independent JPEG Group's software.
00006  * For conditions of distribution and use, see the accompanying README file.
00007  *
00008  * This file implements djpeg's "-map file" switch.  It reads a source image
00009  * and constructs a colormap to be supplied to the JPEG decompressor.
00010  *
00011  * Currently, these file formats are supported for the map file:
00012  *   GIF: the contents of the GIF's global colormap are used.
00013  *   PPM (either text or raw flavor): the entire file is read and
00014  *      each unique pixel value is entered in the map.
00015  * Note that reading a large PPM file will be horrendously slow.
00016  * Typically, a PPM-format map file should contain just one pixel
00017  * of each desired color.  Such a file can be extracted from an
00018  * ordinary image PPM file with ppmtomap(1).
00019  *
00020  * Rescaling a PPM that has a maxval unequal to MAXJSAMPLE is not
00021  * currently implemented.
00022  */
00023 
00024 #include "cdjpeg.h"         /* Common decls for cjpeg/djpeg applications */
00025 
00026 #ifdef QUANT_2PASS_SUPPORTED       /* otherwise can't quantize to supplied map */
00027 
00028 /* Portions of this code are based on the PBMPLUS library, which is:
00029 **
00030 ** Copyright (C) 1988 by Jef Poskanzer.
00031 **
00032 ** Permission to use, copy, modify, and distribute this software and its
00033 ** documentation for any purpose and without fee is hereby granted, provided
00034 ** that the above copyright notice appear in all copies and that both that
00035 ** copyright notice and this permission notice appear in supporting
00036 ** documentation.  This software is provided "as is" without express or
00037 ** implied warranty.
00038 */
00039 
00040 
00041 /*
00042  * Add a (potentially) new color to the color map.
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   /* Check for duplicate color. */
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;               /* color is already in map */
00060   }
00061 
00062   /* Check for map overflow. */
00063   if (ncolors >= (MAXJSAMPLE+1))
00064     ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, (MAXJSAMPLE+1));
00065 
00066   /* OK, add color to map. */
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  * Extract color map from a GIF file.
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   /* Initial 'G' has already been read by read_color_map */
00086   /* Read the rest of the GIF header and logical screen descriptor */
00087   for (i = 1; i < 13; i++) {
00088     if ((header[i] = getc(infile)) == EOF)
00089       ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
00090   }
00091 
00092   /* Verify GIF Header */
00093   if (header[1] != 'I' || header[2] != 'F')
00094     ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
00095 
00096   /* There must be a global color map. */
00097   if ((header[10] & 0x80) == 0)
00098     ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
00099 
00100   /* OK, fetch it. */
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 /* Support routines for reading PPM */
00118 
00119 
00120 LOCAL(int)
00121 pbm_getc (FILE * infile)
00122 /* Read next char, skipping over any comments */
00123 /* A comment/newline sequence is returned as a newline */
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 /* Read an unsigned decimal integer from the PPM file */
00140 /* Swallows one trailing character after the integer */
00141 /* Note that on a 16-bit-int machine, only values up to 64k can be read. */
00142 /* This should not be a problem in practice. */
00143 {
00144   register int ch;
00145   register unsigned int val;
00146   
00147   /* Skip any leading whitespace */
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  * Extract color map from a PPM file.
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   /* Initial 'P' has already been read by read_color_map */
00178   c = getc(infile);         /* save format discriminator for a sec */
00179 
00180   /* while we fetch the remaining header info */
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) /* error check */
00186     ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
00187 
00188   /* For now, we don't support rescaling from an unusual maxval. */
00189   if (maxval != (unsigned int) MAXJSAMPLE)
00190     ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
00191 
00192   switch (c) {
00193   case '3':                 /* it's a text-format PPM file */
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':                 /* it's a raw-format PPM file */
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  * Main entry point from djpeg.c.
00226  *  Input: opened input file (from file name argument on command line).
00227  *  Output: colormap and actual_number_of_colors fields are set in cinfo.
00228  */
00229 
00230 GLOBAL(void)
00231 read_color_map (j_decompress_ptr cinfo, FILE * infile)
00232 {
00233   /* Allocate space for a color map of maximum supported size. */
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; /* initialize map to empty */
00238 
00239   /* Read first byte to determine file format */
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 /* QUANT_2PASS_SUPPORTED */