Coverage Report

Created: 2025-10-10 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.dev/src/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-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
163k
#define UCH(x)  ((int)(x))
39
40
41
#define ReadOK(file, buffer, len) \
42
1.69M
  (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
444k
{
84
444k
  register FILE *infile = sinfo->pub.input_file;
85
444k
  register int c;
86
87
444k
  if ((c = getc(infile)) == EOF)
88
1.57k
    ERREXIT(sinfo->cinfo, JERR_INPUT_EOF);
89
444k
  return c;
90
444k
}
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
1.99k
{
97
1.99k
  int i, gray = 1;
98
99
1.99k
  switch (mapentrysize) {
100
595
  case 3:
101
    /* BGR format (occurs in OS/2 files) */
102
40.5k
    for (i = 0; i < cmaplen; i++) {
103
39.9k
      sinfo->colormap[2][i] = (JSAMPLE)read_byte(sinfo);
104
39.9k
      sinfo->colormap[1][i] = (JSAMPLE)read_byte(sinfo);
105
39.9k
      sinfo->colormap[0][i] = (JSAMPLE)read_byte(sinfo);
106
39.9k
      if (sinfo->colormap[2][i] != sinfo->colormap[1][i] ||
107
23.3k
          sinfo->colormap[1][i] != sinfo->colormap[0][i])
108
20.9k
        gray = 0;
109
39.9k
    }
110
595
    break;
111
1.40k
  case 4:
112
    /* BGR0 format (occurs in MS Windows files) */
113
39.1k
    for (i = 0; i < cmaplen; i++) {
114
37.7k
      sinfo->colormap[2][i] = (JSAMPLE)read_byte(sinfo);
115
37.7k
      sinfo->colormap[1][i] = (JSAMPLE)read_byte(sinfo);
116
37.7k
      sinfo->colormap[0][i] = (JSAMPLE)read_byte(sinfo);
117
37.7k
      (void)read_byte(sinfo);
118
37.7k
      if (sinfo->colormap[2][i] != sinfo->colormap[1][i] ||
119
20.3k
          sinfo->colormap[1][i] != sinfo->colormap[0][i])
120
20.7k
        gray = 0;
121
37.7k
    }
122
1.40k
    break;
123
0
  default:
124
0
    ERREXIT(sinfo->cinfo, JERR_BMP_BADCMAP);
125
0
    break;
126
1.99k
  }
127
128
728
  if ((sinfo->cinfo->in_color_space == JCS_UNKNOWN ||
129
728
       sinfo->cinfo->in_color_space == JCS_RGB) && gray)
130
0
    sinfo->cinfo->in_color_space = JCS_GRAYSCALE;
131
132
728
  if (sinfo->cinfo->in_color_space == JCS_GRAYSCALE && !gray)
133
60
    ERREXIT(sinfo->cinfo, JERR_BAD_IN_COLORSPACE);
134
728
}
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
510k
{
148
510k
  bmp_source_ptr source = (bmp_source_ptr)sinfo;
149
510k
  register JSAMPARRAY colormap = source->colormap;
150
510k
  int cmaplen = source->cmap_length;
151
510k
  JSAMPARRAY image_ptr;
152
510k
  register int t;
153
510k
  register JSAMPROW inptr, outptr;
154
510k
  register JDIMENSION col;
155
156
510k
  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
510k
  } else {
164
510k
    if (!ReadOK(source->pub.input_file, source->iobuffer, source->row_width))
165
239
      ERREXIT(cinfo, JERR_INPUT_EOF);
166
510k
    inptr = source->iobuffer;
167
510k
  }
168
169
  /* Expand the colormap indexes to real data */
170
510k
  outptr = source->pub.buffer[0];
171
510k
  if (cinfo->in_color_space == JCS_GRAYSCALE) {
172
3.42k
    for (col = cinfo->image_width; col > 0; col--) {
173
2.66k
      t = *inptr++;
174
2.66k
      if (t >= cmaplen)
175
23
        ERREXIT(cinfo, JERR_BMP_OUTOFRANGE);
176
2.66k
      *outptr++ = colormap[0][t];
177
2.66k
    }
178
509k
  } else if (cinfo->in_color_space == JCS_CMYK) {
179
3.63M
    for (col = cinfo->image_width; col > 0; col--) {
180
3.55M
      t = *inptr++;
181
3.55M
      if (t >= cmaplen)
182
32
        ERREXIT(cinfo, JERR_BMP_OUTOFRANGE);
183
3.55M
      rgb_to_cmyk(255, colormap[0][t], colormap[1][t], colormap[2][t], outptr,
184
3.55M
                  outptr + 1, outptr + 2, outptr + 3);
185
3.55M
      outptr += 4;
186
3.55M
    }
187
424k
  } else {
188
424k
    register int rindex = rgb_red[cinfo->in_color_space];
189
424k
    register int gindex = rgb_green[cinfo->in_color_space];
190
424k
    register int bindex = rgb_blue[cinfo->in_color_space];
191
424k
    register int aindex = alpha_index[cinfo->in_color_space];
192
424k
    register int ps = rgb_pixelsize[cinfo->in_color_space];
193
194
424k
    if (aindex >= 0) {
195
3.63M
      for (col = cinfo->image_width; col > 0; col--) {
196
3.55M
        t = *inptr++;
197
3.55M
        if (t >= cmaplen)
198
32
          ERREXIT(cinfo, JERR_BMP_OUTOFRANGE);
199
3.55M
        outptr[rindex] = colormap[0][t];
200
3.55M
        outptr[gindex] = colormap[1][t];
201
3.55M
        outptr[bindex] = colormap[2][t];
202
3.55M
        outptr[aindex] = 0xFF;
203
3.55M
        outptr += ps;
204
3.55M
      }
205
339k
    } else {
206
14.5M
      for (col = cinfo->image_width; col > 0; col--) {
207
14.2M
        t = *inptr++;
208
14.2M
        if (t >= cmaplen)
209
128
          ERREXIT(cinfo, JERR_BMP_OUTOFRANGE);
210
14.2M
        outptr[rindex] = colormap[0][t];
211
14.2M
        outptr[gindex] = colormap[1][t];
212
14.2M
        outptr[bindex] = colormap[2][t];
213
14.2M
        outptr += ps;
214
14.2M
      }
215
339k
    }
216
424k
  }
217
218
510k
  return 1;
219
510k
}
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
394k
{
226
394k
  bmp_source_ptr source = (bmp_source_ptr)sinfo;
227
394k
  JSAMPARRAY image_ptr;
228
394k
  register JSAMPROW inptr, outptr;
229
394k
  register JDIMENSION col;
230
231
394k
  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
394k
  } else {
239
394k
    if (!ReadOK(source->pub.input_file, source->iobuffer, source->row_width))
240
588
      ERREXIT(cinfo, JERR_INPUT_EOF);
241
394k
    inptr = source->iobuffer;
242
394k
  }
