Coverage Report

Created: 2025-07-11 07:02

/src/libjpeg-turbo.main/src/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-2024, 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
162k
#define UCH(x)  ((int)(x))
39
40
41
#define ReadOK(file, buffer, len) \
42
3.24M
  (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
1.10M
{
84
1.10M
  register FILE *infile = sinfo->pub.input_file;
85
1.10M
  register int c;
86
87
1.10M
  if ((c = getc(infile)) == EOF)
88
1.57k
    ERREXIT(sinfo->cinfo, JERR_INPUT_EOF);
89
1.10M
  return c;
90
1.10M
}
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.17k
{
97
2.17k
  int i, gray = 1;
98
99
2.17k
  switch (mapentrysize) {
100
630
  case 3:
101
    /* BGR format (occurs in OS/2 files) */
102
42.4k
    for (i = 0; i < cmaplen; i++) {
103
41.7k
      sinfo->colormap[2][i] = (JSAMPLE)read_byte(sinfo);
104
41.7k
      sinfo->colormap[1][i] = (JSAMPLE)read_byte(sinfo);
105
41.7k
      sinfo->colormap[0][i] = (JSAMPLE)read_byte(sinfo);
106
41.7k
      if (sinfo->colormap[2][i] != sinfo->colormap[1][i] ||
107
41.7k
          sinfo->colormap[1][i] != sinfo->colormap[0][i])
108
18.7k
        gray = 0;
109
41.7k
    }
110
630
    break;
111
1.54k
  case 4:
112
    /* BGR0 format (occurs in MS Windows files) */
113
43.9k
    for (i = 0; i < cmaplen; i++) {
114
42.4k
      sinfo->colormap[2][i] = (JSAMPLE)read_byte(sinfo);
115
42.4k
      sinfo->colormap[1][i] = (JSAMPLE)read_byte(sinfo);
116
42.4k
      sinfo->colormap[0][i] = (JSAMPLE)read_byte(sinfo);
117
42.4k
      (void)read_byte(sinfo);
118
42.4k
      if (sinfo->colormap[2][i] != sinfo->colormap[1][i] ||
119
42.4k
          sinfo->colormap[1][i] != sinfo->colormap[0][i])
120
14.2k
        gray = 0;
121
42.4k
    }
122
1.54k
    break;
123
0
  default:
124
0
    ERREXIT(sinfo->cinfo, JERR_BMP_BADCMAP);
125
0
    break;
126
2.17k
  }
127
128
903
  if ((sinfo->cinfo->in_color_space == JCS_UNKNOWN ||
129
903
       sinfo->cinfo->in_color_space == JCS_RGB) && gray)
130
0
    sinfo->cinfo->in_color_space = JCS_GRAYSCALE;
131
132
903
  if (sinfo->cinfo->in_color_space == JCS_GRAYSCALE && !gray)
133
76
    ERREXIT(sinfo->cinfo, JERR_BAD_IN_COLORSPACE);
134
903
}
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
1.63M
{
148
1.63M
  bmp_source_ptr source = (bmp_source_ptr)sinfo;
149
1.63M
  register JSAMPARRAY colormap = source->colormap;
150
1.63M
  int cmaplen = source->cmap_length;
151
1.63M
  JSAMPARRAY image_ptr;
152
1.63M
  register int t;
153
1.63M
  register JSAMPROW inptr, outptr;
154
1.63M
  register JDIMENSION col;
155
156
1.63M
  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
1.63M
  } else {
164
1.63M
    if (!ReadOK(source->pub.input_file, source->iobuffer, source->row_width))
165
312
      ERREXIT(cinfo, JERR_INPUT_EOF);
166
1.63M
    inptr = source->iobuffer;
167
1.63M
  }
168
169
  /* Expand the colormap indexes to real data */
170
1.63M
  outptr = source->pub.buffer[0];
171
1.63M
  if (cinfo->in_color_space == JCS_GRAYSCALE) {
172
200k
    for (col = cinfo->image_width; col > 0; col--) {
173
199k
      t = *inptr++;
174
199k
      if (t >= cmaplen)
175
29
        ERREXIT(cinfo, JERR_BMP_OUTOFRANGE);
176
199k
      *outptr++ = colormap[0][t];
177
199k
    }
178
1.63M
  } else if (cinfo->in_color_space == JCS_CMYK) {
179
1.80M
    for (col = cinfo->image_width; col > 0; col--) {
180
1.53M
      t = *inptr++;
181
1.53M
      if (t >= cmaplen)
182
37
        ERREXIT(cinfo, JERR_BMP_OUTOFRANGE);
183
1.53M
      rgb_to_cmyk(255, colormap[0][t], colormap[1][t], colormap[2][t], outptr,
184
1.53M
                  outptr + 1, outptr + 2, outptr + 3);
185
1.53M
      outptr += 4;
186
1.53M
    }
187
1.35M
  } else {
188
1.35M
    register int rindex = rgb_red[cinfo->in_color_space];
189
1.35M
    register int gindex = rgb_green[cinfo->in_color_space];
190
1.35M
    register int bindex = rgb_blue[cinfo->in_color_space];
191
1.35M
    register int aindex = alpha_index[cinfo->in_color_space];
192
1.35M
    register int ps = rgb_pixelsize[cinfo->in_color_space];
193
194
1.35M
    if (aindex >= 0) {
195
1.80M
      for (col = cinfo->image_width; col > 0; col--) {
196
1.53M
        t = *inptr++;
197
1.53M
        if (t >= cmaplen)
198
37
          ERREXIT(cinfo, JERR_BMP_OUTOFRANGE);
199
1.53M
        outptr[rindex] = colormap[0][t];
200
1.53M
        outptr[gindex] = colormap[1][t];
201
1.53M
        outptr[bindex] = colormap[2][t];
202
1.53M
        outptr[aindex] = 0xFF;
203
1.53M
        outptr += ps;
204
1.53M
      }
205
1.08M
    } else {
206
7.22M
      for (col = cinfo->image_width; col > 0; col--) {
207
6.14M
        t = *inptr++;
208
6.14M
        if (t >= cmaplen)
209
148
          ERREXIT(cinfo, JERR_BMP_OUTOFRANGE);
210
6.14M
        outptr[rindex] = colormap[0][t];
211
6.14M
        outptr[gindex] = colormap[1][t];
212
6.14M
        outptr[bindex] = colormap[2][t];
213
6.14M
        outptr += ps;
214
6.14M
      }
215
1.08M
    }
216
1.35M
  }
217
218
1.63M
  return 1;
219
1.63M
}
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
1.19M
{
226
1.19M
  bmp_source_ptr source = (bmp_source_ptr)sinfo;
227
1.19M
  JSAMPARRAY image_ptr;
228
1.19M
  register JSAMPROW inptr, outptr;
229
1.19M
  register JDIMENSION col;
230
231
1.19M
  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
1.19M
  } else {
239
1.19M
    if (!ReadOK(source->pub.input_file, source->iobuffer, source->row_width))
240
492
      ERREXIT(cinfo, JERR_INPUT_EOF);
241
1.19M
    inptr = source->iobuffer;
242
1.19M
  }
