1/* 2 * wrtarga.c 3 * 4 * This file was part of the Independent JPEG Group's software: 5 * Copyright (C) 1991-1996, Thomas G. Lane. 6 * It was modified by The libjpeg-turbo Project to include only code and 7 * information relevant to libjpeg-turbo. 8 * 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 19#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ 20 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 33 34/* Private version of data destination object */ 35 36typedef struct { 37 struct djpeg_dest_struct pub; /* public fields */ 38 39 char *iobuffer; /* physical I/O buffer */ 40 JDIMENSION buffer_width; /* width of one row */ 41} tga_dest_struct; 42 43typedef tga_dest_struct * tga_dest_ptr; 44 45 46LOCAL(void) 47write_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 */ 53 MEMZERO(targaheader, sizeof(targaheader)); 54 55 if (num_colors > 0) { 56 targaheader[1] = 1; /* color map type 1 */ 57 targaheader[5] = (char) (num_colors & 0xFF); 58 targaheader[6] = (char) (num_colors >> 8); 59 targaheader[7] = 24; /* 24 bits per cmap entry */ 60 } 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); 66 targaheader[17] = 0x20; /* Top-down, non-interlaced */ 67 68 if (cinfo->out_color_space == JCS_GRAYSCALE) { 69 targaheader[2] = 3; /* image type = uncompressed grayscale */ 70 targaheader[16] = 8; /* bits per pixel */ 71 } else { /* must be RGB */ 72 if (num_colors > 0) { 73 targaheader[2] = 1; /* image type = colormapped RGB */ 74 targaheader[16] = 8; 75 } else { 76 targaheader[2] = 2; /* image type = uncompressed RGB */ 77 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 91METHODDEF(void) 92put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, 93 JDIMENSION rows_supplied) 94/* 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 112METHODDEF(void) 113put_gray_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, 114 JDIMENSION rows_supplied) 115/* 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 136METHODDEF(void) 137put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, 138 JDIMENSION rows_supplied) 139{ 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 159METHODDEF(void) 160start_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) 179 ERREXIT1(cinfo, JERR_TOO_MANY_COLORS, num_colors); 180 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++) { 184 putc(GETJSAMPLE(cinfo->colormap[2][i]), outfile); 185 putc(GETJSAMPLE(cinfo->colormap[1][i]), outfile); 186 putc(GETJSAMPLE(cinfo->colormap[0][i]), outfile); 187 } 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 203METHODDEF(void) 204finish_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 217GLOBAL(djpeg_dest_ptr) 218jinit_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, 225 sizeof(tga_dest_struct)); 226 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 232 /* Create I/O buffer. */ 233 dest->buffer_width = cinfo->output_width * cinfo->output_components; 234 dest->iobuffer = (char *) 235 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 236 (size_t) (dest->buffer_width * sizeof(char))); 237 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 */ 247