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
4.00k
  (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
83.2k
{
78
83.2k
  register FILE *infile = sinfo->pub.input_file;
79
83.2k
  register int c;
80
81
83.2k
  if ((c = getc(infile)) == EOF)
82
378
    ERREXIT(sinfo->cinfo, JERR_INPUT_EOF);
83
83.2k
  return c;
84
83.2k
}
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
444
{
91
444
  int i, gray = 1;
92
93
444
  switch (mapentrysize) {
94
120
  case 3:
95
    /* BGR format (occurs in OS/2 files) */
96
3.19k
    for (i = 0; i < cmaplen; i++) {
97
3.07k
      sinfo->colormap[2][i] = (JSAMPLE)read_byte(sinfo);
98
3.07k
      sinfo->colormap[1][i] = (JSAMPLE)read_byte(sinfo);
99
3.07k
      sinfo->colormap[0][i] = (JSAMPLE)read_byte(sinfo);
100
3.07k
      if (sinfo->colormap[2][i] != sinfo->colormap[1][i] ||
101
1.91k
          sinfo->colormap[1][i] != sinfo->colormap[0][i])
102
1.45k
        gray = 0;
103
3.07k
    }
104
120
    break;
105
324
  case 4:
106
    /* BGR0 format (occurs in MS Windows files) */
107
5.53k
    for (i = 0; i < cmaplen; i++) {
108
5.21k
      sinfo->colormap[2][i] = (JSAMPLE)read_byte(sinfo);
109
5.21k
      sinfo->colormap[1][i] = (JSAMPLE)read_byte(sinfo);
110
5.21k
      sinfo->colormap[0][i] = (JSAMPLE)read_byte(sinfo);
111
5.21k
      (void)read_byte(sinfo);
112
5.21k
      if (sinfo->colormap[2][i] != sinfo->colormap[1][i] ||
113
3.93k
          sinfo->colormap[1][i] != sinfo->colormap[0][i])
114
1.66k
        gray = 0;
115
5.21k
    }
116
324
    break;
117
0
  default:
118
0
    ERREXIT(sinfo->cinfo, JERR_BMP_BADCMAP);
119
0
    break;
120
444
  }
121
122
140
  if ((sinfo->cinfo->in_color_space == JCS_UNKNOWN ||
123
140
       sinfo->cinfo->in_color_space == JCS_RGB) && gray)
124
66
    sinfo->cinfo->in_color_space = JCS_GRAYSCALE;
125
126
140
  if (sinfo->cinfo->in_color_space == JCS_GRAYSCALE && !gray)
127
0
    ERREXIT(sinfo->cinfo, JERR_BAD_IN_COLORSPACE);
128
140
}
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
703
{
142
703
  bmp_source_ptr source = (bmp_source_ptr)sinfo;
143
703
  register JSAMPARRAY colormap = source->colormap;
144
703
  int cmaplen = source->cmap_length;
145
703
  JSAMPARRAY image_ptr;
146
703
  register int t;
147
703
  register JSAMPROW inptr, outptr;
148
703
  register JDIMENSION col;
149
150
703
  if (source->use_inversion_array) {
151
    /* Fetch next row from virtual array */
152
703
    source->source_row--;
153
703
    image_ptr = (*cinfo->mem->access_virt_sarray)
154
703
      ((j_common_ptr)cinfo, source->whole_image,
155
703
       source->source_row, (JDIMENSION)1, FALSE);
156
703
    inptr = image_ptr[0];
157
703
  } else {
158
0
    if (!ReadOK(source->pub.input_file, source->iobuffer, source->row_width))
159
0
      ERREXIT(cinfo, JERR_INPUT_EOF);
160
0
    inptr = source->iobuffer;
161
0
  }
162
163
  /* Expand the colormap indexes to real data */
164
703
  outptr = source->pub.buffer[0];
165
703
  if (cinfo->in_color_space == JCS_GRAYSCALE) {
166
2.08k
    for (col = cinfo->image_width; col > 0; col--) {
167
1.74k
      t = *inptr++;
168
1.74k
      if (t >= cmaplen)
169
19
        ERREXIT(cinfo, JERR_BMP_OUTOFRANGE);
170
1.74k
      *outptr++ = colormap[0][t];
171
1.74k
    }
172
362
  } else if (cinfo->in_color_space == JCS_CMYK) {
173
0
    for (col = cinfo->image_width; col > 0; col--) {
174
0
      t = *inptr++;
175
0
      if (t >= cmaplen)
176
0
        ERREXIT(cinfo, JERR_BMP_OUTOFRANGE);
177
0
      rgb_to_cmyk(colormap[0][t], colormap[1][t], colormap[2][t], outptr,
178
0
                  outptr + 1, outptr + 2, outptr + 3);
179
0
      outptr += 4;
180
0
    }
181
362
  } else {
182
362
    register int rindex = rgb_red[cinfo->in_color_space];
183
362
    register int gindex = rgb_green[cinfo->in_color_space];
184
362
    register int bindex = rgb_blue[cinfo->in_color_space];
185
362
    register int aindex = alpha_index[cinfo->in_color_space];
186
362
    register int ps = rgb_pixelsize[cinfo->in_color_space];
187
188
362
    if (aindex >= 0) {
189
0
      for (col = cinfo->image_width; col > 0; col--) {
190
0
        t = *inptr++;
191
0
        if (t >= cmaplen)
192
0
          ERREXIT(cinfo, JERR_BMP_OUTOFRANGE);
193
0
        outptr[rindex] = colormap[0][t];
194
0
        outptr[gindex] = colormap[1][t];
195
0
        outptr[bindex] = colormap[2][t];
196
0
        outptr[aindex] = 0xFF;
197
0
        outptr += ps;
198
0
      }
199
362
    } else {
200
2.13k
      for (col = cinfo->image_width; col > 0; col--) {
201
1.77k
        t = *inptr++;
202
1.77k
        if (t >= cmaplen)
203
24
          ERREXIT(cinfo, JERR_BMP_OUTOFRANGE);
204
1.77k
        outptr[rindex] = colormap[0][t];
205
1.77k
        outptr[gindex] = colormap[1][t];
206
1.77k
        outptr[bindex] = colormap[2][t];
207
1.77k
        outptr += ps;
208
1.77k
      }
209
362
    }
210
362
  }
211
212
703
  return 1;
213
703
}
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
416
{
220
416
  bmp_source_ptr source = (bmp_source_ptr)sinfo;
221
416
  JSAMPARRAY image_ptr;
222
416
  register JSAMPROW inptr, outptr;
223
416
  register JDIMENSION col;
224
225
416
  if (source->use_inversion_array) {
226
    /* Fetch next row from virtual array */
227
416
    source->source_row--;
228
416
    image_ptr = (*cinfo->mem->access_virt_sarray)
229
416
      ((j_common_ptr)cinfo, source->whole_image,
230
416
       source->source_row, (JDIMENSION)1, FALSE);
231
416
    inptr = image_ptr[0];
232
416
  } else {
233
0
    if (!ReadOK(source->pub.input_file, source->iobuffer, source->row_width))
234
0
      ERREXIT(cinfo, JERR_INPUT_EOF);
235
0
    inptr = source->iobuffer;
236
0
  }
237
238
  /* Transfer data.  Note source values are in BGR order
239
   * (even though Microsoft's own documents say the opposite).
240
   */
241
416
  outptr = source->pub.buffer[0];
242
416
  if (cinfo->in_color_space == JCS_EXT_BGR) {
243
0
    memcpy(outptr, inptr, source->row_width);
244
416
  } else if (cinfo->in_color_space == JCS_CMYK) {
245
0
    for (col = cinfo->image_width; col > 0; col--) {
246
0
      JSAMPLE b = *inptr++, g = *inptr++, r = *inptr++;
247
0
      rgb_to_cmyk(r, g, b, outptr, outptr + 1, outptr + 2, outptr + 3);
248
0
      outptr += 4;
249
0
    }
250
416
  } else {
251
416
    register int rindex = rgb_red[cinfo->in_color_space];
252
416
    register int gindex = rgb_green[cinfo->in_color_space];
253
416
    register int bindex = rgb_blue[cinfo->in_color_space];
254
416
    register int aindex = alpha_index[cinfo->in_color_space];
255
416
    register int ps = rgb_pixelsize[cinfo->in_color_space];
256
257
416
    if (aindex >= 0) {
258
0
      for (col = cinfo->image_width; col > 0; col--) {
259
0
        outptr[bindex] = *inptr++;
260
0
        outptr[gindex] = *inptr++;
261
0
        outptr[rindex] = *inptr++;
262
0
        outptr[aindex] = 0xFF;
263
0
        outptr += ps;
264
0
      }
265
416
    } else {
266
3.17k
      for (col = cinfo->image_width; col > 0; col--) {
267
2.75k
        outptr[bindex] = *inptr++;
268
2.75k
        outptr[gindex] = *inptr++;
269
2.75k
        outptr[rindex] = *inptr++;
270
2.75k
        outptr += ps;
271
2.75k
      }
272
416
    }
273
416
  }
274
275
416
  return 1;
276
416
}
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
310
{
283
310
  bmp_source_ptr source = (bmp_source_ptr)sinfo;
284
310
  JSAMPARRAY image_ptr;
285
310
  register JSAMPROW inptr, outptr;
286
310
  register JDIMENSION col;
287
288
310
  if (source->use_inversion_array) {
289
    /* Fetch next row from virtual array */
290
310
    source->source_row--;
291
310
    image_ptr = (*cinfo->mem->access_virt_sarray)
292
310
      ((j_common_ptr)cinfo, source->whole_image,
293
310
       source->source_row, (JDIMENSION)1, FALSE);
294
310
    inptr = image_ptr[0];
295
310
  } else {
296
0
    if (!ReadOK(source->pub.input_file, source->iobuffer, source->row_width))
297
0
      ERREXIT(cinfo, JERR_INPUT_EOF);
298
0
    inptr = source->iobuffer;
299
0
  }
300
301
  /* Transfer data.  Note source values are in BGR order
302
   * (even though Microsoft's own documents say the opposite).
303
   */
304
310
  outptr = source->pub.buffer[0];
305
310
  if (cinfo->in_color_space == JCS_EXT_BGRX ||
306
310
      cinfo->in_color_space == JCS_EXT_BGRA) {
307
0
    memcpy(outptr, inptr, source->row_width);
308
310
  } else if (cinfo->in_color_space == JCS_CMYK) {
309
0
    for (col = cinfo->image_width; col > 0; col--) {
310
0
      JSAMPLE b = *inptr++, g = *inptr++, r = *inptr++;
311
0
      rgb_to_cmyk(r, g, b, outptr, outptr + 1, outptr + 2, outptr + 3);
312
0
      inptr++;                          /* skip the 4th byte (Alpha channel) */
313
0
      outptr += 4;
314
0
    }
315
310
  } else {
316
310
    register int rindex = rgb_red[cinfo->in_color_space];
317
310
    register int gindex = rgb_green[cinfo->in_color_space];
318
310
    register int bindex = rgb_blue[cinfo->in_color_space];
319
310
    register int aindex = alpha_index[cinfo->in_color_space];
320
310
    register int ps = rgb_pixelsize[cinfo->in_color_space];
321
322
310
    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
310
    } else {
331
848
      for (col = cinfo->image_width; col > 0; col--) {
332
538
        outptr[bindex] = *inptr++;
333
538
        outptr[gindex] = *inptr++;
334
538
        outptr[rindex] = *inptr++;
335
538
        inptr++;                        /* skip the 4th byte (Alpha channel) */
336
538
        outptr += ps;
337
538
      }
338
310
    }
339
310
  }
340
341
310
  return 1;
342
310
}
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
163
{
354
163
  bmp_source_ptr source = (bmp_source_ptr)sinfo;
355
163
  register FILE *infile = source->pub.input_file;
356
163
  register JSAMPROW out_ptr;
357
163
  JSAMPARRAY image_ptr;
358
163
  JDIMENSION row;
359
163
  cd_progress_ptr progress = (cd_progress_ptr)cinfo->progress;
360
361
  /* Read the data into a virtual array in input-file row order. */
362
2.56k
  for (row = 0; row < cinfo->image_height; row++) {
363
2.40k
    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
2.40k
    image_ptr = (*cinfo->mem->access_virt_sarray)
369
2.40k
      ((j_common_ptr)cinfo, source->whole_image, row, (JDIMENSION)1, TRUE);
370
2.40k
    out_ptr = image_ptr[0];
371
2.40k
    if (fread(out_ptr, 1, source->row_width, infile) != source->row_width) {
372
64
      if (feof(infile))
373
64
        ERREXIT(cinfo, JERR_INPUT_EOF);
374
0
      else
375
0
        ERREXIT(cinfo, JERR_FILE_READ);
376
64
    }
377
2.40k
  }
378
163
  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
163
  switch (source->bits_per_pixel) {
383
61
  case 8:
384
61
    source->pub.get_pixel_rows = get_8bit_row;
385
61
    break;
386
20
  case 24:
387
20
    source->pub.get_pixel_rows = get_24bit_row;
388
20
    break;
389
18
  case 32:
390
18
    source->pub.get_pixel_rows = get_32bit_row;
391
18
    break;
392
0
  default:
393
0
    ERREXIT(cinfo, JERR_BMP_BADDEPTH);
394
163
  }
395
99
  source->source_row = cinfo->image_height;
396
397
  /* And read the first row */
398
99
  return (*source->pub.get_pixel_rows) (cinfo, sinfo);
399
163
}
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
1.33k
{
409
1.33k
  bmp_source_ptr source = (bmp_source_ptr)sinfo;
410
1.33k
  unsigned char bmpfileheader[14];
411
1.33k
  unsigned char bmpinfoheader[64];
412
413
1.33k
#define GET_2B(array, offset) \
414
4.12k
  ((unsigned short)array[offset] + \
415
4.12k
   (((unsigned short)array[offset + 1]) << 8))
416
1.33k
#define GET_4B(array, offset) \
417
7.53k
  ((unsigned int)array[offset] + \
418
7.53k
   (((unsigned int)array[offset + 1]) << 8) + \
419
7.53k
   (((unsigned int)array[offset + 2]) << 16) + \
420
7.53k
   (((unsigned int)array[offset + 3]) << 24))
421
422
1.33k
  int bfOffBits;
423
1.33k
  int headerSize;
424
1.33k
  int biWidth;
425
1.33k
  int biHeight;
426
1.33k
  unsigned short biPlanes;
427
1.33k
  unsigned int biCompression;
428
1.33k
  int biXPelsPerMeter, biYPelsPerMeter;
429
1.33k
  int biClrUsed = 0;
430
1.33k
  int mapentrysize = 0;         /* 0 indicates no colormap */
431
1.33k
  int bPad;
432
1.33k
  JDIMENSION row_width = 0;
433
434
  /* Read and verify the bitmap file header */
435
1.33k
  if (!ReadOK(source->pub.input_file, bmpfileheader, 14))
436
14
    ERREXIT(cinfo, JERR_INPUT_EOF);
437
1.33k
  if (GET_2B(bmpfileheader, 0) != 0x4D42) /* 'BM' */
438
24
    ERREXIT(cinfo, JERR_BMP_NOT);
439
1.33k
  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
1.33k
  if (!ReadOK(source->pub.input_file, bmpinfoheader, 4))
446
6
    ERREXIT(cinfo, JERR_INPUT_EOF);
447
1.33k
  headerSize = GET_4B(bmpinfoheader, 0);
448
1.33k
  if (headerSize < 12 || headerSize > 64 || (headerSize + 14) > bfOffBits)
449
180
    ERREXIT(cinfo, JERR_BMP_BADHEADER);
450
1.33k
  if (!ReadOK(source->pub.input_file, bmpinfoheader + 4, headerSize - 4))
451
8
    ERREXIT(cinfo, JERR_INPUT_EOF);
452
453
1.33k
  switch (headerSize) {
454
292
  case 12:
455
    /* Decode OS/2 1.x header (Microsoft calls this a BITMAPCOREHEADER) */
456
292
    biWidth = (int)GET_2B(bmpinfoheader, 4);
457
292
    biHeight = (int)GET_2B(bmpinfoheader, 6);
458
292
    biPlanes = GET_2B(bmpinfoheader, 8);
459
292
    source->bits_per_pixel = (int)GET_2B(bmpinfoheader, 10);
460
461
292
    switch (source->bits_per_pixel) {
462
178
    case 8:                     /* colormapped image */
463
178
      mapentrysize = 3;         /* OS/2 uses RGBTRIPLE colormap */
464
178
      TRACEMS2(cinfo, 1, JTRC_BMP_OS2_MAPPED, biWidth, biHeight);
465
178
      break;
466
62
    case 24:                    /* RGB image */
467
112
    case 32:                    /* RGB image + Alpha channel */
468
112
      TRACEMS3(cinfo, 1, JTRC_BMP_OS2, biWidth, biHeight,
469
112
               source->bits_per_pixel);
470
112
      break;
471
2
    default:
472
2
      ERREXIT(cinfo, JERR_BMP_BADDEPTH);
473
2
      break;
474
292
    }
475
290
    break;
476
808
  case 40:
477
810
  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
810
    biWidth = (int)GET_4B(bmpinfoheader, 4);
481
810
    biHeight = (int)GET_4B(bmpinfoheader, 8);
482
810
    biPlanes = GET_2B(bmpinfoheader, 12);
483
810
    source->bits_per_pixel = (int)GET_2B(bmpinfoheader, 14);
484
810
    biCompression = GET_4B(bmpinfoheader, 16);
485
810
    biXPelsPerMeter = (int)GET_4B(bmpinfoheader, 24);
486
810
    biYPelsPerMeter = (int)GET_4B(bmpinfoheader, 28);
487
810
    biClrUsed = GET_4B(bmpinfoheader, 32);
488
    /* biSizeImage, biClrImportant fields are ignored */
489
490
810
    switch (source->bits_per_pixel) {
491
542
    case 8:                     /* colormapped image */
492
542
      mapentrysize = 4;         /* Windows uses RGBQUAD colormap */
493
542
      TRACEMS2(cinfo, 1, JTRC_BMP_MAPPED, biWidth, biHeight);
494
542
      break;
495
186
    case 24:                    /* RGB image */
496
264
    case 32:                    /* RGB image + Alpha channel */
497
264
      TRACEMS3(cinfo, 1, JTRC_BMP, biWidth, biHeight, source->bits_per_pixel);
498
264
      break;
499
4
    default:
500
4
      ERREXIT(cinfo, JERR_BMP_BADDEPTH);
501
4
      break;
502
810
    }
503
806
    if (biCompression != 0)
504
70
      ERREXIT(cinfo, JERR_BMP_COMPRESSED);
505
506
806
    if (biXPelsPerMeter > 0 && biYPelsPerMeter > 0) {
507
      /* Set JFIF density parameters from the BMP data */
508
260
      cinfo->X_density = (UINT16)(biXPelsPerMeter / 100); /* 100 cm per meter */
509
260
      cinfo->Y_density = (UINT16)(biYPelsPerMeter / 100);
510
260
      cinfo->density_unit = 2;  /* dots/cm */
511
260
    }
512
806
    break;
513
2
  default:
514
2
    ERREXIT(cinfo, JERR_BMP_BADHEADER);
515
2
    return;
516
1.33k
  }
517
518
1.02k
  if (biWidth <= 0 || biHeight <= 0)
519
148
    ERREXIT(cinfo, JERR_BMP_EMPTY);
520
1.02k
  if (biWidth > JPEG_MAX_DIMENSION || biHeight > JPEG_MAX_DIMENSION)
521
96
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, JPEG_MAX_DIMENSION);
522
1.02k
  if (sinfo->max_pixels &&
523
782
      (unsigned long long)biWidth * biHeight > sinfo->max_pixels)