243
244
  /* Transfer data.  Note source values are in BGR order
245
   * (even though Microsoft's own documents say the opposite).
246
   */
247
394k
  outptr = source->pub.buffer[0];
248
394k
  if (cinfo->in_color_space == JCS_EXT_BGR) {
249
65.6k
    memcpy(outptr, inptr, source->row_width);
250
328k
  } else if (cinfo->in_color_space == JCS_CMYK) {
251
141k
    for (col = cinfo->image_width; col > 0; col--) {
252
76.1k
      JSAMPLE b = *inptr++, g = *inptr++, r = *inptr++;
253
76.1k
      rgb_to_cmyk(255, r, g, b, outptr, outptr + 1, outptr + 2, outptr + 3);
254
76.1k
      outptr += 4;
255
76.1k
    }
256
263k
  } else {
257
263k
    register int rindex = rgb_red[cinfo->in_color_space];
258
263k
    register int gindex = rgb_green[cinfo->in_color_space];
259
263k
    register int bindex = rgb_blue[cinfo->in_color_space];
260
263k
    register int aindex = alpha_index[cinfo->in_color_space];
261
263k
    register int ps = rgb_pixelsize[cinfo->in_color_space];
262
263
263k
    if (aindex >= 0) {
264
141k
      for (col = cinfo->image_width; col > 0; col--) {
265
76.1k
        outptr[bindex] = *inptr++;
266
76.1k
        outptr[gindex] = *inptr++;
267
76.1k
        outptr[rindex] = *inptr++;
268
76.1k
        outptr[aindex] = 0xFF;
269
76.1k
        outptr += ps;
270
76.1k
      }
271
197k
    } else {
272
425k
      for (col = cinfo->image_width; col > 0; col--) {
273
228k
        outptr[bindex] = *inptr++;
274
228k
        outptr[gindex] = *inptr++;
275
228k
        outptr[rindex] = *inptr++;
276
228k
        outptr += ps;
277
228k
      }
278
197k
    }
279
263k
  }
