wrtarga.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * wrtarga.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 and
  7. * information relevant to libjpeg-turbo.
  8. * For conditions of distribution and use, see the accompanying README.ijg
  9. * file.
  10. *
  11. * This file contains routines to write output images in Targa format.
  12. *
  13. * These routines may need modification for non-Unix environments or
  14. * specialized applications. As they stand, they assume output to
  15. * an ordinary stdio stream.
  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. /*
  22. * To support 12-bit JPEG data, we'd have to scale output down to 8 bits.
  23. * This is not yet implemented.
  24. */
  25. #if BITS_IN_JSAMPLE != 8
  26. Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
  27. #endif
  28. /* Private version of data destination object */
  29. typedef struct {
  30. struct djpeg_dest_struct pub; /* public fields */
  31. char *iobuffer; /* physical I/O buffer */
  32. JDIMENSION buffer_width; /* width of one row */
  33. } tga_dest_struct;
  34. typedef tga_dest_struct *tga_dest_ptr;
  35. LOCAL(void)
  36. write_header (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, int num_colors)
  37. /* Create and write a Targa header */
  38. {
  39. char targaheader[18];
  40. /* Set unused fields of header to 0 */
  41. MEMZERO(targaheader, sizeof(targaheader));
  42. if (num_colors > 0) {
  43. targaheader[1] = 1; /* color map type 1 */
  44. targaheader[5] = (char) (num_colors & 0xFF);
  45. targaheader[6] = (char) (num_colors >> 8);
  46. targaheader[7] = 24; /* 24 bits per cmap entry */
  47. }
  48. targaheader[12] = (char) (cinfo->output_width & 0xFF);
  49. targaheader[13] = (char) (cinfo->output_width >> 8);
  50. targaheader[14] = (char) (cinfo->output_height & 0xFF);
  51. targaheader[15] = (char) (cinfo->output_height >> 8);
  52. targaheader[17] = 0x20; /* Top-down, non-interlaced */
  53. if (cinfo->out_color_space == JCS_GRAYSCALE) {
  54. targaheader[2] = 3; /* image type = uncompressed grayscale */
  55. targaheader[16] = 8; /* bits per pixel */
  56. } else { /* must be RGB */
  57. if (num_colors > 0) {
  58. targaheader[2] = 1; /* image type = colormapped RGB */
  59. targaheader[16] = 8;
  60. } else {
  61. targaheader[2] = 2; /* image type = uncompressed RGB */
  62. targaheader[16] = 24;
  63. }
  64. }
  65. if (JFWRITE(dinfo->output_file, targaheader, 18) != (size_t) 18)
  66. ERREXIT(cinfo, JERR_FILE_WRITE);
  67. }
  68. /*
  69. * Write some pixel data.
  70. * In this module rows_supplied will always be 1.
  71. */
  72. METHODDEF(void)
  73. put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
  74. JDIMENSION rows_supplied)
  75. /* used for unquantized full-color output */
  76. {
  77. tga_dest_ptr dest = (tga_dest_ptr) dinfo;
  78. register JSAMPROW inptr;
  79. register char *outptr;
  80. register JDIMENSION col;
  81. inptr = dest->pub.buffer[0];
  82. outptr = dest->iobuffer;
  83. for (col = cinfo->output_width; col > 0; col--) {
  84. outptr[0] = (char) GETJSAMPLE(inptr[2]); /* RGB to BGR order */
  85. outptr[1] = (char) GETJSAMPLE(inptr[1]);
  86. outptr[2] = (char) GETJSAMPLE(inptr[0]);
  87. inptr += 3, outptr += 3;
  88. }
  89. (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
  90. }
  91. METHODDEF(void)
  92. put_gray_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
  93. JDIMENSION rows_supplied)
  94. /* used for grayscale OR quantized color output */
  95. {
  96. tga_dest_ptr dest = (tga_dest_ptr) dinfo;
  97. register JSAMPROW inptr;
  98. register char *outptr;
  99. register JDIMENSION col;
  100. inptr = dest->pub.buffer[0];
  101. outptr = dest->iobuffer;
  102. for (col = cinfo->output_width; col > 0; col--) {
  103. *outptr++ = (char) GETJSAMPLE(*inptr++);
  104. }
  105. (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
  106. }
  107. /*
  108. * Write some demapped pixel data when color quantization is in effect.
  109. * For Targa, this is only applied to grayscale data.
  110. */
  111. METHODDEF(void)
  112. put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
  113. JDIMENSION rows_supplied)
  114. {
  115. tga_dest_ptr dest = (tga_dest_ptr) dinfo;
  116. register JSAMPROW inptr;
  117. register char *outptr;
  118. register JSAMPROW color_map0 = cinfo->colormap[0];
  119. register JDIMENSION col;
  120. inptr = dest->pub.buffer[0];
  121. outptr = dest->iobuffer;
  122. for (col = cinfo->output_width; col > 0; col--) {
  123. *outptr++ = (char) GETJSAMPLE(color_map0[GETJSAMPLE(*inptr++)]);
  124. }
  125. (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
  126. }
  127. /*
  128. * Startup: write the file header.
  129. */
  130. METHODDEF(void)
  131. start_output_tga (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
  132. {
  133. tga_dest_ptr dest = (tga_dest_ptr) dinfo;
  134. int num_colors, i;
  135. FILE *outfile;
  136. if (cinfo->out_color_space == JCS_GRAYSCALE) {
  137. /* Targa doesn't have a mapped grayscale format, so we will */
  138. /* demap quantized gray output. Never emit a colormap. */
  139. write_header(cinfo, dinfo, 0);
  140. if (cinfo->quantize_colors)
  141. dest->pub.put_pixel_rows = put_demapped_gray;
  142. else
  143. dest->pub.put_pixel_rows = put_gray_rows;
  144. } else if (cinfo->out_color_space == JCS_RGB) {
  145. if (cinfo->quantize_colors) {
  146. /* We only support 8-bit colormap indexes, so only 256 colors */
  147. num_colors = cinfo->actual_number_of_colors;
  148. if (num_colors > 256)
  149. ERREXIT1(cinfo, JERR_TOO_MANY_COLORS, num_colors);
  150. write_header(cinfo, dinfo, num_colors);
  151. /* Write the colormap. Note Targa uses BGR byte order */
  152. outfile = dest->pub.output_file;
  153. for (i = 0; i < num_colors; i++) {
  154. putc(GETJSAMPLE(cinfo->colormap[2][i]), outfile);
  155. putc(GETJSAMPLE(cinfo->colormap[1][i]), outfile);
  156. putc(GETJSAMPLE(cinfo->colormap[0][i]), outfile);
  157. }
  158. dest->pub.put_pixel_rows = put_gray_rows;
  159. } else {
  160. write_header(cinfo, dinfo, 0);
  161. dest->pub.put_pixel_rows = put_pixel_rows;
  162. }
  163. } else {
  164. ERREXIT(cinfo, JERR_TGA_COLORSPACE);
  165. }
  166. }
  167. /*
  168. * Finish up at the end of the file.
  169. */
  170. METHODDEF(void)
  171. finish_output_tga (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
  172. {
  173. /* Make sure we wrote the output file OK */
  174. fflush(dinfo->output_file);
  175. if (ferror(dinfo->output_file))
  176. ERREXIT(cinfo, JERR_FILE_WRITE);
  177. }
  178. /*
  179. * The module selection routine for Targa format output.
  180. */
  181. GLOBAL(djpeg_dest_ptr)
  182. jinit_write_targa (j_decompress_ptr cinfo)
  183. {
  184. tga_dest_ptr dest;
  185. /* Create module interface object, fill in method pointers */
  186. dest = (tga_dest_ptr)
  187. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  188. sizeof(tga_dest_struct));
  189. dest->pub.start_output = start_output_tga;
  190. dest->pub.finish_output = finish_output_tga;
  191. /* Calculate output image dimensions so we can allocate space */
  192. jpeg_calc_output_dimensions(cinfo);
  193. /* Create I/O buffer. */
  194. dest->buffer_width = cinfo->output_width * cinfo->output_components;
  195. dest->iobuffer = (char *)
  196. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  197. (size_t) (dest->buffer_width * sizeof(char)));
  198. /* Create decompressor output buffer. */
  199. dest->pub.buffer = (*cinfo->mem->alloc_sarray)
  200. ((j_common_ptr) cinfo, JPOOL_IMAGE, dest->buffer_width, (JDIMENSION) 1);
  201. dest->pub.buffer_height = 1;
  202. return (djpeg_dest_ptr) dest;
  203. }
  204. #endif /* TARGA_SUPPORTED */