rdtarga.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * rdtarga.c
  3. *
  4. * This file was part of the Independent JPEG Group's software:
  5. * Copyright (C) 1991-1996, Thomas G. Lane.
  6. * Modified 2017 by Guido Vollbeding.
  7. * libjpeg-turbo Modifications:
  8. * Copyright (C) 2018, 2021-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 Targa format.
  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 Targa format).
  20. *
  21. * Based on code contributed by Lee Daniel Crocker.
  22. */
  23. #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
  24. #ifdef TARGA_SUPPORTED
  25. /* Macros to deal with unsigned chars as efficiently as compiler allows */
  26. typedef unsigned char U_CHAR;
  27. #define UCH(x) ((int)(x))
  28. #define ReadOK(file, buffer, len) \
  29. (fread(buffer, 1, len, file) == ((size_t)(len)))
  30. /* Private version of data source object */
  31. typedef struct _tga_source_struct *tga_source_ptr;
  32. typedef struct _tga_source_struct {
  33. struct cjpeg_source_struct pub; /* public fields */
  34. j_compress_ptr cinfo; /* back link saves passing separate parm */
  35. JSAMPARRAY colormap; /* Targa colormap (converted to my format) */
  36. jvirt_sarray_ptr whole_image; /* Needed if funny input row order */
  37. JDIMENSION current_row; /* Current logical row number to read */
  38. /* Pointer to routine to extract next Targa pixel from input file */
  39. void (*read_pixel) (tga_source_ptr sinfo);
  40. /* Result of read_pixel is delivered here: */
  41. U_CHAR tga_pixel[4];
  42. int pixel_size; /* Bytes per Targa pixel (1 to 4) */
  43. int cmap_length; /* colormap length */
  44. /* State info for reading RLE-coded pixels; both counts must be init to 0 */
  45. int block_count; /* # of pixels remaining in RLE block */
  46. int dup_pixel_count; /* # of times to duplicate previous pixel */
  47. /* This saves the correct pixel-row-expansion method for preload_image */
  48. JDIMENSION (*get_pixel_rows) (j_compress_ptr cinfo, cjpeg_source_ptr sinfo);
  49. } tga_source_struct;
  50. /* For expanding 5-bit pixel values to 8-bit with best rounding */
  51. static const UINT8 c5to8bits[32] = {
  52. 0, 8, 16, 25, 33, 41, 49, 58,
  53. 66, 74, 82, 90, 99, 107, 115, 123,
  54. 132, 140, 148, 156, 165, 173, 181, 189,
  55. 197, 206, 214, 222, 230, 239, 247, 255
  56. };
  57. LOCAL(int)
  58. read_byte(tga_source_ptr sinfo)
  59. /* Read next byte from Targa file */
  60. {
  61. register FILE *infile = sinfo->pub.input_file;
  62. register int c;
  63. if ((c = getc(infile)) == EOF)
  64. ERREXIT(sinfo->cinfo, JERR_INPUT_EOF);
  65. return c;
  66. }
  67. LOCAL(void)
  68. read_colormap(tga_source_ptr sinfo, int cmaplen, int mapentrysize)
  69. /* Read the colormap from a Targa file */
  70. {
  71. int i;
  72. /* Presently only handles 24-bit BGR format */
  73. if (mapentrysize != 24)
  74. ERREXIT(sinfo->cinfo, JERR_TGA_BADCMAP);
  75. for (i = 0; i < cmaplen; i++) {
  76. sinfo->colormap[2][i] = (JSAMPLE)read_byte(sinfo);
  77. sinfo->colormap[1][i] = (JSAMPLE)read_byte(sinfo);
  78. sinfo->colormap[0][i] = (JSAMPLE)read_byte(sinfo);
  79. }
  80. }
  81. /*
  82. * read_pixel methods: get a single pixel from Targa file into tga_pixel[]
  83. */
  84. METHODDEF(void)
  85. read_non_rle_pixel(tga_source_ptr sinfo)
  86. /* Read one Targa pixel from the input file; no RLE expansion */
  87. {
  88. register int i;
  89. for (i = 0; i < sinfo->pixel_size; i++) {
  90. sinfo->tga_pixel[i] = (U_CHAR)read_byte(sinfo);
  91. }
  92. }
  93. METHODDEF(void)
  94. read_rle_pixel(tga_source_ptr sinfo)
  95. /* Read one Targa pixel from the input file, expanding RLE data as needed */
  96. {
  97. register int i;
  98. /* Duplicate previously read pixel? */
  99. if (sinfo->dup_pixel_count > 0) {
  100. sinfo->dup_pixel_count--;
  101. return;
  102. }
  103. /* Time to read RLE block header? */
  104. if (--sinfo->block_count < 0) { /* decrement pixels remaining in block */
  105. i = read_byte(sinfo);
  106. if (i & 0x80) { /* Start of duplicate-pixel block? */
  107. sinfo->dup_pixel_count = i & 0x7F; /* number of dups after this one */
  108. sinfo->block_count = 0; /* then read new block header */
  109. } else {
  110. sinfo->block_count = i & 0x7F; /* number of pixels after this one */
  111. }
  112. }
  113. /* Read next pixel */
  114. for (i = 0; i < sinfo->pixel_size; i++) {
  115. sinfo->tga_pixel[i] = (U_CHAR)read_byte(sinfo);
  116. }
  117. }
  118. /*
  119. * Read one row of pixels.
  120. *
  121. * We provide several different versions depending on input file format.
  122. */
  123. METHODDEF(JDIMENSION)
  124. get_8bit_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  125. /* This version is for reading 8-bit grayscale pixels */
  126. {
  127. tga_source_ptr source = (tga_source_ptr)sinfo;
  128. register JSAMPROW ptr;
  129. register JDIMENSION col;
  130. ptr = source->pub.buffer[0];
  131. for (col = cinfo->image_width; col > 0; col--) {
  132. (*source->read_pixel) (source); /* Load next pixel into tga_pixel */
  133. *ptr++ = (JSAMPLE)UCH(source->tga_pixel[0]);
  134. }
  135. return 1;
  136. }
  137. METHODDEF(JDIMENSION)
  138. get_8bit_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  139. /* This version is for reading 8-bit colormap indexes */
  140. {
  141. tga_source_ptr source = (tga_source_ptr)sinfo;
  142. register int t;
  143. register JSAMPROW ptr;
  144. register JDIMENSION col;
  145. register JSAMPARRAY colormap = source->colormap;
  146. int cmaplen = source->cmap_length;
  147. ptr = source->pub.buffer[0];
  148. for (col = cinfo->image_width; col > 0; col--) {
  149. (*source->read_pixel) (source); /* Load next pixel into tga_pixel */
  150. t = UCH(source->tga_pixel[0]);
  151. if (t >= cmaplen)
  152. ERREXIT(cinfo, JERR_TGA_BADPARMS);
  153. *ptr++ = colormap[0][t];
  154. *ptr++ = colormap[1][t];
  155. *ptr++ = colormap[2][t];
  156. }
  157. return 1;
  158. }
  159. METHODDEF(JDIMENSION)
  160. get_16bit_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  161. /* This version is for reading 16-bit pixels */
  162. {
  163. tga_source_ptr source = (tga_source_ptr)sinfo;
  164. register int t;
  165. register JSAMPROW ptr;
  166. register JDIMENSION col;
  167. ptr = source->pub.buffer[0];
  168. for (col = cinfo->image_width; col > 0; col--) {
  169. (*source->read_pixel) (source); /* Load next pixel into tga_pixel */
  170. t = UCH(source->tga_pixel[0]);
  171. t += UCH(source->tga_pixel[1]) << 8;
  172. /* We expand 5 bit data to 8 bit sample width.
  173. * The format of the 16-bit (LSB first) input word is
  174. * xRRRRRGGGGGBBBBB
  175. */
  176. ptr[2] = (JSAMPLE)c5to8bits[t & 0x1F];
  177. t >>= 5;
  178. ptr[1] = (JSAMPLE)c5to8bits[t & 0x1F];
  179. t >>= 5;
  180. ptr[0] = (JSAMPLE)c5to8bits[t & 0x1F];
  181. ptr += 3;
  182. }
  183. return 1;
  184. }
  185. METHODDEF(JDIMENSION)
  186. get_24bit_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  187. /* This version is for reading 24-bit pixels */
  188. {
  189. tga_source_ptr source = (tga_source_ptr)sinfo;
  190. register JSAMPROW ptr;
  191. register JDIMENSION col;
  192. ptr = source->pub.buffer[0];
  193. for (col = cinfo->image_width; col > 0; col--) {
  194. (*source->read_pixel) (source); /* Load next pixel into tga_pixel */
  195. *ptr++ = (JSAMPLE)UCH(source->tga_pixel[2]); /* change BGR to RGB order */
  196. *ptr++ = (JSAMPLE)UCH(source->tga_pixel[1]);
  197. *ptr++ = (JSAMPLE)UCH(source->tga_pixel[0]);
  198. }
  199. return 1;
  200. }
  201. /*
  202. * Targa also defines a 32-bit pixel format with order B,G,R,A.
  203. * We presently ignore the attribute byte, so the code for reading
  204. * these pixels is identical to the 24-bit routine above.
  205. * This works because the actual pixel length is only known to read_pixel.
  206. */
  207. #define get_32bit_row get_24bit_row
  208. /*
  209. * This method is for re-reading the input data in standard top-down
  210. * row order. The entire image has already been read into whole_image
  211. * with proper conversion of pixel format, but it's in a funny row order.
  212. */
  213. METHODDEF(JDIMENSION)
  214. get_memory_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  215. {
  216. tga_source_ptr source = (tga_source_ptr)sinfo;
  217. JDIMENSION source_row;
  218. /* Compute row of source that maps to current_row of normal order */
  219. /* For now, assume image is bottom-up and not interlaced. */
  220. /* NEEDS WORK to support interlaced images! */
  221. source_row = cinfo->image_height - source->current_row - 1;
  222. /* Fetch that row from virtual array */
  223. source->pub.buffer = (*cinfo->mem->access_virt_sarray)
  224. ((j_common_ptr)cinfo, source->whole_image,
  225. source_row, (JDIMENSION)1, FALSE);
  226. source->current_row++;
  227. return 1;
  228. }
  229. /*
  230. * This method loads the image into whole_image during the first call on
  231. * get_pixel_rows. The get_pixel_rows pointer is then adjusted to call
  232. * get_memory_row on subsequent calls.
  233. */
  234. METHODDEF(JDIMENSION)
  235. preload_image(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  236. {
  237. tga_source_ptr source = (tga_source_ptr)sinfo;
  238. JDIMENSION row;
  239. cd_progress_ptr progress = (cd_progress_ptr)cinfo->progress;
  240. /* Read the data into a virtual array in input-file row order. */
  241. for (row = 0; row < cinfo->image_height; row++) {
  242. if (progress != NULL) {
  243. progress->pub.pass_counter = (long)row;
  244. progress->pub.pass_limit = (long)cinfo->image_height;
  245. (*progress->pub.progress_monitor) ((j_common_ptr)cinfo);
  246. }
  247. source->pub.buffer = (*cinfo->mem->access_virt_sarray)
  248. ((j_common_ptr)cinfo, source->whole_image, row, (JDIMENSION)1, TRUE);
  249. (*source->get_pixel_rows) (cinfo, sinfo);
  250. }
  251. if (progress != NULL)
  252. progress->completed_extra_passes++;
  253. /* Set up to read from the virtual array in unscrambled order */
  254. source->pub.get_pixel_rows = get_memory_row;
  255. source->current_row = 0;
  256. /* And read the first row */
  257. return get_memory_row(cinfo, sinfo);
  258. }
  259. /*
  260. * Read the file header; return image size and component count.
  261. */
  262. METHODDEF(void)
  263. start_input_tga(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  264. {
  265. tga_source_ptr source = (tga_source_ptr)sinfo;
  266. U_CHAR targaheader[18];
  267. int idlen, cmaptype, subtype, flags, interlace_type, components;
  268. unsigned int width, height, maplen;
  269. boolean is_bottom_up;
  270. #define GET_2B(offset) \
  271. ((unsigned int)UCH(targaheader[offset]) + \
  272. (((unsigned int)UCH(targaheader[offset + 1])) << 8))
  273. if (!ReadOK(source->pub.input_file, targaheader, 18))
  274. ERREXIT(cinfo, JERR_INPUT_EOF);
  275. /* Pretend "15-bit" pixels are 16-bit --- we ignore attribute bit anyway */
  276. if (targaheader[16] == 15)
  277. targaheader[16] = 16;
  278. idlen = UCH(targaheader[0]);
  279. cmaptype = UCH(targaheader[1]);
  280. subtype = UCH(targaheader[2]);
  281. maplen = GET_2B(5);
  282. width = GET_2B(12);
  283. height = GET_2B(14);
  284. source->pixel_size = UCH(targaheader[16]) >> 3;
  285. flags = UCH(targaheader[17]); /* Image Descriptor byte */
  286. is_bottom_up = ((flags & 0x20) == 0); /* bit 5 set => top-down */
  287. interlace_type = flags >> 6; /* bits 6/7 are interlace code */
  288. if (cmaptype > 1 || /* cmaptype must be 0 or 1 */
  289. source->pixel_size < 1 || source->pixel_size > 4 ||
  290. (UCH(targaheader[16]) & 7) != 0 || /* bits/pixel must be multiple of 8 */
  291. interlace_type != 0 || /* currently don't allow interlaced image */
  292. width == 0 || height == 0) /* image width/height must be non-zero */
  293. ERREXIT(cinfo, JERR_TGA_BADPARMS);
  294. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  295. if (sinfo->max_pixels &&
  296. (unsigned long long)width * height > sinfo->max_pixels)
  297. ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  298. #endif
  299. if (subtype > 8) {
  300. /* It's an RLE-coded file */
  301. source->read_pixel = read_rle_pixel;
  302. source->block_count = source->dup_pixel_count = 0;
  303. subtype -= 8;
  304. } else {
  305. /* Non-RLE file */
  306. source->read_pixel = read_non_rle_pixel;
  307. }
  308. /* Now should have subtype 1, 2, or 3 */
  309. components = 3; /* until proven different */
  310. cinfo->in_color_space = JCS_RGB;
  311. switch (subtype) {
  312. case 1: /* Colormapped image */
  313. if (source->pixel_size == 1 && cmaptype == 1)
  314. source->get_pixel_rows = get_8bit_row;
  315. else
  316. ERREXIT(cinfo, JERR_TGA_BADPARMS);
  317. TRACEMS2(cinfo, 1, JTRC_TGA_MAPPED, width, height);
  318. break;
  319. case 2: /* RGB image */
  320. switch (source->pixel_size) {
  321. case 2:
  322. source->get_pixel_rows = get_16bit_row;
  323. break;
  324. case 3:
  325. source->get_pixel_rows = get_24bit_row;
  326. break;
  327. case 4:
  328. source->get_pixel_rows = get_32bit_row;
  329. break;
  330. default:
  331. ERREXIT(cinfo, JERR_TGA_BADPARMS);
  332. break;
  333. }
  334. TRACEMS2(cinfo, 1, JTRC_TGA, width, height);
  335. break;
  336. case 3: /* Grayscale image */
  337. components = 1;
  338. cinfo->in_color_space = JCS_GRAYSCALE;
  339. if (source->pixel_size == 1)
  340. source->get_pixel_rows = get_8bit_gray_row;
  341. else
  342. ERREXIT(cinfo, JERR_TGA_BADPARMS);
  343. TRACEMS2(cinfo, 1, JTRC_TGA_GRAY, width, height);
  344. break;
  345. default:
  346. ERREXIT(cinfo, JERR_TGA_BADPARMS);
  347. break;
  348. }
  349. if (is_bottom_up) {
  350. /* Create a virtual array to buffer the upside-down image. */
  351. source->whole_image = (*cinfo->mem->request_virt_sarray)
  352. ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
  353. (JDIMENSION)width * components, (JDIMENSION)height, (JDIMENSION)1);
  354. if (cinfo->progress != NULL) {
  355. cd_progress_ptr progress = (cd_progress_ptr)cinfo->progress;
  356. progress->total_extra_passes++; /* count file input as separate pass */
  357. }
  358. /* source->pub.buffer will point to the virtual array. */
  359. source->pub.buffer_height = 1; /* in case anyone looks at it */
  360. source->pub.get_pixel_rows = preload_image;
  361. } else {
  362. /* Don't need a virtual array, but do need a one-row input buffer. */
  363. source->whole_image = NULL;
  364. source->pub.buffer = (*cinfo->mem->alloc_sarray)
  365. ((j_common_ptr)cinfo, JPOOL_IMAGE,
  366. (JDIMENSION)width * components, (JDIMENSION)1);
  367. source->pub.buffer_height = 1;
  368. source->pub.get_pixel_rows = source->get_pixel_rows;
  369. }
  370. while (idlen--) /* Throw away ID field */
  371. (void)read_byte(source);
  372. if (maplen > 0) {
  373. if (maplen > 256 || GET_2B(3) != 0)
  374. ERREXIT(cinfo, JERR_TGA_BADCMAP);
  375. /* Allocate space to store the colormap */
  376. source->colormap = (*cinfo->mem->alloc_sarray)
  377. ((j_common_ptr)cinfo, JPOOL_IMAGE, (JDIMENSION)maplen, (JDIMENSION)3);
  378. source->cmap_length = (int)maplen;
  379. /* and read it from the file */
  380. read_colormap(source, (int)maplen, UCH(targaheader[7]));
  381. } else {
  382. if (cmaptype) /* but you promised a cmap! */
  383. ERREXIT(cinfo, JERR_TGA_BADPARMS);
  384. source->colormap = NULL;
  385. source->cmap_length = 0;
  386. }
  387. cinfo->input_components = components;
  388. cinfo->data_precision = 8;
  389. cinfo->image_width = width;
  390. cinfo->image_height = height;
  391. }
  392. /*
  393. * Finish up at the end of the file.
  394. */
  395. METHODDEF(void)
  396. finish_input_tga(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  397. {
  398. /* no work */
  399. }
  400. /*
  401. * The module selection routine for Targa format input.
  402. */
  403. GLOBAL(cjpeg_source_ptr)
  404. jinit_read_targa(j_compress_ptr cinfo)
  405. {
  406. tga_source_ptr source;
  407. /* Create module interface object */
  408. source = (tga_source_ptr)
  409. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  410. sizeof(tga_source_struct));
  411. source->cinfo = cinfo; /* make back link for subroutines */
  412. /* Fill in method ptrs, except get_pixel_rows which start_input sets */
  413. source->pub.start_input = start_input_tga;
  414. source->pub.finish_input = finish_input_tga;
  415. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  416. source->pub.max_pixels = 0;
  417. #endif
  418. return (cjpeg_source_ptr)source;
  419. }
  420. #endif /* TARGA_SUPPORTED */