blob: 5fbfc538986e369308b3d21bedc5a349df97733e [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * wrtarga.c
3 *
DRC5033f3e2014-05-18 18:33:44 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane489583f1996-02-07 00:00:00 +00005 * Copyright (C) 1991-1996, Thomas G. Lane.
DRC5de454b2014-05-18 19:04:03 +00006 * It was modified by The libjpeg-turbo Project to include only code and
7 * information relevant to libjpeg-turbo.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00008 * For conditions of distribution and use, see the accompanying README file.
9 *
10 * This file contains routines to write output images in Targa format.
11 *
12 * These routines may need modification for non-Unix environments or
13 * specialized applications. As they stand, they assume output to
14 * an ordinary stdio stream.
15 *
16 * Based on code contributed by Lee Daniel Crocker.
17 */
18
DRCb7753512014-05-11 09:36:25 +000019#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000020
21#ifdef TARGA_SUPPORTED
22
23
24/*
25 * To support 12-bit JPEG data, we'd have to scale output down to 8 bits.
26 * This is not yet implemented.
27 */
28
29#if BITS_IN_JSAMPLE != 8
30 Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
31#endif
32
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000033
34/* Private version of data destination object */
35
36typedef struct {
DRCb7753512014-05-11 09:36:25 +000037 struct djpeg_dest_struct pub; /* public fields */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000038
DRCb7753512014-05-11 09:36:25 +000039 char *iobuffer; /* physical I/O buffer */
40 JDIMENSION buffer_width; /* width of one row */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000041} tga_dest_struct;
42
43typedef tga_dest_struct * tga_dest_ptr;
44
45
Thomas G. Lane489583f1996-02-07 00:00:00 +000046LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000047write_header (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, int num_colors)
48/* Create and write a Targa header */
49{
50 char targaheader[18];
51
52 /* Set unused fields of header to 0 */
DRC5de454b2014-05-18 19:04:03 +000053 MEMZERO(targaheader, sizeof(targaheader));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000054
55 if (num_colors > 0) {
DRCb7753512014-05-11 09:36:25 +000056 targaheader[1] = 1; /* color map type 1 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000057 targaheader[5] = (char) (num_colors & 0xFF);
58 targaheader[6] = (char) (num_colors >> 8);
DRCb7753512014-05-11 09:36:25 +000059 targaheader[7] = 24; /* 24 bits per cmap entry */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000060 }
61
62 targaheader[12] = (char) (cinfo->output_width & 0xFF);
63 targaheader[13] = (char) (cinfo->output_width >> 8);
64 targaheader[14] = (char) (cinfo->output_height & 0xFF);
65 targaheader[15] = (char) (cinfo->output_height >> 8);
DRCb7753512014-05-11 09:36:25 +000066 targaheader[17] = 0x20; /* Top-down, non-interlaced */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000067
68 if (cinfo->out_color_space == JCS_GRAYSCALE) {
DRC90d6c382014-05-12 09:08:39 +000069 targaheader[2] = 3; /* image type = uncompressed grayscale */
DRCb7753512014-05-11 09:36:25 +000070 targaheader[16] = 8; /* bits per pixel */
71 } else { /* must be RGB */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000072 if (num_colors > 0) {
DRCb7753512014-05-11 09:36:25 +000073 targaheader[2] = 1; /* image type = colormapped RGB */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000074 targaheader[16] = 8;
75 } else {
DRCb7753512014-05-11 09:36:25 +000076 targaheader[2] = 2; /* image type = uncompressed RGB */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000077 targaheader[16] = 24;
78 }
79 }
80
81 if (JFWRITE(dinfo->output_file, targaheader, 18) != (size_t) 18)
82 ERREXIT(cinfo, JERR_FILE_WRITE);
83}
84
85
86/*
87 * Write some pixel data.
88 * In this module rows_supplied will always be 1.
89 */
90
Thomas G. Lane489583f1996-02-07 00:00:00 +000091METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000092put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
DRCb7753512014-05-11 09:36:25 +000093 JDIMENSION rows_supplied)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000094/* used for unquantized full-color output */
95{
96 tga_dest_ptr dest = (tga_dest_ptr) dinfo;
97 register JSAMPROW inptr;
98 register char * outptr;
99 register JDIMENSION col;
100
101 inptr = dest->pub.buffer[0];
102 outptr = dest->iobuffer;
103 for (col = cinfo->output_width; col > 0; col--) {
104 outptr[0] = (char) GETJSAMPLE(inptr[2]); /* RGB to BGR order */
105 outptr[1] = (char) GETJSAMPLE(inptr[1]);
106 outptr[2] = (char) GETJSAMPLE(inptr[0]);
107 inptr += 3, outptr += 3;
108 }
109 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
110}
111
Thomas G. Lane489583f1996-02-07 00:00:00 +0000112METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000113put_gray_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
DRCb7753512014-05-11 09:36:25 +0000114 JDIMENSION rows_supplied)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000115/* used for grayscale OR quantized color output */
116{
117 tga_dest_ptr dest = (tga_dest_ptr) dinfo;
118 register JSAMPROW inptr;
119 register char * outptr;
120 register JDIMENSION col;
121
122 inptr = dest->pub.buffer[0];
123 outptr = dest->iobuffer;
124 for (col = cinfo->output_width; col > 0; col--) {
125 *outptr++ = (char) GETJSAMPLE(*inptr++);
126 }
127 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
128}
129
130
131/*
132 * Write some demapped pixel data when color quantization is in effect.
133 * For Targa, this is only applied to grayscale data.
134 */
135
Thomas G. Lane489583f1996-02-07 00:00:00 +0000136METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000137put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
DRCb7753512014-05-11 09:36:25 +0000138 JDIMENSION rows_supplied)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000139{
140 tga_dest_ptr dest = (tga_dest_ptr) dinfo;
141 register JSAMPROW inptr;
142 register char * outptr;
143 register JSAMPROW color_map0 = cinfo->colormap[0];
144 register JDIMENSION col;
145
146 inptr = dest->pub.buffer[0];
147 outptr = dest->iobuffer;
148 for (col = cinfo->output_width; col > 0; col--) {
149 *outptr++ = (char) GETJSAMPLE(color_map0[GETJSAMPLE(*inptr++)]);
150 }
151 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
152}
153
154
155/*
156 * Startup: write the file header.
157 */
158
Thomas G. Lane489583f1996-02-07 00:00:00 +0000159METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000160start_output_tga (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
161{
162 tga_dest_ptr dest = (tga_dest_ptr) dinfo;
163 int num_colors, i;
164 FILE *outfile;
165
166 if (cinfo->out_color_space == JCS_GRAYSCALE) {
167 /* Targa doesn't have a mapped grayscale format, so we will */
168 /* demap quantized gray output. Never emit a colormap. */
169 write_header(cinfo, dinfo, 0);
170 if (cinfo->quantize_colors)
171 dest->pub.put_pixel_rows = put_demapped_gray;
172 else
173 dest->pub.put_pixel_rows = put_gray_rows;
174 } else if (cinfo->out_color_space == JCS_RGB) {
175 if (cinfo->quantize_colors) {
176 /* We only support 8-bit colormap indexes, so only 256 colors */
177 num_colors = cinfo->actual_number_of_colors;
178 if (num_colors > 256)
DRCb7753512014-05-11 09:36:25 +0000179 ERREXIT1(cinfo, JERR_TOO_MANY_COLORS, num_colors);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000180 write_header(cinfo, dinfo, num_colors);
181 /* Write the colormap. Note Targa uses BGR byte order */
182 outfile = dest->pub.output_file;
183 for (i = 0; i < num_colors; i++) {
DRCb7753512014-05-11 09:36:25 +0000184 putc(GETJSAMPLE(cinfo->colormap[2][i]), outfile);
185 putc(GETJSAMPLE(cinfo->colormap[1][i]), outfile);
186 putc(GETJSAMPLE(cinfo->colormap[0][i]), outfile);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000187 }
188 dest->pub.put_pixel_rows = put_gray_rows;
189 } else {
190 write_header(cinfo, dinfo, 0);
191 dest->pub.put_pixel_rows = put_pixel_rows;
192 }
193 } else {
194 ERREXIT(cinfo, JERR_TGA_COLORSPACE);
195 }
196}
197
198
199/*
200 * Finish up at the end of the file.
201 */
202
Thomas G. Lane489583f1996-02-07 00:00:00 +0000203METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000204finish_output_tga (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
205{
206 /* Make sure we wrote the output file OK */
207 fflush(dinfo->output_file);
208 if (ferror(dinfo->output_file))
209 ERREXIT(cinfo, JERR_FILE_WRITE);
210}
211
212
213/*
214 * The module selection routine for Targa format output.
215 */
216
Thomas G. Lane489583f1996-02-07 00:00:00 +0000217GLOBAL(djpeg_dest_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000218jinit_write_targa (j_decompress_ptr cinfo)
219{
220 tga_dest_ptr dest;
221
222 /* Create module interface object, fill in method pointers */
223 dest = (tga_dest_ptr)
224 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000225 sizeof(tga_dest_struct));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000226 dest->pub.start_output = start_output_tga;
227 dest->pub.finish_output = finish_output_tga;
228
229 /* Calculate output image dimensions so we can allocate space */
230 jpeg_calc_output_dimensions(cinfo);
231
DRC5033f3e2014-05-18 18:33:44 +0000232 /* Create I/O buffer. */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000233 dest->buffer_width = cinfo->output_width * cinfo->output_components;
234 dest->iobuffer = (char *)
235 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000236 (size_t) (dest->buffer_width * sizeof(char)));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000237
238 /* Create decompressor output buffer. */
239 dest->pub.buffer = (*cinfo->mem->alloc_sarray)
240 ((j_common_ptr) cinfo, JPOOL_IMAGE, dest->buffer_width, (JDIMENSION) 1);
241 dest->pub.buffer_height = 1;
242
243 return (djpeg_dest_ptr) dest;
244}
245
246#endif /* TARGA_SUPPORTED */