rdppm.c 24 KB

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