243
244
  /* Transfer data.  Note source values are in BGR order
245
   * (even though Microsoft's own documents say the opposite).
246
   */
247
1.19M
  outptr = source->pub.buffer[0];
248
1.19M
  if (cinfo->in_color_space == JCS_EXT_BGR) {
249
199k
    memcpy(outptr, inptr, source->row_width);
250
999k
  } else if (cinfo->in_color_space == JCS_CMYK) {
251
932k
    for (col = cinfo->image_width; col > 0; col--) {
252
733k
      JSAMPLE b = *inptr++, g = *inptr++, r = *inptr++;
253
733k
      rgb_to_cmyk(255, r, g, b, outptr, outptr + 1, outptr + 2, outptr + 3);
254
733k
      outptr += 4;
255
733k
    }
256
799k
  } else {
257
799k
    register int rindex = rgb_red[cinfo->in_color_space];
258
799k
    register int gindex = rgb_green[cinfo->in_color_space];
259
799k
    register int bindex = rgb_blue[cinfo->in_color_space];
260
799k
    register int aindex = alpha_index[cinfo->in_color_space];
261
799k
    register int ps = rgb_pixelsize[cinfo->in_color_space];
262
263
799k
    if (aindex >= 0) {
264
932k
      for (col = cinfo->image_width; col > 0; col--) {
265
733k
        outptr[bindex] = *inptr++;
266
733k
        outptr[gindex] = *inptr++;
267
733k
        outptr[rindex] = *inptr++;
268
733k
        outptr[aindex] = 0xFF;
269
733k
        outptr += ps;
270
733k
      }
271
599k
    } else {
272
2.79M
      for (col = cinfo->image_width; col > 0; col--) {
273
2.19M
        outptr[bindex] = *inptr++;
274
2.19M
        outptr[gindex] = *inptr++;
275
2.19M
        outptr[rindex] = *inptr++;
276
2.19M
        outptr += ps;
277
2.19M
      }
278
599k
    }
279
799k
  }