280
281
394k
  return 1;
282
394k
}
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
770k
{
289
770k
  bmp_source_ptr source = (bmp_source_ptr)sinfo;
290
770k
  JSAMPARRAY image_ptr;
291
770k
  register JSAMPROW inptr, outptr;
292
770k
  register JDIMENSION col;
293
294
770k
  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
770k
  } else {
302
770k
    if (!ReadOK(source->pub.input_file, source->iobuffer, source->row_width))
303
468
      ERREXIT(cinfo, JERR_INPUT_EOF);
304
770k
    inptr = source->iobuffer;
305
770k
  }
306
307
  /* Transfer data.  Note source values are in BGR order
308
   * (even though Microsoft's own documents say the opposite).
309
   */
310
770k
  outptr = source->pub.buffer[0];
311
770k
  if (cinfo->in_color_space == JCS_EXT_BGRX ||
312
769k
      cinfo->in_color_space == JCS_EXT_BGRA) {
313
128k
    memcpy(outptr, inptr, source->row_width);
314
641k
  } else if (cinfo->in_color_space == JCS_CMYK) {
315
656k
    for (col = cinfo->image_width; col > 0; col--) {
316
528k
      JSAMPLE b = *inptr++, g = *inptr++, r = *inptr++;
317
528k
      rgb_to_cmyk(255, r, g, b, outptr, outptr + 1, outptr + 2, outptr + 3);
318
528k
      inptr++;                          /* skip the 4th byte (Alpha channel) */
319
528k
      outptr += 4;
320
528k
    }
321
513k
  } else {
322
513k
    register int rindex = rgb_red[cinfo->in_color_space];
323
513k
    register int gindex = rgb_green[cinfo->in_color_space];
324
513k
    register int bindex = rgb_blue[cinfo->in_color_space];
325
513k
    register int aindex = alpha_index[cinfo->in_color_space];
326
513k
    register int ps = rgb_pixelsize[cinfo->in_color_space];
327
328
513k
    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
513k
    } else {
337
2.62M
      for (col = cinfo->image_width; col > 0; col--) {
338
2.11M
        outptr[bindex] = *inptr++;
339
2.11M
        outptr[gindex] = *inptr++;
340
2.11M
        outptr[rindex] = *inptr++;
341
2.11M
        inptr++;                        /* skip the 4th byte (Alpha channel) */
342
2.11M
        outptr += ps;
343
2.11M
      }
344
513k
    }
345
513k
  }
346
347
770k
  return 1;
