xref: /reactos/dll/3rdparty/libjpeg/wrtarga.c (revision 8a978a17)
1 /*
2  * wrtarga.c
3  *
4  * Copyright (C) 1991-1996, Thomas G. Lane.
5  * Modified 2015-2019 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)
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)
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)
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)
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)
168 start_output_tga (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
169 {
170   int num_colors, i;
171   FILE *outfile;
172 
173   switch (cinfo->out_color_space) {
174   case 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       dinfo->put_pixel_rows = put_demapped_gray;
180     else
181       dinfo->put_pixel_rows = put_gray_rows;
182     break;
183   case JCS_RGB:
184     if (cinfo->quantize_colors) {
185       /* We only support 8-bit colormap indexes, so only 256 colors */
186       num_colors = cinfo->actual_number_of_colors;
187       if (num_colors > 256)
188 	ERREXIT1(cinfo, JERR_TOO_MANY_COLORS, num_colors);
189       write_header(cinfo, dinfo, num_colors);
190       /* Write the colormap.  Note Targa uses BGR byte order */
191       outfile = dinfo->output_file;
192       for (i = 0; i < num_colors; i++) {
193 	putc(GETJSAMPLE(cinfo->colormap[2][i]), outfile);
194 	putc(GETJSAMPLE(cinfo->colormap[1][i]), outfile);
195 	putc(GETJSAMPLE(cinfo->colormap[0][i]), outfile);
196       }
197       dinfo->put_pixel_rows = put_gray_rows;
198     } else {
199       write_header(cinfo, dinfo, 0);
200       dinfo->put_pixel_rows = put_pixel_rows;
201     }
202     break;
203   default:
204     ERREXIT(cinfo, JERR_TGA_COLORSPACE);
205   }
206 }
207 
208 
209 /*
210  * Finish up at the end of the file.
211  */
212 
213 METHODDEF(void)
214 finish_output_tga (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
215 {
216   /* Make sure we wrote the output file OK */
217   JFFLUSH(dinfo->output_file);
218   if (JFERROR(dinfo->output_file))
219     ERREXIT(cinfo, JERR_FILE_WRITE);
220 }
221 
222 
223 /*
224  * The module selection routine for Targa format output.
225  */
226 
227 GLOBAL(djpeg_dest_ptr)
228 jinit_write_targa (j_decompress_ptr cinfo)
229 {
230   tga_dest_ptr dest;
231 
232   /* Create module interface object, fill in method pointers */
233   dest = (tga_dest_ptr) (*cinfo->mem->alloc_small)
234     ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(tga_dest_struct));
235   dest->pub.start_output = start_output_tga;
236   dest->pub.finish_output = finish_output_tga;
237 
238   /* Calculate output image dimensions so we can allocate space */
239   jpeg_calc_output_dimensions(cinfo);
240 
241   /* Create I/O buffer.  Note we make this near on a PC. */
242   dest->buffer_width = cinfo->output_width * cinfo->output_components;
243   dest->iobuffer = (char *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo,
244     JPOOL_IMAGE, (size_t) dest->buffer_width * SIZEOF(char));
245 
246   /* Create decompressor output buffer. */
247   dest->pub.buffer = (*cinfo->mem->alloc_sarray) ((j_common_ptr) cinfo,
248     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