Coverage Report

Created: 2026-02-26 07:09

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
38.6k
#define UCH(x)  ((int)(x))
39
40
41
#define ReadOK(file, buffer, len) \
42
3.82k
  (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
33.4k
{
84
33.4k
  register FILE *infile = sinfo->pub.input_file;
85
33.4k
  register int c;
86
87
33.4k
  if ((c = getc(infile)) == EOF)
88
370
    ERREXIT(sinfo->cinfo, JERR_INPUT_EOF);
89
33.4k
  return c;
90
33.4k
}
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
400
{
97
400
  int i, gray = 1;
98
99
400
  switch (mapentrysize) {
100
102
  case 3:
101
    /* BGR format (occurs in OS/2 files) */
102
1.82k
    for (i = 0; i < cmaplen; i++) {
103
1.71k
      sinfo->colormap[2][i] = (JSAMPLE)read_byte(sinfo);
104
1.71k
      sinfo->colormap[1][i] = (JSAMPLE)read_byte(sinfo);
105
1.71k
      sinfo->colormap[0][i] = (JSAMPLE)read_byte(sinfo);
106
1.71k
      if (sinfo->colormap[2][i] != sinfo->colormap[1][i] ||
107
1.07k
          sinfo->colormap[1][i] != sinfo->colormap[0][i])
108
798
        gray = 0;
109
1.71k
    }
110
102
    break;
111
298
  case 4:
112
    /* BGR0 format (occurs in MS Windows files) */
113
5.16k
    for (i = 0; i < cmaplen; i++) {
114
4.86k
      sinfo->colormap[2][i] = (JSAMPLE)read_byte(sinfo);
115
4.86k
      sinfo->colormap[1][i] = (JSAMPLE)read_byte(sinfo);
116
4.86k
      sinfo->colormap[0][i] = (JSAMPLE)read_byte(sinfo);
117
4.86k
      (void)read_byte(sinfo);
118
4.86k
      if (sinfo->colormap[2][i] != sinfo->colormap[1][i] ||
119
2.90k
          sinfo->colormap[1][i] != sinfo->colormap[0][i])
120
2.31k
        gray = 0;
121
4.86k
    }
122
298
    break;
123
0
  default:
124
0
    ERREXIT(sinfo->cinfo, JERR_BMP_BADCMAP);
125
0
    break;
126
400
  }
127
128
112
  if ((sinfo->cinfo->in_color_space == JCS_UNKNOWN ||
129
112
       sinfo->cinfo->in_color_space == JCS_RGB) && gray)
130
38
    sinfo->cinfo->in_color_space = JCS_GRAYSCALE;
131
132
112
  if (sinfo->cinfo->in_color_space == JCS_GRAYSCALE && !gray)
133
0
    ERREXIT(sinfo->cinfo, JERR_BAD_IN_COLORSPACE);
134
112
}
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
582
{
148
582
  bmp_source_ptr source = (bmp_source_ptr)sinfo;
149
582
  register JSAMPARRAY colormap = source->colormap;
150
582
  int cmaplen = source->cmap_length;
151
582
  JSAMPARRAY image_ptr;
152
582
  register int t;
153
582
  register JSAMPROW inptr, outptr;
154
582
  register JDIMENSION col;
155
156
582
  if (source->use_inversion_array) {
157
    /* Fetch next row from virtual array */
158
582
    source->source_row--;
159
582
    image_ptr = (*cinfo->mem->access_virt_sarray)
160
582
      ((j_common_ptr)cinfo, source->whole_image,
161
582
       source->source_row, (JDIMENSION)1, FALSE);
162
582
    inptr = image_ptr[0];
163
582
  } else {
164
0
    if (!ReadOK(source->pub.input_file, source->iobuffer, source->row_width))
165
0
      ERREXIT(cinfo, JERR_INPUT_EOF);
166
0
    inptr = source->iobuffer;
167
0
  }
168
169
  /* Expand the colormap indexes to real data */
170
582
  outptr = source->pub.buffer[0];
171
582
  if (cinfo->in_color_space == JCS_GRAYSCALE) {
172
1.87k
    for (col = cinfo->image_width; col > 0; col--) {
173
1.62k
      t = *inptr++;
174
1.62k
      if (t >= cmaplen)
175
26
        ERREXIT(cinfo, JERR_BMP_OUTOFRANGE);
176
1.62k
      *outptr++ = colormap[0][t];
177
1.62k
    }
178
338
  } else if (cinfo->in_color_space == JCS_CMYK) {
179
0
    for (col = cinfo->image_width; col > 0; col--) {
180
0
      t = *inptr++;
181
0
      if (t >= cmaplen)
182
0
        ERREXIT(cinfo, JERR_BMP_OUTOFRANGE);
183
0
      rgb_to_cmyk(255, colormap[0][t], colormap[1][t], colormap[2][t], outptr,
184
0
                  outptr + 1, outptr + 2, outptr + 3);
185
0
      outptr += 4;
186
0
    }
187
338
  } else {
188
338
    register int rindex = rgb_red[cinfo->in_color_space];
189
338
    register int gindex = rgb_green[cinfo->in_color_space];
190
338
    register int bindex = rgb_blue[cinfo->in_color_space];
191
338
    register int aindex = alpha_index[cinfo->in_color_space];
192
338
    register int ps = rgb_pixelsize[cinfo->in_color_space];
193
194
338
    if (aindex >= 0) {
195
0
      for (col = cinfo->image_width; col > 0; col--) {
196
0
        t = *inptr++;
197
0
        if (t >= cmaplen)
198
0
          ERREXIT(cinfo, JERR_BMP_OUTOFRANGE);
199
0
        outptr[rindex] = colormap[0][t];
200
0
        outptr[gindex] = colormap[1][t];
201
0
        outptr[bindex] = colormap[2][t];
202
0
        outptr[aindex] = 0xFF;
203
0
        outptr += ps;
204
0
      }
205
338
    } else {
206
2.21k
      for (col = cinfo->image_width; col > 0; col--) {
207
1.87k
        t = *inptr++;
208
1.87k
        if (t >= cmaplen)
209
34
          ERREXIT(cinfo, JERR_BMP_OUTOFRANGE);
210
1.87k
        outptr[rindex] = colormap[0][t];
211
1.87k
        outptr[gindex] = colormap[1][t];
212
1.87k
        outptr[bindex] = colormap[2][t];
213
1.87k
        outptr += ps;
214
1.87k
      }
215
338
    }
216
338
  }
217
218
582
  return 1;
219
582
}
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
226
{
226
226
  bmp_source_ptr source = (bmp_source_ptr)sinfo;
227
226
  JSAMPARRAY image_ptr;
228
226
  register JSAMPROW inptr, outptr;
229
226
  register JDIMENSION col;
230
231
226
  if (source->use_inversion_array) {
232
    /* Fetch next row from virtual array */
233
226
    source->source_row--;
234
226
    image_ptr = (*cinfo->mem->access_virt_sarray)
235
226
      ((j_common_ptr)cinfo, source->whole_image,
236
226
       source->source_row, (JDIMENSION)1, FALSE);
237
226
    inptr = image_ptr[0];
238
226
  } else {
239
0
    if (!ReadOK(source->pub.input_file, source->iobuffer, source->row_width))
240
0
      ERREXIT(cinfo, JERR_INPUT_EOF);
241
0
    inptr = source->iobuffer;
242
0
  }
243
244
  /* Transfer data.  Note source values are in BGR order
245
   * (even though Microsoft's own documents say the opposite).
246
   */
247
226
  outptr = source->pub.buffer[0];
248
226
  if (cinfo->in_color_space == JCS_EXT_BGR) {
249
0
    memcpy(outptr, inptr, source->row_width);
250
226
  } else if (cinfo->in_color_space == JCS_CMYK) {
251
0
    for (col = cinfo->image_width; col > 0; col--) {
252
0
      JSAMPLE b = *inptr++, g = *inptr++, r = *inptr++;
253
0
      rgb_to_cmyk(255, r, g, b, outptr, outptr + 1, outptr + 2, outptr + 3);
254
0
      outptr += 4;
255
0
    }
256
226
  } else {
257
226
    register int rindex = rgb_red[cinfo->in_color_space];
258
226
    register int gindex = rgb_green[cinfo->in_color_space];
259
226
    register int bindex = rgb_blue[cinfo->in_color_space];
260
226
    register int aindex = alpha_index[cinfo->in_color_space];
261
226
    register int ps = rgb_pixelsize[cinfo->in_color_space];
262
263
226
    if (aindex >= 0) {
264
0
      for (col = cinfo->image_width; col > 0; col--) {
265
0
        outptr[bindex] = *inptr++;
266
0
        outptr[gindex] = *inptr++;
267
0
        outptr[rindex] = *inptr++;
268
0
        outptr[aindex] = 0xFF;
269
0
        outptr += ps;
270
0
      }
271
226
    } else {
272
792
      for (col = cinfo->image_width; col > 0; col--) {
273
566
        outptr[bindex] = *inptr++;
274
566
        outptr[gindex] = *inptr++;
275
566
        outptr[rindex] = *inptr++;
276
566
        outptr += ps;
277
566
      }
278
226
    }
279
226
  }
280
281
226
  return 1;
282
226
}
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
246
{
289
246
  bmp_source_ptr source = (bmp_source_ptr)sinfo;
290
246
  JSAMPARRAY image_ptr;
291
246
  register JSAMPROW inptr, outptr;
292
246
  register JDIMENSION col;
293
294
246
  if (source->use_inversion_array) {
295
    /* Fetch next row from virtual array */
296
246
    source->source_row--;
297
246
    image_ptr = (*cinfo->mem->access_virt_sarray)
298
246
      ((j_common_ptr)cinfo, source->whole_image,
299
246
       source->source_row, (JDIMENSION)1, FALSE);
300
246
    inptr = image_ptr[0];
301
246
  } else {
302
0
    if (!ReadOK(source->pub.input_file, source->iobuffer, source->row_width))
303
0
      ERREXIT(cinfo, JERR_INPUT_EOF);
304
0
    inptr = source->iobuffer;
305
0
  }
306
307
  /* Transfer data.  Note source values are in BGR order
308
   * (even though Microsoft's own documents say the opposite).
309
   */
310
246
  outptr = source->pub.buffer[0];
311
246
  if (cinfo->in_color_space == JCS_EXT_BGRX ||
312
246
      cinfo->in_color_space == JCS_EXT_BGRA) {
313
0
    memcpy(outptr, inptr, source->row_width);
314
246
  } else if (cinfo->in_color_space == JCS_CMYK) {
315
0
    for (col = cinfo->image_width; col > 0; col--) {
316
0
      JSAMPLE b = *inptr++, g = *inptr++, r = *inptr++;
317
0
      rgb_to_cmyk(255, r, g, b, outptr, outptr + 1, outptr + 2, outptr + 3);
318
0
      inptr++;                          /* skip the 4th byte (Alpha channel) */
319
0
      outptr += 4;
320
0
    }
321
246
  } else {
322
246
    register int rindex = rgb_red[cinfo->in_color_space];
323
246
    register int gindex = rgb_green[cinfo->in_color_space];
324
246
    register int bindex = rgb_blue[cinfo->in_color_space];
325
246
    register int aindex = alpha_index[cinfo->in_color_space];
326
246
    register int ps = rgb_pixelsize[cinfo->in_color_space];
327
328
246
    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
246
    } else {
337
722
      for (col = cinfo->image_width; col > 0; col--) {
338
476
        outptr[bindex] = *inptr++;
339
476
        outptr[gindex] = *inptr++;
340
476
        outptr[rindex] = *inptr++;
341
476
        inptr++;                        /* skip the 4th byte (Alpha channel) */
342
476
        outptr += ps;
343
476
      }
344
246
    }
345
246
  }
346
347
246
  return 1;
348
246
}
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
150
{
360
150
  bmp_source_ptr source = (bmp_source_ptr)sinfo;
361
150
  register FILE *infile = source->pub.input_file;
362
150
  register JSAMPROW out_ptr;
363
150
  JSAMPARRAY image_ptr;
364
150
  JDIMENSION row;
365
150
  cd_progress_ptr progress = (cd_progress_ptr)cinfo->progress;
366
367
  /* Read the data into a virtual array in input-file row order. */
368
1.56k
  for (row = 0; row < cinfo->image_height; row++) {
369
1.41k
    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
1.41k
    image_ptr = (*cinfo->mem->access_virt_sarray)
375
1.41k
      ((j_common_ptr)cinfo, source->whole_image, row, (JDIMENSION)1, TRUE);
376
1.41k
    out_ptr = image_ptr[0];
377
1.41k
    if (fread(out_ptr, 1, source->row_width, infile) != source->row_width) {
378
36
      if (feof(infile))
379
36
        ERREXIT(cinfo, JERR_INPUT_EOF);
380
0
      else
381
0
        ERREXIT(cinfo, JERR_FILE_READ);
382
36
    }
383
1.41k
  }
384
150
  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
150
  switch (source->bits_per_pixel) {
389
76
  case 8:
390
76
    source->pub.get_pixel_rows = get_8bit_row;
391
76
    break;
392
18
  case 24:
393
18
    source->pub.get_pixel_rows = get_24bit_row;
394
18
    break;
395
20
  case 32:
396
20
    source->pub.get_pixel_rows = get_32bit_row;
397
20
    break;
398
0
  default:
399
0
    ERREXIT(cinfo, JERR_BMP_BADDEPTH);
400
150
  }
401
114
  source->source_row = cinfo->image_height;
402
403
  /* And read the first row */
404
114
  return (*source->pub.get_pixel_rows) (cinfo, sinfo);
405
150
}
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
1.27k
{
415
1.27k
  bmp_source_ptr source = (bmp_source_ptr)sinfo;
416
1.27k
  U_CHAR bmpfileheader[14];
417
1.27k
  U_CHAR bmpinfoheader[64];
418
419
1.27k
#define GET_2B(array, offset) \
420
3.69k
  ((unsigned short)UCH(array[offset]) + \
421
3.69k
   (((unsigned short)UCH(array[offset + 1])) << 8))
422
1.27k
#define GET_4B(array, offset) \
423
7.82k
  ((unsigned int)UCH(array[offset]) + \
424
7.82k
   (((unsigned int)UCH(array[offset + 1])) << 8) + \
425
7.82k
   (((unsigned int)UCH(array[offset + 2])) << 16) + \
426
7.82k
   (((unsigned int)UCH(array[offset + 3])) << 24))
427
428
1.27k
  int bfOffBits;
429
1.27k
  int headerSize;
430
1.27k
  int biWidth;
431
1.27k
  int biHeight;
432
1.27k
  unsigned short biPlanes;
433
1.27k
  unsigned int biCompression;
434
1.27k
  int biXPelsPerMeter, biYPelsPerMeter;
435
1.27k
  int biClrUsed = 0;
436
1.27k
  int mapentrysize = 0;         /* 0 indicates no colormap */
437
1.27k
  int bPad;
438
1.27k
  JDIMENSION row_width = 0;
439
440
  /* Read and verify the bitmap file header */
441
1.27k
  if (!ReadOK(source->pub.input_file, bmpfileheader, 14))
442
14
    ERREXIT(cinfo, JERR_INPUT_EOF);
443
1.27k
  if (GET_2B(bmpfileheader, 0) != 0x4D42) /* 'BM' */
444
16
    ERREXIT(cinfo, JERR_BMP_NOT);
445
1.27k
  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
1.27k
  if (!ReadOK(source->pub.input_file, bmpinfoheader, 4))
452
6
    ERREXIT(cinfo, JERR_INPUT_EOF);
453
1.27k
  headerSize = GET_4B(bmpinfoheader, 0);
454
1.27k
  if (headerSize < 12 || headerSize > 64 || (headerSize + 14) > bfOffBits)
455
178
    ERREXIT(cinfo, JERR_BMP_BADHEADER);
456
1.27k
  if (!ReadOK(source->pub.input_file, bmpinfoheader + 4, headerSize - 4))
457
16
    ERREXIT(cinfo, JERR_INPUT_EOF);
458
459
1.27k
  switch (headerSize) {
460
166
  case 12:
461
    /* Decode OS/2 1.x header (Microsoft calls this a BITMAPCOREHEADER) */
462
166
    biWidth = (int)GET_2B(bmpinfoheader, 4);
463
166
    biHeight = (int)GET_2B(bmpinfoheader, 6);
464
166
    biPlanes = GET_2B(bmpinfoheader, 8);
465
166
    source->bits_per_pixel = (int)GET_2B(bmpinfoheader, 10);
466
467
166
    switch (source->bits_per_pixel) {
468
128
    case 8:                     /* colormapped image */
469
128
      mapentrysize = 3;         /* OS/2 uses RGBTRIPLE colormap */
470
128
      TRACEMS2(cinfo, 1, JTRC_BMP_OS2_MAPPED, biWidth, biHeight);
471
128
      break;
472
16
    case 24:                    /* RGB image */
473
36
    case 32:                    /* RGB image + Alpha channel */
474
36
      TRACEMS3(cinfo, 1, JTRC_BMP_OS2, biWidth, biHeight,
475
36
               source->bits_per_pixel);
476
36
      break;
477
2
    default:
478
2
      ERREXIT(cinfo, JERR_BMP_BADDEPTH);
479
2
      break;
480
166
    }
481
164
    break;
482
870
  case 40:
483
878
  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
878
    biWidth = (int)GET_4B(bmpinfoheader, 4);
487
878
    biHeight = (int)GET_4B(bmpinfoheader, 8);
488
878
    biPlanes = GET_2B(bmpinfoheader, 12);
489
878
    source->bits_per_pixel = (int)GET_2B(bmpinfoheader, 14);
490
878
    biCompression = GET_4B(bmpinfoheader, 16);
491
878
    biXPelsPerMeter = (int)GET_4B(bmpinfoheader, 24);
492
878
    biYPelsPerMeter = (int)GET_4B(bmpinfoheader, 28);
493
878
    biClrUsed = GET_4B(bmpinfoheader, 32);
494
    /* biSizeImage, biClrImportant fields are ignored */
495
496
878
    switch (source->bits_per_pixel) {
497
560
    case 8:                     /* colormapped image */
498
560
      mapentrysize = 4;         /* Windows uses RGBQUAD colormap */
499
560
      TRACEMS2(cinfo, 1, JTRC_BMP_MAPPED, biWidth, biHeight);
500
560
      break;
501
208
    case 24:                    /* RGB image */
502
314
    case 32:                    /* RGB image + Alpha channel */
503
314
      TRACEMS3(cinfo, 1, JTRC_BMP, biWidth, biHeight, source->bits_per_pixel);
504
314
      break;
505
4
    default:
506
4
      ERREXIT(cinfo, JERR_BMP_BADDEPTH);
507
4
      break;
508
878
    }
509
874
    if (biCompression != 0)
510
80
      ERREXIT(cinfo, JERR_BMP_COMPRESSED);
511
512
874
    if (biXPelsPerMeter > 0 && biYPelsPerMeter > 0) {
513
      /* Set JFIF density parameters from the BMP data */
514
358
      cinfo->X_density = (UINT16)(biXPelsPerMeter / 100); /* 100 cm per meter */
515
358
      cinfo->Y_density = (UINT16)(biYPelsPerMeter / 100);
516
358
      cinfo->density_unit = 2;  /* dots/cm */
517
358
    }
518
874
    break;
519
2
  default:
520
2
    ERREXIT(cinfo, JERR_BMP_BADHEADER);
521
2
    return;
522
1.27k
  }
523
524
958
  if (biWidth <= 0 || biHeight <= 0)
525
124
    ERREXIT(cinfo, JERR_BMP_EMPTY);
526
958
  if (sinfo->max_pixels &&
527
834
      (unsigned long long)biWidth * biHeight > sinfo->max_pixels)
528
170
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
529
958
  if (biPlanes != 1)
530
34
    ERREXIT(cinfo, JERR_BMP_BADPLANES);
531
532
  /* Compute distance to bitmap data --- will adjust for colormap below */
533
958
  bPad = bfOffBits - (headerSize + 14);
534
535
  /* Read the colormap, if any */
536
958
  if (mapentrysize > 0) {
537
446
    if (biClrUsed <= 0)
538
268
      biClrUsed = 256;          /* assume it's 256 */
539
178
    else if (biClrUsed > 256)
540
46
      ERREXIT(cinfo, JERR_BMP_BADCMAP);
541
    /* Allocate space to store the colormap */
542
446
    source->colormap = (*cinfo->mem->alloc_sarray)
543
446
      ((j_common_ptr)cinfo, JPOOL_IMAGE, (JDIMENSION)biClrUsed, (JDIMENSION)3);
544
446
    source->cmap_length = (int)biClrUsed;
545
    /* and read it from the file */
546
446
    read_colormap(source, (int)biClrUsed, mapentrysize);
547
    /* account for size of colormap */
548
446
    bPad -= biClrUsed * mapentrysize;
549
446
  }
550
551
  /* Skip any remaining pad bytes */
552
958
  if (bPad < 0)                 /* incorrect bfOffBits value? */
553
22
    ERREXIT(cinfo, JERR_BMP_BADHEADER);
554
10.2k
  while (--bPad >= 0) {
555
9.33k
    (void)read_byte(source);
556
9.33k
  }
557
558
  /* Compute row width in file, including padding to 4-byte boundary */
559
958
  switch (source->bits_per_pixel) {
560
80
  case 8:
561
80
    if (cinfo->in_color_space == JCS_UNKNOWN)
562
0
      cinfo->in_color_space = JCS_EXT_RGB;
563
80
    if (IsExtRGB(cinfo->in_color_space))
564
44
      cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
565
36
    else if (cinfo->in_color_space == JCS_GRAYSCALE)
566
36
      cinfo->input_components = 1;
567
0
    else if (cinfo->in_color_space == JCS_CMYK)
568
0
      cinfo->input_components = 4;
569
0
    else
570
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
571
80
    row_width = (JDIMENSION)biWidth;
572
80
    break;
573
68
  case 24:
574
68
    if (cinfo->in_color_space == JCS_UNKNOWN)
575
0
      cinfo->in_color_space = JCS_EXT_BGR;
576
68
    if (IsExtRGB(cinfo->in_color_space))
577
68
      cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
578
0
    else if (cinfo->in_color_space == JCS_CMYK)
579
0
      cinfo->input_components = 4;
580
0
    else
581
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
582
68
    if ((unsigned long long)biWidth * 3ULL > 0xFFFFFFFFULL)
583
0
      ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
584
68
    row_width = (JDIMENSION)biWidth * 3;
585
68
    break;
586
44
  case 32:
587
44
    if (cinfo->in_color_space == JCS_UNKNOWN)
588
0
      cinfo->in_color_space = JCS_EXT_BGRA;
589
44
    if (IsExtRGB(cinfo->in_color_space))
590
44
      cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
591
0
    else if (cinfo->in_color_space == JCS_CMYK)
592
0
      cinfo->input_components = 4;
593
0
    else
594
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
595
44
    if ((unsigned long long)biWidth * 4ULL > 0xFFFFFFFFULL)
596
0
      ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
597
44
    row_width = (JDIMENSION)biWidth * 4;
598
44
    break;
599
0
  default:
600
0
    ERREXIT(cinfo, JERR_BMP_BADDEPTH);
601
958
  }
602
370
  while ((row_width & 3) != 0) row_width++;
603
192
  source->row_width = row_width;
604
605
192
  if (source->use_inversion_array) {
606
    /* Allocate space for inversion array, prepare for preload pass */
607
192
    source->whole_image = (*cinfo->mem->request_virt_sarray)
608
192
      ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
609
192
       row_width, (JDIMENSION)biHeight, (JDIMENSION)1);
610
192
    source->pub.get_pixel_rows = preload_image;
611
192
    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
192
  } else {
616
0
    source->iobuffer = (U_CHAR *)
617
0
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, row_width);
618
0
    switch (source->bits_per_pixel) {
619
0
    case 8:
620
0
      source->pub.get_pixel_rows = get_8bit_row;
621
0
      break;
622
0
    case 24:
623
0
      source->pub.get_pixel_rows = get_24bit_row;
624
0
      break;
625
0
    case 32:
626
0
      source->pub.get_pixel_rows = get_32bit_row;
627
0
      break;
628
0
    default:
629
0
      ERREXIT(cinfo, JERR_BMP_BADDEPTH);
630
0
    }
