hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * wrtarga.c |
| 3 | * |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 4 | * This file was part of the Independent JPEG Group's software: |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 5 | * Copyright (C) 1991-1996, Thomas G. Lane. |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 6 | * libjpeg-turbo Modifications: |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 7 | * Copyright (C) 2017, 2019, D. R. Commander. |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 8 | * For conditions of distribution and use, see the accompanying README.ijg |
| 9 | * file. |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 10 | * |
| 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 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 20 | #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 21 | |
| 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 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 34 | |
| 35 | /* Private version of data destination object */ |
| 36 | |
| 37 | typedef struct { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 38 | struct djpeg_dest_struct pub; /* public fields */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 39 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 40 | char *iobuffer; /* physical I/O buffer */ |
| 41 | JDIMENSION buffer_width; /* width of one row */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 42 | } tga_dest_struct; |
| 43 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 44 | typedef tga_dest_struct *tga_dest_ptr; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 45 | |
| 46 | |
| 47 | LOCAL(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 48 | write_header(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, int num_colors) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 49 | /* Create and write a Targa header */ |
| 50 | { |
| 51 | char targaheader[18]; |
| 52 | |
| 53 | /* Set unused fields of header to 0 */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 54 | MEMZERO(targaheader, sizeof(targaheader)); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 55 | |
| 56 | if (num_colors > 0) { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 57 | targaheader[1] = 1; /* color map type 1 */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 58 | targaheader[5] = (char)(num_colors & 0xFF); |
| 59 | targaheader[6] = (char)(num_colors >> 8); |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 60 | targaheader[7] = 24; /* 24 bits per cmap entry */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 63 | 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); |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 67 | targaheader[17] = 0x20; /* Top-down, non-interlaced */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 68 | |
| 69 | if (cinfo->out_color_space == JCS_GRAYSCALE) { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 70 | targaheader[2] = 3; /* image type = uncompressed grayscale */ |
| 71 | targaheader[16] = 8; /* bits per pixel */ |
| 72 | } else { /* must be RGB */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 73 | if (num_colors > 0) { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 74 | targaheader[2] = 1; /* image type = colormapped RGB */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 75 | targaheader[16] = 8; |
| 76 | } else { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 77 | targaheader[2] = 2; /* image type = uncompressed RGB */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 78 | targaheader[16] = 24; |
| 79 | } |
| 80 | } |
| 81 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 82 | if (JFWRITE(dinfo->output_file, targaheader, 18) != (size_t)18) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 83 | 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 | |
| 92 | METHODDEF(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 93 | put_pixel_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, |
| 94 | JDIMENSION rows_supplied) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 95 | /* used for unquantized full-color output */ |
| 96 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 97 | tga_dest_ptr dest = (tga_dest_ptr)dinfo; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 98 | register JSAMPROW inptr; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 99 | register char *outptr; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 100 | register JDIMENSION col; |
| 101 | |
| 102 | inptr = dest->pub.buffer[0]; |
| 103 | outptr = dest->iobuffer; |
| 104 | for (col = cinfo->output_width; col > 0; col--) { |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 105 | outptr[0] = inptr[2]; /* RGB to BGR order */ |
| 106 | outptr[1] = inptr[1]; |
| 107 | outptr[2] = inptr[0]; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 108 | inptr += 3, outptr += 3; |
| 109 | } |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 110 | (void)JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | METHODDEF(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 114 | put_gray_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, |
| 115 | JDIMENSION rows_supplied) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 116 | /* used for grayscale OR quantized color output */ |
| 117 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 118 | tga_dest_ptr dest = (tga_dest_ptr)dinfo; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 119 | register JSAMPROW inptr; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 120 | register char *outptr; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 121 | |
| 122 | inptr = dest->pub.buffer[0]; |
| 123 | outptr = dest->iobuffer; |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 124 | MEMCOPY(outptr, inptr, cinfo->output_width); |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 125 | (void)JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | |
| 129 | /* |
| 130 | * Write some demapped pixel data when color quantization is in effect. |
| 131 | * For Targa, this is only applied to grayscale data. |
| 132 | */ |
| 133 | |
| 134 | METHODDEF(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 135 | put_demapped_gray(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, |
| 136 | JDIMENSION rows_supplied) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 137 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 138 | tga_dest_ptr dest = (tga_dest_ptr)dinfo; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 139 | register JSAMPROW inptr; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 140 | register char *outptr; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 141 | register JSAMPROW color_map0 = cinfo->colormap[0]; |
| 142 | register JDIMENSION col; |
| 143 | |
| 144 | inptr = dest->pub.buffer[0]; |
| 145 | outptr = dest->iobuffer; |
| 146 | for (col = cinfo->output_width; col > 0; col--) { |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 147 | *outptr++ = color_map0[*inptr++]; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 148 | } |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 149 | (void)JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | |
| 153 | /* |
| 154 | * Startup: write the file header. |
| 155 | */ |
| 156 | |
| 157 | METHODDEF(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 158 | start_output_tga(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 159 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 160 | tga_dest_ptr dest = (tga_dest_ptr)dinfo; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 161 | int num_colors, i; |
| 162 | FILE *outfile; |
| 163 | |
| 164 | if (cinfo->out_color_space == JCS_GRAYSCALE) { |
| 165 | /* Targa doesn't have a mapped grayscale format, so we will */ |
| 166 | /* demap quantized gray output. Never emit a colormap. */ |
| 167 | write_header(cinfo, dinfo, 0); |
| 168 | if (cinfo->quantize_colors) |
| 169 | dest->pub.put_pixel_rows = put_demapped_gray; |
| 170 | else |
| 171 | dest->pub.put_pixel_rows = put_gray_rows; |
| 172 | } else if (cinfo->out_color_space == JCS_RGB) { |
| 173 | if (cinfo->quantize_colors) { |
| 174 | /* We only support 8-bit colormap indexes, so only 256 colors */ |
| 175 | num_colors = cinfo->actual_number_of_colors; |
| 176 | if (num_colors > 256) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 177 | ERREXIT1(cinfo, JERR_TOO_MANY_COLORS, num_colors); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 178 | write_header(cinfo, dinfo, num_colors); |
| 179 | /* Write the colormap. Note Targa uses BGR byte order */ |
| 180 | outfile = dest->pub.output_file; |
| 181 | for (i = 0; i < num_colors; i++) { |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 182 | putc(cinfo->colormap[2][i], outfile); |
| 183 | putc(cinfo->colormap[1][i], outfile); |
| 184 | putc(cinfo->colormap[0][i], outfile); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 185 | } |
| 186 | dest->pub.put_pixel_rows = put_gray_rows; |
| 187 | } else { |
| 188 | write_header(cinfo, dinfo, 0); |
| 189 | dest->pub.put_pixel_rows = put_pixel_rows; |
| 190 | } |
| 191 | } else { |
| 192 | ERREXIT(cinfo, JERR_TGA_COLORSPACE); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | |
| 197 | /* |
| 198 | * Finish up at the end of the file. |
| 199 | */ |
| 200 | |
| 201 | METHODDEF(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 202 | finish_output_tga(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 203 | { |
| 204 | /* Make sure we wrote the output file OK */ |
| 205 | fflush(dinfo->output_file); |
| 206 | if (ferror(dinfo->output_file)) |
| 207 | ERREXIT(cinfo, JERR_FILE_WRITE); |
| 208 | } |
| 209 | |
| 210 | |
| 211 | /* |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 212 | * Re-calculate buffer dimensions based on output dimensions. |
| 213 | */ |
| 214 | |
| 215 | METHODDEF(void) |
| 216 | calc_buffer_dimensions_tga(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) |
| 217 | { |
| 218 | tga_dest_ptr dest = (tga_dest_ptr)dinfo; |
| 219 | |
| 220 | dest->buffer_width = cinfo->output_width * cinfo->output_components; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | /* |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 225 | * The module selection routine for Targa format output. |
| 226 | */ |
| 227 | |
| 228 | GLOBAL(djpeg_dest_ptr) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 229 | jinit_write_targa(j_decompress_ptr cinfo) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 230 | { |
| 231 | tga_dest_ptr dest; |
| 232 | |
| 233 | /* Create module interface object, fill in method pointers */ |
| 234 | dest = (tga_dest_ptr) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 235 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
| 236 | sizeof(tga_dest_struct)); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 237 | dest->pub.start_output = start_output_tga; |
| 238 | dest->pub.finish_output = finish_output_tga; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 239 | dest->pub.calc_buffer_dimensions = calc_buffer_dimensions_tga; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 240 | |
| 241 | /* Calculate output image dimensions so we can allocate space */ |
| 242 | jpeg_calc_output_dimensions(cinfo); |
| 243 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 244 | /* Create I/O buffer. */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 245 | dest->pub.calc_buffer_dimensions(cinfo, (djpeg_dest_ptr)dest); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 246 | dest->iobuffer = (char *) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 247 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
| 248 | (size_t)(dest->buffer_width * sizeof(char))); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 249 | |
| 250 | /* Create decompressor output buffer. */ |
| 251 | dest->pub.buffer = (*cinfo->mem->alloc_sarray) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 252 | ((j_common_ptr)cinfo, JPOOL_IMAGE, dest->buffer_width, (JDIMENSION)1); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 253 | dest->pub.buffer_height = 1; |
| 254 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 255 | return (djpeg_dest_ptr)dest; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | #endif /* TARGA_SUPPORTED */ |