348
770k
}
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.09k
{
415
6.09k
  bmp_source_ptr source = (bmp_source_ptr)sinfo;
416
6.09k
  U_CHAR bmpfileheader[14];
417
6.09k
  U_CHAR bmpinfoheader[64];
418
419
6.09k
#define GET_2B(array, offset) \
420
21.1k
  ((unsigned short)UCH(array[offset]) + \
421
21.1k
   (((unsigned short)UCH(array[offset + 1])) << 8))
422
6.09k
#define GET_4B(array, offset) \
423
30.3k
  ((unsigned int)UCH(array[offset]) + \
424
30.3k
   (((unsigned int)UCH(array[offset + 1])) << 8) + \
425
30.3k
   (((unsigned int)UCH(array[offset + 2])) << 16) + \
426
30.3k
   (((unsigned int)UCH(array[offset + 3])) << 24))
427
428
6.09k
  int bfOffBits;
429
6.09k
  int headerSize;
430
6.09k
  int biWidth;
431
6.09k
  int biHeight;
432
6.09k
  unsigned short biPlanes;
433
6.09k
  unsigned int biCompression;
434
6.09k
  int biXPelsPerMeter, biYPelsPerMeter;
435
6.09k
  int biClrUsed = 0;
436
6.09k
  int mapentrysize = 0;         /* 0 indicates no colormap */
437
6.09k
  int bPad;
438
6.09k
  JDIMENSION row_width = 0;
439
440
  /* Read and verify the bitmap file header */
441
6.09k
  if (!ReadOK(source->pub.input_file, bmpfileheader, 14))
442
42
    ERREXIT(cinfo, JERR_INPUT_EOF);
443
6.09k
  if (GET_2B(bmpfileheader, 0) != 0x4D42) /* 'BM' */
444
70
    ERREXIT(cinfo, JERR_BMP_NOT);
445
6.09k
  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.09k
  if (!ReadOK(source->pub.input_file, bmpinfoheader, 4))
452
14
    ERREXIT(cinfo, JERR_INPUT_EOF);
453
6.09k
  headerSize = GET_4B(bmpinfoheader, 0);
454
6.09k
  if (headerSize < 12 || headerSize > 64 || (headerSize + 14) > bfOffBits)
455
637
    ERREXIT(cinfo, JERR_BMP_BADHEADER);
456
6.09k
  if (!ReadOK(source->pub.input_file, bmpinfoheader + 4, headerSize - 4))
457
49
    ERREXIT(cinfo, JERR_INPUT_EOF);
458
459
6.09k
  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
714
    case 8:                     /* colormapped image */
469
714
      mapentrysize = 3;         /* OS/2 uses RGBTRIPLE colormap */
470
714
      TRACEMS2(cinfo, 1, JTRC_BMP_OS2_MAPPED, biWidth, biHeight);
471
714
      break;
472
791
    case 24:                    /* RGB image */
473
1.51k
    case 32:                    /* RGB image + Alpha channel */
474
1.51k
      TRACEMS3(cinfo, 1, JTRC_BMP_OS2, biWidth, biHeight,
475
1.51k
               source->bits_per_pixel);
476
1.51k
      break;
477
7
    default:
478
7
      ERREXIT(cinfo, JERR_BMP_BADDEPTH);
479
7
      break;
480
2.24k
    }
481
2.23k
    break;
482
3.02k
  case 40:
483
3.03k
  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.03k
    biWidth = (int)GET_4B(bmpinfoheader, 4);
487
3.03k
    biHeight = (int)GET_4B(bmpinfoheader, 8);
488
3.03k
    biPlanes = GET_2B(bmpinfoheader, 12);
489
3.03k
    source->bits_per_pixel = (int)GET_2B(bmpinfoheader, 14);
490
3.03k
    biCompression = GET_4B(bmpinfoheader, 16);
491
3.03k
    biXPelsPerMeter = (int)GET_4B(bmpinfoheader, 24);
492
3.03k
    biYPelsPerMeter = (int)GET_4B(bmpinfoheader, 28);
493
3.03k
    biClrUsed = GET_4B(bmpinfoheader, 32);
494
    /* biSizeImage, biClrImportant fields are ignored */
