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