Coverage Report

Created: 2023-06-07 06:03

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