280
281
1.19M
  return 1;
282
1.19M
}
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
392k
{
289
392k
  bmp_source_ptr source = (bmp_source_ptr)sinfo;
290
392k
  JSAMPARRAY image_ptr;
291
392k
  register JSAMPROW inptr, outptr;
292
392k
  register JDIMENSION col;
293
294
392k
  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
392k
  } else {
302
392k
    if (!ReadOK(source->pub.input_file, source->iobuffer, source->row_width))
303
444
      ERREXIT(cinfo, JERR_INPUT_EOF);
304
392k
    inptr = source->iobuffer;
305
392k
  }
306
307
  /* Transfer data.  Note source values are in BGR order
308
   * (even though Microsoft's own documents say the opposite).
309
   */
310
392k
  outptr = source->pub.buffer[0];
311
392k
  if (cinfo->in_color_space == JCS_EXT_BGRX ||
312
392k
      cinfo->in_color_space == JCS_EXT_BGRA) {
313
65.4k
    memcpy(outptr, inptr, source->row_width);
314
327k
  } else if (cinfo->in_color_space == JCS_CMYK) {
315
366k
    for (col = cinfo->image_width; col > 0; col--) {
316
300k
      JSAMPLE b = *inptr++, g = *inptr++, r = *inptr++;
317
300k
      rgb_to_cmyk(255, r, g, b, outptr, outptr + 1, outptr + 2, outptr + 3);
318
300k
      inptr++;                          /* skip the 4th byte (Alpha channel) */
319
300k
      outptr += 4;
320
300k
    }
321
262k
  } else {
322
262k
    register int rindex = rgb_red[cinfo->in_color_space];
323
262k
    register int gindex = rgb_green[cinfo->in_color_space];
324
262k
    register int bindex = rgb_blue[cinfo->in_color_space];
325
262k
    register int aindex = alpha_index[cinfo->in_color_space];
326
262k
    register int ps = rgb_pixelsize[cinfo->in_color_space];
327
328
262k
    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
262k
    } else {
337
1.46M
      for (col = cinfo->image_width; col > 0; col--) {
338
1.20M
        outptr[bindex] = *inptr++;
339
1.20M
        outptr[gindex] = *inptr++;
340
1.20M
        outptr[rindex] = *inptr++;
341
1.20M
        inptr++;                        /* skip the 4th byte (Alpha channel) */
342
1.20M
        outptr += ps;
343
1.20M
      }
344
262k
    }
345
262k
  }
346
347
392k
  return 1;
