Coverage Report

Created: 2025-07-01 06:27

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