rdcolmap.c 6.9 KB

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