348
392k
}
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.01k
{
415
6.01k
  bmp_source_ptr source = (bmp_source_ptr)sinfo;
416
6.01k
  U_CHAR bmpfileheader[14];
417
6.01k
  U_CHAR bmpinfoheader[64];
418
419
6.01k
#define GET_2B(array, offset) \
420
20.9k
  ((unsigned short)UCH(array[offset]) + \
421
20.9k
   (((unsigned short)UCH(array[offset + 1])) << 8))
422
6.01k
#define GET_4B(array, offset) \
423
30.0k
  ((unsigned int)UCH(array[offset]) + \
424
30.0k
   (((unsigned int)UCH(array[offset + 1])) << 8) + \
425
30.0k
   (((unsigned int)UCH(array[offset + 2])) << 16) + \
426
30.0k
   (((unsigned int)UCH(array[offset + 3])) << 24))
427
428
6.01k
  int bfOffBits;
429
6.01k
  int headerSize;
430
6.01k
  int biWidth;
431
6.01k
  int biHeight;
432
6.01k
  unsigned short biPlanes;
433
6.01k
  unsigned int biCompression;
434
6.01k
  int biXPelsPerMeter, biYPelsPerMeter;
435
6.01k
  int biClrUsed = 0;
436
6.01k
  int mapentrysize = 0;         /* 0 indicates no colormap */
437
6.01k
  int bPad;
438
6.01k
  JDIMENSION row_width = 0;
439
440
  /* Read and verify the bitmap file header */
441
6.01k
  if (!ReadOK(source->pub.input_file, bmpfileheader, 14))
442
49
    ERREXIT(cinfo, JERR_INPUT_EOF);
443
6.01k
  if (GET_2B(bmpfileheader, 0) != 0x4D42) /* 'BM' */
444
63
    ERREXIT(cinfo, JERR_BMP_NOT);
445
6.01k
  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.01k
  if (!ReadOK(source->pub.input_file, bmpinfoheader, 4))
452
21
    ERREXIT(cinfo, JERR_INPUT_EOF);
453
6.01k
  headerSize = GET_4B(bmpinfoheader, 0);
454
6.01k
  if (headerSize < 12 || headerSize > 64 || (headerSize + 14) > bfOffBits)
455
581
    ERREXIT(cinfo, JERR_BMP_BADHEADER);
456
6.01k
  if (!ReadOK(source->pub.input_file, bmpinfoheader + 4, headerSize - 4))
457
42
    ERREXIT(cinfo, JERR_INPUT_EOF);
458
459
6.01k
  switch (headerSize) {
460
2.24k
  case 12:
461
    /* Decode OS/2 1.x header (Microsoft calls this a BITMAPCOREHEADER) */
462
2.24k
    biWidth = (int)GET_2B(bmpinfoheader, 4);
463
2.24k
    biHeight = (int)GET_2B(bmpinfoheader, 6);
464
2.24k
    biPlanes = GET_2B(bmpinfoheader, 8);
465
2.24k
    source->bits_per_pixel = (int)GET_2B(bmpinfoheader, 10);
466
467
2.24k
    switch (source->bits_per_pixel) {
468
644
    case 8:                     /* colormapped image */
469
644
      mapentrysize = 3;         /* OS/2 uses RGBTRIPLE colormap */
470
644
      TRACEMS2(cinfo, 1, JTRC_BMP_OS2_MAPPED, biWidth, biHeight);
471
644
      break;
472
861
    case 24:                    /* RGB image */
473
1.58k
    case 32:                    /* RGB image + Alpha channel */
474
1.58k
      TRACEMS3(cinfo, 1, JTRC_BMP_OS2, biWidth, biHeight,
475
1.58k
               source->bits_per_pixel);
476
1.58k
      break;
477
7
    default:
478
7
      ERREXIT(cinfo, JERR_BMP_BADDEPTH);
479
7
      break;
480
2.24k
    }
481
2.23k
    break;
482
3.00k
  case 40:
483
3.01k
  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.01k
    biWidth = (int)GET_4B(bmpinfoheader, 4);
487
3.01k
    biHeight = (int)GET_4B(bmpinfoheader, 8);
488
3.01k
    biPlanes = GET_2B(bmpinfoheader, 12);
489
3.01k
    source->bits_per_pixel = (int)GET_2B(bmpinfoheader, 14);
490
3.01k
    biCompression = GET_4B(bmpinfoheader, 16);
491
3.01k
    biXPelsPerMeter = (int)GET_4B(bmpinfoheader, 24);
492
3.01k
    biYPelsPerMeter = (int)GET_4B(bmpinfoheader, 28);
493
3.01k
    biClrUsed = GET_4B(bmpinfoheader, 32);
494
    /* biSizeImage, biClrImportant fields are ignored */
495
496
3.01k
    switch (source->bits_per_pixel) {
497
2.51k
    case 8:                     /* colormapped image */
498
2.51k
      mapentrysize = 4;         /* Windows uses RGBQUAD colormap */
499
2.51k
      TRACEMS2(cinfo, 1, JTRC_BMP_MAPPED, biWidth, biHeight);
500
2.51k
      break;
501
161
    case 24:                    /* RGB image */
502
490
    case 32:                    /* RGB image + Alpha channel */
503
490
      TRACEMS3(cinfo, 1, JTRC_BMP, biWidth, biHeight, source->bits_per_pixel);
504
490
      break;
505
7
    default:
506
7
      ERREXIT(cinfo, JERR_BMP_BADDEPTH);
507
7
      break;
508
3.01k
    }
509
3.00k
    if (biCompression != 0)
510
259
      ERREXIT(cinfo, JERR_BMP_COMPRESSED);
511
512
3.00k
    if (biXPelsPerMeter > 0 && biYPelsPerMeter > 0) {
513
      /* Set JFIF density parameters from the BMP data */
514
847
      cinfo->X_density = (UINT16)(biXPelsPerMeter / 100); /* 100 cm per meter */
515
847
      cinfo->Y_density = (UINT16)(biYPelsPerMeter / 100);
516
847
      cinfo->density_unit = 2;  /* dots/cm */
517
847
    }
518
3.00k
    break;
519
7
  default:
520
7
    ERREXIT(cinfo, JERR_BMP_BADHEADER);
521
7
    return;
522
6.01k
  }
523
524
4.97k
  if (biWidth <= 0 || biHeight <= 0)
525
420
    ERREXIT(cinfo, JERR_BMP_EMPTY);
526
4.97k
  if (sinfo->max_pixels &&
527
4.97k
      (unsigned long long)biWidth * biHeight > sinfo->max_pixels)
528
581
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
529
4.97k
  if (biPlanes != 1)
530
91
    ERREXIT(cinfo, JERR_BMP_BADPLANES);
531
532
  /* Compute distance to bitmap data --- will adjust for colormap below */
533
4.97k
  bPad = bfOffBits - (headerSize + 14);
534
535
  /* Read the colormap, if any */
536
4.97k
  if (mapentrysize > 0) {
537
2.34k
    if (biClrUsed <= 0)
538
1.34k
      biClrUsed = 256;          /* assume it's 256 */
539
1.00k
    else if (biClrUsed > 256)
540
168
      ERREXIT(cinfo, JERR_BMP_BADCMAP);
541
    /* Allocate space to store the colormap */
542
2.34k
    source->colormap = (*cinfo->mem->alloc_sarray)
543
2.34k
      ((j_common_ptr)cinfo, JPOOL_IMAGE, (JDIMENSION)biClrUsed, (JDIMENSION)3);
544
2.34k
    source->cmap_length = (int)biClrUsed;
545
    /* and read it from the file */
546
2.34k
    read_colormap(source, (int)biClrUsed, mapentrysize);
547
    /* account for size of colormap */
548
2.34k
    bPad -= biClrUsed * mapentrysize;
549
2.34k
  }
550
551
  /* Skip any remaining pad bytes */
552
4.97k
  if (bPad < 0)                 /* incorrect bfOffBits value? */
553
79
    ERREXIT(cinfo, JERR_BMP_BADHEADER);
554
813k
  while (--bPad >= 0) {
555
808k
    (void)read_byte(source);
556
808k
  }
557
558
  /* Compute row width in file, including padding to 4-byte boundary */
559
4.97k
  switch (source->bits_per_pixel) {
560
742
  case 8:
561
742
    if (cinfo->in_color_space == JCS_UNKNOWN)
562
0
      cinfo->in_color_space = JCS_EXT_RGB;
563
742
    if (IsExtRGB(cinfo->in_color_space))
564
575
      cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
565
167
    else if (cinfo->in_color_space == JCS_GRAYSCALE)
566
52
      cinfo->input_components = 1;
567
115
    else if (cinfo->in_color_space == JCS_CMYK)
568
115
      cinfo->input_components = 4;
569
0
    else
570
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
571
742
    row_width = (JDIMENSION)biWidth;
572
742
    break;
573
707
  case 24:
574
707
    if (cinfo->in_color_space == JCS_UNKNOWN)
575
0
      cinfo->in_color_space = JCS_EXT_BGR;
576
707
    if (IsExtRGB(cinfo->in_color_space))
577
505
      cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
578
202
    else if (cinfo->in_color_space == JCS_CMYK)
579
101
      cinfo->input_components = 4;
580
101
    else
581
101
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
582
707
    if ((unsigned long long)biWidth * 3ULL > 0xFFFFFFFFULL)
583
0
      ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
584
707
    row_width = (JDIMENSION)biWidth * 3;
585
707
    break;
586
539
  case 32:
587
539
    if (cinfo->in_color_space == JCS_UNKNOWN)
588
0
      cinfo->in_color_space = JCS_EXT_BGRA;
589
539
    if (IsExtRGB(cinfo->in_color_space))
590
385
      cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
591
154
    else if (cinfo->in_color_space == JCS_CMYK)
592
77
      cinfo->input_components = 4;
593
77
    else
594
77
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
595
539
    if ((unsigned long long)biWidth * 4ULL > 0xFFFFFFFFULL)
596
0
      ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
597
539
    row_width = (JDIMENSION)biWidth * 4;
598
539
    break;
599
0
  default:
600
0
    ERREXIT(cinfo, JERR_BMP_BADDEPTH);
601
4.97k
  }
602
3.22k
  while ((row_width & 3) != 0) row_width++;
603
1.81k
  source->row_width = row_width;
604
605
1.81k
  if (source->use_inversion_array) {
606
    /* Allocate space for inversion array, prepare for preload pass */
607
0
    source->whole_image = (*cinfo->mem->request_virt_sarray)
608
0
      ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
609
0
       row_width, (JDIMENSION)biHeight, (JDIMENSION)1);
610
0
    source->pub.get_pixel_rows = preload_image;
611
0
    if (cinfo->progress != NULL) {
612
0
      cd_progress_ptr progress = (cd_progress_ptr)cinfo->progress;
613
0
      progress->total_extra_passes++; /* count file input as separate pass */
614
0
    }
615
1.81k
  } else {
616
1.81k
    source->iobuffer = (U_CHAR *)
617
1.81k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, row_width);
618
1.81k
    switch (source->bits_per_pixel) {
619
742
    case 8:
620
742
      source->pub.get_pixel_rows = get_8bit_row;
621
742
      break;
622
606
    case 24:
623
606
      source->pub.get_pixel_rows = get_24bit_row;
624
606
      break;
625
462
    case 32:
626
462
      source->pub.get_pixel_rows = get_32bit_row;
627
462
      break;
628
0
    default:
629
0
      ERREXIT(cinfo, JERR_BMP_BADDEPTH);
630
1.81k
    }
