ReactOS 0.4.15-dev-8231-g29a56f3
wrtarga.c
Go to the documentation of this file.
1/*
2 * wrtarga.c
3 *
4 * Copyright (C) 1991-1996, Thomas G. Lane.
5 * Modified 2015-2019 by Guido Vollbeding.
6 * This file is part of the Independent JPEG Group's software.
7 * For conditions of distribution and use, see the accompanying README file.
8 *
9 * This file contains routines to write output images in Targa format.
10 *
11 * These routines may need modification for non-Unix environments or
12 * specialized applications. As they stand, they assume output to
13 * an ordinary stdio stream.
14 *
15 * Based on code contributed by Lee Daniel Crocker.
16 */
17
18#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
19
20#ifdef TARGA_SUPPORTED
21
22
23/*
24 * To support 12-bit JPEG data, we'd have to scale output down to 8 bits.
25 * This is not yet implemented.
26 */
27
28#if BITS_IN_JSAMPLE != 8
29 Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
30#endif
31
32/*
33 * The output buffer needs to be writable by fwrite(). On PCs, we must
34 * allocate the buffer in near data space, because we are assuming small-data
35 * memory model, wherein fwrite() can't reach far memory. If you need to
36 * process very wide images on a PC, you might have to compile in large-memory
37 * model, or else replace fwrite() with a putc() loop --- which will be much
38 * slower.
39 */
40
41
42/* Private version of data destination object */
43
44typedef struct {
45 struct djpeg_dest_struct pub; /* public fields */
46
47 char *iobuffer; /* physical I/O buffer */
48 JDIMENSION buffer_width; /* width of one row */
49} tga_dest_struct;
50
51typedef tga_dest_struct * tga_dest_ptr;
52
53
54LOCAL(void)
55write_header (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, int num_colors)
56/* Create and write a Targa header */
57{
58 char targaheader[18];
59
60 /* Set unused fields of header to 0 */
61 MEMZERO(targaheader, SIZEOF(targaheader));
62
63 if (num_colors > 0) {
64 targaheader[1] = 1; /* color map type 1 */
65 targaheader[5] = (char) (num_colors & 0xFF);
66 targaheader[6] = (char) (num_colors >> 8);
67 targaheader[7] = 24; /* 24 bits per cmap entry */
68 }
69
70 targaheader[12] = (char) (cinfo->output_width & 0xFF);
71 targaheader[13] = (char) (cinfo->output_width >> 8);
72 targaheader[14] = (char) (cinfo->output_height & 0xFF);
73 targaheader[15] = (char) (cinfo->output_height >> 8);
74 targaheader[17] = 0x20; /* Top-down, non-interlaced */
75
76 if (cinfo->out_color_space == JCS_GRAYSCALE) {
77 targaheader[2] = 3; /* image type = uncompressed grayscale */
78 targaheader[16] = 8; /* bits per pixel */
79 } else { /* must be RGB */
80 if (num_colors > 0) {
81 targaheader[2] = 1; /* image type = colormapped RGB */
82 targaheader[16] = 8;
83 } else {
84 targaheader[2] = 2; /* image type = uncompressed RGB */
85 targaheader[16] = 24;
86 }
87 }
88
89 if (JFWRITE(dinfo->output_file, targaheader, 18) != (size_t) 18)
90 ERREXIT(cinfo, JERR_FILE_WRITE);
91}
92
93
94/*
95 * Write some pixel data.
96 * In this module rows_supplied will always be 1.
97 */
98
99METHODDEF(void)
100put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
101 JDIMENSION rows_supplied)
102/* used for unquantized full-color output */
103{
104 tga_dest_ptr dest = (tga_dest_ptr) dinfo;
105 register JSAMPROW inptr;
106 register char * outptr;
107 register JDIMENSION col;
108
109 inptr = dest->pub.buffer[0];
110 outptr = dest->iobuffer;
111 for (col = cinfo->output_width; col > 0; col--) {
112 outptr[0] = (char) GETJSAMPLE(inptr[2]); /* RGB to BGR order */
113 outptr[1] = (char) GETJSAMPLE(inptr[1]);
114 outptr[2] = (char) GETJSAMPLE(inptr[0]);
115 inptr += 3, outptr += 3;
116 }
117 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
118}
119
120METHODDEF(void)
121put_gray_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
122 JDIMENSION rows_supplied)
123/* used for grayscale OR quantized color output */
124{
125 tga_dest_ptr dest = (tga_dest_ptr) dinfo;
126 register JSAMPROW inptr;
127 register char * outptr;
128 register JDIMENSION col;
129
130 inptr = dest->pub.buffer[0];
131 outptr = dest->iobuffer;
132 for (col = cinfo->output_width; col > 0; col--) {
133 *outptr++ = (char) GETJSAMPLE(*inptr++);
134 }
135 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
136}
137
138
139/*
140 * Write some demapped pixel data when color quantization is in effect.
141 * For Targa, this is only applied to grayscale data.
142 */
143
144METHODDEF(void)
145put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
146 JDIMENSION rows_supplied)
147{
148 tga_dest_ptr dest = (tga_dest_ptr) dinfo;
149 register JSAMPROW inptr;
150 register char * outptr;
151 register JSAMPROW color_map0 = cinfo->colormap[0];
152 register JDIMENSION col;
153
154 inptr = dest->pub.buffer[0];
155 outptr = dest->iobuffer;
156 for (col = cinfo->output_width; col > 0; col--) {
157 *outptr++ = (char) GETJSAMPLE(color_map0[GETJSAMPLE(*inptr++)]);
158 }
159 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
160}
161
162
163/*
164 * Startup: write the file header.
165 */
166
167METHODDEF(void)
168start_output_tga (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
169{
170 int num_colors, i;
171 FILE *outfile;
172
173 switch (cinfo->out_color_space) {
174 case JCS_GRAYSCALE:
175 /* Targa doesn't have a mapped grayscale format, so we will */
176 /* demap quantized gray output. Never emit a colormap. */
177 write_header(cinfo, dinfo, 0);
178 if (cinfo->quantize_colors)
179 dinfo->put_pixel_rows = put_demapped_gray;
180 else
181 dinfo->put_pixel_rows = put_gray_rows;
182 break;
183 case JCS_RGB:
184 if (cinfo->quantize_colors) {
185 /* We only support 8-bit colormap indexes, so only 256 colors */
186 num_colors = cinfo->actual_number_of_colors;
187 if (num_colors > 256)
188 ERREXIT1(cinfo, JERR_TOO_MANY_COLORS, num_colors);
189 write_header(cinfo, dinfo, num_colors);
190 /* Write the colormap. Note Targa uses BGR byte order */
191 outfile = dinfo->output_file;
192 for (i = 0; i < num_colors; i++) {
193 putc(GETJSAMPLE(cinfo->colormap[2][i]), outfile);
194 putc(GETJSAMPLE(cinfo->colormap[1][i]), outfile);
195 putc(GETJSAMPLE(cinfo->colormap[0][i]), outfile);
196 }
197 dinfo->put_pixel_rows = put_gray_rows;
198 } else {
199 write_header(cinfo, dinfo, 0);
200 dinfo->put_pixel_rows = put_pixel_rows;
201 }
202 break;
203 default:
204 ERREXIT(cinfo, JERR_TGA_COLORSPACE);
205 }
206}
207
208
209/*
210 * Finish up at the end of the file.
211 */
212
213METHODDEF(void)
214finish_output_tga (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
215{
216 /* Make sure we wrote the output file OK */
217 JFFLUSH(dinfo->output_file);
218 if (JFERROR(dinfo->output_file))
219 ERREXIT(cinfo, JERR_FILE_WRITE);
220}
221
222
223/*
224 * The module selection routine for Targa format output.
225 */
226
228jinit_write_targa (j_decompress_ptr cinfo)
229{
230 tga_dest_ptr dest;
231
232 /* Create module interface object, fill in method pointers */
233 dest = (tga_dest_ptr) (*cinfo->mem->alloc_small)
234 ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(tga_dest_struct));
235 dest->pub.start_output = start_output_tga;
236 dest->pub.finish_output = finish_output_tga;
237
238 /* Calculate output image dimensions so we can allocate space */
240
241 /* Create I/O buffer. Note we make this near on a PC. */
242 dest->buffer_width = cinfo->output_width * cinfo->output_components;
243 dest->iobuffer = (char *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo,
244 JPOOL_IMAGE, (size_t) dest->buffer_width * SIZEOF(char));
245
246 /* Create decompressor output buffer. */
247 dest->pub.buffer = (*cinfo->mem->alloc_sarray) ((j_common_ptr) cinfo,
248 JPOOL_IMAGE, dest->buffer_width, (JDIMENSION) 1);
249 dest->pub.buffer_height = 1;
250
251 return &dest->pub;
252}
253
254#endif /* TARGA_SUPPORTED */
#define SIZEOF(_ar)
Definition: calc.h:97
unsigned char
Definition: typeof.h:29
__kernel_size_t size_t
Definition: linux.h:237
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
_Check_return_opt_ _CRTIMP int __cdecl putc(_In_ int _Ch, _Inout_ FILE *_File)
Sorry
Definition: jdcolor.c:19
jpeg_calc_output_dimensions(j_decompress_ptr cinfo)
Definition: jdmaster.c:101
#define ERREXIT1(cinfo, code, p1)
Definition: jerror.h:212
#define JFFLUSH(file)
Definition: jinclude.h:96
#define JFERROR(file)
Definition: jinclude.h:97
#define JFWRITE(file, buf, sizeofbuf)
Definition: jinclude.h:94
unsigned int JDIMENSION
Definition: jmorecfg.h:229
#define LOCAL(type)
Definition: jmorecfg.h:289
#define METHODDEF(type)
Definition: jmorecfg.h:287
#define GLOBAL(type)
Definition: jmorecfg.h:291
#define GETJSAMPLE(value)
Definition: jmorecfg.h:78
struct jpeg_common_struct * j_common_ptr
Definition: jpeglib.h:284
@ JCS_GRAYSCALE
Definition: jpeglib.h:222
@ JCS_RGB
Definition: jpeglib.h:223
JSAMPLE FAR * JSAMPROW
Definition: jpeglib.h:75
#define JPOOL_IMAGE
Definition: jpeglib.h:808
static char * dest
Definition: rtl.c:135
#define ERREXIT(msg)
Definition: rdjpgcom.c:72
void write_header(const statement_list_t *stmts)
Definition: header.c:1790
Definition: inflate.c:139
FILE * output_file
Definition: cdjpeg.h:63
JDIMENSION output_height
Definition: jpeglib.h:508
JSAMPARRAY colormap
Definition: jpeglib.h:527
boolean quantize_colors
Definition: jpeglib.h:491
J_COLOR_SPACE out_color_space
Definition: jpeglib.h:478
JDIMENSION output_width
Definition: jpeglib.h:507
#define MEMZERO(addr, type, size)
Definition: svc_dg.c:324
static FILE * outfile
Definition: wrjpgcom.c:81