495
496
3.03k
    switch (source->bits_per_pixel) {
497
2.18k
    case 8:                     /* colormapped image */
498
2.18k
      mapentrysize = 4;         /* Windows uses RGBQUAD colormap */
499
2.18k
      TRACEMS2(cinfo, 1, JTRC_BMP_MAPPED, biWidth, biHeight);
500
2.18k
      break;
501
497
    case 24:                    /* RGB image */
502
833
    case 32:                    /* RGB image + Alpha channel */
503
833
      TRACEMS3(cinfo, 1, JTRC_BMP, biWidth, biHeight, source->bits_per_pixel);
504
833
      break;
505
14
    default:
506
14
      ERREXIT(cinfo, JERR_BMP_BADDEPTH);
507
14
      break;
508
3.03k
    }
509
3.01k
    if (biCompression != 0)
510
266
      ERREXIT(cinfo, JERR_BMP_COMPRESSED);
511
512
3.01k
    if (biXPelsPerMeter > 0 && biYPelsPerMeter > 0) {
513
      /* Set JFIF density parameters from the BMP data */
514
1.04k
      cinfo->X_density = (UINT16)(biXPelsPerMeter / 100); /* 100 cm per meter */
515
1.04k
      cinfo->Y_density = (UINT16)(biYPelsPerMeter / 100);
516
1.04k
      cinfo->density_unit = 2;  /* dots/cm */
517
1.04k
    }
518
3.01k
    break;
519
7
  default:
520
7
    ERREXIT(cinfo, JERR_BMP_BADHEADER);
521
7
    return;
522
6.09k
  }
523
524
4.98k
  if (biWidth <= 0 || biHeight <= 0)
525
434
    ERREXIT(cinfo, JERR_BMP_EMPTY);
526
4.98k
  if (sinfo->max_pixels &&
527
4.55k
      (unsigned long long)biWidth * biHeight > sinfo->max_pixels)
528
567
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
529
4.98k
  if (biPlanes != 1)
530
91
    ERREXIT(cinfo, JERR_BMP_BADPLANES);
531
532
  /* Compute distance to bitmap data --- will adjust for colormap below */
533
4.98k
  bPad = bfOffBits - (headerSize + 14);
534
535
  /* Read the colormap, if any */
536
4.98k
  if (mapentrysize > 0) {
537
2.15k
    if (biClrUsed <= 0)
538
1.31k
      biClrUsed = 256;          /* assume it's 256 */
539
840
    else if (biClrUsed > 256)
540
161
      ERREXIT(cinfo, JERR_BMP_BADCMAP);
541
    /* Allocate space to store the colormap */
542
2.15k
    source->colormap = (*cinfo->mem->alloc_sarray)
543
2.15k
      ((j_common_ptr)cinfo, JPOOL_IMAGE, (JDIMENSION)biClrUsed, (JDIMENSION)3);
544
2.15k
    source->cmap_length = (int)biClrUsed;
545
    /* and read it from the file */
546
2.15k
    read_colormap(source, (int)biClrUsed, mapentrysize);
547
    /* account for size of colormap */
548
2.15k
    bPad -= biClrUsed * mapentrysize;
549
2.15k
  }
550
551
  /* Skip any remaining pad bytes */
552
4.98k
  if (bPad < 0)                 /* incorrect bfOffBits value? */
553
48
    ERREXIT(cinfo, JERR_BMP_BADHEADER);
554
181k
  while (--bPad >= 0) {
555
176k
    (void)read_byte(source);
556
176k
  }
557
558
  /* Compute row width in file, including padding to 4-byte boundary */