631
1.81k
  }
632
633
  /* Ensure that biWidth * cinfo->input_components doesn't exceed the maximum
634
     value of the JDIMENSION type.  This is only a danger with BMP files, since
635
     their width and height fields are 32-bit integers. */
636
1.81k
  if ((unsigned long long)biWidth *
637
1.81k
      (unsigned long long)cinfo->input_components > 0xFFFFFFFFULL)
638
0
    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
639
  /* Allocate one-row buffer for returned data */
640
1.81k
  source->pub.buffer = (*cinfo->mem->alloc_sarray)
641
1.81k
    ((j_common_ptr)cinfo, JPOOL_IMAGE,
642
1.81k
     (JDIMENSION)biWidth * (JDIMENSION)cinfo->input_components, (JDIMENSION)1);
643
1.81k
  source->pub.buffer_height = 1;
644
645
1.81k
  cinfo->data_precision = 8;
646
1.81k
  cinfo->image_width = (JDIMENSION)biWidth;
647
1.81k
  cinfo->image_height = (JDIMENSION)biHeight;
648
1.81k
}
649
650
651
/*
652
 * Finish up at the end of the file.
653
 */
654
655
METHODDEF(void)
656
finish_input_bmp(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
657
311
{
658
  /* no work */
659
311
}
660
661
662
/*
663
 * The module selection routine for BMP format input.
664
 */
665
666
GLOBAL(cjpeg_source_ptr)
667
jinit_read_bmp(j_compress_ptr cinfo, boolean use_inversion_array)
668
6.01k
{
669
6.01k
  bmp_source_ptr source;
670
671
6.01k
  if (cinfo->data_precision != 8)
672
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
673
674
  /* Create module interface object */
675
6.01k
  source = (bmp_source_ptr)
676
6.01k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
677
6.01k
                                sizeof(bmp_source_struct));
678
6.01k
  source->cinfo = cinfo;        /* make back link for subroutines */
679
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
680
6.01k
  source->pub.start_input = start_input_bmp;
681
6.01k
  source->pub.finish_input = finish_input_bmp;
682
6.01k
  source->pub.max_pixels = 0;
683
684
6.01k
  source->use_inversion_array = use_inversion_array;
685
686
6.01k
  return (cjpeg_source_ptr)source;
687
6.01k
}
688
689
#endif /* BMP_SUPPORTED */