rdppm.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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-2017, 2020-2022, 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 "cmyk.h"
  25. #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
  26. #ifdef PPM_SUPPORTED
  27. /* Portions of this code are based on the PBMPLUS library, which is:
  28. **
  29. ** Copyright (C) 1988 by Jef Poskanzer.
  30. **
  31. ** Permission to use, copy, modify, and distribute this software and its
  32. ** documentation for any purpose and without fee is hereby granted, provided
  33. ** that the above copyright notice appear in all copies and that both that
  34. ** copyright notice and this permission notice appear in supporting
  35. ** documentation. This software is provided "as is" without express or
  36. ** implied warranty.
  37. */
  38. /* Macros to deal with unsigned chars as efficiently as compiler allows */
  39. typedef unsigned char U_CHAR;
  40. #define UCH(x) ((int)(x))
  41. #define ReadOK(file, buffer, len) \
  42. (fread(buffer, 1, len, file) == ((size_t)(len)))
  43. static int alpha_index[JPEG_NUMCS] = {
  44. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1
  45. };
  46. /* Private version of data source object */
  47. typedef struct {
  48. struct cjpeg_source_struct pub; /* public fields */
  49. /* Usually these two pointers point to the same place: */
  50. U_CHAR *iobuffer; /* fread's I/O buffer */
  51. JSAMPROW pixrow; /* compressor input buffer */
  52. size_t buffer_width; /* width of I/O buffer */
  53. JSAMPLE *rescale; /* => maxval-remapping array, or NULL */
  54. unsigned int maxval;
  55. } ppm_source_struct;
  56. typedef ppm_source_struct *ppm_source_ptr;
  57. LOCAL(int)
  58. pbm_getc(FILE *infile)
  59. /* Read next char, skipping over any comments */
  60. /* A comment/newline sequence is returned as a newline */
  61. {
  62. register int ch;
  63. ch = getc(infile);
  64. if (ch == '#') {
  65. do {
  66. ch = getc(infile);
  67. } while (ch != '\n' && ch != EOF);
  68. }
  69. return ch;
  70. }
  71. LOCAL(unsigned int)
  72. read_pbm_integer(j_compress_ptr cinfo, FILE *infile, unsigned int maxval)
  73. /* Read an unsigned decimal integer from the PPM file */
  74. /* Swallows one trailing character after the integer */
  75. /* Note that on a 16-bit-int machine, only values up to 64k can be read. */
  76. /* This should not be a problem in practice. */
  77. {
  78. register int ch;
  79. register unsigned int val;
  80. /* Skip any leading whitespace */
  81. do {
  82. ch = pbm_getc(infile);
  83. if (ch == EOF)
  84. ERREXIT(cinfo, JERR_INPUT_EOF);
  85. } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
  86. if (ch < '0' || ch > '9')
  87. ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
  88. val = ch - '0';
  89. while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
  90. val *= 10;
  91. val += ch - '0';
  92. if (val > maxval)
  93. ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
  94. }
  95. return val;
  96. }
  97. /*
  98. * Read one row of pixels.
  99. *
  100. * We provide several different versions depending on input file format.
  101. * In all cases, input is scaled to the size of JSAMPLE.
  102. *
  103. * A really fast path is provided for reading byte/sample raw files with
  104. * maxval = MAXJSAMPLE, which is the normal case for 8-bit data.
  105. */
  106. METHODDEF(JDIMENSION)
  107. get_text_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  108. /* This version is for reading text-format PGM files with any maxval */
  109. {
  110. ppm_source_ptr source = (ppm_source_ptr)sinfo;
  111. FILE *infile = source->pub.input_file;
  112. register JSAMPROW ptr;
  113. register JSAMPLE *rescale = source->rescale;
  114. JDIMENSION col;
  115. unsigned int maxval = source->maxval;
  116. ptr = source->pub.buffer[0];
  117. for (col = cinfo->image_width; col > 0; col--) {
  118. *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
  119. }
  120. return 1;
  121. }
  122. #define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \
  123. for (col = cinfo->image_width; col > 0; col--) { \
  124. ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \
  125. alpha_set_op \
  126. ptr += ps; \
  127. } \
  128. }
  129. METHODDEF(JDIMENSION)
  130. get_text_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  131. /* This version is for reading text-format PGM files with any maxval and
  132. converting to extended RGB */
  133. {
  134. ppm_source_ptr source = (ppm_source_ptr)sinfo;
  135. FILE *infile = source->pub.input_file;
  136. register JSAMPROW ptr;
  137. register JSAMPLE *rescale = source->rescale;
  138. JDIMENSION col;
  139. unsigned int maxval = source->maxval;
  140. register int rindex = rgb_red[cinfo->in_color_space];
  141. register int gindex = rgb_green[cinfo->in_color_space];
  142. register int bindex = rgb_blue[cinfo->in_color_space];
  143. register int aindex = alpha_index[cinfo->in_color_space];
  144. register int ps = rgb_pixelsize[cinfo->in_color_space];
  145. ptr = source->pub.buffer[0];
  146. if (maxval == MAXJSAMPLE) {
  147. if (aindex >= 0)
  148. GRAY_RGB_READ_LOOP((JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
  149. ptr[aindex] = 0xFF;)
  150. else
  151. GRAY_RGB_READ_LOOP((JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
  152. } else {
  153. if (aindex >= 0)
  154. GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
  155. ptr[aindex] = 0xFF;)
  156. else
  157. GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
  158. }
  159. return 1;
  160. }
  161. METHODDEF(JDIMENSION)
  162. get_text_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  163. /* This version is for reading text-format PGM files with any maxval and
  164. converting to CMYK */
  165. {
  166. ppm_source_ptr source = (ppm_source_ptr)sinfo;
  167. FILE *infile = source->pub.input_file;
  168. register JSAMPROW ptr;
  169. register JSAMPLE *rescale = source->rescale;
  170. JDIMENSION col;
  171. unsigned int maxval = source->maxval;
  172. ptr = source->pub.buffer[0];
  173. if (maxval == MAXJSAMPLE) {
  174. for (col = cinfo->image_width; col > 0; col--) {
  175. JSAMPLE gray = (JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
  176. rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
  177. ptr += 4;
  178. }
  179. } else {
  180. for (col = cinfo->image_width; col > 0; col--) {
  181. JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
  182. rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
  183. ptr += 4;
  184. }
  185. }
  186. return 1;
  187. }
  188. #define RGB_READ_LOOP(read_op, alpha_set_op) { \
  189. for (col = cinfo->image_width; col > 0; col--) { \
  190. ptr[rindex] = read_op; \
  191. ptr[gindex] = read_op; \
  192. ptr[bindex] = read_op; \
  193. alpha_set_op \
  194. ptr += ps; \
  195. } \
  196. }
  197. METHODDEF(JDIMENSION)
  198. get_text_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  199. /* This version is for reading text-format PPM files with any maxval */
  200. {
  201. ppm_source_ptr source = (ppm_source_ptr)sinfo;
  202. FILE *infile = source->pub.input_file;
  203. register JSAMPROW ptr;
  204. register JSAMPLE *rescale = source->rescale;
  205. JDIMENSION col;
  206. unsigned int maxval = source->maxval;
  207. register int rindex = rgb_red[cinfo->in_color_space];
  208. register int gindex = rgb_green[cinfo->in_color_space];
  209. register int bindex = rgb_blue[cinfo->in_color_space];
  210. register int aindex = alpha_index[cinfo->in_color_space];
  211. register int ps = rgb_pixelsize[cinfo->in_color_space];
  212. ptr = source->pub.buffer[0];
  213. if (maxval == MAXJSAMPLE) {
  214. if (aindex >= 0)
  215. RGB_READ_LOOP((JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
  216. ptr[aindex] = 0xFF;)
  217. else
  218. RGB_READ_LOOP((JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
  219. } else {
  220. if (aindex >= 0)
  221. RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
  222. ptr[aindex] = 0xFF;)
  223. else
  224. RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
  225. }
  226. return 1;
  227. }
  228. METHODDEF(JDIMENSION)
  229. get_text_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  230. /* This version is for reading text-format PPM files with any maxval and
  231. converting to CMYK */
  232. {
  233. ppm_source_ptr source = (ppm_source_ptr)sinfo;
  234. FILE *infile = source->pub.input_file;
  235. register JSAMPROW ptr;
  236. register JSAMPLE *rescale = source->rescale;
  237. JDIMENSION col;
  238. unsigned int maxval = source->maxval;
  239. ptr = source->pub.buffer[0];
  240. if (maxval == MAXJSAMPLE) {
  241. for (col = cinfo->image_width; col > 0; col--) {
  242. JSAMPLE r = (JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
  243. JSAMPLE g = (JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
  244. JSAMPLE b = (JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
  245. rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
  246. ptr += 4;
  247. }
  248. } else {
  249. for (col = cinfo->image_width; col > 0; col--) {
  250. JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
  251. JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
  252. JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
  253. rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
  254. ptr += 4;
  255. }
  256. }
  257. return 1;
  258. }
  259. METHODDEF(JDIMENSION)
  260. get_scaled_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  261. /* This version is for reading raw-byte-format PGM files with any maxval */
  262. {
  263. ppm_source_ptr source = (ppm_source_ptr)sinfo;
  264. register JSAMPROW ptr;
  265. register U_CHAR *bufferptr;
  266. register JSAMPLE *rescale = source->rescale;
  267. JDIMENSION col;
  268. if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
  269. ERREXIT(cinfo, JERR_INPUT_EOF);
  270. ptr = source->pub.buffer[0];
  271. bufferptr = source->iobuffer;
  272. for (col = cinfo->image_width; col > 0; col--) {
  273. *ptr++ = rescale[UCH(*bufferptr++)];
  274. }
  275. return 1;
  276. }
  277. METHODDEF(JDIMENSION)
  278. get_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  279. /* This version is for reading raw-byte-format PGM files with any maxval
  280. and converting to extended RGB */
  281. {
  282. ppm_source_ptr source = (ppm_source_ptr)sinfo;
  283. register JSAMPROW ptr;
  284. register U_CHAR *bufferptr;
  285. register JSAMPLE *rescale = source->rescale;
  286. JDIMENSION col;
  287. unsigned int maxval = source->maxval;
  288. register int rindex = rgb_red[cinfo->in_color_space];
  289. register int gindex = rgb_green[cinfo->in_color_space];
  290. register int bindex = rgb_blue[cinfo->in_color_space];
  291. register int aindex = alpha_index[cinfo->in_color_space];
  292. register int ps = rgb_pixelsize[cinfo->in_color_space];
  293. if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
  294. ERREXIT(cinfo, JERR_INPUT_EOF);
  295. ptr = source->pub.buffer[0];
  296. bufferptr = source->iobuffer;
  297. if (maxval == MAXJSAMPLE) {
  298. if (aindex >= 0)
  299. GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = 0xFF;)
  300. else
  301. GRAY_RGB_READ_LOOP(*bufferptr++, {})
  302. } else {
  303. if (aindex >= 0)
  304. GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], ptr[aindex] = 0xFF;)
  305. else
  306. GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
  307. }
  308. return 1;
  309. }
  310. METHODDEF(JDIMENSION)
  311. get_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  312. /* This version is for reading raw-byte-format PGM files with any maxval
  313. and converting to CMYK */
  314. {
  315. ppm_source_ptr source = (ppm_source_ptr)sinfo;
  316. register JSAMPROW ptr;
  317. register U_CHAR *bufferptr;
  318. register JSAMPLE *rescale = source->rescale;
  319. JDIMENSION col;
  320. unsigned int maxval = source->maxval;
  321. if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
  322. ERREXIT(cinfo, JERR_INPUT_EOF);
  323. ptr = source->pub.buffer[0];
  324. bufferptr = source->iobuffer;
  325. if (maxval == MAXJSAMPLE) {
  326. for (col = cinfo->image_width; col > 0; col--) {
  327. JSAMPLE gray = *bufferptr++;
  328. rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
  329. ptr += 4;
  330. }
  331. } else {
  332. for (col = cinfo->image_width; col > 0; col--) {
  333. JSAMPLE gray = rescale[UCH(*bufferptr++)];
  334. rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
  335. ptr += 4;
  336. }
  337. }
  338. return 1;
  339. }
  340. METHODDEF(JDIMENSION)
  341. get_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  342. /* This version is for reading raw-byte-format PPM files with any maxval */
  343. {
  344. ppm_source_ptr source = (ppm_source_ptr)sinfo;
  345. register JSAMPROW ptr;
  346. register U_CHAR *bufferptr;
  347. register JSAMPLE *rescale = source->rescale;
  348. JDIMENSION col;
  349. unsigned int maxval = source->maxval;
  350. register int rindex = rgb_red[cinfo->in_color_space];
  351. register int gindex = rgb_green[cinfo->in_color_space];
  352. register int bindex = rgb_blue[cinfo->in_color_space];
  353. register int aindex = alpha_index[cinfo->in_color_space];
  354. register int ps = rgb_pixelsize[cinfo->in_color_space];
  355. if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
  356. ERREXIT(cinfo, JERR_INPUT_EOF);
  357. ptr = source->pub.buffer[0];
  358. bufferptr = source->iobuffer;
  359. if (maxval == MAXJSAMPLE) {
  360. if (aindex >= 0)
  361. RGB_READ_LOOP(*bufferptr++, ptr[aindex] = 0xFF;)
  362. else
  363. RGB_READ_LOOP(*bufferptr++, {})
  364. } else {
  365. if (aindex >= 0)
  366. RGB_READ_LOOP(rescale[UCH(*bufferptr++)], ptr[aindex] = 0xFF;)
  367. else
  368. RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
  369. }
  370. return 1;
  371. }
  372. METHODDEF(JDIMENSION)
  373. get_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  374. /* This version is for reading raw-byte-format PPM files with any maxval and
  375. converting to CMYK */
  376. {
  377. ppm_source_ptr source = (ppm_source_ptr)sinfo;
  378. register JSAMPROW ptr;
  379. register U_CHAR *bufferptr;
  380. register JSAMPLE *rescale = source->rescale;
  381. JDIMENSION col;
  382. unsigned int maxval = source->maxval;
  383. if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
  384. ERREXIT(cinfo, JERR_INPUT_EOF);
  385. ptr = source->pub.buffer[0];
  386. bufferptr = source->iobuffer;
  387. if (maxval == MAXJSAMPLE) {
  388. for (col = cinfo->image_width; col > 0; col--) {
  389. JSAMPLE r = *bufferptr++;
  390. JSAMPLE g = *bufferptr++;
  391. JSAMPLE b = *bufferptr++;
  392. rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
  393. ptr += 4;
  394. }
  395. } else {
  396. for (col = cinfo->image_width; col > 0; col--) {
  397. JSAMPLE r = rescale[UCH(*bufferptr++)];
  398. JSAMPLE g = rescale[UCH(*bufferptr++)];
  399. JSAMPLE b = rescale[UCH(*bufferptr++)];
  400. rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
  401. ptr += 4;
  402. }
  403. }
  404. return 1;
  405. }
  406. METHODDEF(JDIMENSION)
  407. get_raw_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  408. /* This version is for reading raw-byte-format files with maxval = MAXJSAMPLE.
  409. * In this case we just read right into the JSAMPLE buffer!
  410. * Note that same code works for PPM and PGM files.
  411. */
  412. {
  413. ppm_source_ptr source = (ppm_source_ptr)sinfo;
  414. if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
  415. ERREXIT(cinfo, JERR_INPUT_EOF);
  416. return 1;
  417. }
  418. METHODDEF(JDIMENSION)
  419. get_word_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  420. /* This version is for reading raw-word-format PGM files with any maxval */
  421. {
  422. ppm_source_ptr source = (ppm_source_ptr)sinfo;
  423. register JSAMPROW ptr;
  424. register U_CHAR *bufferptr;
  425. register JSAMPLE *rescale = source->rescale;
  426. JDIMENSION col;
  427. unsigned int maxval = source->maxval;
  428. if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
  429. ERREXIT(cinfo, JERR_INPUT_EOF);
  430. ptr = source->pub.buffer[0];
  431. bufferptr = source->iobuffer;
  432. for (col = cinfo->image_width; col > 0; col--) {
  433. register unsigned int temp;
  434. temp = UCH(*bufferptr++) << 8;
  435. temp |= UCH(*bufferptr++);
  436. if (temp > maxval)
  437. ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
  438. *ptr++ = rescale[temp];
  439. }
  440. return 1;
  441. }
  442. METHODDEF(JDIMENSION)
  443. get_word_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  444. /* This version is for reading raw-word-format PPM files with any maxval */
  445. {
  446. ppm_source_ptr source = (ppm_source_ptr)sinfo;
  447. register JSAMPROW ptr;
  448. register U_CHAR *bufferptr;
  449. register JSAMPLE *rescale = source->rescale;
  450. JDIMENSION col;
  451. unsigned int maxval = source->maxval;
  452. register int rindex = rgb_red[cinfo->in_color_space];
  453. register int gindex = rgb_green[cinfo->in_color_space];
  454. register int bindex = rgb_blue[cinfo->in_color_space];
  455. register int aindex = alpha_index[cinfo->in_color_space];
  456. register int ps = rgb_pixelsize[cinfo->in_color_space];
  457. if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
  458. ERREXIT(cinfo, JERR_INPUT_EOF);
  459. ptr = source->pub.buffer[0];
  460. bufferptr = source->iobuffer;
  461. for (col = cinfo->image_width; col > 0; col--) {
  462. register unsigned int temp;
  463. temp = UCH(*bufferptr++) << 8;
  464. temp |= UCH(*bufferptr++);
  465. if (temp > maxval)
  466. ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
  467. ptr[rindex] = rescale[temp];
  468. temp = UCH(*bufferptr++) << 8;
  469. temp |= UCH(*bufferptr++);
  470. if (temp > maxval)
  471. ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
  472. ptr[gindex] = rescale[temp];
  473. temp = UCH(*bufferptr++) << 8;
  474. temp |= UCH(*bufferptr++);
  475. if (temp > maxval)
  476. ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
  477. ptr[bindex] = rescale[temp];
  478. if (aindex >= 0)
  479. ptr[aindex] = 0xFF;
  480. ptr += ps;
  481. }
  482. return 1;
  483. }
  484. /*
  485. * Read the file header; return image size and component count.
  486. */
  487. METHODDEF(void)
  488. start_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  489. {
  490. ppm_source_ptr source = (ppm_source_ptr)sinfo;
  491. int c;
  492. unsigned int w, h, maxval;
  493. boolean need_iobuffer, use_raw_buffer, need_rescale;
  494. if (getc(source->pub.input_file) != 'P')
  495. ERREXIT(cinfo, JERR_PPM_NOT);
  496. c = getc(source->pub.input_file); /* subformat discriminator character */
  497. /* detect unsupported variants (ie, PBM) before trying to read header */
  498. switch (c) {
  499. case '2': /* it's a text-format PGM file */
  500. case '3': /* it's a text-format PPM file */
  501. case '5': /* it's a raw-format PGM file */
  502. case '6': /* it's a raw-format PPM file */
  503. break;
  504. default:
  505. ERREXIT(cinfo, JERR_PPM_NOT);
  506. break;
  507. }
  508. /* fetch the remaining header info */
  509. w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
  510. h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
  511. maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
  512. if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
  513. ERREXIT(cinfo, JERR_PPM_NOT);
  514. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  515. if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
  516. ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  517. #endif
  518. cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */
  519. cinfo->image_width = (JDIMENSION)w;
  520. cinfo->image_height = (JDIMENSION)h;
  521. source->maxval = maxval;
  522. /* initialize flags to most common settings */
  523. need_iobuffer = TRUE; /* do we need an I/O buffer? */
  524. use_raw_buffer = FALSE; /* do we map input buffer onto I/O buffer? */
  525. need_rescale = TRUE; /* do we need a rescale array? */
  526. switch (c) {
  527. case '2': /* it's a text-format PGM file */
  528. if (cinfo->in_color_space == JCS_UNKNOWN ||
  529. cinfo->in_color_space == JCS_RGB)
  530. cinfo->in_color_space = JCS_GRAYSCALE;
  531. TRACEMS2(cinfo, 1, JTRC_PGM_TEXT, w, h);
  532. if (cinfo->in_color_space == JCS_GRAYSCALE)
  533. source->pub.get_pixel_rows = get_text_gray_row;
  534. else if (IsExtRGB(cinfo->in_color_space))
  535. source->pub.get_pixel_rows = get_text_gray_rgb_row;
  536. else if (cinfo->in_color_space == JCS_CMYK)
  537. source->pub.get_pixel_rows = get_text_gray_cmyk_row;
  538. else
  539. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  540. need_iobuffer = FALSE;
  541. break;
  542. case '3': /* it's a text-format PPM file */
  543. if (cinfo->in_color_space == JCS_UNKNOWN)
  544. cinfo->in_color_space = JCS_EXT_RGB;
  545. TRACEMS2(cinfo, 1, JTRC_PPM_TEXT, w, h);
  546. if (IsExtRGB(cinfo->in_color_space))
  547. source->pub.get_pixel_rows = get_text_rgb_row;
  548. else if (cinfo->in_color_space == JCS_CMYK)
  549. source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
  550. else
  551. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  552. need_iobuffer = FALSE;
  553. break;
  554. case '5': /* it's a raw-format PGM file */
  555. if (cinfo->in_color_space == JCS_UNKNOWN ||
  556. cinfo->in_color_space == JCS_RGB)
  557. cinfo->in_color_space = JCS_GRAYSCALE;
  558. TRACEMS2(cinfo, 1, JTRC_PGM, w, h);
  559. if (maxval > 255) {
  560. if (cinfo->in_color_space == JCS_GRAYSCALE)
  561. source->pub.get_pixel_rows = get_word_gray_row;
  562. else
  563. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  564. } else if (maxval == MAXJSAMPLE && sizeof(JSAMPLE) == sizeof(U_CHAR) &&
  565. cinfo->in_color_space == JCS_GRAYSCALE) {
  566. source->pub.get_pixel_rows = get_raw_row;
  567. use_raw_buffer = TRUE;
  568. need_rescale = FALSE;
  569. } else {
  570. if (cinfo->in_color_space == JCS_GRAYSCALE)
  571. source->pub.get_pixel_rows = get_scaled_gray_row;
  572. else if (IsExtRGB(cinfo->in_color_space))
  573. source->pub.get_pixel_rows = get_gray_rgb_row;
  574. else if (cinfo->in_color_space == JCS_CMYK)
  575. source->pub.get_pixel_rows = get_gray_cmyk_row;
  576. else
  577. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  578. }
  579. break;
  580. case '6': /* it's a raw-format PPM file */
  581. if (cinfo->in_color_space == JCS_UNKNOWN)
  582. cinfo->in_color_space = JCS_EXT_RGB;
  583. TRACEMS2(cinfo, 1, JTRC_PPM, w, h);
  584. if (maxval > 255) {
  585. if (IsExtRGB(cinfo->in_color_space))
  586. source->pub.get_pixel_rows = get_word_rgb_row;
  587. else
  588. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  589. } else if (maxval == MAXJSAMPLE && sizeof(JSAMPLE) == sizeof(U_CHAR) &&
  590. #if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
  591. (cinfo->in_color_space == JCS_EXT_RGB ||
  592. cinfo->in_color_space == JCS_RGB)) {
  593. #else
  594. cinfo->in_color_space == JCS_EXT_RGB) {
  595. #endif
  596. source->pub.get_pixel_rows = get_raw_row;
  597. use_raw_buffer = TRUE;
  598. need_rescale = FALSE;
  599. } else {
  600. if (IsExtRGB(cinfo->in_color_space))
  601. source->pub.get_pixel_rows = get_rgb_row;
  602. else if (cinfo->in_color_space == JCS_CMYK)
  603. source->pub.get_pixel_rows = get_rgb_cmyk_row;
  604. else
  605. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  606. }
  607. break;
  608. }
  609. if (IsExtRGB(cinfo->in_color_space))
  610. cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
  611. else if (cinfo->in_color_space == JCS_GRAYSCALE)
  612. cinfo->input_components = 1;
  613. else if (cinfo->in_color_space == JCS_CMYK)
  614. cinfo->input_components = 4;
  615. /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
  616. if (need_iobuffer) {
  617. if (c == '6')
  618. source->buffer_width = (size_t)w * 3 *
  619. ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
  620. else
  621. source->buffer_width = (size_t)w *
  622. ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
  623. source->iobuffer = (U_CHAR *)
  624. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  625. source->buffer_width);
  626. }
  627. /* Create compressor input buffer. */
  628. if (use_raw_buffer) {
  629. /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
  630. /* Synthesize a JSAMPARRAY pointer structure */
  631. source->pixrow = (JSAMPROW)source->iobuffer;
  632. source->pub.buffer = &source->pixrow;
  633. source->pub.buffer_height = 1;
  634. } else {
  635. /* Need to translate anyway, so make a separate sample buffer. */
  636. source->pub.buffer = (*cinfo->mem->alloc_sarray)
  637. ((j_common_ptr)cinfo, JPOOL_IMAGE,
  638. (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
  639. source->pub.buffer_height = 1;
  640. }
  641. /* Compute the rescaling array if required. */
  642. if (need_rescale) {
  643. long val, half_maxval;
  644. /* On 16-bit-int machines we have to be careful of maxval = 65535 */
  645. source->rescale = (JSAMPLE *)
  646. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  647. (size_t)(((long)MAX(maxval, 255) + 1L) *
  648. sizeof(JSAMPLE)));
  649. memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
  650. sizeof(JSAMPLE)));
  651. half_maxval = maxval / 2;
  652. for (val = 0; val <= (long)maxval; val++) {
  653. /* The multiplication here must be done in 32 bits to avoid overflow */
  654. source->rescale[val] = (JSAMPLE)((val * MAXJSAMPLE + half_maxval) /
  655. maxval);
  656. }
  657. }
  658. }
  659. /*
  660. * Finish up at the end of the file.
  661. */
  662. METHODDEF(void)
  663. finish_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  664. {
  665. /* no work */
  666. }
  667. /*
  668. * The module selection routine for PPM format input.
  669. */
  670. GLOBAL(cjpeg_source_ptr)
  671. jinit_read_ppm(j_compress_ptr cinfo)
  672. {
  673. ppm_source_ptr source;
  674. /* Create module interface object */
  675. source = (ppm_source_ptr)
  676. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  677. sizeof(ppm_source_struct));
  678. /* Fill in method ptrs, except get_pixel_rows which start_input sets */
  679. source->pub.start_input = start_input_ppm;
  680. source->pub.finish_input = finish_input_ppm;
  681. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  682. source->pub.max_pixels = 0;
  683. #endif
  684. return (cjpeg_source_ptr)source;
  685. }
  686. #endif /* PPM_SUPPORTED */