Coverage Report

Created: 2026-04-12 06:05

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