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