rdppm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * rdppm.c
  3. *
  4. * Copyright (C) 1991-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 file.
  7. *
  8. * This file contains routines to read input images in PPM/PGM format.
  9. * The extended 2-byte-per-sample raw PPM/PGM formats are supported.
  10. * The PBMPLUS library is NOT required to compile this software
  11. * (but it is highly useful as a set of PPM image manipulation programs).
  12. *
  13. * These routines may need modification for non-Unix environments or
  14. * specialized applications. As they stand, they assume input from
  15. * an ordinary stdio stream. They further assume that reading begins
  16. * at the start of the file; start_input may need work if the
  17. * user interface has already read some data (e.g., to determine that
  18. * the file is indeed PPM format).
  19. */
  20. #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
  21. #ifdef PPM_SUPPORTED
  22. /* Portions of this code are based on the PBMPLUS library, which is:
  23. **
  24. ** Copyright (C) 1988 by Jef Poskanzer.
  25. **
  26. ** Permission to use, copy, modify, and distribute this software and its
  27. ** documentation for any purpose and without fee is hereby granted, provided
  28. ** that the above copyright notice appear in all copies and that both that
  29. ** copyright notice and this permission notice appear in supporting
  30. ** documentation. This software is provided "as is" without express or
  31. ** implied warranty.
  32. */
  33. /* Macros to deal with unsigned chars as efficiently as compiler allows */
  34. #ifdef HAVE_UNSIGNED_CHAR
  35. typedef unsigned char U_CHAR;
  36. #define UCH(x) ((int) (x))
  37. #else /* !HAVE_UNSIGNED_CHAR */
  38. #ifdef CHAR_IS_UNSIGNED
  39. typedef char U_CHAR;
  40. #define UCH(x) ((int) (x))
  41. #else
  42. typedef char U_CHAR;
  43. #define UCH(x) ((int) (x) & 0xFF)
  44. #endif
  45. #endif /* HAVE_UNSIGNED_CHAR */
  46. #define ReadOK(file,buffer,len) (JFREAD(file,buffer,len) == ((size_t) (len)))
  47. /*
  48. * On most systems, reading individual bytes with getc() is drastically less
  49. * efficient than buffering a row at a time with fread(). On PCs, we must
  50. * allocate the buffer in near data space, because we are assuming small-data
  51. * memory model, wherein fread() can't reach far memory. If you need to
  52. * process very wide images on a PC, you might have to compile in large-memory
  53. * model, or else replace fread() with a getc() loop --- which will be much
  54. * slower.
  55. */
  56. /* Private version of data source object */
  57. typedef struct {
  58. struct cjpeg_source_struct pub; /* public fields */
  59. U_CHAR *iobuffer; /* non-FAR pointer to I/O buffer */
  60. JSAMPROW pixrow; /* FAR pointer to same */
  61. size_t buffer_width; /* width of I/O buffer */
  62. JSAMPLE *rescale; /* => maxval-remapping array, or NULL */
  63. } ppm_source_struct;
  64. typedef ppm_source_struct * ppm_source_ptr;
  65. LOCAL(int)
  66. pbm_getc (FILE * infile)
  67. /* Read next char, skipping over any comments */
  68. /* A comment/newline sequence is returned as a newline */
  69. {
  70. register int ch;
  71. ch = getc(infile);
  72. if (ch == '#') {
  73. do {
  74. ch = getc(infile);
  75. } while (ch != '\n' && ch != EOF);
  76. }
  77. return ch;
  78. }
  79. LOCAL(unsigned int)
  80. read_pbm_integer (j_compress_ptr cinfo, FILE * infile)
  81. /* Read an unsigned decimal integer from the PPM file */
  82. /* Swallows one trailing character after the integer */
  83. /* Note that on a 16-bit-int machine, only values up to 64k can be read. */
  84. /* This should not be a problem in practice. */
  85. {
  86. register int ch;
  87. register unsigned int val;
  88. /* Skip any leading whitespace */
  89. do {
  90. ch = pbm_getc(infile);
  91. if (ch == EOF)
  92. ERREXIT(cinfo, JERR_INPUT_EOF);
  93. } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
  94. if (ch < '0' || ch > '9')
  95. ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
  96. val = ch - '0';
  97. while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
  98. val *= 10;
  99. val += ch - '0';
  100. }
  101. return val;
  102. }
  103. /*
  104. * Read one row of pixels.
  105. *
  106. * We provide several different versions depending on input file format.
  107. * In all cases, input is scaled to the size of JSAMPLE.
  108. *
  109. * A really fast path is provided for reading byte/sample raw files with
  110. * maxval = MAXJSAMPLE, which is the normal case for 8-bit data.
  111. */
  112. METHODDEF(JDIMENSION)
  113. get_text_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  114. /* This version is for reading text-format PGM files with any maxval */
  115. {
  116. ppm_source_ptr source = (ppm_source_ptr) sinfo;
  117. FILE * infile = source->pub.input_file;
  118. register JSAMPROW ptr;
  119. register JSAMPLE *rescale = source->rescale;
  120. JDIMENSION col;
  121. ptr = source->pub.buffer[0];
  122. for (col = cinfo->image_width; col > 0; col--) {
  123. *ptr++ = rescale[read_pbm_integer(cinfo, infile)];
  124. }
  125. return 1;
  126. }
  127. METHODDEF(JDIMENSION)
  128. get_text_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  129. /* This version is for reading text-format PPM files with any maxval */
  130. {
  131. ppm_source_ptr source = (ppm_source_ptr) sinfo;
  132. FILE * infile = source->pub.input_file;
  133. register JSAMPROW ptr;
  134. register JSAMPLE *rescale = source->rescale;
  135. JDIMENSION col;
  136. ptr = source->pub.buffer[0];
  137. for (col = cinfo->image_width; col > 0; col--) {
  138. *ptr++ = rescale[read_pbm_integer(cinfo, infile)];
  139. *ptr++ = rescale[read_pbm_integer(cinfo, infile)];
  140. *ptr++ = rescale[read_pbm_integer(cinfo, infile)];
  141. }
  142. return 1;
  143. }
  144. METHODDEF(JDIMENSION)
  145. get_scaled_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  146. /* This version is for reading raw-byte-format PGM files with any maxval */
  147. {
  148. ppm_source_ptr source = (ppm_source_ptr) sinfo;
  149. register JSAMPROW ptr;
  150. register U_CHAR * bufferptr;
  151. register JSAMPLE *rescale = source->rescale;
  152. JDIMENSION col;
  153. if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
  154. ERREXIT(cinfo, JERR_INPUT_EOF);
  155. ptr = source->pub.buffer[0];
  156. bufferptr = source->iobuffer;
  157. for (col = cinfo->image_width; col > 0; col--) {
  158. *ptr++ = rescale[UCH(*bufferptr++)];
  159. }
  160. return 1;
  161. }
  162. METHODDEF(JDIMENSION)
  163. get_scaled_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  164. /* This version is for reading raw-byte-format PPM files with any maxval */
  165. {
  166. ppm_source_ptr source = (ppm_source_ptr) sinfo;
  167. register JSAMPROW ptr;
  168. register U_CHAR * bufferptr;
  169. register JSAMPLE *rescale = source->rescale;
  170. JDIMENSION col;
  171. if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
  172. ERREXIT(cinfo, JERR_INPUT_EOF);
  173. ptr = source->pub.buffer[0];
  174. bufferptr = source->iobuffer;
  175. for (col = cinfo->image_width; col > 0; col--) {
  176. *ptr++ = rescale[UCH(*bufferptr++)];
  177. *ptr++ = rescale[UCH(*bufferptr++)];
  178. *ptr++ = rescale[UCH(*bufferptr++)];
  179. }
  180. return 1;
  181. }
  182. METHODDEF(JDIMENSION)
  183. get_raw_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  184. /* This version is for reading raw-byte-format files with maxval = MAXJSAMPLE.
  185. * In this case we just read right into the JSAMPLE buffer!
  186. * Note that same code works for PPM and PGM files.
  187. */
  188. {
  189. ppm_source_ptr source = (ppm_source_ptr) sinfo;
  190. if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
  191. ERREXIT(cinfo, JERR_INPUT_EOF);
  192. return 1;
  193. }
  194. METHODDEF(JDIMENSION)
  195. get_word_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  196. /* This version is for reading raw-word-format PGM files with any maxval */
  197. {
  198. ppm_source_ptr source = (ppm_source_ptr) sinfo;
  199. register JSAMPROW ptr;
  200. register U_CHAR * bufferptr;
  201. register JSAMPLE *rescale = source->rescale;
  202. JDIMENSION col;
  203. if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
  204. ERREXIT(cinfo, JERR_INPUT_EOF);
  205. ptr = source->pub.buffer[0];
  206. bufferptr = source->iobuffer;
  207. for (col = cinfo->image_width; col > 0; col--) {
  208. register int temp;
  209. temp = UCH(*bufferptr++);
  210. temp |= UCH(*bufferptr++) << 8;
  211. *ptr++ = rescale[temp];
  212. }
  213. return 1;
  214. }
  215. METHODDEF(JDIMENSION)
  216. get_word_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  217. /* This version is for reading raw-word-format PPM files with any maxval */
  218. {
  219. ppm_source_ptr source = (ppm_source_ptr) sinfo;
  220. register JSAMPROW ptr;
  221. register U_CHAR * bufferptr;
  222. register JSAMPLE *rescale = source->rescale;
  223. JDIMENSION col;
  224. if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
  225. ERREXIT(cinfo, JERR_INPUT_EOF);
  226. ptr = source->pub.buffer[0];
  227. bufferptr = source->iobuffer;
  228. for (col = cinfo->image_width; col > 0; col--) {
  229. register int temp;
  230. temp = UCH(*bufferptr++);
  231. temp |= UCH(*bufferptr++) << 8;
  232. *ptr++ = rescale[temp];
  233. temp = UCH(*bufferptr++);
  234. temp |= UCH(*bufferptr++) << 8;
  235. *ptr++ = rescale[temp];
  236. temp = UCH(*bufferptr++);
  237. temp |= UCH(*bufferptr++) << 8;
  238. *ptr++ = rescale[temp];
  239. }
  240. return 1;
  241. }
  242. /*
  243. * Read the file header; return image size and component count.
  244. */
  245. METHODDEF(void)
  246. start_input_ppm (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  247. {
  248. ppm_source_ptr source = (ppm_source_ptr) sinfo;
  249. int c;
  250. unsigned int w, h, maxval;
  251. boolean need_iobuffer, use_raw_buffer, need_rescale;
  252. if (getc(source->pub.input_file) != 'P')
  253. ERREXIT(cinfo, JERR_PPM_NOT);
  254. c = getc(source->pub.input_file); /* save format discriminator for a sec */
  255. /* fetch the remaining header info */
  256. w = read_pbm_integer(cinfo, source->pub.input_file);
  257. h = read_pbm_integer(cinfo, source->pub.input_file);
  258. maxval = read_pbm_integer(cinfo, source->pub.input_file);
  259. if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
  260. ERREXIT(cinfo, JERR_PPM_NOT);
  261. cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */
  262. cinfo->image_width = (JDIMENSION) w;
  263. cinfo->image_height = (JDIMENSION) h;
  264. /* initialize flags to most common settings */
  265. need_iobuffer = TRUE; /* do we need an I/O buffer? */
  266. use_raw_buffer = FALSE; /* do we map input buffer onto I/O buffer? */
  267. need_rescale = TRUE; /* do we need a rescale array? */
  268. switch (c) {
  269. case '2': /* it's a text-format PGM file */
  270. cinfo->input_components = 1;
  271. cinfo->in_color_space = JCS_GRAYSCALE;
  272. TRACEMS2(cinfo, 1, JTRC_PGM_TEXT, w, h);
  273. source->pub.get_pixel_rows = get_text_gray_row;
  274. need_iobuffer = FALSE;
  275. break;
  276. case '3': /* it's a text-format PPM file */
  277. cinfo->input_components = 3;
  278. cinfo->in_color_space = JCS_RGB;
  279. TRACEMS2(cinfo, 1, JTRC_PPM_TEXT, w, h);
  280. source->pub.get_pixel_rows = get_text_rgb_row;
  281. need_iobuffer = FALSE;
  282. break;
  283. case '5': /* it's a raw-format PGM file */
  284. cinfo->input_components = 1;
  285. cinfo->in_color_space = JCS_GRAYSCALE;
  286. TRACEMS2(cinfo, 1, JTRC_PGM, w, h);
  287. if (maxval > 255) {
  288. source->pub.get_pixel_rows = get_word_gray_row;
  289. } else if (maxval == MAXJSAMPLE && SIZEOF(JSAMPLE) == SIZEOF(U_CHAR)) {
  290. source->pub.get_pixel_rows = get_raw_row;
  291. use_raw_buffer = TRUE;
  292. need_rescale = FALSE;
  293. } else {
  294. source->pub.get_pixel_rows = get_scaled_gray_row;
  295. }
  296. break;
  297. case '6': /* it's a raw-format PPM file */
  298. cinfo->input_components = 3;
  299. cinfo->in_color_space = JCS_RGB;
  300. TRACEMS2(cinfo, 1, JTRC_PPM, w, h);
  301. if (maxval > 255) {
  302. source->pub.get_pixel_rows = get_word_rgb_row;
  303. } else if (maxval == MAXJSAMPLE && SIZEOF(JSAMPLE) == SIZEOF(U_CHAR)) {
  304. source->pub.get_pixel_rows = get_raw_row;
  305. use_raw_buffer = TRUE;
  306. need_rescale = FALSE;
  307. } else {
  308. source->pub.get_pixel_rows = get_scaled_rgb_row;
  309. }
  310. break;
  311. default:
  312. ERREXIT(cinfo, JERR_PPM_NOT);
  313. break;
  314. }
  315. /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
  316. if (need_iobuffer) {
  317. source->buffer_width = (size_t) w * cinfo->input_components *
  318. ((maxval<=255) ? SIZEOF(U_CHAR) : (2*SIZEOF(U_CHAR)));
  319. source->iobuffer = (U_CHAR *)
  320. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  321. source->buffer_width);
  322. }
  323. /* Create compressor input buffer. */
  324. if (use_raw_buffer) {
  325. /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
  326. /* Synthesize a JSAMPARRAY pointer structure */
  327. /* Cast here implies near->far pointer conversion on PCs */
  328. source->pixrow = (JSAMPROW) source->iobuffer;
  329. source->pub.buffer = & source->pixrow;
  330. source->pub.buffer_height = 1;
  331. } else {
  332. /* Need to translate anyway, so make a separate sample buffer. */
  333. source->pub.buffer = (*cinfo->mem->alloc_sarray)
  334. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  335. (JDIMENSION) w * cinfo->input_components, (JDIMENSION) 1);
  336. source->pub.buffer_height = 1;
  337. }
  338. /* Compute the rescaling array if required. */
  339. if (need_rescale) {
  340. INT32 val, half_maxval;
  341. /* On 16-bit-int machines we have to be careful of maxval = 65535 */
  342. source->rescale = (JSAMPLE *)
  343. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  344. (size_t) (((long) maxval + 1L) * SIZEOF(JSAMPLE)));
  345. half_maxval = maxval / 2;
  346. for (val = 0; val <= (INT32) maxval; val++) {
  347. /* The multiplication here must be done in 32 bits to avoid overflow */
  348. source->rescale[val] = (JSAMPLE) ((val*MAXJSAMPLE + half_maxval)/maxval);
  349. }
  350. }
  351. }
  352. /*
  353. * Finish up at the end of the file.
  354. */
  355. METHODDEF(void)
  356. finish_input_ppm (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  357. {
  358. /* no work */
  359. }
  360. /*
  361. * The module selection routine for PPM format input.
  362. */
  363. GLOBAL(cjpeg_source_ptr)
  364. jinit_read_ppm (j_compress_ptr cinfo)
  365. {
  366. ppm_source_ptr source;
  367. /* Create module interface object */
  368. source = (ppm_source_ptr)
  369. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  370. SIZEOF(ppm_source_struct));
  371. /* Fill in method ptrs, except get_pixel_rows which start_input sets */
  372. source->pub.start_input = start_input_ppm;
  373. source->pub.finish_input = finish_input_ppm;
  374. return (cjpeg_source_ptr) source;
  375. }
  376. #endif /* PPM_SUPPORTED */