[LIBJPEG] Update to version 9c. CORE-14291
[reactos.git] / dll / 3rdparty / libjpeg / rdtarga.c
1 /*
2 * rdtarga.c
3 *
4 * Copyright (C) 1991-1996, Thomas G. Lane.
5 * Modified 2017 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 read input images in Targa format.
10 *
11 * These routines may need modification for non-Unix environments or
12 * specialized applications. As they stand, they assume input from
13 * an ordinary stdio stream. They further assume that reading begins
14 * at the start of the file; start_input may need work if the
15 * user interface has already read some data (e.g., to determine that
16 * the file is indeed Targa format).
17 *
18 * Based on code contributed by Lee Daniel Crocker.
19 */
20
21 #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
22
23 #ifdef TARGA_SUPPORTED
24
25
26 /* Macros to deal with unsigned chars as efficiently as compiler allows */
27
28 #ifdef HAVE_UNSIGNED_CHAR
29 typedef unsigned char U_CHAR;
30 #define UCH(x) ((int) (x))
31 #else /* !HAVE_UNSIGNED_CHAR */
32 #ifdef CHAR_IS_UNSIGNED
33 typedef char U_CHAR;
34 #define UCH(x) ((int) (x))
35 #else
36 typedef char U_CHAR;
37 #define UCH(x) ((int) (x) & 0xFF)
38 #endif
39 #endif /* HAVE_UNSIGNED_CHAR */
40
41
42 #define ReadOK(file,buffer,len) (JFREAD(file,buffer,len) == ((size_t) (len)))
43
44
45 /* Private version of data source object */
46
47 typedef struct _tga_source_struct * tga_source_ptr;
48
49 typedef struct _tga_source_struct {
50 struct cjpeg_source_struct pub; /* public fields */
51
52 j_compress_ptr cinfo; /* back link saves passing separate parm */
53
54 JSAMPARRAY colormap; /* Targa colormap (converted to my format) */
55
56 jvirt_sarray_ptr whole_image; /* Needed if funny input row order */
57 JDIMENSION current_row; /* Current logical row number to read */
58
59 /* Pointer to routine to extract next Targa pixel from input file */
60 JMETHOD(void, read_pixel, (tga_source_ptr sinfo));
61
62 /* Result of read_pixel is delivered here: */
63 U_CHAR tga_pixel[4];
64
65 int pixel_size; /* Bytes per Targa pixel (1 to 4) */
66 int cmap_length; /* colormap length */
67
68 /* State info for reading RLE-coded pixels; both counts must be init to 0 */
69 int block_count; /* # of pixels remaining in RLE block */
70 int dup_pixel_count; /* # of times to duplicate previous pixel */
71
72 /* This saves the correct pixel-row-expansion method for preload_image */
73 JMETHOD(JDIMENSION, get_pixel_rows, (j_compress_ptr cinfo,
74 cjpeg_source_ptr sinfo));
75 } tga_source_struct;
76
77
78 /* For expanding 5-bit pixel values to 8-bit with best rounding */
79
80 static const UINT8 c5to8bits[32] = {
81 0, 8, 16, 25, 33, 41, 49, 58,
82 66, 74, 82, 90, 99, 107, 115, 123,
83 132, 140, 148, 156, 165, 173, 181, 189,
84 197, 206, 214, 222, 230, 239, 247, 255
85 };
86
87
88
89 LOCAL(int)
90 read_byte (tga_source_ptr sinfo)
91 /* Read next byte from Targa file */
92 {
93 register FILE *infile = sinfo->pub.input_file;
94 register int c;
95
96 if ((c = getc(infile)) == EOF)
97 ERREXIT(sinfo->cinfo, JERR_INPUT_EOF);
98 return c;
99 }
100
101
102 LOCAL(void)
103 read_colormap (tga_source_ptr sinfo, int cmaplen, int mapentrysize)
104 /* Read the colormap from a Targa file */
105 {
106 int i;
107
108 /* Presently only handles 24-bit BGR format */
109 if (mapentrysize != 24)
110 ERREXIT(sinfo->cinfo, JERR_TGA_BADCMAP);
111
112 for (i = 0; i < cmaplen; i++) {
113 sinfo->colormap[2][i] = (JSAMPLE) read_byte(sinfo);
114 sinfo->colormap[1][i] = (JSAMPLE) read_byte(sinfo);
115 sinfo->colormap[0][i] = (JSAMPLE) read_byte(sinfo);
116 }
117 }
118
119
120 /*
121 * read_pixel methods: get a single pixel from Targa file into tga_pixel[]
122 */
123
124 METHODDEF(void)
125 read_non_rle_pixel (tga_source_ptr sinfo)
126 /* Read one Targa pixel from the input file; no RLE expansion */
127 {
128 register FILE *infile = sinfo->pub.input_file;
129 register int i;
130
131 for (i = 0; i < sinfo->pixel_size; i++) {
132 sinfo->tga_pixel[i] = (U_CHAR) getc(infile);
133 }
134 }
135
136
137 METHODDEF(void)
138 read_rle_pixel (tga_source_ptr sinfo)
139 /* Read one Targa pixel from the input file, expanding RLE data as needed */
140 {
141 register FILE *infile = sinfo->pub.input_file;
142 register int i;
143
144 /* Duplicate previously read pixel? */
145 if (sinfo->dup_pixel_count > 0) {
146 sinfo->dup_pixel_count--;
147 return;
148 }
149
150 /* Time to read RLE block header? */
151 if (--sinfo->block_count < 0) { /* decrement pixels remaining in block */
152 i = read_byte(sinfo);
153 if (i & 0x80) { /* Start of duplicate-pixel block? */
154 sinfo->dup_pixel_count = i & 0x7F; /* number of dups after this one */
155 sinfo->block_count = 0; /* then read new block header */
156 } else {
157 sinfo->block_count = i & 0x7F; /* number of pixels after this one */
158 }
159 }
160
161 /* Read next pixel */
162 for (i = 0; i < sinfo->pixel_size; i++) {
163 sinfo->tga_pixel[i] = (U_CHAR) getc(infile);
164 }
165 }
166
167
168 /*
169 * Read one row of pixels.
170 *
171 * We provide several different versions depending on input file format.
172 */
173
174
175 METHODDEF(JDIMENSION)
176 get_8bit_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
177 /* This version is for reading 8-bit grayscale pixels */
178 {
179 tga_source_ptr source = (tga_source_ptr) sinfo;
180 register JSAMPROW ptr;
181 register JDIMENSION col;
182
183 ptr = source->pub.buffer[0];
184 for (col = cinfo->image_width; col > 0; col--) {
185 (*source->read_pixel) (source); /* Load next pixel into tga_pixel */
186 *ptr++ = (JSAMPLE) UCH(source->tga_pixel[0]);
187 }
188 return 1;
189 }
190
191 METHODDEF(JDIMENSION)
192 get_8bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
193 /* This version is for reading 8-bit colormap indexes */
194 {
195 tga_source_ptr source = (tga_source_ptr) sinfo;
196 register JSAMPROW ptr;
197 register JSAMPARRAY colormap;
198 register JDIMENSION col;
199 register int t;
200 int cmaplen;
201
202 ptr = source->pub.buffer[0];
203 colormap = source->colormap;
204 cmaplen = source->cmap_length;
205 for (col = cinfo->image_width; col > 0; col--) {
206 (*source->read_pixel) (source); /* Load next pixel into tga_pixel */
207 t = UCH(source->tga_pixel[0]);
208 if (t >= cmaplen)
209 ERREXIT(cinfo, JERR_TGA_BADPARMS);
210 *ptr++ = colormap[0][t];
211 *ptr++ = colormap[1][t];
212 *ptr++ = colormap[2][t];
213 }
214 return 1;
215 }
216
217 METHODDEF(JDIMENSION)
218 get_16bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
219 /* This version is for reading 16-bit pixels */
220 {
221 tga_source_ptr source = (tga_source_ptr) sinfo;
222 register int t;
223 register JSAMPROW ptr;
224 register JDIMENSION col;
225
226 ptr = source->pub.buffer[0];
227 for (col = cinfo->image_width; col > 0; col--) {
228 (*source->read_pixel) (source); /* Load next pixel into tga_pixel */
229 t = UCH(source->tga_pixel[0]);
230 t += UCH(source->tga_pixel[1]) << 8;
231 /* We expand 5 bit data to 8 bit sample width.
232 * The format of the 16-bit (LSB first) input word is
233 * xRRRRRGGGGGBBBBB
234 */
235 ptr[2] = (JSAMPLE) c5to8bits[t & 0x1F];
236 t >>= 5;
237 ptr[1] = (JSAMPLE) c5to8bits[t & 0x1F];
238 t >>= 5;
239 ptr[0] = (JSAMPLE) c5to8bits[t & 0x1F];
240 ptr += 3;
241 }
242 return 1;
243 }
244
245 METHODDEF(JDIMENSION)
246 get_24bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
247 /* This version is for reading 24-bit pixels */
248 {
249 tga_source_ptr source = (tga_source_ptr) sinfo;
250 register JSAMPROW ptr;
251 register JDIMENSION col;
252
253 ptr = source->pub.buffer[0];
254 for (col = cinfo->image_width; col > 0; col--) {
255 (*source->read_pixel) (source); /* Load next pixel into tga_pixel */
256 *ptr++ = (JSAMPLE) UCH(source->tga_pixel[2]); /* change BGR to RGB order */
257 *ptr++ = (JSAMPLE) UCH(source->tga_pixel[1]);
258 *ptr++ = (JSAMPLE) UCH(source->tga_pixel[0]);
259 }
260 return 1;
261 }
262
263 /*
264 * Targa also defines a 32-bit pixel format with order B,G,R,A.
265 * We presently ignore the attribute byte, so the code for reading
266 * these pixels is identical to the 24-bit routine above.
267 * This works because the actual pixel length is only known to read_pixel.
268 */
269
270 #define get_32bit_row get_24bit_row
271
272
273 /*
274 * This method is for re-reading the input data in standard top-down
275 * row order. The entire image has already been read into whole_image
276 * with proper conversion of pixel format, but it's in a funny row order.
277 */
278
279 METHODDEF(JDIMENSION)
280 get_memory_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
281 {
282 tga_source_ptr source = (tga_source_ptr) sinfo;
283 JDIMENSION source_row;
284
285 /* Compute row of source that maps to current_row of normal order */
286 /* For now, assume image is bottom-up and not interlaced. */
287 /* NEEDS WORK to support interlaced images! */
288 source_row = cinfo->image_height - source->current_row - 1;
289
290 /* Fetch that row from virtual array */
291 source->pub.buffer = (*cinfo->mem->access_virt_sarray)
292 ((j_common_ptr) cinfo, source->whole_image,
293 source_row, (JDIMENSION) 1, FALSE);
294
295 source->current_row++;
296 return 1;
297 }
298
299
300 /*
301 * This method loads the image into whole_image during the first call on
302 * get_pixel_rows. The get_pixel_rows pointer is then adjusted to call
303 * get_memory_row on subsequent calls.
304 */
305
306 METHODDEF(JDIMENSION)
307 preload_image (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
308 {
309 tga_source_ptr source = (tga_source_ptr) sinfo;
310 JDIMENSION row;
311 cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
312
313 /* Read the data into a virtual array in input-file row order. */
314 for (row = 0; row < cinfo->image_height; row++) {
315 if (progress != NULL) {
316 progress->pub.pass_counter = (long) row;
317 progress->pub.pass_limit = (long) cinfo->image_height;
318 (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
319 }
320 source->pub.buffer = (*cinfo->mem->access_virt_sarray)
321 ((j_common_ptr) cinfo, source->whole_image, row, (JDIMENSION) 1, TRUE);
322 (*source->get_pixel_rows) (cinfo, sinfo);
323 }
324 if (progress != NULL)
325 progress->completed_extra_passes++;
326
327 /* Set up to read from the virtual array in unscrambled order */
328 source->pub.get_pixel_rows = get_memory_row;
329 source->current_row = 0;
330 /* And read the first row */
331 return get_memory_row(cinfo, sinfo);
332 }
333
334
335 /*
336 * Read the file header; return image size and component count.
337 */
338
339 METHODDEF(void)
340 start_input_tga (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
341 {
342 tga_source_ptr source = (tga_source_ptr) sinfo;
343 U_CHAR targaheader[18];
344 int idlen, cmaptype, subtype, flags, interlace_type, components;
345 unsigned int width, height, maplen;
346 boolean is_bottom_up;
347
348 #define GET_2B(offset) ((unsigned int) UCH(targaheader[offset]) + \
349 (((unsigned int) UCH(targaheader[offset+1])) << 8))
350
351 if (! ReadOK(source->pub.input_file, targaheader, 18))
352 ERREXIT(cinfo, JERR_INPUT_EOF);
353
354 /* Pretend "15-bit" pixels are 16-bit --- we ignore attribute bit anyway */
355 if (targaheader[16] == 15)
356 targaheader[16] = 16;
357
358 idlen = UCH(targaheader[0]);
359 cmaptype = UCH(targaheader[1]);
360 subtype = UCH(targaheader[2]);
361 maplen = GET_2B(5);
362 width = GET_2B(12);
363 height = GET_2B(14);
364 source->pixel_size = UCH(targaheader[16]) >> 3;
365 flags = UCH(targaheader[17]); /* Image Descriptor byte */
366
367 is_bottom_up = ((flags & 0x20) == 0); /* bit 5 set => top-down */
368 interlace_type = flags >> 6; /* bits 6/7 are interlace code */
369
370 if (cmaptype > 1 || /* cmaptype must be 0 or 1 */
371 width <= 0 || height <= 0 ||
372 source->pixel_size < 1 || source->pixel_size > 4 ||
373 (UCH(targaheader[16]) & 7) != 0 || /* bits/pixel must be multiple of 8 */
374 interlace_type != 0) /* currently don't allow interlaced image */
375 ERREXIT(cinfo, JERR_TGA_BADPARMS);
376
377 if (subtype > 8) {
378 /* It's an RLE-coded file */
379 source->read_pixel = read_rle_pixel;
380 source->block_count = source->dup_pixel_count = 0;
381 subtype -= 8;
382 } else {
383 /* Non-RLE file */
384 source->read_pixel = read_non_rle_pixel;
385 }
386
387 /* Now should have subtype 1, 2, or 3 */
388 components = 3; /* until proven different */
389 cinfo->in_color_space = JCS_RGB;
390
391 switch (subtype) {
392 case 1: /* Colormapped image */
393 if (source->pixel_size == 1 && cmaptype == 1)
394 source->get_pixel_rows = get_8bit_row;
395 else
396 ERREXIT(cinfo, JERR_TGA_BADPARMS);
397 TRACEMS2(cinfo, 1, JTRC_TGA_MAPPED, width, height);
398 break;
399 case 2: /* RGB image */
400 switch (source->pixel_size) {
401 case 2:
402 source->get_pixel_rows = get_16bit_row;
403 break;
404 case 3:
405 source->get_pixel_rows = get_24bit_row;
406 break;
407 case 4:
408 source->get_pixel_rows = get_32bit_row;
409 break;
410 default:
411 ERREXIT(cinfo, JERR_TGA_BADPARMS);
412 break;
413 }
414 TRACEMS2(cinfo, 1, JTRC_TGA, width, height);
415 break;
416 case 3: /* Grayscale image */
417 components = 1;
418 cinfo->in_color_space = JCS_GRAYSCALE;
419 if (source->pixel_size == 1)
420 source->get_pixel_rows = get_8bit_gray_row;
421 else
422 ERREXIT(cinfo, JERR_TGA_BADPARMS);
423 TRACEMS2(cinfo, 1, JTRC_TGA_GRAY, width, height);
424 break;
425 default:
426 ERREXIT(cinfo, JERR_TGA_BADPARMS);
427 break;
428 }
429
430 if (is_bottom_up) {
431 /* Create a virtual array to buffer the upside-down image. */
432 source->whole_image = (*cinfo->mem->request_virt_sarray)
433 ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
434 (JDIMENSION) width * components, (JDIMENSION) height, (JDIMENSION) 1);
435 if (cinfo->progress != NULL) {
436 cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
437 progress->total_extra_passes++; /* count file input as separate pass */
438 }
439 /* source->pub.buffer will point to the virtual array. */
440 source->pub.buffer_height = 1; /* in case anyone looks at it */
441 source->pub.get_pixel_rows = preload_image;
442 } else {
443 /* Don't need a virtual array, but do need a one-row input buffer. */
444 source->whole_image = NULL;
445 source->pub.buffer = (*cinfo->mem->alloc_sarray)
446 ((j_common_ptr) cinfo, JPOOL_IMAGE,
447 (JDIMENSION) width * components, (JDIMENSION) 1);
448 source->pub.buffer_height = 1;
449 source->pub.get_pixel_rows = source->get_pixel_rows;
450 }
451
452 while (idlen--) /* Throw away ID field */
453 (void) read_byte(source);
454
455 if (maplen > 0) {
456 if (maplen > 256 || GET_2B(3) != 0)
457 ERREXIT(cinfo, JERR_TGA_BADCMAP);
458 /* Allocate space to store the colormap */
459 source->colormap = (*cinfo->mem->alloc_sarray)
460 ((j_common_ptr) cinfo, JPOOL_IMAGE, (JDIMENSION) maplen, (JDIMENSION) 3);
461 source->cmap_length = (int) maplen;
462 /* and read it from the file */
463 read_colormap(source, (int) maplen, UCH(targaheader[7]));
464 } else {
465 if (cmaptype) /* but you promised a cmap! */
466 ERREXIT(cinfo, JERR_TGA_BADPARMS);
467 source->colormap = NULL;
468 source->cmap_length = 0;
469 }
470
471 cinfo->input_components = components;
472 cinfo->data_precision = 8;
473 cinfo->image_width = width;
474 cinfo->image_height = height;
475 }
476
477
478 /*
479 * Finish up at the end of the file.
480 */
481
482 METHODDEF(void)
483 finish_input_tga (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
484 {
485 /* no work */
486 }
487
488
489 /*
490 * The module selection routine for Targa format input.
491 */
492
493 GLOBAL(cjpeg_source_ptr)
494 jinit_read_targa (j_compress_ptr cinfo)
495 {
496 tga_source_ptr source;
497
498 /* Create module interface object */
499 source = (tga_source_ptr)
500 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
501 SIZEOF(tga_source_struct));
502 source->cinfo = cinfo; /* make back link for subroutines */
503 /* Fill in method ptrs, except get_pixel_rows which start_input sets */
504 source->pub.start_input = start_input_tga;
505 source->pub.finish_input = finish_input_tga;
506
507 return &source->pub;
508 }
509
510 #endif /* TARGA_SUPPORTED */