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