631
0
  }
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
192
  if ((unsigned long long)biWidth *
637
192
      (unsigned long long)cinfo->input_components > 0xFFFFFFFFULL)
638
0
    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
639
  /* Allocate one-row buffer for returned data */
640
192
  source->pub.buffer = (*cinfo->mem->alloc_sarray)
641
192
    ((j_common_ptr)cinfo, JPOOL_IMAGE,
642
192
     (JDIMENSION)biWidth * (JDIMENSION)cinfo->input_components, (JDIMENSION)1);
643
192
  source->pub.buffer_height = 1;
644
645
192
  cinfo->data_precision = 8;
646
192
  cinfo->image_width = (JDIMENSION)biWidth;
647
192
  cinfo->image_height = (JDIMENSION)biHeight;
648
192
}
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
54
{
658
  /* no work */
659
54
}
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
1.27k
{
669
1.27k
  bmp_source_ptr source;
670
671
1.27k
  if (cinfo->data_precision != 8)
672
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
673
674
  /* Create module interface object */
675
1.27k
  source = (bmp_source_ptr)
676
1.27k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
677
1.27k
                                sizeof(bmp_source_struct));
678
1.27k
  source->cinfo = cinfo;        /* make back link for subroutines */
679
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
680
1.27k
  source->pub.start_input = start_input_bmp;
681
1.27k
  source->pub.finish_input = finish_input_bmp;
682
1.27k
  source->pub.max_pixels = 0;
683
684
1.27k
  source->use_inversion_array = use_inversion_array;
685
686
1.27k
  return (cjpeg_source_ptr)source;
687
1.27k
}
688
689
#endif /* BMP_SUPPORTED */