rdppm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * rdppm.c
  3. *
  4. * This file was part of the Independent JPEG Group's software:
  5. * Copyright (C) 1991-1997, Thomas G. Lane.
  6. * Modified 2009 by Bill Allombert, Guido Vollbeding.
  7. * libjpeg-turbo Modifications:
  8. * Copyright (C) 2015, 2016, D. R. Commander.
  9. * For conditions of distribution and use, see the accompanying README.ijg
  10. * file.
  11. *
  12. * This file contains routines to read input images in PPM/PGM format.
  13. * The extended 2-byte-per-sample raw PPM/PGM formats are supported.
  14. * The PBMPLUS library is NOT required to compile this software
  15. * (but it is highly useful as a set of PPM image manipulation programs).
  16. *
  17. * These routines may need modification for non-Unix environments or
  18. * specialized applications. As they stand, they assume input from
  19. * an ordinary stdio stream. They further assume that reading begins
  20. * at the start of the file; start_input may need work if the
  21. * user interface has already read some data (e.g., to determine that
  22. * the file is indeed PPM format).
  23. */
  24. #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
  25. #ifdef PPM_SUPPORTED
  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. /* Macros to deal with unsigned chars as efficiently as compiler allows */
  38. #ifdef HAVE_UNSIGNED_CHAR
  39. typedef unsigned char U_CHAR;
  40. #define UCH(x) ((int) (x))
  41. #else /* !HAVE_UNSIGNED_CHAR */
  42. #ifdef __CHAR_UNSIGNED__
  43. typedef char U_CHAR;
  44. #define UCH(x) ((int) (x))
  45. #else
  46. typedef char U_CHAR;
  47. #define UCH(x) ((int) (x) & 0xFF)
  48. #endif
  49. #endif /* HAVE_UNSIGNED_CHAR */
  50. #define ReadOK(file,buffer,len) (JFREAD(file,buffer,len) == ((size_t) (len)))
  51. /* Private version of data source object */
  52. typedef struct {
  53. struct cjpeg_source_struct pub; /* public fields */
  54. /* Usually these two pointers point to the same place: */
  55. U_CHAR *iobuffer; /* fread's I/O buffer */
  56. JSAMPROW pixrow; /* compressor input buffer */
  57. size_t buffer_width; /* width of I/O buffer */
  58. JSAMPLE *rescale; /* => maxval-remapping array, or NULL */
  59. int maxval;
  60. } ppm_source_struct;
  61. typedef ppm_source_struct *ppm_source_ptr;
  62. LOCAL(int)
  63. pbm_getc (FILE *infile)
  64. /* Read next char, skipping over any comments */
  65. /* A comment/newline sequence is returned as a newline */
  66. {
  67. register int ch;
  68. ch = getc(infile);
  69. if (ch == '#') {
  70. do {
  71. ch = getc(infile);
  72. } while (ch != '\n' && ch != EOF);
  73. }
  74. return ch;
  75. }
  76. LOCAL(unsigned int)
  77. read_pbm_integer (j_compress_ptr cinfo, FILE *infile, unsigned int maxval)
  78. /* Read an unsigned decimal integer from the PPM file */
  79. /* Swallows one trailing character after the integer */
  80. /* Note that on a 16-bit-int machine, only values up to 64k can be read. */
  81. /* This should not be a problem in practice. */
  82. {
  83. register int ch;
  84. register unsigned int val;
  85. /* Skip any leading whitespace */
  86. do {
  87. ch = pbm_getc(infile);
  88. if (ch == EOF)
  89. ERREXIT(cinfo, JERR_INPUT_EOF);
  90. } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
  91. if (ch < '0' || ch > '9')
  92. ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
  93. val = ch - '0';
  94. while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
  95. val *= 10;
  96. val += ch - '0';
  97. }
  98. if (val > maxval)
  99. ERREXIT(cinfo, JERR_PPM_TOOLARGE);
  100. return val;
  101. }
  102. /*
  103. * Read one row of pixels.
  104. *
  105. * We provide several different versions depending on input file format.
  106. * In all cases, input is scaled to the size of JSAMPLE.
  107. *
  108. * A really fast path is provided for reading byte/sample raw files with
  109. * maxval = MAXJSAMPLE, which is the normal case for 8-bit data.
  110. */
  111. METHODDEF(JDIMENSION)
  112. get_text_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  113. /* This version is for reading text-format PGM files with any maxval */
  114. {
  115. ppm_source_ptr source = (ppm_source_ptr) sinfo;
  116. FILE *infile = source->pub.input_file;
  117. register JSAMPROW ptr;
  118. register JSAMPLE *rescale = source->rescale;
  119. JDIMENSION col;
  120. unsigned int maxval = source->maxval;
  121. ptr = source->pub.buffer[0];
  122. for (col = cinfo->image_width; col > 0; col--) {
  123. *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
  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. unsigned int maxval = source->maxval;
  137. ptr = source->pub.buffer[0];
  138. for (col = cinfo->image_width; col > 0; col--) {
  139. *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
  140. *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
  141. *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
  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. unsigned int maxval = source->maxval;
  205. if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
  206. ERREXIT(cinfo, JERR_INPUT_EOF);
  207. ptr = source->pub.buffer[0];
  208. bufferptr = source->iobuffer;
  209. for (col = cinfo->image_width; col > 0; col--) {
  210. register int temp;
  211. temp = UCH(*bufferptr++) << 8;
  212. temp |= UCH(*bufferptr++);
  213. if (temp > maxval)
  214. ERREXIT(cinfo, JERR_PPM_TOOLARGE);
  215. *ptr++ = rescale[temp];
  216. }
  217. return 1;
  218. }
  219. METHODDEF(JDIMENSION)
  220. get_word_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  221. /* This version is for reading raw-word-format PPM files with any maxval */
  222. {
  223. ppm_source_ptr source = (ppm_source_ptr) sinfo;
  224. register JSAMPROW ptr;
  225. register U_CHAR *bufferptr;
  226. register JSAMPLE *rescale = source->rescale;
  227. JDIMENSION col;
  228. unsigned int maxval = source->maxval;
  229. if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
  230. ERREXIT(cinfo, JERR_INPUT_EOF);
  231. ptr = source->pub.buffer[0];
  232. bufferptr = source->iobuffer;
  233. for (col = cinfo->image_width; col > 0; col--) {
  234. register int temp;
  235. temp = UCH(*bufferptr++) << 8;
  236. temp |= UCH(*bufferptr++);
  237. if (temp > maxval)
  238. ERREXIT(cinfo, JERR_PPM_TOOLARGE);
  239. *ptr++ = rescale[temp];
  240. temp = UCH(*bufferptr++) << 8;
  241. temp |= UCH(*bufferptr++);
  242. if (temp > maxval)
  243. ERREXIT(cinfo, JERR_PPM_TOOLARGE);
  244. *ptr++ = rescale[temp];
  245. temp = UCH(*bufferptr++) << 8;
  246. temp |= UCH(*bufferptr++);
  247. if (temp > maxval)
  248. ERREXIT(cinfo, JERR_PPM_TOOLARGE);
  249. *ptr++ = rescale[temp];
  250. }
  251. return 1;
  252. }
  253. /*
  254. * Read the file header; return image size and component count.
  255. */
  256. METHODDEF(void)
  257. start_input_ppm (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  258. {
  259. ppm_source_ptr source = (ppm_source_ptr) sinfo;
  260. int c;
  261. unsigned int w, h, maxval;
  262. boolean need_iobuffer, use_raw_buffer, need_rescale;
  263. if (getc(source->pub.input_file) != 'P')
  264. ERREXIT(cinfo, JERR_PPM_NOT);
  265. c = getc(source->pub.input_file); /* subformat discriminator character */
  266. /* detect unsupported variants (ie, PBM) before trying to read header */
  267. switch (c) {
  268. case '2': /* it's a text-format PGM file */
  269. case '3': /* it's a text-format PPM file */
  270. case '5': /* it's a raw-format PGM file */
  271. case '6': /* it's a raw-format PPM file */
  272. break;
  273. default:
  274. ERREXIT(cinfo, JERR_PPM_NOT);
  275. break;
  276. }
  277. /* fetch the remaining header info */
  278. w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
  279. h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
  280. maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
  281. if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
  282. ERREXIT(cinfo, JERR_PPM_NOT);
  283. cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */
  284. cinfo->image_width = (JDIMENSION) w;
  285. cinfo->image_height = (JDIMENSION) h;
  286. source->maxval = maxval;
  287. /* initialize flags to most common settings */
  288. need_iobuffer = TRUE; /* do we need an I/O buffer? */
  289. use_raw_buffer = FALSE; /* do we map input buffer onto I/O buffer? */
  290. need_rescale = TRUE; /* do we need a rescale array? */
  291. switch (c) {
  292. case '2': /* it's a text-format PGM file */
  293. cinfo->input_components = 1;
  294. cinfo->in_color_space = JCS_GRAYSCALE;
  295. TRACEMS2(cinfo, 1, JTRC_PGM_TEXT, w, h);
  296. source->pub.get_pixel_rows = get_text_gray_row;
  297. need_iobuffer = FALSE;
  298. break;
  299. case '3': /* it's a text-format PPM file */
  300. cinfo->input_components = 3;
  301. cinfo->in_color_space = JCS_RGB;
  302. TRACEMS2(cinfo, 1, JTRC_PPM_TEXT, w, h);
  303. source->pub.get_pixel_rows = get_text_rgb_row;
  304. need_iobuffer = FALSE;
  305. break;
  306. case '5': /* it's a raw-format PGM file */
  307. cinfo->input_components = 1;
  308. cinfo->in_color_space = JCS_GRAYSCALE;
  309. TRACEMS2(cinfo, 1, JTRC_PGM, w, h);
  310. if (maxval > 255) {
  311. source->pub.get_pixel_rows = get_word_gray_row;
  312. } else if (maxval == MAXJSAMPLE && sizeof(JSAMPLE) == sizeof(U_CHAR)) {
  313. source->pub.get_pixel_rows = get_raw_row;
  314. use_raw_buffer = TRUE;
  315. need_rescale = FALSE;
  316. } else {
  317. source->pub.get_pixel_rows = get_scaled_gray_row;
  318. }
  319. break;
  320. case '6': /* it's a raw-format PPM file */
  321. cinfo->input_components = 3;
  322. cinfo->in_color_space = JCS_RGB;
  323. TRACEMS2(cinfo, 1, JTRC_PPM, w, h);
  324. if (maxval > 255) {
  325. source->pub.get_pixel_rows = get_word_rgb_row;
  326. } else if (maxval == MAXJSAMPLE && sizeof(JSAMPLE) == sizeof(U_CHAR)) {
  327. source->pub.get_pixel_rows = get_raw_row;
  328. use_raw_buffer = TRUE;
  329. need_rescale = FALSE;
  330. } else {
  331. source->pub.get_pixel_rows = get_scaled_rgb_row;
  332. }
  333. break;
  334. }
  335. /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
  336. if (need_iobuffer) {
  337. source->buffer_width = (size_t) w * cinfo->input_components *
  338. ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
  339. source->iobuffer = (U_CHAR *)
  340. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  341. source->buffer_width);
  342. }
  343. /* Create compressor input buffer. */
  344. if (use_raw_buffer) {
  345. /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
  346. /* Synthesize a JSAMPARRAY pointer structure */
  347. source->pixrow = (JSAMPROW) source->iobuffer;
  348. source->pub.buffer = & source->pixrow;
  349. source->pub.buffer_height = 1;
  350. } else {
  351. /* Need to translate anyway, so make a separate sample buffer. */
  352. source->pub.buffer = (*cinfo->mem->alloc_sarray)
  353. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  354. (JDIMENSION) w * cinfo->input_components, (JDIMENSION) 1);
  355. source->pub.buffer_height = 1;
  356. }
  357. /* Compute the rescaling array if required. */
  358. if (need_rescale) {
  359. long val, half_maxval;
  360. /* On 16-bit-int machines we have to be careful of maxval = 65535 */
  361. source->rescale = (JSAMPLE *)
  362. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  363. (size_t) (((long) maxval + 1L) *
  364. sizeof(JSAMPLE)));
  365. half_maxval = maxval / 2;
  366. for (val = 0; val <= (long) maxval; val++) {
  367. /* The multiplication here must be done in 32 bits to avoid overflow */
  368. source->rescale[val] = (JSAMPLE) ((val * MAXJSAMPLE + half_maxval) /
  369. maxval);
  370. }
  371. }
  372. }
  373. /*
  374. * Finish up at the end of the file.
  375. */
  376. METHODDEF(void)
  377. finish_input_ppm (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  378. {
  379. /* no work */
  380. }
  381. /*
  382. * The module selection routine for PPM format input.
  383. */
  384. GLOBAL(cjpeg_source_ptr)
  385. jinit_read_ppm (j_compress_ptr cinfo)
  386. {
  387. ppm_source_ptr source;
  388. /* Create module interface object */
  389. source = (ppm_source_ptr)
  390. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  391. sizeof(ppm_source_struct));
  392. /* Fill in method ptrs, except get_pixel_rows which start_input sets */
  393. source->pub.start_input = start_input_ppm;
  394. source->pub.finish_input = finish_input_ppm;
  395. return (cjpeg_source_ptr) source;
  396. }
  397. #endif /* PPM_SUPPORTED */