wrtarga.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. * libjpeg-turbo Modifications:
  7. * Copyright (C) 2017, 2019, 2022, D. R. Commander.
  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. memset(targaheader, 0, 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 (fwrite(targaheader, 1, 18, dinfo->output_file) != (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] = inptr[2]; /* RGB to BGR order */
  85. outptr[1] = inptr[1];
  86. outptr[2] = inptr[0];
  87. inptr += 3, outptr += 3;
  88. }
  89. fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
  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. inptr = dest->pub.buffer[0];
  100. outptr = dest->iobuffer;
  101. memcpy(outptr, inptr, cinfo->output_width);
  102. fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
  103. }
  104. /*
  105. * Write some demapped pixel data when color quantization is in effect.
  106. * For Targa, this is only applied to grayscale data.
  107. */
  108. METHODDEF(void)
  109. put_demapped_gray(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
  110. JDIMENSION rows_supplied)
  111. {
  112. tga_dest_ptr dest = (tga_dest_ptr)dinfo;
  113. register JSAMPROW inptr;
  114. register char *outptr;
  115. register JSAMPROW color_map0 = cinfo->colormap[0];
  116. register JDIMENSION col;
  117. inptr = dest->pub.buffer[0];
  118. outptr = dest->iobuffer;
  119. for (col = cinfo->output_width; col > 0; col--) {
  120. *outptr++ = color_map0[*inptr++];
  121. }
  122. fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
  123. }
  124. /*
  125. * Startup: write the file header.
  126. */
  127. METHODDEF(void)
  128. start_output_tga(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
  129. {
  130. tga_dest_ptr dest = (tga_dest_ptr)dinfo;
  131. int num_colors, i;
  132. FILE *outfile;
  133. if (cinfo->out_color_space == JCS_GRAYSCALE) {
  134. /* Targa doesn't have a mapped grayscale format, so we will */
  135. /* demap quantized gray output. Never emit a colormap. */
  136. write_header(cinfo, dinfo, 0);
  137. if (cinfo->quantize_colors)
  138. dest->pub.put_pixel_rows = put_demapped_gray;
  139. else
  140. dest->pub.put_pixel_rows = put_gray_rows;
  141. } else if (cinfo->out_color_space == JCS_RGB) {
  142. if (cinfo->quantize_colors) {
  143. /* We only support 8-bit colormap indexes, so only 256 colors */
  144. num_colors = cinfo->actual_number_of_colors;
  145. if (num_colors > 256)
  146. ERREXIT1(cinfo, JERR_TOO_MANY_COLORS, num_colors);
  147. write_header(cinfo, dinfo, num_colors);
  148. /* Write the colormap. Note Targa uses BGR byte order */
  149. outfile = dest->pub.output_file;
  150. for (i = 0; i < num_colors; i++) {
  151. putc(cinfo->colormap[2][i], outfile);
  152. putc(cinfo->colormap[1][i], outfile);
  153. putc(cinfo->colormap[0][i], outfile);
  154. }
  155. dest->pub.put_pixel_rows = put_gray_rows;
  156. } else {
  157. write_header(cinfo, dinfo, 0);
  158. dest->pub.put_pixel_rows = put_pixel_rows;
  159. }
  160. } else {
  161. ERREXIT(cinfo, JERR_TGA_COLORSPACE);
  162. }
  163. }
  164. /*
  165. * Finish up at the end of the file.
  166. */
  167. METHODDEF(void)
  168. finish_output_tga(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
  169. {
  170. /* Make sure we wrote the output file OK */
  171. fflush(dinfo->output_file);
  172. if (ferror(dinfo->output_file))
  173. ERREXIT(cinfo, JERR_FILE_WRITE);
  174. }
  175. /*
  176. * Re-calculate buffer dimensions based on output dimensions.
  177. */
  178. METHODDEF(void)
  179. calc_buffer_dimensions_tga(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
  180. {
  181. tga_dest_ptr dest = (tga_dest_ptr)dinfo;
  182. dest->buffer_width = cinfo->output_width * cinfo->output_components;
  183. }
  184. /*
  185. * The module selection routine for Targa format output.
  186. */
  187. GLOBAL(djpeg_dest_ptr)
  188. jinit_write_targa(j_decompress_ptr cinfo)
  189. {
  190. tga_dest_ptr dest;
  191. /* Create module interface object, fill in method pointers */
  192. dest = (tga_dest_ptr)
  193. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  194. sizeof(tga_dest_struct));
  195. dest->pub.start_output = start_output_tga;
  196. dest->pub.finish_output = finish_output_tga;
  197. dest->pub.calc_buffer_dimensions = calc_buffer_dimensions_tga;
  198. /* Calculate output image dimensions so we can allocate space */
  199. jpeg_calc_output_dimensions(cinfo);
  200. /* Create I/O buffer. */
  201. dest->pub.calc_buffer_dimensions(cinfo, (djpeg_dest_ptr)dest);
  202. dest->iobuffer = (char *)
  203. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  204. (size_t)(dest->buffer_width * sizeof(char)));
  205. /* Create decompressor output buffer. */
  206. dest->pub.buffer = (*cinfo->mem->alloc_sarray)
  207. ((j_common_ptr)cinfo, JPOOL_IMAGE, dest->buffer_width, (JDIMENSION)1);
  208. dest->pub.buffer_height = 1;
  209. return (djpeg_dest_ptr)dest;
  210. }
  211. #endif /* TARGA_SUPPORTED */