Coverage Report

Created: 2023-06-07 06:03

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