Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
wrtarga.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "cdjpeg.h"
00018
00019 #ifdef TARGA_SUPPORTED
00020
00021
00022
00023
00024
00025
00026
00027 #if BITS_IN_JSAMPLE != 8
00028 Sorry, this code only copes with 8-bit JSAMPLEs.
00029 #endif
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 typedef struct {
00044 struct djpeg_dest_struct pub;
00045
00046 char *iobuffer;
00047 JDIMENSION buffer_width;
00048 } tga_dest_struct;
00049
00050 typedef tga_dest_struct * tga_dest_ptr;
00051
00052
00053 LOCAL(void)
00054 write_header (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, int num_colors)
00055
00056 {
00057 char targaheader[18];
00058
00059
00060 MEMZERO(targaheader, SIZEOF(targaheader));
00061
00062 if (num_colors > 0) {
00063 targaheader[1] = 1;
00064 targaheader[5] = (char) (num_colors & 0xFF);
00065 targaheader[6] = (char) (num_colors >> 8);
00066 targaheader[7] = 24;
00067 }
00068
00069 targaheader[12] = (char) (cinfo->output_width & 0xFF);
00070 targaheader[13] = (char) (cinfo->output_width >> 8);
00071 targaheader[14] = (char) (cinfo->output_height & 0xFF);
00072 targaheader[15] = (char) (cinfo->output_height >> 8);
00073 targaheader[17] = 0x20;
00074
00075 if (cinfo->out_color_space == JCS_GRAYSCALE) {
00076 targaheader[2] = 3;
00077 targaheader[16] = 8;
00078 } else {
00079 if (num_colors > 0) {
00080 targaheader[2] = 1;
00081 targaheader[16] = 8;
00082 } else {
00083 targaheader[2] = 2;
00084 targaheader[16] = 24;
00085 }
00086 }
00087
00088 if (JFWRITE(dinfo->output_file, targaheader, 18) != (size_t) 18)
00089 ERREXIT(cinfo, JERR_FILE_WRITE);
00090 }
00091
00092
00093
00094
00095
00096
00097
00098 METHODDEF(void)
00099 put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
00100 JDIMENSION rows_supplied)
00101
00102 {
00103 tga_dest_ptr dest = (tga_dest_ptr) dinfo;
00104 register JSAMPROW inptr;
00105 register char * outptr;
00106 register JDIMENSION col;
00107
00108 inptr = dest->pub.buffer[0];
00109 outptr = dest->iobuffer;
00110 for (col = cinfo->output_width; col > 0; col--) {
00111 outptr[0] = (char) GETJSAMPLE(inptr[2]);
00112 outptr[1] = (char) GETJSAMPLE(inptr[1]);
00113 outptr[2] = (char) GETJSAMPLE(inptr[0]);
00114 inptr += 3, outptr += 3;
00115 }
00116 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
00117 }
00118
00119 METHODDEF(void)
00120 put_gray_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
00121 JDIMENSION rows_supplied)
00122
00123 {
00124 tga_dest_ptr dest = (tga_dest_ptr) dinfo;
00125 register JSAMPROW inptr;
00126 register char * outptr;
00127 register JDIMENSION col;
00128
00129 inptr = dest->pub.buffer[0];
00130 outptr = dest->iobuffer;
00131 for (col = cinfo->output_width; col > 0; col--) {
00132 *outptr++ = (char) GETJSAMPLE(*inptr++);
00133 }
00134 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
00135 }
00136
00137
00138
00139
00140
00141
00142
00143 METHODDEF(void)
00144 put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
00145 JDIMENSION rows_supplied)
00146 {
00147 tga_dest_ptr dest = (tga_dest_ptr) dinfo;
00148 register JSAMPROW inptr;
00149 register char * outptr;
00150 register JSAMPROW color_map0 = cinfo->colormap[0];
00151 register JDIMENSION col;
00152
00153 inptr = dest->pub.buffer[0];
00154 outptr = dest->iobuffer;
00155 for (col = cinfo->output_width; col > 0; col--) {
00156 *outptr++ = (char) GETJSAMPLE(color_map0[GETJSAMPLE(*inptr++)]);
00157 }
00158 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
00159 }
00160
00161
00162
00163
00164
00165
00166 METHODDEF(void)
00167 start_output_tga (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
00168 {
00169 tga_dest_ptr dest = (tga_dest_ptr) dinfo;
00170 int num_colors, i;
00171 FILE *outfile;
00172
00173 if (cinfo->out_color_space == JCS_GRAYSCALE) {
00174
00175
00176 write_header(cinfo, dinfo, 0);
00177 if (cinfo->quantize_colors)
00178 dest->pub.put_pixel_rows = put_demapped_gray;
00179 else
00180 dest->pub.put_pixel_rows = put_gray_rows;
00181 } else if (cinfo->out_color_space == JCS_RGB) {
00182 if (cinfo->quantize_colors) {
00183
00184 num_colors = cinfo->actual_number_of_colors;
00185 if (num_colors > 256)
00186 ERREXIT1(cinfo, JERR_TOO_MANY_COLORS, num_colors);
00187 write_header(cinfo, dinfo, num_colors);
00188
00189 outfile = dest->pub.output_file;
00190 for (i = 0; i < num_colors; i++) {
00191 putc(GETJSAMPLE(cinfo->colormap[2][i]), outfile);
00192 putc(GETJSAMPLE(cinfo->colormap[1][i]), outfile);
00193 putc(GETJSAMPLE(cinfo->colormap[0][i]), outfile);
00194 }
00195 dest->pub.put_pixel_rows = put_gray_rows;
00196 } else {
00197 write_header(cinfo, dinfo, 0);
00198 dest->pub.put_pixel_rows = put_pixel_rows;
00199 }
00200 } else {
00201 ERREXIT(cinfo, JERR_TGA_COLORSPACE);
00202 }
00203 }
00204
00205
00206
00207
00208
00209
00210 METHODDEF(void)
00211 finish_output_tga (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
00212 {
00213
00214 fflush(dinfo->output_file);
00215 if (ferror(dinfo->output_file))
00216 ERREXIT(cinfo, JERR_FILE_WRITE);
00217 }
00218
00219
00220
00221
00222
00223
00224 GLOBAL(djpeg_dest_ptr)
00225 jinit_write_targa (j_decompress_ptr cinfo)
00226 {
00227 tga_dest_ptr dest;
00228
00229
00230 dest = (tga_dest_ptr)
00231 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00232 SIZEOF(tga_dest_struct));
00233 dest->pub.start_output = start_output_tga;
00234 dest->pub.finish_output = finish_output_tga;
00235
00236
00237 jpeg_calc_output_dimensions(cinfo);
00238
00239
00240 dest->buffer_width = cinfo->output_width * cinfo->output_components;
00241 dest->iobuffer = (char *)
00242 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00243 (size_t) (dest->buffer_width * SIZEOF(char)));
00244
00245
00246 dest->pub.buffer = (*cinfo->mem->alloc_sarray)
00247 ((j_common_ptr) cinfo, JPOOL_IMAGE, dest->buffer_width, (JDIMENSION) 1);
00248 dest->pub.buffer_height = 1;
00249
00250 return (djpeg_dest_ptr) dest;
00251 }
00252
00253 #endif