rdtarga.c 15 KB

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