rdppm.c 14 KB

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