524
98
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
525
1.02k
  if (biPlanes != 1)
526
36
    ERREXIT(cinfo, JERR_BMP_BADPLANES);
527
528
  /* Compute distance to bitmap data --- will adjust for colormap below */
529
1.02k
  bPad = bfOffBits - (headerSize + 14);
530
531
  /* Read the colormap, if any */
532
1.02k
  if (mapentrysize > 0) {
533
498
    if (biClrUsed <= 0)
534
278
      biClrUsed = 256;          /* assume it's 256 */
535
220
    else if (biClrUsed > 256)
536
54
      ERREXIT(cinfo, JERR_BMP_BADCMAP);
537
    /* Allocate space to store the colormap */
538
498
    source->colormap = (*cinfo->mem->alloc_sarray)
539
498
      ((j_common_ptr)cinfo, JPOOL_IMAGE, (JDIMENSION)biClrUsed, (JDIMENSION)3);
540
498
    source->cmap_length = (int)biClrUsed;
541
    /* and read it from the file */
542
498
    read_colormap(source, (int)biClrUsed, mapentrysize);
543
    /* account for size of colormap */
544
498
    bPad -= biClrUsed * mapentrysize;
545
498
  }
546
547
  /* Skip any remaining pad bytes */
548
1.02k
  if (bPad < 0)                 /* incorrect bfOffBits value? */
