Coverage Report

Created: 2025-07-01 06:26

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