rdcolmap.c 6.8 KB

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