rdppm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * rdppm.c
  3. *
  4. * Copyright (C) 1991-1997, Thomas G. Lane.
  5. * Modified 2009-2020 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. /* Portions of this code are based on the PBMPLUS library, which is:
  22. **
  23. ** Copyright (C) 1988 by Jef Poskanzer.
  24. **
  25. ** Permission to use, copy, modify, and distribute this software and its
  26. ** documentation for any purpose and without fee is hereby granted, provided
  27. ** that the above copyright notice appear in all copies and that both that
  28. ** copyright notice and this permission notice appear in supporting
  29. ** documentation. This software is provided "as is" without express or
  30. ** implied warranty.
  31. */
  32. #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
  33. #ifdef PPM_SUPPORTED
  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. typedef char U_CHAR;
  40. #ifdef CHAR_IS_UNSIGNED
  41. #define UCH(x) ((int) (x))
  42. #else
  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. /* Usually these two pointers point to the same place: */
  60. U_CHAR *iobuffer; /* fread's I/O buffer */
  61. JSAMPROW pixrow; /* compressor input buffer */
  62. size_t buffer_width; /* width of I/O buffer */
  63. JSAMPLE *rescale; /* => maxval-remapping array, or NULL */
  64. unsigned int maxval;
  65. } ppm_source_struct;
  66. typedef ppm_source_struct * ppm_source_ptr;
  67. LOCAL(int)
  68. pbm_getc (FILE * infile)
  69. /* Read next char, skipping over any comments */
  70. /* A comment/newline sequence is returned as a newline */
  71. {
  72. register int ch;
  73. ch = getc(infile);
  74. if (ch == '#') {
  75. do {
  76. ch = getc(infile);
  77. } while (ch != '\n' && ch != EOF);
  78. }
  79. return ch;
  80. }
  81. LOCAL(unsigned int)
  82. read_pbm_integer (j_compress_ptr cinfo, FILE * infile)
  83. /* Read an unsigned decimal integer from the PPM file */
  84. /* Swallows one trailing character after the integer */
  85. /* Note that on a 16-bit-int machine, only values up to 64k can be read. */
  86. /* This should not be a problem in practice. */
  87. {
  88. register int ch;
  89. register unsigned int val;
  90. /* Skip any leading whitespace */
  91. do {
  92. ch = pbm_getc(infile);
  93. if (ch == EOF)
  94. ERREXIT(cinfo, JERR_INPUT_EOF);
  95. } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
  96. if (ch < '0' || ch > '9')
  97. ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
  98. val = ch - '0';
  99. while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
  100. val *= 10;
  101. val += ch - '0';
  102. }
  103. return val;
  104. }
  105. /*
  106. * Read one row of pixels.
  107. *
  108. * We provide several different versions depending on input file format.
  109. * In all cases, input is scaled to the size of JSAMPLE.
  110. *
  111. * A really fast path is provided for reading byte/sample raw files with
  112. * maxval = MAXJSAMPLE, which is the normal case for 8-bit data.
  113. */
  114. METHODDEF(JDIMENSION)
  115. get_text_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  116. /* This version is for reading text-format PGM files with any maxval */
  117. {
  118. ppm_source_ptr source = (ppm_source_ptr) sinfo;
  119. FILE * infile = source->pub.input_file;
  120. register JSAMPROW ptr;
  121. register JSAMPLE *rescale = source->rescale;
  122. unsigned int maxval = source->maxval;
  123. JDIMENSION col;
  124. ptr = source->pixrow;
  125. for (col = cinfo->image_width; col > 0; col--) {
  126. register unsigned int temp;
  127. temp = read_pbm_integer(cinfo, infile);
  128. if (temp > maxval)
  129. ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
  130. *ptr++ = rescale[temp];
  131. }
  132. return 1;
  133. }
  134. METHODDEF(JDIMENSION)
  135. get_text_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  136. /* This version is for reading text-format PPM files with any maxval */
  137. {
  138. ppm_source_ptr source = (ppm_source_ptr) sinfo;
  139. FILE * infile = source->pub.input_file;
  140. register JSAMPROW ptr;
  141. register JSAMPLE *rescale = source->rescale;
  142. unsigned int maxval = source->maxval;
  143. JDIMENSION col;
  144. ptr = source->pixrow;
  145. for (col = cinfo->image_width; col > 0; col--) {
  146. register unsigned int temp;
  147. temp = read_pbm_integer(cinfo, infile);
  148. if (temp > maxval)
  149. ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
  150. *ptr++ = rescale[temp];
  151. temp = read_pbm_integer(cinfo, infile);
  152. if (temp > maxval)
  153. ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
  154. *ptr++ = rescale[temp];
  155. temp = read_pbm_integer(cinfo, infile);
  156. if (temp > maxval)
  157. ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
  158. *ptr++ = rescale[temp];
  159. }
  160. return 1;
  161. }
  162. METHODDEF(JDIMENSION)
  163. get_scaled_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  164. /* This version is for reading raw-byte-format PGM 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. unsigned int maxval = source->maxval;
  171. JDIMENSION col;
  172. if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
  173. ERREXIT(cinfo, JERR_INPUT_EOF);
  174. ptr = source->pixrow;
  175. bufferptr = source->iobuffer;
  176. for (col = cinfo->image_width; col > 0; col--) {
  177. register unsigned int temp;
  178. temp = (unsigned int) UCH(*bufferptr++);
  179. if (temp > maxval)
  180. ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
  181. *ptr++ = rescale[temp];
  182. }
  183. return 1;
  184. }
  185. METHODDEF(JDIMENSION)
  186. get_scaled_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  187. /* This version is for reading raw-byte-format PPM files with any maxval */
  188. {
  189. ppm_source_ptr source = (ppm_source_ptr) sinfo;
  190. register JSAMPROW ptr;
  191. register U_CHAR * bufferptr;
  192. register JSAMPLE *rescale = source->rescale;
  193. unsigned int maxval = source->maxval;
  194. JDIMENSION col;
  195. if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
  196. ERREXIT(cinfo, JERR_INPUT_EOF);
  197. ptr = source->pixrow;
  198. bufferptr = source->iobuffer;
  199. for (col = cinfo->image_width; col > 0; col--) {
  200. register unsigned int temp;
  201. temp = (unsigned int) UCH(*bufferptr++);
  202. if (temp > maxval)
  203. ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
  204. *ptr++ = rescale[temp];
  205. temp = (unsigned int) UCH(*bufferptr++);
  206. if (temp > maxval)
  207. ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
  208. *ptr++ = rescale[temp];
  209. temp = (unsigned int) UCH(*bufferptr++);
  210. if (temp > maxval)
  211. ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
  212. *ptr++ = rescale[temp];
  213. }
  214. return 1;
  215. }
  216. METHODDEF(JDIMENSION)
  217. get_raw_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  218. /* This version is for reading raw-byte-format files with maxval = MAXJSAMPLE.
  219. * In this case we just read right into the JSAMPLE buffer!
  220. * Note that same code works for PPM and PGM files.
  221. */
  222. {
  223. ppm_source_ptr source = (ppm_source_ptr) sinfo;
  224. if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
  225. ERREXIT(cinfo, JERR_INPUT_EOF);
  226. return 1;
  227. }
  228. METHODDEF(JDIMENSION)
  229. get_word_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  230. /* This version is for reading raw-word-format PGM files with any maxval */
  231. {
  232. ppm_source_ptr source = (ppm_source_ptr) sinfo;
  233. register JSAMPROW ptr;
  234. register U_CHAR * bufferptr;
  235. register JSAMPLE *rescale = source->rescale;
  236. unsigned int maxval = source->maxval;
  237. JDIMENSION col;
  238. if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
  239. ERREXIT(cinfo, JERR_INPUT_EOF);
  240. ptr = source->pixrow;
  241. bufferptr = source->iobuffer;
  242. for (col = cinfo->image_width; col > 0; col--) {
  243. register unsigned int temp;
  244. temp = ((unsigned int) UCH(*bufferptr++)) << 8;
  245. temp |= (unsigned int) UCH(*bufferptr++);
  246. if (temp > maxval)
  247. ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
  248. *ptr++ = rescale[temp];
  249. }
  250. return 1;
  251. }
  252. METHODDEF(JDIMENSION)
  253. get_word_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  254. /* This version is for reading raw-word-format PPM files with any maxval */
  255. {
  256. ppm_source_ptr source = (ppm_source_ptr) sinfo;
  257. register JSAMPROW ptr;
  258. register U_CHAR * bufferptr;
  259. register JSAMPLE *rescale = source->rescale;
  260. unsigned int maxval = source->maxval;
  261. JDIMENSION col;
  262. if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
  263. ERREXIT(cinfo, JERR_INPUT_EOF);
  264. ptr = source->pixrow;
  265. bufferptr = source->iobuffer;
  266. for (col = cinfo->image_width; col > 0; col--) {
  267. register unsigned int temp;
  268. temp = ((unsigned int) UCH(*bufferptr++)) << 8;
  269. temp |= (unsigned int) UCH(*bufferptr++);
  270. if (temp > maxval)
  271. ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
  272. *ptr++ = rescale[temp];
  273. temp = ((unsigned int) UCH(*bufferptr++)) << 8;
  274. temp |= (unsigned int) UCH(*bufferptr++);
  275. if (temp > maxval)
  276. ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
  277. *ptr++ = rescale[temp];
  278. temp = ((unsigned int) UCH(*bufferptr++)) << 8;
  279. temp |= (unsigned int) UCH(*bufferptr++);
  280. if (temp > maxval)
  281. ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
  282. *ptr++ = rescale[temp];
  283. }
  284. return 1;
  285. }
  286. /*
  287. * Read the file header; return image size and component count.
  288. */
  289. METHODDEF(void)
  290. start_input_ppm (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  291. {
  292. ppm_source_ptr source = (ppm_source_ptr) sinfo;
  293. int c;
  294. unsigned int w, h, maxval;
  295. boolean need_iobuffer, use_raw_buffer, need_rescale;
  296. if (getc(source->pub.input_file) != 'P')
  297. ERREXIT(cinfo, JERR_PPM_NOT);
  298. c = getc(source->pub.input_file); /* subformat discriminator character */
  299. /* detect unsupported variants (ie, PBM) before trying to read header */
  300. switch (c) {
  301. case '2': /* it's a text-format PGM file */
  302. case '3': /* it's a text-format PPM file */
  303. case '5': /* it's a raw-format PGM file */
  304. case '6': /* it's a raw-format PPM file */
  305. break;
  306. default:
  307. ERREXIT(cinfo, JERR_PPM_NOT);
  308. }
  309. /* fetch the remaining header info */
  310. w = read_pbm_integer(cinfo, source->pub.input_file);
  311. h = read_pbm_integer(cinfo, source->pub.input_file);
  312. maxval = read_pbm_integer(cinfo, source->pub.input_file);
  313. if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
  314. ERREXIT(cinfo, JERR_PPM_NOT);
  315. if (((long) w >> 24) || /* sanity check for buffer allocation below */
  316. ((long) maxval >> 16)) /* support max 16-bit (2-byte) sample values */
  317. ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
  318. cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */
  319. cinfo->image_width = (JDIMENSION) w;
  320. cinfo->image_height = (JDIMENSION) h;
  321. source->maxval = maxval;
  322. /* initialize flags to most common settings */
  323. need_iobuffer = TRUE; /* do we need an I/O buffer? */
  324. use_raw_buffer = FALSE; /* do we map input buffer onto I/O buffer? */
  325. need_rescale = TRUE; /* do we need a rescale array? */
  326. switch (c) {
  327. case '2': /* it's a text-format PGM file */
  328. cinfo->input_components = 1;
  329. cinfo->in_color_space = JCS_GRAYSCALE;
  330. TRACEMS2(cinfo, 1, JTRC_PGM_TEXT, w, h);
  331. source->pub.get_pixel_rows = get_text_gray_row;
  332. need_iobuffer = FALSE;
  333. break;
  334. case '3': /* it's a text-format PPM file */
  335. cinfo->input_components = 3;
  336. cinfo->in_color_space = JCS_RGB;
  337. TRACEMS2(cinfo, 1, JTRC_PPM_TEXT, w, h);
  338. source->pub.get_pixel_rows = get_text_rgb_row;
  339. need_iobuffer = FALSE;
  340. break;
  341. case '5': /* it's a raw-format PGM file */
  342. cinfo->input_components = 1;
  343. cinfo->in_color_space = JCS_GRAYSCALE;
  344. TRACEMS2(cinfo, 1, JTRC_PGM, w, h);
  345. if (maxval > 255) {
  346. source->pub.get_pixel_rows = get_word_gray_row;
  347. } else if (maxval == MAXJSAMPLE && SIZEOF(JSAMPLE) == SIZEOF(U_CHAR)) {
  348. source->pub.get_pixel_rows = get_raw_row;
  349. use_raw_buffer = TRUE;
  350. need_rescale = FALSE;
  351. } else {
  352. source->pub.get_pixel_rows = get_scaled_gray_row;
  353. }
  354. break;
  355. case '6': /* it's a raw-format PPM file */
  356. cinfo->input_components = 3;
  357. cinfo->in_color_space = JCS_RGB;
  358. TRACEMS2(cinfo, 1, JTRC_PPM, w, h);
  359. if (maxval > 255) {
  360. source->pub.get_pixel_rows = get_word_rgb_row;
  361. } else if (maxval == MAXJSAMPLE && SIZEOF(JSAMPLE) == SIZEOF(U_CHAR)) {
  362. source->pub.get_pixel_rows = get_raw_row;
  363. use_raw_buffer = TRUE;
  364. need_rescale = FALSE;
  365. } else {
  366. source->pub.get_pixel_rows = get_scaled_rgb_row;
  367. }
  368. break;
  369. }
  370. /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
  371. if (need_iobuffer) {
  372. source->buffer_width = (size_t) w * (size_t) cinfo->input_components *
  373. ((maxval <= 255) ? SIZEOF(U_CHAR) : (2 * SIZEOF(U_CHAR)));
  374. source->iobuffer = (U_CHAR *) (*cinfo->mem->alloc_small)
  375. ((j_common_ptr) cinfo, JPOOL_IMAGE, source->buffer_width);
  376. }
  377. /* Create compressor input buffer. */
  378. if (use_raw_buffer) {
  379. /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
  380. /* Cast here implies near->far pointer conversion on PCs */
  381. source->pixrow = (JSAMPROW) source->iobuffer;
  382. } else {
  383. /* Need to translate anyway, so make a separate sample buffer. */
  384. source->pixrow = (JSAMPROW) (*cinfo->mem->alloc_large)
  385. ((j_common_ptr) cinfo, JPOOL_IMAGE, (size_t) w *
  386. (size_t) cinfo->input_components * SIZEOF(JSAMPLE));
  387. }
  388. /* Synthesize a JSAMPARRAY pointer structure */
  389. source->pub.buffer = & source->pixrow;
  390. source->pub.buffer_height = 1;
  391. /* Compute the rescaling array if required. */
  392. if (need_rescale) {
  393. INT32 val, half_maxval;
  394. /* On 16-bit-int machines we have to be careful of maxval = 65535 */
  395. source->rescale = (JSAMPLE *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo,
  396. JPOOL_IMAGE, ((size_t) maxval + (size_t) 1) * SIZEOF(JSAMPLE));
  397. half_maxval = maxval / 2;
  398. for (val = 0; val <= (INT32) maxval; val++) {
  399. /* The multiplication here must be done in 32 bits to avoid overflow */
  400. source->rescale[val] = (JSAMPLE) ((val * MAXJSAMPLE + half_maxval) / maxval);
  401. }
  402. }
  403. }
  404. /*
  405. * Finish up at the end of the file.
  406. */
  407. METHODDEF(void)
  408. finish_input_ppm (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  409. {
  410. /* no work */
  411. }
  412. /*
  413. * The module selection routine for PPM format input.
  414. */
  415. GLOBAL(cjpeg_source_ptr)
  416. jinit_read_ppm (j_compress_ptr cinfo)
  417. {
  418. ppm_source_ptr source;
  419. /* Create module interface object */
  420. source = (ppm_source_ptr) (*cinfo->mem->alloc_small)
  421. ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(ppm_source_struct));
  422. /* Fill in method ptrs, except get_pixel_rows which start_input sets */
  423. source->pub.start_input = start_input_ppm;
  424. source->pub.finish_input = finish_input_ppm;
  425. return &source->pub;
  426. }
  427. #endif /* PPM_SUPPORTED */