559
4.98k
  switch (source->bits_per_pixel) {
560
608
  case 8:
561
608
    if (cinfo->in_color_space == JCS_UNKNOWN)
562
0
      cinfo->in_color_space = JCS_EXT_RGB;
563
608
    if (IsExtRGB(cinfo->in_color_space))
564
470
      cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
565
138
    else if (cinfo->in_color_space == JCS_GRAYSCALE)
566
44
      cinfo->input_components = 1;
567
94
    else if (cinfo->in_color_space == JCS_CMYK)
568
94
      cinfo->input_components = 4;
569
0
    else
570
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
571
608
    row_width = (JDIMENSION)biWidth;
572
608
    break;
573
784
  case 24:
574
784
    if (cinfo->in_color_space == JCS_UNKNOWN)
575
0
      cinfo->in_color_space = JCS_EXT_BGR;
576
784
    if (IsExtRGB(cinfo->in_color_space))
577
560
      cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
578
224
    else if (cinfo->in_color_space == JCS_CMYK)
579
112
      cinfo->input_components = 4;
580
112
    else
581
112
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
582
784
    if ((unsigned long long)biWidth * 3ULL > 0xFFFFFFFFULL)
583
0
      ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
584
784
    row_width = (JDIMENSION)biWidth * 3;
585
784
    break;
586
658
  case 32:
587
658
    if (cinfo->in_color_space == JCS_UNKNOWN)
588
0
      cinfo->in_color_space = JCS_EXT_BGRA;
589
658
    if (IsExtRGB(cinfo->in_color_space))
590
470
      cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
591
188
    else if (cinfo->in_color_space == JCS_CMYK)
592
94
      cinfo->input_components = 4;
593
94
    else
594
94
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
595
658
    if ((unsigned long long)biWidth * 4ULL > 0xFFFFFFFFULL)
596
0
      ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
597
658
    row_width = (JDIMENSION)biWidth * 4;
598
658
    break;
599
0
  default:
600
0
    ERREXIT(cinfo, JERR_BMP_BADDEPTH);
601
4.98k
  }
602
3.22k
  while ((row_width & 3) != 0) row_width++;
603
1.84k
  source->row_width = row_width;
604
605
1.84k
  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.84k
  } else {
616
1.84k
    source->iobuffer = (U_CHAR *)
617
1.84k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, row_width);
618
1.84k
    switch (source->bits_per_pixel) {
619
608
    case 8:
620
608
      source->pub.get_pixel_rows = get_8bit_row;
621
608
      break;
622
672
    case 24:
623
672
      source->pub.get_pixel_rows = get_24bit_row;
624
672
      break;
625
564
    case 32:
626
564
      source->pub.get_pixel_rows = get_32bit_row;
627
564
      break;
628
0
    default:
629
0
      ERREXIT(cinfo, JERR_BMP_BADDEPTH);
630
1.84k
    }
631
1.84k
  }
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.84k
  if ((unsigned long long)biWidth *
637
1.84k
      (unsigned long long)cinfo->input_components > 0xFFFFFFFFULL)
638
0
    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
639
  /* Allocate one-row buffer for returned data */
640
1.84k
  source->pub.buffer = (*cinfo->mem->alloc_sarray)
641
1.84k
    ((j_common_ptr)cinfo, JPOOL_IMAGE,
642
1.84k
     (JDIMENSION)biWidth * (JDIMENSION)cinfo->input_components, (JDIMENSION)1);
643
1.84k
  source->pub.buffer_height = 1;
644
645
1.84k
  cinfo->data_precision = 8;
646
1.84k
  cinfo->image_width = (JDIMENSION)biWidth;
647
1.84k
  cinfo->image_height = (JDIMENSION)biHeight;
648
1.84k
}
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
334
{
658
  /* no work */
659
334
}
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.09k
{
669
6.09k
  bmp_source_ptr source;
670
671
6.09k
  if (cinfo->data_precision != 8)
672
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
673
674
  /* Create module interface object */
675
6.09k
  source = (bmp_source_ptr)
676
6.09k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
677
6.09k
                                sizeof(bmp_source_struct));
678
6.09k
  source->cinfo = cinfo;        /* make back link for subroutines */
679
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
680
6.09k
  source->pub.start_input = start_input_bmp;
681
6.09k
  source->pub.finish_input = finish_input_bmp;
682
6.09k
  source->pub.max_pixels = 0;
683
684
6.09k
  source->use_inversion_array = use_inversion_array;
685
686
6.09k
  return (cjpeg_source_ptr)source;
687
6.09k
}
688
689
#endif /* BMP_SUPPORTED */