549
22
    ERREXIT(cinfo, JERR_BMP_BADHEADER);
550
54.7k
  while (--bPad >= 0) {
551
53.7k
    (void)read_byte(source);
552
53.7k
  }
553
554
  /* Compute row width in file, including padding to 4-byte boundary */
555
1.02k
  switch (source->bits_per_pixel) {
556
106
  case 8:
557
106
    if (cinfo->in_color_space == JCS_UNKNOWN)
558
0
      cinfo->in_color_space = JCS_EXT_RGB;
559
106
    if (IsExtRGB(cinfo->in_color_space))
560
44
      cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
561
62
    else if (cinfo->in_color_space == JCS_GRAYSCALE)
562
62
      cinfo->input_components = 1;
563
0
    else if (cinfo->in_color_space == JCS_CMYK)
564
0
      cinfo->input_components = 4;
565
0
    else
566
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
567
106
    row_width = (JDIMENSION)biWidth;
568
106
    break;
569
52
  case 24:
570
52
    if (cinfo->in_color_space == JCS_UNKNOWN)
571
0
      cinfo->in_color_space = JCS_EXT_BGR;
572
52
    if (IsExtRGB(cinfo->in_color_space))
573
52
      cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
574
0
    else if (cinfo->in_color_space == JCS_CMYK)
575
0
      cinfo->input_components = 4;
576
0
    else
577
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
578
52
    row_width = (JDIMENSION)biWidth * 3;
579
52
    break;
580
36
  case 32:
581
36
    if (cinfo->in_color_space == JCS_UNKNOWN)
582
0
      cinfo->in_color_space = JCS_EXT_BGRA;
583
36
    if (IsExtRGB(cinfo->in_color_space))
584
36
      cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
585
0
    else if (cinfo->in_color_space == JCS_CMYK)
586
0
      cinfo->input_components = 4;
587
0
    else
588
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
589
36
    row_width = (JDIMENSION)biWidth * 4;
590
36
    break;
591
0
  default:
592
0
    ERREXIT(cinfo, JERR_BMP_BADDEPTH);
593
1.02k
  }
