Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * rdbmp.c |
| 3 | * |
DRC | cf763c0 | 2013-01-01 09:51:37 +0000 | [diff] [blame] | 4 | * This file was part of the Independent JPEG Group's software: |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 5 | * Copyright (C) 1994-1996, Thomas G. Lane. |
Guido Vollbeding | f18f81b | 2010-02-28 00:00:00 +0000 | [diff] [blame] | 6 | * Modified 2009-2010 by Guido Vollbeding. |
DRC | a6ef282 | 2013-09-28 03:23:49 +0000 | [diff] [blame] | 7 | * libjpeg-turbo Modifications: |
DRC | 049aef5 | 2011-04-25 22:41:14 +0000 | [diff] [blame] | 8 | * Modified 2011 by Siarhei Siamashka. |
DRC | a3ef34c | 2015-08-13 11:09:05 -0500 | [diff] [blame^] | 9 | * Copyright (C) 2015, D. R. Commander. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 10 | * For conditions of distribution and use, see the accompanying README file. |
| 11 | * |
| 12 | * This file contains routines to read input images in Microsoft "BMP" |
| 13 | * format (MS Windows 3.x, OS/2 1.x, and OS/2 2.x flavors). |
| 14 | * Currently, only 8-bit and 24-bit images are supported, not 1-bit or |
| 15 | * 4-bit (feeding such low-depth images into JPEG would be silly anyway). |
| 16 | * Also, we don't support RLE-compressed files. |
| 17 | * |
| 18 | * These routines may need modification for non-Unix environments or |
| 19 | * specialized applications. As they stand, they assume input from |
| 20 | * an ordinary stdio stream. They further assume that reading begins |
| 21 | * at the start of the file; start_input may need work if the |
| 22 | * user interface has already read some data (e.g., to determine that |
| 23 | * the file is indeed BMP format). |
| 24 | * |
| 25 | * This code contributed by James Arthur Boucher. |
| 26 | */ |
| 27 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 28 | #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 29 | |
| 30 | #ifdef BMP_SUPPORTED |
| 31 | |
| 32 | |
| 33 | /* Macros to deal with unsigned chars as efficiently as compiler allows */ |
| 34 | |
| 35 | #ifdef HAVE_UNSIGNED_CHAR |
| 36 | typedef unsigned char U_CHAR; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 37 | #define UCH(x) ((int) (x)) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 38 | #else /* !HAVE_UNSIGNED_CHAR */ |
DRC | 61976bd | 2014-04-20 19:13:10 +0000 | [diff] [blame] | 39 | #ifdef __CHAR_UNSIGNED__ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 40 | typedef char U_CHAR; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 41 | #define UCH(x) ((int) (x)) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 42 | #else |
| 43 | typedef char U_CHAR; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 44 | #define UCH(x) ((int) (x) & 0xFF) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 45 | #endif |
| 46 | #endif /* HAVE_UNSIGNED_CHAR */ |
| 47 | |
| 48 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 49 | #define ReadOK(file,buffer,len) (JFREAD(file,buffer,len) == ((size_t) (len))) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 50 | |
| 51 | |
| 52 | /* Private version of data source object */ |
| 53 | |
| 54 | typedef struct _bmp_source_struct * bmp_source_ptr; |
| 55 | |
| 56 | typedef struct _bmp_source_struct { |
| 57 | struct cjpeg_source_struct pub; /* public fields */ |
| 58 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 59 | j_compress_ptr cinfo; /* back link saves passing separate parm */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 60 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 61 | JSAMPARRAY colormap; /* BMP colormap (converted to my format) */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 62 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 63 | jvirt_sarray_ptr whole_image; /* Needed to reverse row order */ |
| 64 | JDIMENSION source_row; /* Current source row number */ |
| 65 | JDIMENSION row_width; /* Physical width of scanlines in file */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 66 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 67 | int bits_per_pixel; /* remembers 8- or 24-bit format */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 68 | } bmp_source_struct; |
| 69 | |
| 70 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 71 | LOCAL(int) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 72 | read_byte (bmp_source_ptr sinfo) |
| 73 | /* Read next byte from BMP file */ |
| 74 | { |
| 75 | register FILE *infile = sinfo->pub.input_file; |
| 76 | register int c; |
| 77 | |
| 78 | if ((c = getc(infile)) == EOF) |
| 79 | ERREXIT(sinfo->cinfo, JERR_INPUT_EOF); |
| 80 | return c; |
| 81 | } |
| 82 | |
| 83 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 84 | LOCAL(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 85 | read_colormap (bmp_source_ptr sinfo, int cmaplen, int mapentrysize) |
| 86 | /* Read the colormap from a BMP file */ |
| 87 | { |
| 88 | int i; |
| 89 | |
| 90 | switch (mapentrysize) { |
| 91 | case 3: |
| 92 | /* BGR format (occurs in OS/2 files) */ |
| 93 | for (i = 0; i < cmaplen; i++) { |
| 94 | sinfo->colormap[2][i] = (JSAMPLE) read_byte(sinfo); |
| 95 | sinfo->colormap[1][i] = (JSAMPLE) read_byte(sinfo); |
| 96 | sinfo->colormap[0][i] = (JSAMPLE) read_byte(sinfo); |
| 97 | } |
| 98 | break; |
| 99 | case 4: |
| 100 | /* BGR0 format (occurs in MS Windows files) */ |
| 101 | for (i = 0; i < cmaplen; i++) { |
| 102 | sinfo->colormap[2][i] = (JSAMPLE) read_byte(sinfo); |
| 103 | sinfo->colormap[1][i] = (JSAMPLE) read_byte(sinfo); |
| 104 | sinfo->colormap[0][i] = (JSAMPLE) read_byte(sinfo); |
| 105 | (void) read_byte(sinfo); |
| 106 | } |
| 107 | break; |
| 108 | default: |
| 109 | ERREXIT(sinfo->cinfo, JERR_BMP_BADCMAP); |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | |
| 115 | /* |
| 116 | * Read one row of pixels. |
| 117 | * The image has been read into the whole_image array, but is otherwise |
| 118 | * unprocessed. We must read it out in top-to-bottom row order, and if |
| 119 | * it is an 8-bit image, we must expand colormapped pixels to 24bit format. |
| 120 | */ |
| 121 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 122 | METHODDEF(JDIMENSION) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 123 | get_8bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 124 | /* This version is for reading 8-bit colormap indexes */ |
| 125 | { |
| 126 | bmp_source_ptr source = (bmp_source_ptr) sinfo; |
| 127 | register JSAMPARRAY colormap = source->colormap; |
| 128 | JSAMPARRAY image_ptr; |
| 129 | register int t; |
| 130 | register JSAMPROW inptr, outptr; |
| 131 | register JDIMENSION col; |
| 132 | |
| 133 | /* Fetch next row from virtual array */ |
| 134 | source->source_row--; |
| 135 | image_ptr = (*cinfo->mem->access_virt_sarray) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 136 | ((j_common_ptr) cinfo, source->whole_image, |
| 137 | source->source_row, (JDIMENSION) 1, FALSE); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 138 | |
| 139 | /* Expand the colormap indexes to real data */ |
| 140 | inptr = image_ptr[0]; |
| 141 | outptr = source->pub.buffer[0]; |
| 142 | for (col = cinfo->image_width; col > 0; col--) { |
| 143 | t = GETJSAMPLE(*inptr++); |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 144 | *outptr++ = colormap[0][t]; /* can omit GETJSAMPLE() safely */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 145 | *outptr++ = colormap[1][t]; |
| 146 | *outptr++ = colormap[2][t]; |
| 147 | } |
| 148 | |
| 149 | return 1; |
| 150 | } |
| 151 | |
| 152 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 153 | METHODDEF(JDIMENSION) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 154 | get_24bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 155 | /* This version is for reading 24-bit pixels */ |
| 156 | { |
| 157 | bmp_source_ptr source = (bmp_source_ptr) sinfo; |
| 158 | JSAMPARRAY image_ptr; |
| 159 | register JSAMPROW inptr, outptr; |
| 160 | register JDIMENSION col; |
| 161 | |
| 162 | /* Fetch next row from virtual array */ |
| 163 | source->source_row--; |
| 164 | image_ptr = (*cinfo->mem->access_virt_sarray) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 165 | ((j_common_ptr) cinfo, source->whole_image, |
| 166 | source->source_row, (JDIMENSION) 1, FALSE); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 167 | |
| 168 | /* Transfer data. Note source values are in BGR order |
| 169 | * (even though Microsoft's own documents say the opposite). |
| 170 | */ |
| 171 | inptr = image_ptr[0]; |
| 172 | outptr = source->pub.buffer[0]; |
| 173 | for (col = cinfo->image_width; col > 0; col--) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 174 | outptr[2] = *inptr++; /* can omit GETJSAMPLE() safely */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 175 | outptr[1] = *inptr++; |
| 176 | outptr[0] = *inptr++; |
| 177 | outptr += 3; |
| 178 | } |
| 179 | |
| 180 | return 1; |
| 181 | } |
| 182 | |
| 183 | |
Guido Vollbeding | f18f81b | 2010-02-28 00:00:00 +0000 | [diff] [blame] | 184 | METHODDEF(JDIMENSION) |
| 185 | get_32bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 186 | /* This version is for reading 32-bit pixels */ |
| 187 | { |
| 188 | bmp_source_ptr source = (bmp_source_ptr) sinfo; |
| 189 | JSAMPARRAY image_ptr; |
| 190 | register JSAMPROW inptr, outptr; |
| 191 | register JDIMENSION col; |
| 192 | |
| 193 | /* Fetch next row from virtual array */ |
| 194 | source->source_row--; |
| 195 | image_ptr = (*cinfo->mem->access_virt_sarray) |
| 196 | ((j_common_ptr) cinfo, source->whole_image, |
| 197 | source->source_row, (JDIMENSION) 1, FALSE); |
| 198 | /* Transfer data. Note source values are in BGR order |
| 199 | * (even though Microsoft's own documents say the opposite). |
| 200 | */ |
| 201 | inptr = image_ptr[0]; |
| 202 | outptr = source->pub.buffer[0]; |
| 203 | for (col = cinfo->image_width; col > 0; col--) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 204 | outptr[2] = *inptr++; /* can omit GETJSAMPLE() safely */ |
Guido Vollbeding | f18f81b | 2010-02-28 00:00:00 +0000 | [diff] [blame] | 205 | outptr[1] = *inptr++; |
| 206 | outptr[0] = *inptr++; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 207 | inptr++; /* skip the 4th byte (Alpha channel) */ |
Guido Vollbeding | f18f81b | 2010-02-28 00:00:00 +0000 | [diff] [blame] | 208 | outptr += 3; |
| 209 | } |
| 210 | |
| 211 | return 1; |
| 212 | } |
| 213 | |
| 214 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 215 | /* |
| 216 | * This method loads the image into whole_image during the first call on |
| 217 | * get_pixel_rows. The get_pixel_rows pointer is then adjusted to call |
Guido Vollbeding | f18f81b | 2010-02-28 00:00:00 +0000 | [diff] [blame] | 218 | * get_8bit_row, get_24bit_row, or get_32bit_row on subsequent calls. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 219 | */ |
| 220 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 221 | METHODDEF(JDIMENSION) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 222 | preload_image (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 223 | { |
| 224 | bmp_source_ptr source = (bmp_source_ptr) sinfo; |
| 225 | register FILE *infile = source->pub.input_file; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 226 | register JSAMPROW out_ptr; |
| 227 | JSAMPARRAY image_ptr; |
DRC | 049aef5 | 2011-04-25 22:41:14 +0000 | [diff] [blame] | 228 | JDIMENSION row; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 229 | cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress; |
| 230 | |
| 231 | /* Read the data into a virtual array in input-file row order. */ |
| 232 | for (row = 0; row < cinfo->image_height; row++) { |
| 233 | if (progress != NULL) { |
| 234 | progress->pub.pass_counter = (long) row; |
| 235 | progress->pub.pass_limit = (long) cinfo->image_height; |
| 236 | (*progress->pub.progress_monitor) ((j_common_ptr) cinfo); |
| 237 | } |
| 238 | image_ptr = (*cinfo->mem->access_virt_sarray) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 239 | ((j_common_ptr) cinfo, source->whole_image, |
| 240 | row, (JDIMENSION) 1, TRUE); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 241 | out_ptr = image_ptr[0]; |
DRC | 049aef5 | 2011-04-25 22:41:14 +0000 | [diff] [blame] | 242 | if (fread(out_ptr, 1, source->row_width, infile) != source->row_width) { |
| 243 | if (feof(infile)) |
| 244 | ERREXIT(cinfo, JERR_INPUT_EOF); |
| 245 | else |
| 246 | ERREXIT(cinfo, JERR_FILE_READ); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | if (progress != NULL) |
| 250 | progress->completed_extra_passes++; |
| 251 | |
| 252 | /* Set up to read from the virtual array in top-to-bottom order */ |
| 253 | switch (source->bits_per_pixel) { |
| 254 | case 8: |
| 255 | source->pub.get_pixel_rows = get_8bit_row; |
| 256 | break; |
| 257 | case 24: |
| 258 | source->pub.get_pixel_rows = get_24bit_row; |
| 259 | break; |
Guido Vollbeding | f18f81b | 2010-02-28 00:00:00 +0000 | [diff] [blame] | 260 | case 32: |
| 261 | source->pub.get_pixel_rows = get_32bit_row; |
| 262 | break; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 263 | default: |
| 264 | ERREXIT(cinfo, JERR_BMP_BADDEPTH); |
| 265 | } |
| 266 | source->source_row = cinfo->image_height; |
| 267 | |
| 268 | /* And read the first row */ |
| 269 | return (*source->pub.get_pixel_rows) (cinfo, sinfo); |
| 270 | } |
| 271 | |
| 272 | |
| 273 | /* |
| 274 | * Read the file header; return image size and component count. |
| 275 | */ |
| 276 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 277 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 278 | start_input_bmp (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 279 | { |
| 280 | bmp_source_ptr source = (bmp_source_ptr) sinfo; |
| 281 | U_CHAR bmpfileheader[14]; |
| 282 | U_CHAR bmpinfoheader[64]; |
DRC | a3ef34c | 2015-08-13 11:09:05 -0500 | [diff] [blame^] | 283 | #define GET_2B(array,offset) ((unsigned short) UCH(array[offset]) + \ |
| 284 | (((unsigned short) UCH(array[offset+1])) << 8)) |
| 285 | #define GET_4B(array,offset) ((unsigned int) UCH(array[offset]) + \ |
| 286 | (((unsigned int) UCH(array[offset+1])) << 8) + \ |
| 287 | (((unsigned int) UCH(array[offset+2])) << 16) + \ |
| 288 | (((unsigned int) UCH(array[offset+3])) << 24)) |
| 289 | unsigned int bfOffBits; |
| 290 | unsigned int headerSize; |
| 291 | int biWidth; |
| 292 | int biHeight; |
| 293 | unsigned short biPlanes; |
| 294 | unsigned int biCompression; |
| 295 | int biXPelsPerMeter,biYPelsPerMeter; |
| 296 | unsigned int biClrUsed = 0; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 297 | int mapentrysize = 0; /* 0 indicates no colormap */ |
DRC | a3ef34c | 2015-08-13 11:09:05 -0500 | [diff] [blame^] | 298 | int bPad; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 299 | JDIMENSION row_width; |
| 300 | |
| 301 | /* Read and verify the bitmap file header */ |
| 302 | if (! ReadOK(source->pub.input_file, bmpfileheader, 14)) |
| 303 | ERREXIT(cinfo, JERR_INPUT_EOF); |
| 304 | if (GET_2B(bmpfileheader,0) != 0x4D42) /* 'BM' */ |
| 305 | ERREXIT(cinfo, JERR_BMP_NOT); |
DRC | a3ef34c | 2015-08-13 11:09:05 -0500 | [diff] [blame^] | 306 | bfOffBits = GET_4B(bmpfileheader,10); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 307 | /* We ignore the remaining fileheader fields */ |
| 308 | |
| 309 | /* The infoheader might be 12 bytes (OS/2 1.x), 40 bytes (Windows), |
| 310 | * or 64 bytes (OS/2 2.x). Check the first 4 bytes to find out which. |
| 311 | */ |
| 312 | if (! ReadOK(source->pub.input_file, bmpinfoheader, 4)) |
| 313 | ERREXIT(cinfo, JERR_INPUT_EOF); |
DRC | a3ef34c | 2015-08-13 11:09:05 -0500 | [diff] [blame^] | 314 | headerSize = GET_4B(bmpinfoheader,0); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 315 | if (headerSize < 12 || headerSize > 64) |
| 316 | ERREXIT(cinfo, JERR_BMP_BADHEADER); |
| 317 | if (! ReadOK(source->pub.input_file, bmpinfoheader+4, headerSize-4)) |
| 318 | ERREXIT(cinfo, JERR_INPUT_EOF); |
| 319 | |
DRC | a3ef34c | 2015-08-13 11:09:05 -0500 | [diff] [blame^] | 320 | switch (headerSize) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 321 | case 12: |
| 322 | /* Decode OS/2 1.x header (Microsoft calls this a BITMAPCOREHEADER) */ |
DRC | a3ef34c | 2015-08-13 11:09:05 -0500 | [diff] [blame^] | 323 | biWidth = (int) GET_2B(bmpinfoheader,4); |
| 324 | biHeight = (int) GET_2B(bmpinfoheader,6); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 325 | biPlanes = GET_2B(bmpinfoheader,8); |
| 326 | source->bits_per_pixel = (int) GET_2B(bmpinfoheader,10); |
| 327 | |
| 328 | switch (source->bits_per_pixel) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 329 | case 8: /* colormapped image */ |
| 330 | mapentrysize = 3; /* OS/2 uses RGBTRIPLE colormap */ |
DRC | a3ef34c | 2015-08-13 11:09:05 -0500 | [diff] [blame^] | 331 | TRACEMS2(cinfo, 1, JTRC_BMP_OS2_MAPPED, biWidth, biHeight); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 332 | break; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 333 | case 24: /* RGB image */ |
DRC | a3ef34c | 2015-08-13 11:09:05 -0500 | [diff] [blame^] | 334 | TRACEMS2(cinfo, 1, JTRC_BMP_OS2, biWidth, biHeight); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 335 | break; |
| 336 | default: |
| 337 | ERREXIT(cinfo, JERR_BMP_BADDEPTH); |
| 338 | break; |
| 339 | } |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 340 | break; |
| 341 | case 40: |
| 342 | case 64: |
| 343 | /* Decode Windows 3.x header (Microsoft calls this a BITMAPINFOHEADER) */ |
| 344 | /* or OS/2 2.x header, which has additional fields that we ignore */ |
DRC | a3ef34c | 2015-08-13 11:09:05 -0500 | [diff] [blame^] | 345 | biWidth = (int) GET_4B(bmpinfoheader,4); |
| 346 | biHeight = (int) GET_4B(bmpinfoheader,8); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 347 | biPlanes = GET_2B(bmpinfoheader,12); |
| 348 | source->bits_per_pixel = (int) GET_2B(bmpinfoheader,14); |
| 349 | biCompression = GET_4B(bmpinfoheader,16); |
DRC | a3ef34c | 2015-08-13 11:09:05 -0500 | [diff] [blame^] | 350 | biXPelsPerMeter = (int) GET_4B(bmpinfoheader,24); |
| 351 | biYPelsPerMeter = (int) GET_4B(bmpinfoheader,28); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 352 | biClrUsed = GET_4B(bmpinfoheader,32); |
| 353 | /* biSizeImage, biClrImportant fields are ignored */ |
| 354 | |
| 355 | switch (source->bits_per_pixel) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 356 | case 8: /* colormapped image */ |
| 357 | mapentrysize = 4; /* Windows uses RGBQUAD colormap */ |
DRC | a3ef34c | 2015-08-13 11:09:05 -0500 | [diff] [blame^] | 358 | TRACEMS2(cinfo, 1, JTRC_BMP_MAPPED, biWidth, biHeight); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 359 | break; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 360 | case 24: /* RGB image */ |
DRC | a3ef34c | 2015-08-13 11:09:05 -0500 | [diff] [blame^] | 361 | TRACEMS2(cinfo, 1, JTRC_BMP, biWidth, biHeight); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 362 | break; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 363 | case 32: /* RGB image + Alpha channel */ |
DRC | a3ef34c | 2015-08-13 11:09:05 -0500 | [diff] [blame^] | 364 | TRACEMS2(cinfo, 1, JTRC_BMP, biWidth, biHeight); |
Guido Vollbeding | f18f81b | 2010-02-28 00:00:00 +0000 | [diff] [blame] | 365 | break; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 366 | default: |
| 367 | ERREXIT(cinfo, JERR_BMP_BADDEPTH); |
| 368 | break; |
| 369 | } |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 370 | if (biCompression != 0) |
| 371 | ERREXIT(cinfo, JERR_BMP_COMPRESSED); |
| 372 | |
| 373 | if (biXPelsPerMeter > 0 && biYPelsPerMeter > 0) { |
| 374 | /* Set JFIF density parameters from the BMP data */ |
| 375 | cinfo->X_density = (UINT16) (biXPelsPerMeter/100); /* 100 cm per meter */ |
| 376 | cinfo->Y_density = (UINT16) (biYPelsPerMeter/100); |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 377 | cinfo->density_unit = 2; /* dots/cm */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 378 | } |
| 379 | break; |
| 380 | default: |
| 381 | ERREXIT(cinfo, JERR_BMP_BADHEADER); |
Guido Vollbeding | 989630f | 2010-01-10 00:00:00 +0000 | [diff] [blame] | 382 | return; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Guido Vollbeding | 989630f | 2010-01-10 00:00:00 +0000 | [diff] [blame] | 385 | if (biWidth <= 0 || biHeight <= 0) |
| 386 | ERREXIT(cinfo, JERR_BMP_EMPTY); |
| 387 | if (biPlanes != 1) |
| 388 | ERREXIT(cinfo, JERR_BMP_BADPLANES); |
| 389 | |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 390 | /* Compute distance to bitmap data --- will adjust for colormap below */ |
| 391 | bPad = bfOffBits - (headerSize + 14); |
| 392 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 393 | /* Read the colormap, if any */ |
| 394 | if (mapentrysize > 0) { |
| 395 | if (biClrUsed <= 0) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 396 | biClrUsed = 256; /* assume it's 256 */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 397 | else if (biClrUsed > 256) |
| 398 | ERREXIT(cinfo, JERR_BMP_BADCMAP); |
| 399 | /* Allocate space to store the colormap */ |
| 400 | source->colormap = (*cinfo->mem->alloc_sarray) |
| 401 | ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 402 | (JDIMENSION) biClrUsed, (JDIMENSION) 3); |
| 403 | /* and read it from the file */ |
| 404 | read_colormap(source, (int) biClrUsed, mapentrysize); |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 405 | /* account for size of colormap */ |
| 406 | bPad -= biClrUsed * mapentrysize; |
| 407 | } |
| 408 | |
| 409 | /* Skip any remaining pad bytes */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 410 | if (bPad < 0) /* incorrect bfOffBits value? */ |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 411 | ERREXIT(cinfo, JERR_BMP_BADHEADER); |
| 412 | while (--bPad >= 0) { |
| 413 | (void) read_byte(source); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /* Compute row width in file, including padding to 4-byte boundary */ |
| 417 | if (source->bits_per_pixel == 24) |
| 418 | row_width = (JDIMENSION) (biWidth * 3); |
Guido Vollbeding | f18f81b | 2010-02-28 00:00:00 +0000 | [diff] [blame] | 419 | else if (source->bits_per_pixel == 32) |
| 420 | row_width = (JDIMENSION) (biWidth * 4); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 421 | else |
| 422 | row_width = (JDIMENSION) biWidth; |
| 423 | while ((row_width & 3) != 0) row_width++; |
| 424 | source->row_width = row_width; |
| 425 | |
| 426 | /* Allocate space for inversion array, prepare for preload pass */ |
| 427 | source->whole_image = (*cinfo->mem->request_virt_sarray) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 428 | ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE, |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 429 | row_width, (JDIMENSION) biHeight, (JDIMENSION) 1); |
| 430 | source->pub.get_pixel_rows = preload_image; |
| 431 | if (cinfo->progress != NULL) { |
| 432 | cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress; |
| 433 | progress->total_extra_passes++; /* count file input as separate pass */ |
| 434 | } |
| 435 | |
| 436 | /* Allocate one-row buffer for returned data */ |
| 437 | source->pub.buffer = (*cinfo->mem->alloc_sarray) |
| 438 | ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 439 | (JDIMENSION) (biWidth * 3), (JDIMENSION) 1); |
| 440 | source->pub.buffer_height = 1; |
| 441 | |
| 442 | cinfo->in_color_space = JCS_RGB; |
| 443 | cinfo->input_components = 3; |
| 444 | cinfo->data_precision = 8; |
| 445 | cinfo->image_width = (JDIMENSION) biWidth; |
| 446 | cinfo->image_height = (JDIMENSION) biHeight; |
| 447 | } |
| 448 | |
| 449 | |
| 450 | /* |
| 451 | * Finish up at the end of the file. |
| 452 | */ |
| 453 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 454 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 455 | finish_input_bmp (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 456 | { |
| 457 | /* no work */ |
| 458 | } |
| 459 | |
| 460 | |
| 461 | /* |
| 462 | * The module selection routine for BMP format input. |
| 463 | */ |
| 464 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 465 | GLOBAL(cjpeg_source_ptr) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 466 | jinit_read_bmp (j_compress_ptr cinfo) |
| 467 | { |
| 468 | bmp_source_ptr source; |
| 469 | |
| 470 | /* Create module interface object */ |
| 471 | source = (bmp_source_ptr) |
| 472 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 473 | sizeof(bmp_source_struct)); |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 474 | source->cinfo = cinfo; /* make back link for subroutines */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 475 | /* Fill in method ptrs, except get_pixel_rows which start_input sets */ |
| 476 | source->pub.start_input = start_input_bmp; |
| 477 | source->pub.finish_input = finish_input_bmp; |
| 478 | |
| 479 | return (cjpeg_source_ptr) source; |
| 480 | } |
| 481 | |
| 482 | #endif /* BMP_SUPPORTED */ |