blob: 9dfa9201936d6171429184db75b9f9c8a0b752b5 [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.
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -05006 * libjpeg-turbo Modifications:
7 * Copyright (C) 2017, D. R. Commander.
Alex Naidis6eb7d372016-10-16 23:10:08 +02008 * For conditions of distribution and use, see the accompanying README.ijg
9 * file.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000010 *
11 * This file contains routines to write output images in Targa format.
12 *
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.
16 *
17 * Based on code contributed by Lee Daniel Crocker.
18 */
19
DRCb7753512014-05-11 09:36:25 +000020#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000021
22#ifdef TARGA_SUPPORTED
23
24
25/*
26 * To support 12-bit JPEG data, we'd have to scale output down to 8 bits.
27 * This is not yet implemented.
28 */
29
30#if BITS_IN_JSAMPLE != 8
31 Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
32#endif
33
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000034
35/* Private version of data destination object */
36
37typedef struct {
DRCb7753512014-05-11 09:36:25 +000038 struct djpeg_dest_struct pub; /* public fields */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000039
DRCb7753512014-05-11 09:36:25 +000040 char *iobuffer; /* physical I/O buffer */
41 JDIMENSION buffer_width; /* width of one row */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000042} tga_dest_struct;
43
Alex Naidis6eb7d372016-10-16 23:10:08 +020044typedef tga_dest_struct *tga_dest_ptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000045
46
Thomas G. Lane489583f1996-02-07 00:00:00 +000047LOCAL(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -040048write_header(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, int num_colors)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000049/* Create and write a Targa header */
50{
51 char targaheader[18];
52
53 /* Set unused fields of header to 0 */
DRC5de454b2014-05-18 19:04:03 +000054 MEMZERO(targaheader, sizeof(targaheader));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000055
56 if (num_colors > 0) {
DRCb7753512014-05-11 09:36:25 +000057 targaheader[1] = 1; /* color map type 1 */
Leon Scroggins III3993b372018-07-16 10:43:45 -040058 targaheader[5] = (char)(num_colors & 0xFF);
59 targaheader[6] = (char)(num_colors >> 8);
DRCb7753512014-05-11 09:36:25 +000060 targaheader[7] = 24; /* 24 bits per cmap entry */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000061 }
62
Leon Scroggins III3993b372018-07-16 10:43:45 -040063 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);
DRCb7753512014-05-11 09:36:25 +000067 targaheader[17] = 0x20; /* Top-down, non-interlaced */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000068
69 if (cinfo->out_color_space == JCS_GRAYSCALE) {
DRC90d6c382014-05-12 09:08:39 +000070 targaheader[2] = 3; /* image type = uncompressed grayscale */
DRCb7753512014-05-11 09:36:25 +000071 targaheader[16] = 8; /* bits per pixel */
72 } else { /* must be RGB */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000073 if (num_colors > 0) {
DRCb7753512014-05-11 09:36:25 +000074 targaheader[2] = 1; /* image type = colormapped RGB */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000075 targaheader[16] = 8;
76 } else {
DRCb7753512014-05-11 09:36:25 +000077 targaheader[2] = 2; /* image type = uncompressed RGB */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000078 targaheader[16] = 24;
79 }
80 }
81
Leon Scroggins III3993b372018-07-16 10:43:45 -040082 if (JFWRITE(dinfo->output_file, targaheader, 18) != (size_t)18)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000083 ERREXIT(cinfo, JERR_FILE_WRITE);
84}
85
86
87/*
88 * Write some pixel data.
89 * In this module rows_supplied will always be 1.
90 */
91
Thomas G. Lane489583f1996-02-07 00:00:00 +000092METHODDEF(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -040093put_pixel_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
94 JDIMENSION rows_supplied)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000095/* used for unquantized full-color output */
96{
Leon Scroggins III3993b372018-07-16 10:43:45 -040097 tga_dest_ptr dest = (tga_dest_ptr)dinfo;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000098 register JSAMPROW inptr;
Alex Naidis6eb7d372016-10-16 23:10:08 +020099 register char *outptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000100 register JDIMENSION col;
101
102 inptr = dest->pub.buffer[0];
103 outptr = dest->iobuffer;
104 for (col = cinfo->output_width; col > 0; col--) {
Leon Scroggins III3993b372018-07-16 10:43:45 -0400105 outptr[0] = (char)GETJSAMPLE(inptr[2]); /* RGB to BGR order */
106 outptr[1] = (char)GETJSAMPLE(inptr[1]);
107 outptr[2] = (char)GETJSAMPLE(inptr[0]);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000108 inptr += 3, outptr += 3;
109 }
Leon Scroggins III3993b372018-07-16 10:43:45 -0400110 (void)JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000111}
112
Thomas G. Lane489583f1996-02-07 00:00:00 +0000113METHODDEF(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400114put_gray_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
115 JDIMENSION rows_supplied)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000116/* used for grayscale OR quantized color output */
117{
Leon Scroggins III3993b372018-07-16 10:43:45 -0400118 tga_dest_ptr dest = (tga_dest_ptr)dinfo;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000119 register JSAMPROW inptr;
Alex Naidis6eb7d372016-10-16 23:10:08 +0200120 register char *outptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000121 register JDIMENSION col;
122
123 inptr = dest->pub.buffer[0];
124 outptr = dest->iobuffer;
125 for (col = cinfo->output_width; col > 0; col--) {
Leon Scroggins III3993b372018-07-16 10:43:45 -0400126 *outptr++ = (char)GETJSAMPLE(*inptr++);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000127 }
Leon Scroggins III3993b372018-07-16 10:43:45 -0400128 (void)JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000129}
130
131
132/*
133 * Write some demapped pixel data when color quantization is in effect.
134 * For Targa, this is only applied to grayscale data.
135 */
136
Thomas G. Lane489583f1996-02-07 00:00:00 +0000137METHODDEF(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400138put_demapped_gray(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
139 JDIMENSION rows_supplied)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000140{
Leon Scroggins III3993b372018-07-16 10:43:45 -0400141 tga_dest_ptr dest = (tga_dest_ptr)dinfo;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000142 register JSAMPROW inptr;
Alex Naidis6eb7d372016-10-16 23:10:08 +0200143 register char *outptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000144 register JSAMPROW color_map0 = cinfo->colormap[0];
145 register JDIMENSION col;
146
147 inptr = dest->pub.buffer[0];
148 outptr = dest->iobuffer;
149 for (col = cinfo->output_width; col > 0; col--) {
Leon Scroggins III3993b372018-07-16 10:43:45 -0400150 *outptr++ = (char)GETJSAMPLE(color_map0[GETJSAMPLE(*inptr++)]);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000151 }
Leon Scroggins III3993b372018-07-16 10:43:45 -0400152 (void)JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000153}
154
155
156/*
157 * Startup: write the file header.
158 */
159
Thomas G. Lane489583f1996-02-07 00:00:00 +0000160METHODDEF(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400161start_output_tga(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000162{
Leon Scroggins III3993b372018-07-16 10:43:45 -0400163 tga_dest_ptr dest = (tga_dest_ptr)dinfo;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000164 int num_colors, i;
165 FILE *outfile;
166
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;
173 else
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)
DRCb7753512014-05-11 09:36:25 +0000180 ERREXIT1(cinfo, JERR_TOO_MANY_COLORS, num_colors);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000181 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++) {
DRCb7753512014-05-11 09:36:25 +0000185 putc(GETJSAMPLE(cinfo->colormap[2][i]), outfile);
186 putc(GETJSAMPLE(cinfo->colormap[1][i]), outfile);
187 putc(GETJSAMPLE(cinfo->colormap[0][i]), outfile);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000188 }
189 dest->pub.put_pixel_rows = put_gray_rows;
190 } else {
191 write_header(cinfo, dinfo, 0);
192 dest->pub.put_pixel_rows = put_pixel_rows;
193 }
194 } else {
195 ERREXIT(cinfo, JERR_TGA_COLORSPACE);
196 }
197}
198
199
200/*
201 * Finish up at the end of the file.
202 */
203
Thomas G. Lane489583f1996-02-07 00:00:00 +0000204METHODDEF(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400205finish_output_tga(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000206{
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);
211}
212
213
214/*
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -0500215 * Re-calculate buffer dimensions based on output dimensions.
216 */
217
218METHODDEF(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400219calc_buffer_dimensions_tga(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -0500220{
Leon Scroggins III3993b372018-07-16 10:43:45 -0400221 tga_dest_ptr dest = (tga_dest_ptr)dinfo;
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -0500222
223 dest->buffer_width = cinfo->output_width * cinfo->output_components;
224}
225
226
227/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000228 * The module selection routine for Targa format output.
229 */
230
Thomas G. Lane489583f1996-02-07 00:00:00 +0000231GLOBAL(djpeg_dest_ptr)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400232jinit_write_targa(j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000233{
234 tga_dest_ptr dest;
235
236 /* Create module interface object, fill in method pointers */
237 dest = (tga_dest_ptr)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400238 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
239 sizeof(tga_dest_struct));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000240 dest->pub.start_output = start_output_tga;
241 dest->pub.finish_output = finish_output_tga;
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -0500242 dest->pub.calc_buffer_dimensions = calc_buffer_dimensions_tga;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000243
244 /* Calculate output image dimensions so we can allocate space */
245 jpeg_calc_output_dimensions(cinfo);
246
DRC5033f3e2014-05-18 18:33:44 +0000247 /* Create I/O buffer. */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400248 dest->pub.calc_buffer_dimensions(cinfo, (djpeg_dest_ptr)dest);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000249 dest->iobuffer = (char *)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400250 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
251 (size_t)(dest->buffer_width * sizeof(char)));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000252
253 /* Create decompressor output buffer. */
254 dest->pub.buffer = (*cinfo->mem->alloc_sarray)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400255 ((j_common_ptr)cinfo, JPOOL_IMAGE, dest->buffer_width, (JDIMENSION)1);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000256 dest->pub.buffer_height = 1;
257
Leon Scroggins III3993b372018-07-16 10:43:45 -0400258 return (djpeg_dest_ptr)dest;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000259}
260
261#endif /* TARGA_SUPPORTED */