594
484
  while ((row_width & 3) != 0) row_width++;
595
194
  source->row_width = row_width;
596
597
194
  if (source->use_inversion_array) {
598
    /* Allocate space for inversion array, prepare for preload pass */
599
194
    source->whole_image = (*cinfo->mem->request_virt_sarray)
600
194
      ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
601
194
       row_width, (JDIMENSION)biHeight, (JDIMENSION)1);
602
194
    source->pub.get_pixel_rows = preload_image;
603
194
    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
194
  } else {
608
0
    source->iobuffer = (unsigned char *)
609
0
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, row_width);
610
0
    switch (source->bits_per_pixel) {
611
0
    case 8:
612
0
      source->pub.get_pixel_rows = get_8bit_row;
613
0
      break;
614
0
    case 24:
615
0
      source->pub.get_pixel_rows = get_24bit_row;
616
0
      break;
617
0
    case 32:
618
0
      source->pub.get_pixel_rows = get_32bit_row;
619
0
      break;
620
0
    default:
621
0
      ERREXIT(cinfo, JERR_BMP_BADDEPTH);
622
0
    }
623
0
  }
624
625
  /* Allocate one-row buffer for returned data */
626
194
  source->pub.buffer = (*cinfo->mem->alloc_sarray)
627
194
    ((j_common_ptr)cinfo, JPOOL_IMAGE,
628
194
     (JDIMENSION)biWidth * (JDIMENSION)cinfo->input_components, (JDIMENSION)1);
629
194
  source->pub.buffer_height = 1;
630
631
194
  cinfo->data_precision = 8;
632
194
  cinfo->image_width = (JDIMENSION)biWidth;
633
194
  cinfo->image_height = (JDIMENSION)biHeight;
634
194
}
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
56
{
644
  /* no work */
645
56
}
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
1.33k
{
655
1.33k
  bmp_source_ptr source;
656
657
1.33k
  if (cinfo->data_precision != 8)
658
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
659
660
  /* Create module interface object */
661
1.33k
  source = (bmp_source_ptr)
662
1.33k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
663
1.33k
                                sizeof(bmp_source_struct));
664
1.33k
  source->cinfo = cinfo;        /* make back link for subroutines */
665
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
666
1.33k
  source->pub.start_input = start_input_bmp;
667
1.33k
  source->pub.finish_input = finish_input_bmp;
668
1.33k
  source->pub.max_pixels = 0;
669
670
1.33k
  source->use_inversion_array = use_inversion_array;
671
672
1.33k
  return (cjpeg_source_ptr)source;
673
1.33k
}
674
675
#endif /* BMP_SUPPORTED */