rdtarga.c 15 KB

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