Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / compiler / libjpeg / test / wrtarga.c
blobe04d9b151d33248f207b53bde2f7cc87382441bc
1 /*
2 $Id$
3 */
5 /*
6 * wrtarga.c
8 * Copyright (C) 1991-1996, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
12 * This file contains routines to write output images in Targa format.
14 * These routines may need modification for non-Unix environments or
15 * specialized applications. As they stand, they assume output to
16 * an ordinary stdio stream.
18 * Based on code contributed by Lee Daniel Crocker.
21 #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
23 #ifdef TARGA_SUPPORTED
27 * To support 12-bit JPEG data, we'd have to scale output down to 8 bits.
28 * This is not yet implemented.
31 #if BITS_IN_JSAMPLE != 8
32 Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
33 #endif
36 * The output buffer needs to be writable by fwrite(). On PCs, we must
37 * allocate the buffer in near data space, because we are assuming small-data
38 * memory model, wherein fwrite() can't reach far memory. If you need to
39 * process very wide images on a PC, you might have to compile in large-memory
40 * model, or else replace fwrite() with a putc() loop --- which will be much
41 * slower.
45 /* Private version of data destination object */
47 typedef struct {
48 struct djpeg_dest_struct pub; /* public fields */
50 char *iobuffer; /* physical I/O buffer */
51 JDIMENSION buffer_width; /* width of one row */
52 } tga_dest_struct;
54 typedef tga_dest_struct * tga_dest_ptr;
57 LOCAL(void)
58 write_header (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, int num_colors)
59 /* Create and write a Targa header */
61 char targaheader[18];
63 /* Set unused fields of header to 0 */
64 MEMZERO(targaheader, SIZEOF(targaheader));
66 if (num_colors > 0) {
67 targaheader[1] = 1; /* color map type 1 */
68 targaheader[5] = (char) (num_colors & 0xFF);
69 targaheader[6] = (char) (num_colors >> 8);
70 targaheader[7] = 24; /* 24 bits per cmap entry */
73 targaheader[12] = (char) (cinfo->output_width & 0xFF);
74 targaheader[13] = (char) (cinfo->output_width >> 8);
75 targaheader[14] = (char) (cinfo->output_height & 0xFF);
76 targaheader[15] = (char) (cinfo->output_height >> 8);
77 targaheader[17] = 0x20; /* Top-down, non-interlaced */
79 if (cinfo->out_color_space == JCS_GRAYSCALE) {
80 targaheader[2] = 3; /* image type = uncompressed gray-scale */
81 targaheader[16] = 8; /* bits per pixel */
82 } else { /* must be RGB */
83 if (num_colors > 0) {
84 targaheader[2] = 1; /* image type = colormapped RGB */
85 targaheader[16] = 8;
86 } else {
87 targaheader[2] = 2; /* image type = uncompressed RGB */
88 targaheader[16] = 24;
92 if (JFWRITE(dinfo->output_file, targaheader, 18) != (size_t) 18)
93 ERREXIT(cinfo, JERR_FILE_WRITE);
98 * Write some pixel data.
99 * In this module rows_supplied will always be 1.
102 METHODDEF(void)
103 put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
104 JDIMENSION rows_supplied)
105 /* used for unquantized full-color output */
107 tga_dest_ptr dest = (tga_dest_ptr) dinfo;
108 register JSAMPROW inptr;
109 register char * outptr;
110 register JDIMENSION col;
112 inptr = dest->pub.buffer[0];
113 outptr = dest->iobuffer;
114 for (col = cinfo->output_width; col > 0; col--) {
115 outptr[0] = (char) GETJSAMPLE(inptr[2]); /* RGB to BGR order */
116 outptr[1] = (char) GETJSAMPLE(inptr[1]);
117 outptr[2] = (char) GETJSAMPLE(inptr[0]);
118 inptr += 3, outptr += 3;
120 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
123 METHODDEF(void)
124 put_gray_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
125 JDIMENSION rows_supplied)
126 /* used for grayscale OR quantized color output */
128 tga_dest_ptr dest = (tga_dest_ptr) dinfo;
129 register JSAMPROW inptr;
130 register char * outptr;
131 register JDIMENSION col;
133 inptr = dest->pub.buffer[0];
134 outptr = dest->iobuffer;
135 for (col = cinfo->output_width; col > 0; col--) {
136 *outptr++ = (char) GETJSAMPLE(*inptr++);
138 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
143 * Write some demapped pixel data when color quantization is in effect.
144 * For Targa, this is only applied to grayscale data.
147 METHODDEF(void)
148 put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
149 JDIMENSION rows_supplied)
151 tga_dest_ptr dest = (tga_dest_ptr) dinfo;
152 register JSAMPROW inptr;
153 register char * outptr;
154 register JSAMPROW color_map0 = cinfo->colormap[0];
155 register JDIMENSION col;
157 inptr = dest->pub.buffer[0];
158 outptr = dest->iobuffer;
159 for (col = cinfo->output_width; col > 0; col--) {
160 *outptr++ = (char) GETJSAMPLE(color_map0[GETJSAMPLE(*inptr++)]);
162 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
167 * Startup: write the file header.
170 METHODDEF(void)
171 start_output_tga (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
173 tga_dest_ptr dest = (tga_dest_ptr) dinfo;
174 int num_colors, i;
175 FILE *outfile;
177 if (cinfo->out_color_space == JCS_GRAYSCALE) {
178 /* Targa doesn't have a mapped grayscale format, so we will */
179 /* demap quantized gray output. Never emit a colormap. */
180 write_header(cinfo, dinfo, 0);
181 if (cinfo->quantize_colors)
182 dest->pub.put_pixel_rows = put_demapped_gray;
183 else
184 dest->pub.put_pixel_rows = put_gray_rows;
185 } else if (cinfo->out_color_space == JCS_RGB) {
186 if (cinfo->quantize_colors) {
187 /* We only support 8-bit colormap indexes, so only 256 colors */
188 num_colors = cinfo->actual_number_of_colors;
189 if (num_colors > 256)
190 ERREXIT1(cinfo, JERR_TOO_MANY_COLORS, num_colors);
191 write_header(cinfo, dinfo, num_colors);
192 /* Write the colormap. Note Targa uses BGR byte order */
193 outfile = dest->pub.output_file;
194 for (i = 0; i < num_colors; i++) {
195 putc(GETJSAMPLE(cinfo->colormap[2][i]), outfile);
196 putc(GETJSAMPLE(cinfo->colormap[1][i]), outfile);
197 putc(GETJSAMPLE(cinfo->colormap[0][i]), outfile);
199 dest->pub.put_pixel_rows = put_gray_rows;
200 } else {
201 write_header(cinfo, dinfo, 0);
202 dest->pub.put_pixel_rows = put_pixel_rows;
204 } else {
205 ERREXIT(cinfo, JERR_TGA_COLORSPACE);
211 * Finish up at the end of the file.
214 METHODDEF(void)
215 finish_output_tga (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
217 /* Make sure we wrote the output file OK */
218 fflush(dinfo->output_file);
219 if (ferror(dinfo->output_file))
220 ERREXIT(cinfo, JERR_FILE_WRITE);
225 * The module selection routine for Targa format output.
228 JGLOBAL(djpeg_dest_ptr)
229 jinit_write_targa (j_decompress_ptr cinfo)
231 tga_dest_ptr dest;
233 /* Create module interface object, fill in method pointers */
234 dest = (tga_dest_ptr)
235 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
236 SIZEOF(tga_dest_struct));
237 dest->pub.start_output = start_output_tga;
238 dest->pub.finish_output = finish_output_tga;
240 /* Calculate output image dimensions so we can allocate space */
241 jpeg_calc_output_dimensions(cinfo);
243 /* Create I/O buffer. Note we make this near on a PC. */
244 dest->buffer_width = cinfo->output_width * cinfo->output_components;
245 dest->iobuffer = (char *)
246 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
247 (size_t) (dest->buffer_width * SIZEOF(char)));
249 /* Create decompressor output buffer. */
250 dest->pub.buffer = (*cinfo->mem->alloc_sarray)
251 ((j_common_ptr) cinfo, JPOOL_IMAGE, dest->buffer_width, (JDIMENSION) 1);
252 dest->pub.buffer_height = 1;
254 return (djpeg_dest_ptr) dest;
257 #endif /* TARGA_SUPPORTED */