Coverage Report

Created: 2026-07-30 06:42

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