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