/src/libjpeg-turbo.2.0.x/rdbmp.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * rdbmp.c |
3 | | * |
4 | | * This file was part of the Independent JPEG Group's software: |
5 | | * Copyright (C) 1994-1996, Thomas G. Lane. |
6 | | * Modified 2009-2017 by Guido Vollbeding. |
7 | | * libjpeg-turbo Modifications: |
8 | | * Modified 2011 by Siarhei Siamashka. |
9 | | * Copyright (C) 2015, 2017-2018, 2021, D. R. Commander. |
10 | | * For conditions of distribution and use, see the accompanying README.ijg |
11 | | * file. |
12 | | * |
13 | | * This file contains routines to read input images in Microsoft "BMP" |
14 | | * format (MS Windows 3.x, OS/2 1.x, and OS/2 2.x flavors). |
15 | | * Currently, only 8-bit and 24-bit images are supported, not 1-bit or |
16 | | * 4-bit (feeding such low-depth images into JPEG would be silly anyway). |
17 | | * Also, we don't support RLE-compressed files. |
18 | | * |
19 | | * These routines may need modification for non-Unix environments or |
20 | | * specialized applications. As they stand, they assume input from |
21 | | * an ordinary stdio stream. They further assume that reading begins |
22 | | * at the start of the file; start_input may need work if the |
23 | | * user interface has already read some data (e.g., to determine that |
24 | | * the file is indeed BMP format). |
25 | | * |
26 | | * This code contributed by James Arthur Boucher. |
27 | | */ |
28 | | |
29 | | #include "cmyk.h" |
30 | | #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ |
31 | | |
32 | | #ifdef BMP_SUPPORTED |
33 | | |
34 | | |
35 | | /* Macros to deal with unsigned chars as efficiently as compiler allows */ |
36 | | |
37 | | #ifdef HAVE_UNSIGNED_CHAR |
38 | | typedef unsigned char U_CHAR; |
39 | 40.8k | #define UCH(x) ((int)(x)) |
40 | | #else /* !HAVE_UNSIGNED_CHAR */ |
41 | | #ifdef __CHAR_UNSIGNED__ |
42 | | typedef char U_CHAR; |
43 | | #define UCH(x) ((int)(x)) |
44 | | #else |
45 | | typedef char U_CHAR; |
46 | | #define UCH(x) ((int)(x) & 0xFF) |
47 | | #endif |
48 | | #endif /* HAVE_UNSIGNED_CHAR */ |
49 | | |
50 | | |
51 | | #define ReadOK(file, buffer, len) \ |
52 | 4.38k | (JFREAD(file, buffer, len) == ((size_t)(len))) |
53 | | |
54 | | static int alpha_index[JPEG_NUMCS] = { |
55 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1 |
56 | | }; |
57 | | |
58 | | |
59 | | /* Private version of data source object */ |
60 | | |
61 | | typedef struct _bmp_source_struct *bmp_source_ptr; |
62 | | |
63 | | typedef struct _bmp_source_struct { |
64 | | struct cjpeg_source_struct pub; /* public fields */ |
65 | | |
66 | | j_compress_ptr cinfo; /* back link saves passing separate parm */ |
67 | | |
68 | | JSAMPARRAY colormap; /* BMP colormap (converted to my format) */ |
69 | | |
70 | | jvirt_sarray_ptr whole_image; /* Needed to reverse row order */ |
71 | | JDIMENSION source_row; /* Current source row number */ |
72 | | JDIMENSION row_width; /* Physical width of scanlines in file */ |
73 | | |
74 | | int bits_per_pixel; /* remembers 8- or 24-bit format */ |
75 | | int cmap_length; /* colormap length */ |
76 | | |
77 | | boolean use_inversion_array; /* TRUE = preload the whole image, which is |
78 | | stored in bottom-up order, and feed it to |
79 | | the calling program in top-down order |
80 | | |
81 | | FALSE = the calling program will maintain |
82 | | its own image buffer and read the rows in |
83 | | bottom-up order */ |
84 | | |
85 | | U_CHAR *iobuffer; /* I/O buffer (used to buffer a single row from |
86 | | disk if use_inversion_array == FALSE) */ |
87 | | } bmp_source_struct; |
88 | | |
89 | | |
90 | | LOCAL(int) |
91 | | read_byte(bmp_source_ptr sinfo) |
92 | | /* Read next byte from BMP file */ |
93 | 37.2k | { |
94 | 37.2k | register FILE *infile = sinfo->pub.input_file; |
95 | 37.2k | register int c; |
96 | | |
97 | 37.2k | if ((c = getc(infile)) == EOF) |
98 | 444 | ERREXIT(sinfo->cinfo, JERR_INPUT_EOF); |
99 | 37.2k | return c; |
100 | 37.2k | } |
101 | | |
102 | | |
103 | | LOCAL(void) |
104 | | read_colormap(bmp_source_ptr sinfo, int cmaplen, int mapentrysize) |
105 | | /* Read the colormap from a BMP file */ |
106 | 470 | { |
107 | 470 | int i, gray = 1; |
108 | | |
109 | 470 | switch (mapentrysize) { |
110 | 134 | case 3: |
111 | | /* BGR format (occurs in OS/2 files) */ |
112 | 1.74k | for (i = 0; i < cmaplen; i++) { |
113 | 1.61k | sinfo->colormap[2][i] = (JSAMPLE)read_byte(sinfo); |
114 | 1.61k | sinfo->colormap[1][i] = (JSAMPLE)read_byte(sinfo); |
115 | 1.61k | sinfo->colormap[0][i] = (JSAMPLE)read_byte(sinfo); |
116 | 1.61k | if (sinfo->colormap[2][i] != sinfo->colormap[1][i] || |
117 | 1.61k | sinfo->colormap[1][i] != sinfo->colormap[0][i]) |
118 | 828 | gray = 0; |
119 | 1.61k | } |
120 | 134 | break; |
121 | 336 | case 4: |
122 | | /* BGR0 format (occurs in MS Windows files) */ |
123 | 2.39k | for (i = 0; i < cmaplen; i++) { |
124 | 2.06k | sinfo->colormap[2][i] = (JSAMPLE)read_byte(sinfo); |
125 | 2.06k | sinfo->colormap[1][i] = (JSAMPLE)read_byte(sinfo); |
126 | 2.06k | sinfo->colormap[0][i] = (JSAMPLE)read_byte(sinfo); |
127 | 2.06k | (void)read_byte(sinfo); |
128 | 2.06k | if (sinfo->colormap[2][i] != sinfo->colormap[1][i] || |
129 | 2.06k | sinfo->colormap[1][i] != sinfo->colormap[0][i]) |
130 | 790 | gray = 0; |
131 | 2.06k | } |
132 | 336 | break; |
133 | 0 | default: |
134 | 0 | ERREXIT(sinfo->cinfo, JERR_BMP_BADCMAP); |
135 | 0 | break; |
136 | 470 | } |
137 | | |
138 | 114 | if ((sinfo->cinfo->in_color_space == JCS_UNKNOWN || |
139 | 114 | sinfo->cinfo->in_color_space == JCS_RGB) && gray) |
140 | 60 | sinfo->cinfo->in_color_space = JCS_GRAYSCALE; |
141 | | |
142 | 114 | if (sinfo->cinfo->in_color_space == JCS_GRAYSCALE && !gray) |
143 | 0 | ERREXIT(sinfo->cinfo, JERR_BAD_IN_COLORSPACE); |
144 | 114 | } |
145 | | |
146 | | |
147 | | /* |
148 | | * Read one row of pixels. |
149 | | * The image has been read into the whole_image array, but is otherwise |
150 | | * unprocessed. We must read it out in top-to-bottom row order, and if |
151 | | * it is an 8-bit image, we must expand colormapped pixels to 24bit format. |
152 | | */ |
153 | | |
154 | | METHODDEF(JDIMENSION) |
155 | | get_8bit_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
156 | | /* This version is for reading 8-bit colormap indexes */ |
157 | 509 | { |
158 | 509 | bmp_source_ptr source = (bmp_source_ptr)sinfo; |
159 | 509 | register JSAMPARRAY colormap = source->colormap; |
160 | 509 | int cmaplen = source->cmap_length; |
161 | 509 | JSAMPARRAY image_ptr; |
162 | 509 | register int t; |
163 | 509 | register JSAMPROW inptr, outptr; |
164 | 509 | register JDIMENSION col; |
165 | | |
166 | 509 | if (source->use_inversion_array) { |
167 | | /* Fetch next row from virtual array */ |
168 | 509 | source->source_row--; |
169 | 509 | image_ptr = (*cinfo->mem->access_virt_sarray) |
170 | 509 | ((j_common_ptr)cinfo, source->whole_image, |
171 | 509 | source->source_row, (JDIMENSION)1, FALSE); |
172 | 509 | inptr = image_ptr[0]; |
173 | 509 | } else { |
174 | 0 | if (!ReadOK(source->pub.input_file, source->iobuffer, source->row_width)) |
175 | 0 | ERREXIT(cinfo, JERR_INPUT_EOF); |
176 | 0 | inptr = source->iobuffer; |
177 | 0 | } |
178 | | |
179 | | /* Expand the colormap indexes to real data */ |
180 | 509 | outptr = source->pub.buffer[0]; |
181 | 509 | if (cinfo->in_color_space == JCS_GRAYSCALE) { |
182 | 1.43k | for (col = cinfo->image_width; col > 0; col--) { |
183 | 1.16k | t = GETJSAMPLE(*inptr++); |
184 | 1.16k | if (t >= cmaplen) |
185 | 11 | ERREXIT(cinfo, JERR_BMP_OUTOFRANGE); |
186 | 1.16k | *outptr++ = colormap[0][t]; |
187 | 1.16k | } |
188 | 269 | } else if (cinfo->in_color_space == JCS_CMYK) { |
189 | 0 | for (col = cinfo->image_width; col > 0; col--) { |
190 | 0 | t = GETJSAMPLE(*inptr++); |
191 | 0 | if (t >= cmaplen) |
192 | 0 | ERREXIT(cinfo, JERR_BMP_OUTOFRANGE); |
193 | 0 | rgb_to_cmyk(colormap[0][t], colormap[1][t], colormap[2][t], outptr, |
194 | 0 | outptr + 1, outptr + 2, outptr + 3); |
195 | 0 | outptr += 4; |
196 | 0 | } |
197 | 240 | } else { |
198 | 240 | register int rindex = rgb_red[cinfo->in_color_space]; |
199 | 240 | register int gindex = rgb_green[cinfo->in_color_space]; |
200 | 240 | register int bindex = rgb_blue[cinfo->in_color_space]; |
201 | 240 | register int aindex = alpha_index[cinfo->in_color_space]; |
202 | 240 | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
203 | | |
204 | 240 | if (aindex >= 0) { |
205 | 0 | for (col = cinfo->image_width; col > 0; col--) { |
206 | 0 | t = GETJSAMPLE(*inptr++); |
207 | 0 | if (t >= cmaplen) |
208 | 0 | ERREXIT(cinfo, JERR_BMP_OUTOFRANGE); |
209 | 0 | outptr[rindex] = colormap[0][t]; |
210 | 0 | outptr[gindex] = colormap[1][t]; |
211 | 0 | outptr[bindex] = colormap[2][t]; |
212 | 0 | outptr[aindex] = 0xFF; |
213 | 0 | outptr += ps; |
214 | 0 | } |
215 | 240 | } else { |
216 | 1.07k | for (col = cinfo->image_width; col > 0; col--) { |
217 | 838 | t = GETJSAMPLE(*inptr++); |
218 | 838 | if (t >= cmaplen) |
219 | 32 | ERREXIT(cinfo, JERR_BMP_OUTOFRANGE); |
220 | 838 | outptr[rindex] = colormap[0][t]; |
221 | 838 | outptr[gindex] = colormap[1][t]; |
222 | 838 | outptr[bindex] = colormap[2][t]; |
223 | 838 | outptr += ps; |
224 | 838 | } |
225 | 240 | } |
226 | 240 | } |
227 | | |
228 | 509 | return 1; |
229 | 509 | } |
230 | | |
231 | | |
232 | | METHODDEF(JDIMENSION) |
233 | | get_24bit_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
234 | | /* This version is for reading 24-bit pixels */ |
235 | 228 | { |
236 | 228 | bmp_source_ptr source = (bmp_source_ptr)sinfo; |
237 | 228 | JSAMPARRAY image_ptr; |
238 | 228 | register JSAMPROW inptr, outptr; |
239 | 228 | register JDIMENSION col; |
240 | | |
241 | 228 | if (source->use_inversion_array) { |
242 | | /* Fetch next row from virtual array */ |
243 | 228 | source->source_row--; |
244 | 228 | image_ptr = (*cinfo->mem->access_virt_sarray) |
245 | 228 | ((j_common_ptr)cinfo, source->whole_image, |
246 | 228 | source->source_row, (JDIMENSION)1, FALSE); |
247 | 228 | inptr = image_ptr[0]; |
248 | 228 | } else { |
249 | 0 | if (!ReadOK(source->pub.input_file, source->iobuffer, source->row_width)) |
250 | 0 | ERREXIT(cinfo, JERR_INPUT_EOF); |
251 | 0 | inptr = source->iobuffer; |
252 | 0 | } |
253 | | |
254 | | /* Transfer data. Note source values are in BGR order |
255 | | * (even though Microsoft's own documents say the opposite). |
256 | | */ |
257 | 228 | outptr = source->pub.buffer[0]; |
258 | 228 | if (cinfo->in_color_space == JCS_EXT_BGR) { |
259 | 0 | MEMCOPY(outptr, inptr, source->row_width); |
260 | 228 | } else if (cinfo->in_color_space == JCS_CMYK) { |
261 | 0 | for (col = cinfo->image_width; col > 0; col--) { |
262 | | /* can omit GETJSAMPLE() safely */ |
263 | 0 | JSAMPLE b = *inptr++, g = *inptr++, r = *inptr++; |
264 | 0 | rgb_to_cmyk(r, g, b, outptr, outptr + 1, outptr + 2, outptr + 3); |
265 | 0 | outptr += 4; |
266 | 0 | } |
267 | 228 | } else { |
268 | 228 | register int rindex = rgb_red[cinfo->in_color_space]; |
269 | 228 | register int gindex = rgb_green[cinfo->in_color_space]; |
270 | 228 | register int bindex = rgb_blue[cinfo->in_color_space]; |
271 | 228 | register int aindex = alpha_index[cinfo->in_color_space]; |
272 | 228 | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
273 | | |
274 | 228 | if (aindex >= 0) { |
275 | 0 | for (col = cinfo->image_width; col > 0; col--) { |
276 | 0 | outptr[bindex] = *inptr++; /* can omit GETJSAMPLE() safely */ |
277 | 0 | outptr[gindex] = *inptr++; |
278 | 0 | outptr[rindex] = *inptr++; |
279 | 0 | outptr[aindex] = 0xFF; |
280 | 0 | outptr += ps; |
281 | 0 | } |
282 | 228 | } else { |
283 | 130k | for (col = cinfo->image_width; col > 0; col--) { |
284 | 129k | outptr[bindex] = *inptr++; /* can omit GETJSAMPLE() safely */ |
285 | 129k | outptr[gindex] = *inptr++; |
286 | 129k | outptr[rindex] = *inptr++; |
287 | 129k | outptr += ps; |
288 | 129k | } |
289 | 228 | } |
290 | 228 | } |
291 | | |
292 | 228 | return 1; |
293 | 228 | } |
294 | | |
295 | | |
296 | | METHODDEF(JDIMENSION) |
297 | | get_32bit_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
298 | | /* This version is for reading 32-bit pixels */ |
299 | 230 | { |
300 | 230 | bmp_source_ptr source = (bmp_source_ptr)sinfo; |
301 | 230 | JSAMPARRAY image_ptr; |
302 | 230 | register JSAMPROW inptr, outptr; |
303 | 230 | register JDIMENSION col; |
304 | | |
305 | 230 | if (source->use_inversion_array) { |
306 | | /* Fetch next row from virtual array */ |
307 | 230 | source->source_row--; |
308 | 230 | image_ptr = (*cinfo->mem->access_virt_sarray) |
309 | 230 | ((j_common_ptr)cinfo, source->whole_image, |
310 | 230 | source->source_row, (JDIMENSION)1, FALSE); |
311 | 230 | inptr = image_ptr[0]; |
312 | 230 | } else { |
313 | 0 | if (!ReadOK(source->pub.input_file, source->iobuffer, source->row_width)) |
314 | 0 | ERREXIT(cinfo, JERR_INPUT_EOF); |
315 | 0 | inptr = source->iobuffer; |
316 | 0 | } |
317 | | |
318 | | /* Transfer data. Note source values are in BGR order |
319 | | * (even though Microsoft's own documents say the opposite). |
320 | | */ |
321 | 230 | outptr = source->pub.buffer[0]; |
322 | 230 | if (cinfo->in_color_space == JCS_EXT_BGRX || |
323 | 230 | cinfo->in_color_space == JCS_EXT_BGRA) { |
324 | 0 | MEMCOPY(outptr, inptr, source->row_width); |
325 | 230 | } else if (cinfo->in_color_space == JCS_CMYK) { |
326 | 0 | for (col = cinfo->image_width; col > 0; col--) { |
327 | | /* can omit GETJSAMPLE() safely */ |
328 | 0 | JSAMPLE b = *inptr++, g = *inptr++, r = *inptr++; |
329 | 0 | rgb_to_cmyk(r, g, b, outptr, outptr + 1, outptr + 2, outptr + 3); |
330 | 0 | inptr++; /* skip the 4th byte (Alpha channel) */ |
331 | 0 | outptr += 4; |
332 | 0 | } |
333 | 230 | } else { |
334 | 230 | register int rindex = rgb_red[cinfo->in_color_space]; |
335 | 230 | register int gindex = rgb_green[cinfo->in_color_space]; |
336 | 230 | register int bindex = rgb_blue[cinfo->in_color_space]; |
337 | 230 | register int aindex = alpha_index[cinfo->in_color_space]; |
338 | 230 | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
339 | | |
340 | 230 | if (aindex >= 0) { |
341 | 0 | for (col = cinfo->image_width; col > 0; col--) { |
342 | 0 | outptr[bindex] = *inptr++; /* can omit GETJSAMPLE() safely */ |
343 | 0 | outptr[gindex] = *inptr++; |
344 | 0 | outptr[rindex] = *inptr++; |
345 | 0 | outptr[aindex] = *inptr++; |
346 | 0 | outptr += ps; |
347 | 0 | } |
348 | 230 | } else { |
349 | 261k | for (col = cinfo->image_width; col > 0; col--) { |
350 | 261k | outptr[bindex] = *inptr++; /* can omit GETJSAMPLE() safely */ |
351 | 261k | outptr[gindex] = *inptr++; |
352 | 261k | outptr[rindex] = *inptr++; |
353 | 261k | inptr++; /* skip the 4th byte (Alpha channel) */ |
354 | 261k | outptr += ps; |
355 | 261k | } |
356 | 230 | } |
357 | 230 | } |
358 | | |
359 | 230 | return 1; |
360 | 230 | } |
361 | | |
362 | | |
363 | | /* |
364 | | * This method loads the image into whole_image during the first call on |
365 | | * get_pixel_rows. The get_pixel_rows pointer is then adjusted to call |
366 | | * get_8bit_row, get_24bit_row, or get_32bit_row on subsequent calls. |
367 | | */ |
368 | | |
369 | | METHODDEF(JDIMENSION) |
370 | | preload_image(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
371 | 260 | { |
372 | 260 | bmp_source_ptr source = (bmp_source_ptr)sinfo; |
373 | 260 | register FILE *infile = source->pub.input_file; |
374 | 260 | register JSAMPROW out_ptr; |
375 | 260 | JSAMPARRAY image_ptr; |
376 | 260 | JDIMENSION row; |
377 | 260 | cd_progress_ptr progress = (cd_progress_ptr)cinfo->progress; |
378 | | |
379 | | /* Read the data into a virtual array in input-file row order. */ |
380 | 1.83k | for (row = 0; row < cinfo->image_height; row++) { |
381 | 1.57k | if (progress != NULL) { |
382 | 0 | progress->pub.pass_counter = (long)row; |
383 | 0 | progress->pub.pass_limit = (long)cinfo->image_height; |
384 | 0 | (*progress->pub.progress_monitor) ((j_common_ptr)cinfo); |
385 | 0 | } |
386 | 1.57k | image_ptr = (*cinfo->mem->access_virt_sarray) |
387 | 1.57k | ((j_common_ptr)cinfo, source->whole_image, row, (JDIMENSION)1, TRUE); |
388 | 1.57k | out_ptr = image_ptr[0]; |
389 | 1.57k | if (fread(out_ptr, 1, source->row_width, infile) != source->row_width) { |
390 | 98 | if (feof(infile)) |
391 | 98 | ERREXIT(cinfo, JERR_INPUT_EOF); |
392 | 0 | else |
393 | 0 | ERREXIT(cinfo, JERR_FILE_READ); |
394 | 98 | } |
395 | 1.57k | } |
396 | 260 | if (progress != NULL) |
397 | 0 | progress->completed_extra_passes++; |
398 | | |
399 | | /* Set up to read from the virtual array in top-to-bottom order */ |
400 | 260 | switch (source->bits_per_pixel) { |
401 | 68 | case 8: |
402 | 68 | source->pub.get_pixel_rows = get_8bit_row; |
403 | 68 | break; |
404 | 48 | case 24: |
405 | 48 | source->pub.get_pixel_rows = get_24bit_row; |
406 | 48 | break; |
407 | 46 | case 32: |
408 | 46 | source->pub.get_pixel_rows = get_32bit_row; |
409 | 46 | break; |
410 | 0 | default: |
411 | 0 | ERREXIT(cinfo, JERR_BMP_BADDEPTH); |
412 | 260 | } |
413 | 162 | source->source_row = cinfo->image_height; |
414 | | |
415 | | /* And read the first row */ |
416 | 162 | return (*source->pub.get_pixel_rows) (cinfo, sinfo); |
417 | 260 | } |
418 | | |
419 | | |
420 | | /* |
421 | | * Read the file header; return image size and component count. |
422 | | */ |
423 | | |
424 | | METHODDEF(void) |
425 | | start_input_bmp(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
426 | 1.46k | { |
427 | 1.46k | bmp_source_ptr source = (bmp_source_ptr)sinfo; |
428 | 1.46k | U_CHAR bmpfileheader[14]; |
429 | 1.46k | U_CHAR bmpinfoheader[64]; |
430 | | |
431 | 1.46k | #define GET_2B(array, offset) \ |
432 | 4.80k | ((unsigned short)UCH(array[offset]) + \ |
433 | 4.80k | (((unsigned short)UCH(array[offset + 1])) << 8)) |
434 | 1.46k | #define GET_4B(array, offset) \ |
435 | 7.80k | ((unsigned int)UCH(array[offset]) + \ |
436 | 7.80k | (((unsigned int)UCH(array[offset + 1])) << 8) + \ |
437 | 7.80k | (((unsigned int)UCH(array[offset + 2])) << 16) + \ |
438 | 7.80k | (((unsigned int)UCH(array[offset + 3])) << 24)) |
439 | | |
440 | 1.46k | int bfOffBits; |
441 | 1.46k | int headerSize; |
442 | 1.46k | int biWidth; |
443 | 1.46k | int biHeight; |
444 | 1.46k | unsigned short biPlanes; |
445 | 1.46k | unsigned int biCompression; |
446 | 1.46k | int biXPelsPerMeter, biYPelsPerMeter; |
447 | 1.46k | int biClrUsed = 0; |
448 | 1.46k | int mapentrysize = 0; /* 0 indicates no colormap */ |
449 | 1.46k | int bPad; |
450 | 1.46k | JDIMENSION row_width = 0; |
451 | | |
452 | | /* Read and verify the bitmap file header */ |
453 | 1.46k | if (!ReadOK(source->pub.input_file, bmpfileheader, 14)) |
454 | 14 | ERREXIT(cinfo, JERR_INPUT_EOF); |
455 | 1.46k | if (GET_2B(bmpfileheader, 0) != 0x4D42) /* 'BM' */ |
456 | 20 | ERREXIT(cinfo, JERR_BMP_NOT); |
457 | 1.46k | bfOffBits = GET_4B(bmpfileheader, 10); |
458 | | /* We ignore the remaining fileheader fields */ |
459 | | |
460 | | /* The infoheader might be 12 bytes (OS/2 1.x), 40 bytes (Windows), |
461 | | * or 64 bytes (OS/2 2.x). Check the first 4 bytes to find out which. |
462 | | */ |
463 | 1.46k | if (!ReadOK(source->pub.input_file, bmpinfoheader, 4)) |
464 | 4 | ERREXIT(cinfo, JERR_INPUT_EOF); |
465 | 1.46k | headerSize = GET_4B(bmpinfoheader, 0); |
466 | 1.46k | if (headerSize < 12 || headerSize > 64 || (headerSize + 14) > bfOffBits) |
467 | 162 | ERREXIT(cinfo, JERR_BMP_BADHEADER); |
468 | 1.46k | if (!ReadOK(source->pub.input_file, bmpinfoheader + 4, headerSize - 4)) |
469 | 18 | ERREXIT(cinfo, JERR_INPUT_EOF); |
470 | | |
471 | 1.46k | switch (headerSize) { |
472 | 428 | case 12: |
473 | | /* Decode OS/2 1.x header (Microsoft calls this a BITMAPCOREHEADER) */ |
474 | 428 | biWidth = (int)GET_2B(bmpinfoheader, 4); |
475 | 428 | biHeight = (int)GET_2B(bmpinfoheader, 6); |
476 | 428 | biPlanes = GET_2B(bmpinfoheader, 8); |
477 | 428 | source->bits_per_pixel = (int)GET_2B(bmpinfoheader, 10); |
478 | | |
479 | 428 | switch (source->bits_per_pixel) { |
480 | 144 | case 8: /* colormapped image */ |
481 | 144 | mapentrysize = 3; /* OS/2 uses RGBTRIPLE colormap */ |
482 | 144 | TRACEMS2(cinfo, 1, JTRC_BMP_OS2_MAPPED, biWidth, biHeight); |
483 | 144 | break; |
484 | 282 | case 24: /* RGB image */ |
485 | 282 | TRACEMS2(cinfo, 1, JTRC_BMP_OS2, biWidth, biHeight); |
486 | 282 | break; |
487 | 2 | default: |
488 | 2 | ERREXIT(cinfo, JERR_BMP_BADDEPTH); |
489 | 2 | break; |
490 | 428 | } |
491 | 426 | break; |
492 | 812 | case 40: |
493 | 814 | case 64: |
494 | | /* Decode Windows 3.x header (Microsoft calls this a BITMAPINFOHEADER) */ |
495 | | /* or OS/2 2.x header, which has additional fields that we ignore */ |
496 | 814 | biWidth = (int)GET_4B(bmpinfoheader, 4); |
497 | 814 | biHeight = (int)GET_4B(bmpinfoheader, 8); |
498 | 814 | biPlanes = GET_2B(bmpinfoheader, 12); |
499 | 814 | source->bits_per_pixel = (int)GET_2B(bmpinfoheader, 14); |
500 | 814 | biCompression = GET_4B(bmpinfoheader, 16); |
501 | 814 | biXPelsPerMeter = (int)GET_4B(bmpinfoheader, 24); |
502 | 814 | biYPelsPerMeter = (int)GET_4B(bmpinfoheader, 28); |
503 | 814 | biClrUsed = GET_4B(bmpinfoheader, 32); |
504 | | /* biSizeImage, biClrImportant fields are ignored */ |
505 | | |
506 | 814 | switch (source->bits_per_pixel) { |
507 | 626 | case 8: /* colormapped image */ |
508 | 626 | mapentrysize = 4; /* Windows uses RGBQUAD colormap */ |
509 | 626 | TRACEMS2(cinfo, 1, JTRC_BMP_MAPPED, biWidth, biHeight); |
510 | 626 | break; |
511 | 72 | case 24: /* RGB image */ |
512 | 72 | TRACEMS2(cinfo, 1, JTRC_BMP, biWidth, biHeight); |
513 | 72 | break; |
514 | 112 | case 32: /* RGB image + Alpha channel */ |
515 | 112 | TRACEMS2(cinfo, 1, JTRC_BMP, biWidth, biHeight); |
516 | 112 | break; |
517 | 4 | default: |
518 | 4 | ERREXIT(cinfo, JERR_BMP_BADDEPTH); |
519 | 4 | break; |
520 | 814 | } |
521 | 810 | if (biCompression != 0) |
522 | 66 | ERREXIT(cinfo, JERR_BMP_COMPRESSED); |
523 | | |
524 | 810 | if (biXPelsPerMeter > 0 && biYPelsPerMeter > 0) { |
525 | | /* Set JFIF density parameters from the BMP data */ |
526 | 234 | cinfo->X_density = (UINT16)(biXPelsPerMeter / 100); /* 100 cm per meter */ |
527 | 234 | cinfo->Y_density = (UINT16)(biYPelsPerMeter / 100); |
528 | 234 | cinfo->density_unit = 2; /* dots/cm */ |
529 | 234 | } |
530 | 810 | break; |
531 | 2 | default: |
532 | 2 | ERREXIT(cinfo, JERR_BMP_BADHEADER); |
533 | 2 | return; |
534 | 1.46k | } |
535 | | |
536 | 1.17k | if (biWidth <= 0 || biHeight <= 0) |
537 | 128 | ERREXIT(cinfo, JERR_BMP_EMPTY); |
538 | 1.17k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
539 | 1.17k | if (sinfo->max_pixels && |
540 | 1.17k | (unsigned long long)biWidth * biHeight > sinfo->max_pixels) |
541 | 180 | ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); |
542 | 1.17k | #endif |
543 | 1.17k | if (biPlanes != 1) |
544 | 34 | ERREXIT(cinfo, JERR_BMP_BADPLANES); |
545 | | |
546 | | /* Compute distance to bitmap data --- will adjust for colormap below */ |
547 | 1.17k | bPad = bfOffBits - (headerSize + 14); |
548 | | |
549 | | /* Read the colormap, if any */ |
550 | 1.17k | if (mapentrysize > 0) { |
551 | 522 | if (biClrUsed <= 0) |
552 | 316 | biClrUsed = 256; /* assume it's 256 */ |
553 | 206 | else if (biClrUsed > 256) |
554 | 52 | ERREXIT(cinfo, JERR_BMP_BADCMAP); |
555 | | /* Allocate space to store the colormap */ |
556 | 522 | source->colormap = (*cinfo->mem->alloc_sarray) |
557 | 522 | ((j_common_ptr)cinfo, JPOOL_IMAGE, (JDIMENSION)biClrUsed, (JDIMENSION)3); |
558 | 522 | source->cmap_length = (int)biClrUsed; |
559 | | /* and read it from the file */ |
560 | 522 | read_colormap(source, (int)biClrUsed, mapentrysize); |
561 | | /* account for size of colormap */ |
562 | 522 | bPad -= biClrUsed * mapentrysize; |
563 | 522 | } |
564 | | |
565 | | /* Skip any remaining pad bytes */ |
566 | 1.17k | if (bPad < 0) /* incorrect bfOffBits value? */ |
567 | 8 | ERREXIT(cinfo, JERR_BMP_BADHEADER); |
568 | 25.9k | while (--bPad >= 0) { |
569 | 24.8k | (void)read_byte(source); |
570 | 24.8k | } |
571 | | |
572 | | /* Compute row width in file, including padding to 4-byte boundary */ |
573 | 1.17k | switch (source->bits_per_pixel) { |
574 | 104 | case 8: |
575 | 104 | if (cinfo->in_color_space == JCS_UNKNOWN) |
576 | 0 | cinfo->in_color_space = JCS_EXT_RGB; |
577 | 104 | if (IsExtRGB(cinfo->in_color_space)) |
578 | 48 | cinfo->input_components = rgb_pixelsize[cinfo->in_color_space]; |
579 | 56 | else if (cinfo->in_color_space == JCS_GRAYSCALE) |
580 | 56 | cinfo->input_components = 1; |
581 | 0 | else if (cinfo->in_color_space == JCS_CMYK) |
582 | 0 | cinfo->input_components = 4; |
583 | 0 | else |
584 | 0 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
585 | 104 | row_width = (JDIMENSION)biWidth; |
586 | 104 | break; |
587 | 146 | case 24: |
588 | 146 | if (cinfo->in_color_space == JCS_UNKNOWN) |
589 | 0 | cinfo->in_color_space = JCS_EXT_BGR; |
590 | 146 | if (IsExtRGB(cinfo->in_color_space)) |
591 | 146 | cinfo->input_components = rgb_pixelsize[cinfo->in_color_space]; |
592 | 0 | else if (cinfo->in_color_space == JCS_CMYK) |
593 | 0 | cinfo->input_components = 4; |
594 | 0 | else |
595 | 0 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
596 | 146 | if ((unsigned long long)biWidth * 3ULL > 0xFFFFFFFFULL) |
597 | 0 | ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); |
598 | 146 | row_width = (JDIMENSION)biWidth * 3; |
599 | 146 | break; |
600 | 74 | case 32: |
601 | 74 | if (cinfo->in_color_space == JCS_UNKNOWN) |
602 | 0 | cinfo->in_color_space = JCS_EXT_BGRA; |
603 | 74 | if (IsExtRGB(cinfo->in_color_space)) |
604 | 74 | cinfo->input_components = rgb_pixelsize[cinfo->in_color_space]; |
605 | 0 | else if (cinfo->in_color_space == JCS_CMYK) |
606 | 0 | cinfo->input_components = 4; |
607 | 0 | else |
608 | 0 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
609 | 74 | if ((unsigned long long)biWidth * 4ULL > 0xFFFFFFFFULL) |
610 | 0 | ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); |
611 | 74 | row_width = (JDIMENSION)biWidth * 4; |
612 | 74 | break; |
613 | 0 | default: |
614 | 0 | ERREXIT(cinfo, JERR_BMP_BADDEPTH); |
615 | 1.17k | } |
616 | 684 | while ((row_width & 3) != 0) row_width++; |
617 | 324 | source->row_width = row_width; |
618 | | |
619 | 324 | if (source->use_inversion_array) { |
620 | | /* Allocate space for inversion array, prepare for preload pass */ |
621 | 324 | source->whole_image = (*cinfo->mem->request_virt_sarray) |
622 | 324 | ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE, |
623 | 324 | row_width, (JDIMENSION)biHeight, (JDIMENSION)1); |
624 | 324 | source->pub.get_pixel_rows = preload_image; |
625 | 324 | if (cinfo->progress != NULL) { |
626 | 0 | cd_progress_ptr progress = (cd_progress_ptr)cinfo->progress; |
627 | 0 | progress->total_extra_passes++; /* count file input as separate pass */ |
628 | 0 | } |
629 | 324 | } else { |
630 | 0 | source->iobuffer = (U_CHAR *) |
631 | 0 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, row_width); |
632 | 0 | switch (source->bits_per_pixel) { |
633 | 0 | case 8: |
634 | 0 | source->pub.get_pixel_rows = get_8bit_row; |
635 | 0 | break; |
636 | 0 | case 24: |
637 | 0 | source->pub.get_pixel_rows = get_24bit_row; |
638 | 0 | break; |
639 | 0 | case 32: |
640 | 0 | source->pub.get_pixel_rows = get_32bit_row; |
641 | 0 | break; |
642 | 0 | default: |
643 | 0 | ERREXIT(cinfo, JERR_BMP_BADDEPTH); |
644 | 0 | } |
645 | 0 | } |
646 | | |
647 | | /* Ensure that biWidth * cinfo->input_components doesn't exceed the maximum |
648 | | value of the JDIMENSION type. This is only a danger with BMP files, since |
649 | | their width and height fields are 32-bit integers. */ |
650 | 324 | if ((unsigned long long)biWidth * |
651 | 324 | (unsigned long long)cinfo->input_components > 0xFFFFFFFFULL) |
652 | 0 | ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); |
653 | | /* Allocate one-row buffer for returned data */ |
654 | 324 | source->pub.buffer = (*cinfo->mem->alloc_sarray) |
655 | 324 | ((j_common_ptr)cinfo, JPOOL_IMAGE, |
656 | 324 | (JDIMENSION)biWidth * (JDIMENSION)cinfo->input_components, (JDIMENSION)1); |
657 | 324 | source->pub.buffer_height = 1; |
658 | | |
659 | 324 | cinfo->data_precision = 8; |
660 | 324 | cinfo->image_width = (JDIMENSION)biWidth; |
661 | 324 | cinfo->image_height = (JDIMENSION)biHeight; |
662 | 324 | } |
663 | | |
664 | | |
665 | | /* |
666 | | * Finish up at the end of the file. |
667 | | */ |
668 | | |
669 | | METHODDEF(void) |
670 | | finish_input_bmp(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
671 | 119 | { |
672 | | /* no work */ |
673 | 119 | } |
674 | | |
675 | | |
676 | | /* |
677 | | * The module selection routine for BMP format input. |
678 | | */ |
679 | | |
680 | | GLOBAL(cjpeg_source_ptr) |
681 | | jinit_read_bmp(j_compress_ptr cinfo, boolean use_inversion_array) |
682 | 1.46k | { |
683 | 1.46k | bmp_source_ptr source; |
684 | | |
685 | | /* Create module interface object */ |
686 | 1.46k | source = (bmp_source_ptr) |
687 | 1.46k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
688 | 1.46k | sizeof(bmp_source_struct)); |
689 | 1.46k | source->cinfo = cinfo; /* make back link for subroutines */ |
690 | | /* Fill in method ptrs, except get_pixel_rows which start_input sets */ |
691 | 1.46k | source->pub.start_input = start_input_bmp; |
692 | 1.46k | source->pub.finish_input = finish_input_bmp; |
693 | 1.46k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
694 | 1.46k | source->pub.max_pixels = 0; |
695 | 1.46k | #endif |
696 | | |
697 | 1.46k | source->use_inversion_array = use_inversion_array; |
698 | | |
699 | 1.46k | return (cjpeg_source_ptr)source; |
700 | 1.46k | } |
701 | | |
702 | | #endif /* BMP_SUPPORTED */ |