/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 | 180k | #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.40M | (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 | 1.39M | { |
94 | 1.39M | register FILE *infile = sinfo->pub.input_file; |
95 | 1.39M | register int c; |
96 | | |
97 | 1.39M | if ((c = getc(infile)) == EOF) |
98 | 1.67k | ERREXIT(sinfo->cinfo, JERR_INPUT_EOF); |
99 | 1.39M | return c; |
100 | 1.39M | } |
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 | 2.42k | { |
107 | 2.42k | int i, gray = 1; |
108 | | |
109 | 2.42k | switch (mapentrysize) { |
110 | 777 | case 3: |
111 | | /* BGR format (occurs in OS/2 files) */ |
112 | 84.1k | for (i = 0; i < cmaplen; i++) { |
113 | 83.3k | sinfo->colormap[2][i] = (JSAMPLE)read_byte(sinfo); |
114 | 83.3k | sinfo->colormap[1][i] = (JSAMPLE)read_byte(sinfo); |
115 | 83.3k | sinfo->colormap[0][i] = (JSAMPLE)read_byte(sinfo); |
116 | 83.3k | if (sinfo->colormap[2][i] != sinfo->colormap[1][i] || |
117 | 83.3k | sinfo->colormap[1][i] != sinfo->colormap[0][i]) |
118 | 52.2k | gray = 0; |
119 | 83.3k | } |
120 | 777 | break; |
121 | 1.64k | case 4: |
122 | | /* BGR0 format (occurs in MS Windows files) */ |
123 | 48.8k | for (i = 0; i < cmaplen; i++) { |
124 | 47.1k | sinfo->colormap[2][i] = (JSAMPLE)read_byte(sinfo); |
125 | 47.1k | sinfo->colormap[1][i] = (JSAMPLE)read_byte(sinfo); |
126 | 47.1k | sinfo->colormap[0][i] = (JSAMPLE)read_byte(sinfo); |
127 | 47.1k | (void)read_byte(sinfo); |
128 | 47.1k | if (sinfo->colormap[2][i] != sinfo->colormap[1][i] || |
129 | 47.1k | sinfo->colormap[1][i] != sinfo->colormap[0][i]) |
130 | 31.3k | gray = 0; |
131 | 47.1k | } |
132 | 1.64k | break; |
133 | 0 | default: |
134 | 0 | ERREXIT(sinfo->cinfo, JERR_BMP_BADCMAP); |
135 | 0 | break; |
136 | 2.42k | } |
137 | | |
138 | 1.04k | if ((sinfo->cinfo->in_color_space == JCS_UNKNOWN || |
139 | 1.04k | sinfo->cinfo->in_color_space == JCS_RGB) && gray) |
140 | 0 | sinfo->cinfo->in_color_space = JCS_GRAYSCALE; |
141 | | |
142 | 1.04k | if (sinfo->cinfo->in_color_space == JCS_GRAYSCALE && !gray) |
143 | 94 | ERREXIT(sinfo->cinfo, JERR_BAD_IN_COLORSPACE); |
144 | 1.04k | } |
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 | 3.96M | { |
158 | 3.96M | bmp_source_ptr source = (bmp_source_ptr)sinfo; |
159 | 3.96M | register JSAMPARRAY colormap = source->colormap; |
160 | 3.96M | int cmaplen = source->cmap_length; |
161 | 3.96M | JSAMPARRAY image_ptr; |
162 | 3.96M | register int t; |
163 | 3.96M | register JSAMPROW inptr, outptr; |
164 | 3.96M | register JDIMENSION col; |
165 | | |
166 | 3.96M | if (source->use_inversion_array) { |
167 | | /* Fetch next row from virtual array */ |
168 | 0 | source->source_row--; |
169 | 0 | image_ptr = (*cinfo->mem->access_virt_sarray) |
170 | 0 | ((j_common_ptr)cinfo, source->whole_image, |
171 | 0 | source->source_row, (JDIMENSION)1, FALSE); |
172 | 0 | inptr = image_ptr[0]; |
173 | 3.96M | } else { |
174 | 3.96M | if (!ReadOK(source->pub.input_file, source->iobuffer, source->row_width)) |
175 | 466 | ERREXIT(cinfo, JERR_INPUT_EOF); |
176 | 3.96M | inptr = source->iobuffer; |
177 | 3.96M | } |
178 | | |
179 | | /* Expand the colormap indexes to real data */ |
180 | 3.96M | outptr = source->pub.buffer[0]; |
181 | 3.96M | if (cinfo->in_color_space == JCS_GRAYSCALE) { |
182 | 2.63k | for (col = cinfo->image_width; col > 0; col--) { |
183 | 1.90k | t = GETJSAMPLE(*inptr++); |
184 | 1.90k | if (t >= cmaplen) |
185 | 20 | ERREXIT(cinfo, JERR_BMP_OUTOFRANGE); |
186 | 1.90k | *outptr++ = colormap[0][t]; |
187 | 1.90k | } |
188 | 3.96M | } else if (cinfo->in_color_space == JCS_CMYK) { |
189 | 7.85M | for (col = cinfo->image_width; col > 0; col--) { |
190 | 7.19M | t = GETJSAMPLE(*inptr++); |
191 | 7.19M | if (t >= cmaplen) |
192 | 24 | ERREXIT(cinfo, JERR_BMP_OUTOFRANGE); |
193 | 7.19M | rgb_to_cmyk(colormap[0][t], colormap[1][t], colormap[2][t], outptr, |
194 | 7.19M | outptr + 1, outptr + 2, outptr + 3); |
195 | 7.19M | outptr += 4; |
196 | 7.19M | } |
197 | 3.30M | } else { |
198 | 3.30M | register int rindex = rgb_red[cinfo->in_color_space]; |
199 | 3.30M | register int gindex = rgb_green[cinfo->in_color_space]; |
200 | 3.30M | register int bindex = rgb_blue[cinfo->in_color_space]; |
201 | 3.30M | register int aindex = alpha_index[cinfo->in_color_space]; |
202 | 3.30M | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
203 | | |
204 | 3.30M | if (aindex >= 0) { |
205 | 7.85M | for (col = cinfo->image_width; col > 0; col--) { |
206 | 7.19M | t = GETJSAMPLE(*inptr++); |
207 | 7.19M | if (t >= cmaplen) |
208 | 24 | ERREXIT(cinfo, JERR_BMP_OUTOFRANGE); |
209 | 7.19M | outptr[rindex] = colormap[0][t]; |
210 | 7.19M | outptr[gindex] = colormap[1][t]; |
211 | 7.19M | outptr[bindex] = colormap[2][t]; |
212 | 7.19M | outptr[aindex] = 0xFF; |
213 | 7.19M | outptr += ps; |
214 | 7.19M | } |
215 | 2.64M | } else { |
216 | 31.4M | for (col = cinfo->image_width; col > 0; col--) { |
217 | 28.7M | t = GETJSAMPLE(*inptr++); |
218 | 28.7M | if (t >= cmaplen) |
219 | 96 | ERREXIT(cinfo, JERR_BMP_OUTOFRANGE); |
220 | 28.7M | outptr[rindex] = colormap[0][t]; |
221 | 28.7M | outptr[gindex] = colormap[1][t]; |
222 | 28.7M | outptr[bindex] = colormap[2][t]; |
223 | 28.7M | outptr += ps; |
224 | 28.7M | } |
225 | 2.64M | } |
226 | 3.30M | } |
227 | | |
228 | 3.96M | return 1; |
229 | 3.96M | } |
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 | 7.51k | { |
236 | 7.51k | bmp_source_ptr source = (bmp_source_ptr)sinfo; |
237 | 7.51k | JSAMPARRAY image_ptr; |
238 | 7.51k | register JSAMPROW inptr, outptr; |
239 | 7.51k | register JDIMENSION col; |
240 | | |
241 | 7.51k | if (source->use_inversion_array) { |
242 | | /* Fetch next row from virtual array */ |
243 | 0 | source->source_row--; |
244 | 0 | image_ptr = (*cinfo->mem->access_virt_sarray) |
245 | 0 | ((j_common_ptr)cinfo, source->whole_image, |
246 | 0 | source->source_row, (JDIMENSION)1, FALSE); |
247 | 0 | inptr = image_ptr[0]; |
248 | 7.51k | } else { |
249 | 7.51k | if (!ReadOK(source->pub.input_file, source->iobuffer, source->row_width)) |
250 | 570 | ERREXIT(cinfo, JERR_INPUT_EOF); |
251 | 7.51k | inptr = source->iobuffer; |
252 | 7.51k | } |
253 | | |
254 | | /* Transfer data. Note source values are in BGR order |
255 | | * (even though Microsoft's own documents say the opposite). |
256 | | */ |
257 | 7.51k | outptr = source->pub.buffer[0]; |
258 | 7.51k | if (cinfo->in_color_space == JCS_EXT_BGR) { |
259 | 1.15k | MEMCOPY(outptr, inptr, source->row_width); |
260 | 6.35k | } else if (cinfo->in_color_space == JCS_CMYK) { |
261 | 145k | for (col = cinfo->image_width; col > 0; col--) { |
262 | | /* can omit GETJSAMPLE() safely */ |
263 | 144k | JSAMPLE b = *inptr++, g = *inptr++, r = *inptr++; |
264 | 144k | rgb_to_cmyk(r, g, b, outptr, outptr + 1, outptr + 2, outptr + 3); |
265 | 144k | outptr += 4; |
266 | 144k | } |
267 | 5.19k | } else { |
268 | 5.19k | register int rindex = rgb_red[cinfo->in_color_space]; |
269 | 5.19k | register int gindex = rgb_green[cinfo->in_color_space]; |
270 | 5.19k | register int bindex = rgb_blue[cinfo->in_color_space]; |
271 | 5.19k | register int aindex = alpha_index[cinfo->in_color_space]; |
272 | 5.19k | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
273 | | |
274 | 5.19k | if (aindex >= 0) { |
275 | 145k | for (col = cinfo->image_width; col > 0; col--) { |
276 | 144k | outptr[bindex] = *inptr++; /* can omit GETJSAMPLE() safely */ |
277 | 144k | outptr[gindex] = *inptr++; |
278 | 144k | outptr[rindex] = *inptr++; |
279 | 144k | outptr[aindex] = 0xFF; |
280 | 144k | outptr += ps; |
281 | 144k | } |
282 | 4.04k | } else { |
283 | 437k | for (col = cinfo->image_width; col > 0; col--) { |
284 | 433k | outptr[bindex] = *inptr++; /* can omit GETJSAMPLE() safely */ |
285 | 433k | outptr[gindex] = *inptr++; |
286 | 433k | outptr[rindex] = *inptr++; |
287 | 433k | outptr += ps; |
288 | 433k | } |
289 | 4.04k | } |
290 | 5.19k | } |
291 | | |
292 | 7.51k | return 1; |
293 | 7.51k | } |
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 | 412k | { |
300 | 412k | bmp_source_ptr source = (bmp_source_ptr)sinfo; |
301 | 412k | JSAMPARRAY image_ptr; |
302 | 412k | register JSAMPROW inptr, outptr; |
303 | 412k | register JDIMENSION col; |
304 | | |
305 | 412k | if (source->use_inversion_array) { |
306 | | /* Fetch next row from virtual array */ |
307 | 0 | source->source_row--; |
308 | 0 | image_ptr = (*cinfo->mem->access_virt_sarray) |
309 | 0 | ((j_common_ptr)cinfo, source->whole_image, |
310 | 0 | source->source_row, (JDIMENSION)1, FALSE); |
311 | 0 | inptr = image_ptr[0]; |
312 | 412k | } else { |
313 | 412k | if (!ReadOK(source->pub.input_file, source->iobuffer, source->row_width)) |
314 | 456 | ERREXIT(cinfo, JERR_INPUT_EOF); |
315 | 412k | inptr = source->iobuffer; |
316 | 412k | } |
317 | | |
318 | | /* Transfer data. Note source values are in BGR order |
319 | | * (even though Microsoft's own documents say the opposite). |
320 | | */ |
321 | 412k | outptr = source->pub.buffer[0]; |
322 | 412k | if (cinfo->in_color_space == JCS_EXT_BGRX || |
323 | 412k | cinfo->in_color_space == JCS_EXT_BGRA) { |
324 | 68.7k | MEMCOPY(outptr, inptr, source->row_width); |
325 | 344k | } else if (cinfo->in_color_space == JCS_CMYK) { |
326 | 277k | for (col = cinfo->image_width; col > 0; col--) { |
327 | | /* can omit GETJSAMPLE() safely */ |
328 | 208k | JSAMPLE b = *inptr++, g = *inptr++, r = *inptr++; |
329 | 208k | rgb_to_cmyk(r, g, b, outptr, outptr + 1, outptr + 2, outptr + 3); |
330 | 208k | inptr++; /* skip the 4th byte (Alpha channel) */ |
331 | 208k | outptr += 4; |
332 | 208k | } |
333 | 275k | } else { |
334 | 275k | register int rindex = rgb_red[cinfo->in_color_space]; |
335 | 275k | register int gindex = rgb_green[cinfo->in_color_space]; |
336 | 275k | register int bindex = rgb_blue[cinfo->in_color_space]; |
337 | 275k | register int aindex = alpha_index[cinfo->in_color_space]; |
338 | 275k | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
339 | | |
340 | 275k | 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 | 275k | } else { |
349 | 1.10M | for (col = cinfo->image_width; col > 0; col--) { |
350 | 834k | outptr[bindex] = *inptr++; /* can omit GETJSAMPLE() safely */ |
351 | 834k | outptr[gindex] = *inptr++; |
352 | 834k | outptr[rindex] = *inptr++; |
353 | 834k | inptr++; /* skip the 4th byte (Alpha channel) */ |
354 | 834k | outptr += ps; |
355 | 834k | } |
356 | 275k | } |
357 | 275k | } |
358 | | |
359 | 412k | return 1; |
360 | 412k | } |
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 | 0 | { |
372 | 0 | bmp_source_ptr source = (bmp_source_ptr)sinfo; |
373 | 0 | register FILE *infile = source->pub.input_file; |
374 | 0 | register JSAMPROW out_ptr; |
375 | 0 | JSAMPARRAY image_ptr; |
376 | 0 | JDIMENSION row; |
377 | 0 | cd_progress_ptr progress = (cd_progress_ptr)cinfo->progress; |
378 | | |
379 | | /* Read the data into a virtual array in input-file row order. */ |
380 | 0 | for (row = 0; row < cinfo->image_height; row++) { |
381 | 0 | 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 | 0 | image_ptr = (*cinfo->mem->access_virt_sarray) |
387 | 0 | ((j_common_ptr)cinfo, source->whole_image, row, (JDIMENSION)1, TRUE); |
388 | 0 | out_ptr = image_ptr[0]; |
389 | 0 | if (fread(out_ptr, 1, source->row_width, infile) != source->row_width) { |
390 | 0 | if (feof(infile)) |
391 | 0 | ERREXIT(cinfo, JERR_INPUT_EOF); |
392 | 0 | else |
393 | 0 | ERREXIT(cinfo, JERR_FILE_READ); |
394 | 0 | } |
395 | 0 | } |
396 | 0 | 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 | 0 | switch (source->bits_per_pixel) { |
401 | 0 | case 8: |
402 | 0 | source->pub.get_pixel_rows = get_8bit_row; |
403 | 0 | break; |
404 | 0 | case 24: |
405 | 0 | source->pub.get_pixel_rows = get_24bit_row; |
406 | 0 | break; |
407 | 0 | case 32: |
408 | 0 | source->pub.get_pixel_rows = get_32bit_row; |
409 | 0 | break; |
410 | 0 | default: |
411 | 0 | ERREXIT(cinfo, JERR_BMP_BADDEPTH); |
412 | 0 | } |
413 | 0 | source->source_row = cinfo->image_height; |
414 | | |
415 | | /* And read the first row */ |
416 | 0 | return (*source->pub.get_pixel_rows) (cinfo, sinfo); |
417 | 0 | } |
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 | 6.34k | { |
427 | 6.34k | bmp_source_ptr source = (bmp_source_ptr)sinfo; |
428 | 6.34k | U_CHAR bmpfileheader[14]; |
429 | 6.34k | U_CHAR bmpinfoheader[64]; |
430 | | |
431 | 6.34k | #define GET_2B(array, offset) \ |
432 | 21.5k | ((unsigned short)UCH(array[offset]) + \ |
433 | 21.5k | (((unsigned short)UCH(array[offset + 1])) << 8)) |
434 | 6.34k | #define GET_4B(array, offset) \ |
435 | 34.3k | ((unsigned int)UCH(array[offset]) + \ |
436 | 34.3k | (((unsigned int)UCH(array[offset + 1])) << 8) + \ |
437 | 34.3k | (((unsigned int)UCH(array[offset + 2])) << 16) + \ |
438 | 34.3k | (((unsigned int)UCH(array[offset + 3])) << 24)) |
439 | | |
440 | 6.34k | int bfOffBits; |
441 | 6.34k | int headerSize; |
442 | 6.34k | int biWidth; |
443 | 6.34k | int biHeight; |
444 | 6.34k | unsigned short biPlanes; |
445 | 6.34k | unsigned int biCompression; |
446 | 6.34k | int biXPelsPerMeter, biYPelsPerMeter; |
447 | 6.34k | int biClrUsed = 0; |
448 | 6.34k | int mapentrysize = 0; /* 0 indicates no colormap */ |
449 | 6.34k | int bPad; |
450 | 6.34k | JDIMENSION row_width = 0; |
451 | | |
452 | | /* Read and verify the bitmap file header */ |
453 | 6.34k | if (!ReadOK(source->pub.input_file, bmpfileheader, 14)) |
454 | 42 | ERREXIT(cinfo, JERR_INPUT_EOF); |
455 | 6.34k | if (GET_2B(bmpfileheader, 0) != 0x4D42) /* 'BM' */ |
456 | 77 | ERREXIT(cinfo, JERR_BMP_NOT); |
457 | 6.34k | 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 | 6.34k | if (!ReadOK(source->pub.input_file, bmpinfoheader, 4)) |
464 | 21 | ERREXIT(cinfo, JERR_INPUT_EOF); |
465 | 6.34k | headerSize = GET_4B(bmpinfoheader, 0); |
466 | 6.34k | if (headerSize < 12 || headerSize > 64 || (headerSize + 14) > bfOffBits) |
467 | 574 | ERREXIT(cinfo, JERR_BMP_BADHEADER); |
468 | 6.34k | if (!ReadOK(source->pub.input_file, bmpinfoheader + 4, headerSize - 4)) |
469 | 21 | ERREXIT(cinfo, JERR_INPUT_EOF); |
470 | | |
471 | 6.34k | switch (headerSize) { |
472 | 2.00k | case 12: |
473 | | /* Decode OS/2 1.x header (Microsoft calls this a BITMAPCOREHEADER) */ |
474 | 2.00k | biWidth = (int)GET_2B(bmpinfoheader, 4); |
475 | 2.00k | biHeight = (int)GET_2B(bmpinfoheader, 6); |
476 | 2.00k | biPlanes = GET_2B(bmpinfoheader, 8); |
477 | 2.00k | source->bits_per_pixel = (int)GET_2B(bmpinfoheader, 10); |
478 | | |
479 | 2.00k | switch (source->bits_per_pixel) { |
480 | 819 | case 8: /* colormapped image */ |
481 | 819 | mapentrysize = 3; /* OS/2 uses RGBTRIPLE colormap */ |
482 | 819 | TRACEMS2(cinfo, 1, JTRC_BMP_OS2_MAPPED, biWidth, biHeight); |
483 | 819 | break; |
484 | 1.17k | case 24: /* RGB image */ |
485 | 1.17k | TRACEMS2(cinfo, 1, JTRC_BMP_OS2, biWidth, biHeight); |
486 | 1.17k | break; |
487 | 7 | default: |
488 | 7 | ERREXIT(cinfo, JERR_BMP_BADDEPTH); |
489 | 7 | break; |
490 | 2.00k | } |
491 | 1.99k | break; |
492 | 3.59k | case 40: |
493 | 3.60k | 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 | 3.60k | biWidth = (int)GET_4B(bmpinfoheader, 4); |
497 | 3.60k | biHeight = (int)GET_4B(bmpinfoheader, 8); |
498 | 3.60k | biPlanes = GET_2B(bmpinfoheader, 12); |
499 | 3.60k | source->bits_per_pixel = (int)GET_2B(bmpinfoheader, 14); |
500 | 3.60k | biCompression = GET_4B(bmpinfoheader, 16); |
501 | 3.60k | biXPelsPerMeter = (int)GET_4B(bmpinfoheader, 24); |
502 | 3.60k | biYPelsPerMeter = (int)GET_4B(bmpinfoheader, 28); |
503 | 3.60k | biClrUsed = GET_4B(bmpinfoheader, 32); |
504 | | /* biSizeImage, biClrImportant fields are ignored */ |
505 | | |
506 | 3.60k | switch (source->bits_per_pixel) { |
507 | 2.54k | case 8: /* colormapped image */ |
508 | 2.54k | mapentrysize = 4; /* Windows uses RGBQUAD colormap */ |
509 | 2.54k | TRACEMS2(cinfo, 1, JTRC_BMP_MAPPED, biWidth, biHeight); |
510 | 2.54k | break; |
511 | 252 | case 24: /* RGB image */ |
512 | 252 | TRACEMS2(cinfo, 1, JTRC_BMP, biWidth, biHeight); |
513 | 252 | break; |
514 | 791 | case 32: /* RGB image + Alpha channel */ |
515 | 791 | TRACEMS2(cinfo, 1, JTRC_BMP, biWidth, biHeight); |
516 | 791 | break; |
517 | 14 | default: |
518 | 14 | ERREXIT(cinfo, JERR_BMP_BADDEPTH); |
519 | 14 | break; |
520 | 3.60k | } |
521 | 3.59k | if (biCompression != 0) |
522 | 287 | ERREXIT(cinfo, JERR_BMP_COMPRESSED); |
523 | | |
524 | 3.59k | if (biXPelsPerMeter > 0 && biYPelsPerMeter > 0) { |
525 | | /* Set JFIF density parameters from the BMP data */ |
526 | 987 | cinfo->X_density = (UINT16)(biXPelsPerMeter / 100); /* 100 cm per meter */ |
527 | 987 | cinfo->Y_density = (UINT16)(biYPelsPerMeter / 100); |
528 | 987 | cinfo->density_unit = 2; /* dots/cm */ |
529 | 987 | } |
530 | 3.59k | break; |
531 | 7 | default: |
532 | 7 | ERREXIT(cinfo, JERR_BMP_BADHEADER); |
533 | 7 | return; |
534 | 6.34k | } |
535 | | |
536 | 5.29k | if (biWidth <= 0 || biHeight <= 0) |
537 | 462 | ERREXIT(cinfo, JERR_BMP_EMPTY); |
538 | 5.29k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
539 | 5.29k | if (sinfo->max_pixels && |
540 | 5.29k | (unsigned long long)biWidth * biHeight > sinfo->max_pixels) |
541 | 644 | ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); |
542 | 5.29k | #endif |
543 | 5.29k | if (biPlanes != 1) |
544 | 77 | ERREXIT(cinfo, JERR_BMP_BADPLANES); |
545 | | |
546 | | /* Compute distance to bitmap data --- will adjust for colormap below */ |
547 | 5.29k | bPad = bfOffBits - (headerSize + 14); |
548 | | |
549 | | /* Read the colormap, if any */ |
550 | 5.29k | if (mapentrysize > 0) { |
551 | 2.54k | if (biClrUsed <= 0) |
552 | 1.58k | biClrUsed = 256; /* assume it's 256 */ |
553 | 959 | else if (biClrUsed > 256) |
554 | 126 | ERREXIT(cinfo, JERR_BMP_BADCMAP); |
555 | | /* Allocate space to store the colormap */ |
556 | 2.54k | source->colormap = (*cinfo->mem->alloc_sarray) |
557 | 2.54k | ((j_common_ptr)cinfo, JPOOL_IMAGE, (JDIMENSION)biClrUsed, (JDIMENSION)3); |
558 | 2.54k | source->cmap_length = (int)biClrUsed; |
559 | | /* and read it from the file */ |
560 | 2.54k | read_colormap(source, (int)biClrUsed, mapentrysize); |
561 | | /* account for size of colormap */ |
562 | 2.54k | bPad -= biClrUsed * mapentrysize; |
563 | 2.54k | } |
564 | | |
565 | | /* Skip any remaining pad bytes */ |
566 | 5.29k | if (bPad < 0) /* incorrect bfOffBits value? */ |
567 | 18 | ERREXIT(cinfo, JERR_BMP_BADHEADER); |
568 | 968k | while (--bPad >= 0) { |
569 | 962k | (void)read_byte(source); |
570 | 962k | } |
571 | | |
572 | | /* Compute row width in file, including padding to 4-byte boundary */ |
573 | 5.29k | switch (source->bits_per_pixel) { |
574 | 919 | case 8: |
575 | 919 | if (cinfo->in_color_space == JCS_UNKNOWN) |
576 | 0 | cinfo->in_color_space = JCS_EXT_RGB; |
577 | 919 | if (IsExtRGB(cinfo->in_color_space)) |
578 | 720 | cinfo->input_components = rgb_pixelsize[cinfo->in_color_space]; |
579 | 199 | else if (cinfo->in_color_space == JCS_GRAYSCALE) |
580 | 55 | cinfo->input_components = 1; |
581 | 144 | else if (cinfo->in_color_space == JCS_CMYK) |
582 | 144 | cinfo->input_components = 4; |
583 | 0 | else |
584 | 0 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
585 | 919 | row_width = (JDIMENSION)biWidth; |
586 | 919 | break; |
587 | 707 | case 24: |
588 | 707 | if (cinfo->in_color_space == JCS_UNKNOWN) |
589 | 0 | cinfo->in_color_space = JCS_EXT_BGR; |
590 | 707 | if (IsExtRGB(cinfo->in_color_space)) |
591 | 505 | cinfo->input_components = rgb_pixelsize[cinfo->in_color_space]; |
592 | 202 | else if (cinfo->in_color_space == JCS_CMYK) |
593 | 101 | cinfo->input_components = 4; |
594 | 101 | else |
595 | 101 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
596 | 707 | if ((unsigned long long)biWidth * 3ULL > 0xFFFFFFFFULL) |
597 | 0 | ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); |
598 | 707 | row_width = (JDIMENSION)biWidth * 3; |
599 | 707 | break; |
600 | 574 | case 32: |
601 | 574 | if (cinfo->in_color_space == JCS_UNKNOWN) |
602 | 0 | cinfo->in_color_space = JCS_EXT_BGRA; |
603 | 574 | if (IsExtRGB(cinfo->in_color_space)) |
604 | 410 | cinfo->input_components = rgb_pixelsize[cinfo->in_color_space]; |
605 | 164 | else if (cinfo->in_color_space == JCS_CMYK) |
606 | 82 | cinfo->input_components = 4; |
607 | 82 | else |
608 | 82 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
609 | 574 | if ((unsigned long long)biWidth * 4ULL > 0xFFFFFFFFULL) |
610 | 0 | ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); |
611 | 574 | row_width = (JDIMENSION)biWidth * 4; |
612 | 574 | break; |
613 | 0 | default: |
614 | 0 | ERREXIT(cinfo, JERR_BMP_BADDEPTH); |
615 | 5.29k | } |
616 | 4.11k | while ((row_width & 3) != 0) row_width++; |
617 | 2.01k | source->row_width = row_width; |
618 | | |
619 | 2.01k | if (source->use_inversion_array) { |
620 | | /* Allocate space for inversion array, prepare for preload pass */ |
621 | 0 | source->whole_image = (*cinfo->mem->request_virt_sarray) |
622 | 0 | ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE, |
623 | 0 | row_width, (JDIMENSION)biHeight, (JDIMENSION)1); |
624 | 0 | source->pub.get_pixel_rows = preload_image; |
625 | 0 | 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 | 2.01k | } else { |
630 | 2.01k | source->iobuffer = (U_CHAR *) |
631 | 2.01k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, row_width); |
632 | 2.01k | switch (source->bits_per_pixel) { |
633 | 919 | case 8: |
634 | 919 | source->pub.get_pixel_rows = get_8bit_row; |
635 | 919 | break; |
636 | 606 | case 24: |
637 | 606 | source->pub.get_pixel_rows = get_24bit_row; |
638 | 606 | break; |
639 | 492 | case 32: |
640 | 492 | source->pub.get_pixel_rows = get_32bit_row; |
641 | 492 | break; |
642 | 0 | default: |
643 | 0 | ERREXIT(cinfo, JERR_BMP_BADDEPTH); |
644 | 2.01k | } |
645 | 2.01k | } |
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 | 2.01k | if ((unsigned long long)biWidth * |
651 | 2.01k | (unsigned long long)cinfo->input_components > 0xFFFFFFFFULL) |
652 | 0 | ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); |
653 | | /* Allocate one-row buffer for returned data */ |
654 | 2.01k | source->pub.buffer = (*cinfo->mem->alloc_sarray) |
655 | 2.01k | ((j_common_ptr)cinfo, JPOOL_IMAGE, |
656 | 2.01k | (JDIMENSION)biWidth * (JDIMENSION)cinfo->input_components, (JDIMENSION)1); |
657 | 2.01k | source->pub.buffer_height = 1; |
658 | | |
659 | 2.01k | cinfo->data_precision = 8; |
660 | 2.01k | cinfo->image_width = (JDIMENSION)biWidth; |
661 | 2.01k | cinfo->image_height = (JDIMENSION)biHeight; |
662 | 2.01k | } |
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 | 361 | { |
672 | | /* no work */ |
673 | 361 | } |
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 | 6.34k | { |
683 | 6.34k | bmp_source_ptr source; |
684 | | |
685 | | /* Create module interface object */ |
686 | 6.34k | source = (bmp_source_ptr) |
687 | 6.34k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
688 | 6.34k | sizeof(bmp_source_struct)); |
689 | 6.34k | source->cinfo = cinfo; /* make back link for subroutines */ |
690 | | /* Fill in method ptrs, except get_pixel_rows which start_input sets */ |
691 | 6.34k | source->pub.start_input = start_input_bmp; |
692 | 6.34k | source->pub.finish_input = finish_input_bmp; |
693 | 6.34k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
694 | 6.34k | source->pub.max_pixels = 0; |
695 | 6.34k | #endif |
696 | | |
697 | 6.34k | source->use_inversion_array = use_inversion_array; |
698 | | |
699 | 6.34k | return (cjpeg_source_ptr)source; |
700 | 6.34k | } |
701 | | |
702 | | #endif /* BMP_SUPPORTED */ |