Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * wrtarga.c |
| 3 | * |
DRC | 5033f3e | 2014-05-18 18:33:44 +0000 | [diff] [blame] | 4 | * This file was part of the Independent JPEG Group's software: |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 5 | * Copyright (C) 1991-1996, Thomas G. Lane. |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 6 | * It was modified by The libjpeg-turbo Project to include only code and |
| 7 | * information relevant to libjpeg-turbo. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 | |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 19 | #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 33 | |
| 34 | /* Private version of data destination object */ |
| 35 | |
| 36 | typedef struct { |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 37 | struct djpeg_dest_struct pub; /* public fields */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 38 | |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 39 | char *iobuffer; /* physical I/O buffer */ |
| 40 | JDIMENSION buffer_width; /* width of one row */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 41 | } tga_dest_struct; |
| 42 | |
| 43 | typedef tga_dest_struct * tga_dest_ptr; |
| 44 | |
| 45 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 46 | LOCAL(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 47 | write_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 */ |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 53 | MEMZERO(targaheader, sizeof(targaheader)); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 54 | |
| 55 | if (num_colors > 0) { |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 56 | targaheader[1] = 1; /* color map type 1 */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 57 | targaheader[5] = (char) (num_colors & 0xFF); |
| 58 | targaheader[6] = (char) (num_colors >> 8); |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 59 | targaheader[7] = 24; /* 24 bits per cmap entry */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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); |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 66 | targaheader[17] = 0x20; /* Top-down, non-interlaced */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 67 | |
| 68 | if (cinfo->out_color_space == JCS_GRAYSCALE) { |
DRC | 90d6c38 | 2014-05-12 09:08:39 +0000 | [diff] [blame] | 69 | targaheader[2] = 3; /* image type = uncompressed grayscale */ |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 70 | targaheader[16] = 8; /* bits per pixel */ |
| 71 | } else { /* must be RGB */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 72 | if (num_colors > 0) { |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 73 | targaheader[2] = 1; /* image type = colormapped RGB */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 74 | targaheader[16] = 8; |
| 75 | } else { |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 76 | targaheader[2] = 2; /* image type = uncompressed RGB */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 91 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 92 | put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 93 | JDIMENSION rows_supplied) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 112 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 113 | put_gray_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 114 | JDIMENSION rows_supplied) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 136 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 137 | put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 138 | JDIMENSION rows_supplied) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 159 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 160 | start_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) |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 179 | ERREXIT1(cinfo, JERR_TOO_MANY_COLORS, num_colors); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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++) { |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 184 | 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. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 203 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 204 | finish_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. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 217 | GLOBAL(djpeg_dest_ptr) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 218 | jinit_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, |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 225 | sizeof(tga_dest_struct)); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 | |
DRC | 5033f3e | 2014-05-18 18:33:44 +0000 | [diff] [blame] | 232 | /* Create I/O buffer. */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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, |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 236 | (size_t) (dest->buffer_width * sizeof(char))); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 */ |