wrtarga.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * wrtarga.c
  3. *
  4. * Copyright (C) 1991-1996, Thomas G. Lane.
  5. * Modified 2015 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 write output images in Targa format.
  10. *
  11. * These routines may need modification for non-Unix environments or
  12. * specialized applications. As they stand, they assume output to
  13. * an ordinary stdio stream.
  14. *
  15. * Based on code contributed by Lee Daniel Crocker.
  16. */
  17. #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
  18. #ifdef TARGA_SUPPORTED
  19. /*
  20. * To support 12-bit JPEG data, we'd have to scale output down to 8 bits.
  21. * This is not yet implemented.
  22. */
  23. #if BITS_IN_JSAMPLE != 8
  24. Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
  25. #endif
  26. /*
  27. * The output buffer needs to be writable by fwrite(). On PCs, we must
  28. * allocate the buffer in near data space, because we are assuming small-data
  29. * memory model, wherein fwrite() can't reach far memory. If you need to
  30. * process very wide images on a PC, you might have to compile in large-memory
  31. * model, or else replace fwrite() with a putc() loop --- which will be much
  32. * slower.
  33. */
  34. /* Private version of data destination object */
  35. typedef struct {
  36. struct djpeg_dest_struct pub; /* public fields */
  37. char *iobuffer; /* physical I/O buffer */
  38. JDIMENSION buffer_width; /* width of one row */
  39. } tga_dest_struct;
  40. typedef tga_dest_struct * tga_dest_ptr;
  41. LOCAL(void)
  42. write_header (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, int num_colors)
  43. /* Create and write a Targa header */
  44. {
  45. char targaheader[18];
  46. /* Set unused fields of header to 0 */
  47. MEMZERO(targaheader, SIZEOF(targaheader));
  48. if (num_colors > 0) {
  49. targaheader[1] = 1; /* color map type 1 */
  50. targaheader[5] = (char) (num_colors & 0xFF);
  51. targaheader[6] = (char) (num_colors >> 8);
  52. targaheader[7] = 24; /* 24 bits per cmap entry */
  53. }
  54. targaheader[12] = (char) (cinfo->output_width & 0xFF);
  55. targaheader[13] = (char) (cinfo->output_width >> 8);
  56. targaheader[14] = (char) (cinfo->output_height & 0xFF);
  57. targaheader[15] = (char) (cinfo->output_height >> 8);
  58. targaheader[17] = 0x20; /* Top-down, non-interlaced */
  59. if (cinfo->out_color_space == JCS_GRAYSCALE) {
  60. targaheader[2] = 3; /* image type = uncompressed grayscale */
  61. targaheader[16] = 8; /* bits per pixel */
  62. } else { /* must be RGB */
  63. if (num_colors > 0) {
  64. targaheader[2] = 1; /* image type = colormapped RGB */
  65. targaheader[16] = 8;
  66. } else {
  67. targaheader[2] = 2; /* image type = uncompressed RGB */
  68. targaheader[16] = 24;
  69. }
  70. }
  71. if (JFWRITE(dinfo->output_file, targaheader, 18) != (size_t) 18)
  72. ERREXIT(cinfo, JERR_FILE_WRITE);
  73. }
  74. /*
  75. * Write some pixel data.
  76. * In this module rows_supplied will always be 1.
  77. */
  78. METHODDEF(void)
  79. put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
  80. JDIMENSION rows_supplied)
  81. /* used for unquantized full-color output */
  82. {
  83. tga_dest_ptr dest = (tga_dest_ptr) dinfo;
  84. register JSAMPROW inptr;
  85. register char * outptr;
  86. register JDIMENSION col;
  87. inptr = dest->pub.buffer[0];
  88. outptr = dest->iobuffer;
  89. for (col = cinfo->output_width; col > 0; col--) {
  90. outptr[0] = (char) GETJSAMPLE(inptr[2]); /* RGB to BGR order */
  91. outptr[1] = (char) GETJSAMPLE(inptr[1]);
  92. outptr[2] = (char) GETJSAMPLE(inptr[0]);
  93. inptr += 3, outptr += 3;
  94. }
  95. (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
  96. }
  97. METHODDEF(void)
  98. put_gray_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
  99. JDIMENSION rows_supplied)
  100. /* used for grayscale OR quantized color output */
  101. {
  102. tga_dest_ptr dest = (tga_dest_ptr) dinfo;
  103. register JSAMPROW inptr;
  104. register char * outptr;
  105. register JDIMENSION col;
  106. inptr = dest->pub.buffer[0];
  107. outptr = dest->iobuffer;
  108. for (col = cinfo->output_width; col > 0; col--) {
  109. *outptr++ = (char) GETJSAMPLE(*inptr++);
  110. }
  111. (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
  112. }
  113. /*
  114. * Write some demapped pixel data when color quantization is in effect.
  115. * For Targa, this is only applied to grayscale data.
  116. */
  117. METHODDEF(void)
  118. put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
  119. JDIMENSION rows_supplied)
  120. {
  121. tga_dest_ptr dest = (tga_dest_ptr) dinfo;
  122. register JSAMPROW inptr;
  123. register char * outptr;
  124. register JSAMPROW color_map0 = cinfo->colormap[0];
  125. register JDIMENSION col;
  126. inptr = dest->pub.buffer[0];
  127. outptr = dest->iobuffer;
  128. for (col = cinfo->output_width; col > 0; col--) {
  129. *outptr++ = (char) GETJSAMPLE(color_map0[GETJSAMPLE(*inptr++)]);
  130. }
  131. (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
  132. }
  133. /*
  134. * Startup: write the file header.
  135. */
  136. METHODDEF(void)
  137. start_output_tga (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
  138. {
  139. tga_dest_ptr dest = (tga_dest_ptr) dinfo;
  140. int num_colors, i;
  141. FILE *outfile;
  142. if (cinfo->out_color_space == JCS_GRAYSCALE) {
  143. /* Targa doesn't have a mapped grayscale format, so we will */
  144. /* demap quantized gray output. Never emit a colormap. */
  145. write_header(cinfo, dinfo, 0);
  146. if (cinfo->quantize_colors)
  147. dest->pub.put_pixel_rows = put_demapped_gray;
  148. else
  149. dest->pub.put_pixel_rows = put_gray_rows;
  150. } else if (cinfo->out_color_space == JCS_RGB) {
  151. if (cinfo->quantize_colors) {
  152. /* We only support 8-bit colormap indexes, so only 256 colors */
  153. num_colors = cinfo->actual_number_of_colors;
  154. if (num_colors > 256)
  155. ERREXIT1(cinfo, JERR_TOO_MANY_COLORS, num_colors);
  156. write_header(cinfo, dinfo, num_colors);
  157. /* Write the colormap. Note Targa uses BGR byte order */
  158. outfile = dest->pub.output_file;
  159. for (i = 0; i < num_colors; i++) {
  160. putc(GETJSAMPLE(cinfo->colormap[2][i]), outfile);
  161. putc(GETJSAMPLE(cinfo->colormap[1][i]), outfile);
  162. putc(GETJSAMPLE(cinfo->colormap[0][i]), outfile);
  163. }
  164. dest->pub.put_pixel_rows = put_gray_rows;
  165. } else {
  166. write_header(cinfo, dinfo, 0);
  167. dest->pub.put_pixel_rows = put_pixel_rows;
  168. }
  169. } else {
  170. ERREXIT(cinfo, JERR_TGA_COLORSPACE);
  171. }
  172. }
  173. /*
  174. * Finish up at the end of the file.
  175. */
  176. METHODDEF(void)
  177. finish_output_tga (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
  178. {
  179. /* Make sure we wrote the output file OK */
  180. fflush(dinfo->output_file);
  181. if (ferror(dinfo->output_file))
  182. ERREXIT(cinfo, JERR_FILE_WRITE);
  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. /* Calculate output image dimensions so we can allocate space */
  198. jpeg_calc_output_dimensions(cinfo);
  199. /* Create I/O buffer. Note we make this near on a PC. */
  200. dest->buffer_width = cinfo->output_width * cinfo->output_components;
  201. dest->iobuffer = (char *)
  202. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  203. (size_t) (dest->buffer_width * SIZEOF(char)));
  204. /* Create decompressor output buffer. */
  205. dest->pub.buffer = (*cinfo->mem->alloc_sarray)
  206. ((j_common_ptr) cinfo, JPOOL_IMAGE, dest->buffer_width, (JDIMENSION) 1);
  207. dest->pub.buffer_height = 1;
  208. return &dest->pub;
  209. }
  210. #endif /* TARGA_SUPPORTED */