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, D. R. Commander.
8 * For conditions of distribution and use, see the accompanying README.ijg
11 * This file contains routines to write output images in Targa format.
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.
17 * Based on code contributed by Lee Daniel Crocker.
20 #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
22 #ifdef TARGA_SUPPORTED
26 * To support 12-bit JPEG data, we'd have to scale output down to 8 bits.
27 * This is not yet implemented.
30 #if BITS_IN_JSAMPLE != 8
31 Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
35 /* Private version of data destination object */
38 struct djpeg_dest_struct pub; /* public fields */
40 char *iobuffer; /* physical I/O buffer */
41 JDIMENSION buffer_width; /* width of one row */
44 typedef tga_dest_struct *tga_dest_ptr;
48 write_header (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, int num_colors)
49 /* Create and write a Targa header */
53 /* Set unused fields of header to 0 */
54 MEMZERO(targaheader, sizeof(targaheader));
57 targaheader[1] = 1; /* color map type 1 */
58 targaheader[5] = (char) (num_colors & 0xFF);
59 targaheader[6] = (char) (num_colors >> 8);
60 targaheader[7] = 24; /* 24 bits per cmap entry */
63 targaheader[12] = (char) (cinfo->output_width & 0xFF);
64 targaheader[13] = (char) (cinfo->output_width >> 8);
65 targaheader[14] = (char) (cinfo->output_height & 0xFF);
66 targaheader[15] = (char) (cinfo->output_height >> 8);
67 targaheader[17] = 0x20; /* Top-down, non-interlaced */
69 if (cinfo->out_color_space == JCS_GRAYSCALE) {
70 targaheader[2] = 3; /* image type = uncompressed grayscale */
71 targaheader[16] = 8; /* bits per pixel */
72 } else { /* must be RGB */
74 targaheader[2] = 1; /* image type = colormapped RGB */
77 targaheader[2] = 2; /* image type = uncompressed RGB */
82 if (JFWRITE(dinfo->output_file, targaheader, 18) != (size_t) 18)
83 ERREXIT(cinfo, JERR_FILE_WRITE);
88 * Write some pixel data.
89 * In this module rows_supplied will always be 1.
93 put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
94 JDIMENSION rows_supplied)
95 /* used for unquantized full-color output */
97 tga_dest_ptr dest = (tga_dest_ptr) dinfo;
98 register JSAMPROW inptr;
99 register char *outptr;
100 register JDIMENSION col;
102 inptr = dest->pub.buffer[0];
103 outptr = dest->iobuffer;
104 for (col = cinfo->output_width; col > 0; col--) {
105 outptr[0] = (char) GETJSAMPLE(inptr[2]); /* RGB to BGR order */
106 outptr[1] = (char) GETJSAMPLE(inptr[1]);
107 outptr[2] = (char) GETJSAMPLE(inptr[0]);
108 inptr += 3, outptr += 3;
110 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
114 put_gray_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
115 JDIMENSION rows_supplied)
116 /* used for grayscale OR quantized color output */
118 tga_dest_ptr dest = (tga_dest_ptr) dinfo;
119 register JSAMPROW inptr;
120 register char *outptr;
121 register JDIMENSION col;
123 inptr = dest->pub.buffer[0];
124 outptr = dest->iobuffer;
125 for (col = cinfo->output_width; col > 0; col--) {
126 *outptr++ = (char) GETJSAMPLE(*inptr++);
128 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
133 * Write some demapped pixel data when color quantization is in effect.
134 * For Targa, this is only applied to grayscale data.
138 put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
139 JDIMENSION rows_supplied)
141 tga_dest_ptr dest = (tga_dest_ptr) dinfo;
142 register JSAMPROW inptr;
143 register char *outptr;
144 register JSAMPROW color_map0 = cinfo->colormap[0];
145 register JDIMENSION col;
147 inptr = dest->pub.buffer[0];
148 outptr = dest->iobuffer;
149 for (col = cinfo->output_width; col > 0; col--) {
150 *outptr++ = (char) GETJSAMPLE(color_map0[GETJSAMPLE(*inptr++)]);
152 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
157 * Startup: write the file header.
161 start_output_tga (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
163 tga_dest_ptr dest = (tga_dest_ptr) dinfo;
167 if (cinfo->out_color_space == JCS_GRAYSCALE) {
168 /* Targa doesn't have a mapped grayscale format, so we will */
169 /* demap quantized gray output. Never emit a colormap. */
170 write_header(cinfo, dinfo, 0);
171 if (cinfo->quantize_colors)
172 dest->pub.put_pixel_rows = put_demapped_gray;
174 dest->pub.put_pixel_rows = put_gray_rows;
175 } else if (cinfo->out_color_space == JCS_RGB) {
176 if (cinfo->quantize_colors) {
177 /* We only support 8-bit colormap indexes, so only 256 colors */
178 num_colors = cinfo->actual_number_of_colors;
179 if (num_colors > 256)
180 ERREXIT1(cinfo, JERR_TOO_MANY_COLORS, num_colors);
181 write_header(cinfo, dinfo, num_colors);
182 /* Write the colormap. Note Targa uses BGR byte order */
183 outfile = dest->pub.output_file;
184 for (i = 0; i < num_colors; i++) {
185 putc(GETJSAMPLE(cinfo->colormap[2][i]), outfile);
186 putc(GETJSAMPLE(cinfo->colormap[1][i]), outfile);
187 putc(GETJSAMPLE(cinfo->colormap[0][i]), outfile);
189 dest->pub.put_pixel_rows = put_gray_rows;
191 write_header(cinfo, dinfo, 0);
192 dest->pub.put_pixel_rows = put_pixel_rows;
195 ERREXIT(cinfo, JERR_TGA_COLORSPACE);
201 * Finish up at the end of the file.
205 finish_output_tga (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
207 /* Make sure we wrote the output file OK */
208 fflush(dinfo->output_file);
209 if (ferror(dinfo->output_file))
210 ERREXIT(cinfo, JERR_FILE_WRITE);
215 * Re-calculate buffer dimensions based on output dimensions.
219 calc_buffer_dimensions_tga (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
221 tga_dest_ptr dest = (tga_dest_ptr) dinfo;
223 dest->buffer_width = cinfo->output_width * cinfo->output_components;
228 * The module selection routine for Targa format output.
231 GLOBAL(djpeg_dest_ptr)
232 jinit_write_targa (j_decompress_ptr cinfo)
236 /* Create module interface object, fill in method pointers */
237 dest = (tga_dest_ptr)
238 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
239 sizeof(tga_dest_struct));
240 dest->pub.start_output = start_output_tga;
241 dest->pub.finish_output = finish_output_tga;
242 dest->pub.calc_buffer_dimensions = calc_buffer_dimensions_tga;
244 /* Calculate output image dimensions so we can allocate space */
245 jpeg_calc_output_dimensions(cinfo);
247 /* Create I/O buffer. */
248 dest->pub.calc_buffer_dimensions (cinfo, (djpeg_dest_ptr) dest);
249 dest->iobuffer = (char *)
250 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
251 (size_t) (dest->buffer_width * sizeof(char)));
253 /* Create decompressor output buffer. */
254 dest->pub.buffer = (*cinfo->mem->alloc_sarray)
255 ((j_common_ptr) cinfo, JPOOL_IMAGE, dest->buffer_width, (JDIMENSION) 1);
256 dest->pub.buffer_height = 1;
258 return (djpeg_dest_ptr) dest;
261 #endif /* TARGA_SUPPORTED */