rdtarga.c 15 KB

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