blob: f9711803f814100254bd5b0478c80cfa18f297dd [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 Vollbeding989630f2010-01-10 00:00:00 +00005 * Modified 2009 by Guido Vollbeding.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00006 * This file is part of the Independent JPEG Group's software.
7 * For conditions of distribution and use, see the accompanying README file.
8 *
9 * This file contains routines to read input images in Microsoft "BMP"
10 * format (MS Windows 3.x, OS/2 1.x, and OS/2 2.x flavors).
11 * Currently, only 8-bit and 24-bit images are supported, not 1-bit or
12 * 4-bit (feeding such low-depth images into JPEG would be silly anyway).
13 * Also, we don't support RLE-compressed files.
14 *
15 * These routines may need modification for non-Unix environments or
16 * specialized applications. As they stand, they assume input from
17 * an ordinary stdio stream. They further assume that reading begins
18 * at the start of the file; start_input may need work if the
19 * user interface has already read some data (e.g., to determine that
20 * the file is indeed BMP format).
21 *
22 * This code contributed by James Arthur Boucher.
23 */
24
25#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
26
27#ifdef BMP_SUPPORTED
28
29
30/* Macros to deal with unsigned chars as efficiently as compiler allows */
31
32#ifdef HAVE_UNSIGNED_CHAR
33typedef unsigned char U_CHAR;
34#define UCH(x) ((int) (x))
35#else /* !HAVE_UNSIGNED_CHAR */
36#ifdef CHAR_IS_UNSIGNED
37typedef char U_CHAR;
38#define UCH(x) ((int) (x))
39#else
40typedef char U_CHAR;
41#define UCH(x) ((int) (x) & 0xFF)
42#endif
43#endif /* HAVE_UNSIGNED_CHAR */
44
45
46#define ReadOK(file,buffer,len) (JFREAD(file,buffer,len) == ((size_t) (len)))
47
48
49/* Private version of data source object */
50
51typedef struct _bmp_source_struct * bmp_source_ptr;
52
53typedef 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- or 24-bit format */
65} bmp_source_struct;
66
67
Thomas G. Lane489583f1996-02-07 00:00:00 +000068LOCAL(int)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000069read_byte (bmp_source_ptr sinfo)
70/* Read next byte from BMP file */
71{
72 register FILE *infile = sinfo->pub.input_file;
73 register int c;
74
75 if ((c = getc(infile)) == EOF)
76 ERREXIT(sinfo->cinfo, JERR_INPUT_EOF);
77 return c;
78}
79
80
Thomas G. Lane489583f1996-02-07 00:00:00 +000081LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000082read_colormap (bmp_source_ptr sinfo, int cmaplen, int mapentrysize)
83/* Read the colormap from a BMP file */
84{
85 int i;
86
87 switch (mapentrysize) {
88 case 3:
89 /* BGR format (occurs in OS/2 files) */
90 for (i = 0; i < cmaplen; i++) {
91 sinfo->colormap[2][i] = (JSAMPLE) read_byte(sinfo);
92 sinfo->colormap[1][i] = (JSAMPLE) read_byte(sinfo);
93 sinfo->colormap[0][i] = (JSAMPLE) read_byte(sinfo);
94 }
95 break;
96 case 4:
97 /* BGR0 format (occurs in MS Windows files) */
98 for (i = 0; i < cmaplen; i++) {
99 sinfo->colormap[2][i] = (JSAMPLE) read_byte(sinfo);
100 sinfo->colormap[1][i] = (JSAMPLE) read_byte(sinfo);
101 sinfo->colormap[0][i] = (JSAMPLE) read_byte(sinfo);
102 (void) read_byte(sinfo);
103 }
104 break;
105 default:
106 ERREXIT(sinfo->cinfo, JERR_BMP_BADCMAP);
107 break;
108 }
109}
110
111
112/*
113 * Read one row of pixels.
114 * The image has been read into the whole_image array, but is otherwise
115 * unprocessed. We must read it out in top-to-bottom row order, and if
116 * it is an 8-bit image, we must expand colormapped pixels to 24bit format.
117 */
118
Thomas G. Lane489583f1996-02-07 00:00:00 +0000119METHODDEF(JDIMENSION)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000120get_8bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
121/* This version is for reading 8-bit colormap indexes */
122{
123 bmp_source_ptr source = (bmp_source_ptr) sinfo;
124 register JSAMPARRAY colormap = source->colormap;
125 JSAMPARRAY image_ptr;
126 register int t;
127 register JSAMPROW inptr, outptr;
128 register JDIMENSION col;
129
130 /* Fetch next row from virtual array */
131 source->source_row--;
132 image_ptr = (*cinfo->mem->access_virt_sarray)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000133 ((j_common_ptr) cinfo, source->whole_image,
134 source->source_row, (JDIMENSION) 1, FALSE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000135
136 /* Expand the colormap indexes to real data */
137 inptr = image_ptr[0];
138 outptr = source->pub.buffer[0];
139 for (col = cinfo->image_width; col > 0; col--) {
140 t = GETJSAMPLE(*inptr++);
141 *outptr++ = colormap[0][t]; /* can omit GETJSAMPLE() safely */
142 *outptr++ = colormap[1][t];
143 *outptr++ = colormap[2][t];
144 }
145
146 return 1;
147}
148
149
Thomas G. Lane489583f1996-02-07 00:00:00 +0000150METHODDEF(JDIMENSION)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000151get_24bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
152/* This version is for reading 24-bit pixels */
153{
154 bmp_source_ptr source = (bmp_source_ptr) sinfo;
155 JSAMPARRAY image_ptr;
156 register JSAMPROW inptr, outptr;
157 register JDIMENSION col;
158
159 /* Fetch next row from virtual array */
160 source->source_row--;
161 image_ptr = (*cinfo->mem->access_virt_sarray)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000162 ((j_common_ptr) cinfo, source->whole_image,
163 source->source_row, (JDIMENSION) 1, FALSE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000164
165 /* Transfer data. Note source values are in BGR order
166 * (even though Microsoft's own documents say the opposite).
167 */
168 inptr = image_ptr[0];
169 outptr = source->pub.buffer[0];
170 for (col = cinfo->image_width; col > 0; col--) {
171 outptr[2] = *inptr++; /* can omit GETJSAMPLE() safely */
172 outptr[1] = *inptr++;
173 outptr[0] = *inptr++;
174 outptr += 3;
175 }
176
177 return 1;
178}
179
180
181/*
182 * This method loads the image into whole_image during the first call on
183 * get_pixel_rows. The get_pixel_rows pointer is then adjusted to call
184 * get_8bit_row or get_24bit_row on subsequent calls.
185 */
186
Thomas G. Lane489583f1996-02-07 00:00:00 +0000187METHODDEF(JDIMENSION)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000188preload_image (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
189{
190 bmp_source_ptr source = (bmp_source_ptr) sinfo;
191 register FILE *infile = source->pub.input_file;
192 register int c;
193 register JSAMPROW out_ptr;
194 JSAMPARRAY image_ptr;
195 JDIMENSION row, col;
196 cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
197
198 /* Read the data into a virtual array in input-file row order. */
199 for (row = 0; row < cinfo->image_height; row++) {
200 if (progress != NULL) {
201 progress->pub.pass_counter = (long) row;
202 progress->pub.pass_limit = (long) cinfo->image_height;
203 (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
204 }
205 image_ptr = (*cinfo->mem->access_virt_sarray)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000206 ((j_common_ptr) cinfo, source->whole_image,
207 row, (JDIMENSION) 1, TRUE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000208 out_ptr = image_ptr[0];
209 for (col = source->row_width; col > 0; col--) {
210 /* inline copy of read_byte() for speed */
211 if ((c = getc(infile)) == EOF)
212 ERREXIT(cinfo, JERR_INPUT_EOF);
213 *out_ptr++ = (JSAMPLE) c;
214 }
215 }
216 if (progress != NULL)
217 progress->completed_extra_passes++;
218
219 /* Set up to read from the virtual array in top-to-bottom order */
220 switch (source->bits_per_pixel) {
221 case 8:
222 source->pub.get_pixel_rows = get_8bit_row;
223 break;
224 case 24:
225 source->pub.get_pixel_rows = get_24bit_row;
226 break;
227 default:
228 ERREXIT(cinfo, JERR_BMP_BADDEPTH);
229 }
230 source->source_row = cinfo->image_height;
231
232 /* And read the first row */
233 return (*source->pub.get_pixel_rows) (cinfo, sinfo);
234}
235
236
237/*
238 * Read the file header; return image size and component count.
239 */
240
Thomas G. Lane489583f1996-02-07 00:00:00 +0000241METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000242start_input_bmp (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
243{
244 bmp_source_ptr source = (bmp_source_ptr) sinfo;
245 U_CHAR bmpfileheader[14];
246 U_CHAR bmpinfoheader[64];
247#define GET_2B(array,offset) ((unsigned int) UCH(array[offset]) + \
248 (((unsigned int) UCH(array[offset+1])) << 8))
249#define GET_4B(array,offset) ((INT32) UCH(array[offset]) + \
250 (((INT32) UCH(array[offset+1])) << 8) + \
251 (((INT32) UCH(array[offset+2])) << 16) + \
252 (((INT32) UCH(array[offset+3])) << 24))
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000253 INT32 bfOffBits;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000254 INT32 headerSize;
Guido Vollbeding989630f2010-01-10 00:00:00 +0000255 INT32 biWidth;
256 INT32 biHeight;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000257 unsigned int biPlanes;
258 INT32 biCompression;
259 INT32 biXPelsPerMeter,biYPelsPerMeter;
260 INT32 biClrUsed = 0;
261 int mapentrysize = 0; /* 0 indicates no colormap */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000262 INT32 bPad;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000263 JDIMENSION row_width;
264
265 /* Read and verify the bitmap file header */
266 if (! ReadOK(source->pub.input_file, bmpfileheader, 14))
267 ERREXIT(cinfo, JERR_INPUT_EOF);
268 if (GET_2B(bmpfileheader,0) != 0x4D42) /* 'BM' */
269 ERREXIT(cinfo, JERR_BMP_NOT);
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000270 bfOffBits = (INT32) GET_4B(bmpfileheader,10);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000271 /* We ignore the remaining fileheader fields */
272
273 /* The infoheader might be 12 bytes (OS/2 1.x), 40 bytes (Windows),
274 * or 64 bytes (OS/2 2.x). Check the first 4 bytes to find out which.
275 */
276 if (! ReadOK(source->pub.input_file, bmpinfoheader, 4))
277 ERREXIT(cinfo, JERR_INPUT_EOF);
278 headerSize = (INT32) GET_4B(bmpinfoheader,0);
279 if (headerSize < 12 || headerSize > 64)
280 ERREXIT(cinfo, JERR_BMP_BADHEADER);
281 if (! ReadOK(source->pub.input_file, bmpinfoheader+4, headerSize-4))
282 ERREXIT(cinfo, JERR_INPUT_EOF);
283
284 switch ((int) headerSize) {
285 case 12:
286 /* Decode OS/2 1.x header (Microsoft calls this a BITMAPCOREHEADER) */
287 biWidth = (INT32) GET_2B(bmpinfoheader,4);
288 biHeight = (INT32) GET_2B(bmpinfoheader,6);
289 biPlanes = GET_2B(bmpinfoheader,8);
290 source->bits_per_pixel = (int) GET_2B(bmpinfoheader,10);
291
292 switch (source->bits_per_pixel) {
293 case 8: /* colormapped image */
294 mapentrysize = 3; /* OS/2 uses RGBTRIPLE colormap */
295 TRACEMS2(cinfo, 1, JTRC_BMP_OS2_MAPPED, (int) biWidth, (int) biHeight);
296 break;
297 case 24: /* RGB image */
298 TRACEMS2(cinfo, 1, JTRC_BMP_OS2, (int) biWidth, (int) biHeight);
299 break;
300 default:
301 ERREXIT(cinfo, JERR_BMP_BADDEPTH);
302 break;
303 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000304 break;
305 case 40:
306 case 64:
307 /* Decode Windows 3.x header (Microsoft calls this a BITMAPINFOHEADER) */
308 /* or OS/2 2.x header, which has additional fields that we ignore */
309 biWidth = GET_4B(bmpinfoheader,4);
310 biHeight = GET_4B(bmpinfoheader,8);
311 biPlanes = GET_2B(bmpinfoheader,12);
312 source->bits_per_pixel = (int) GET_2B(bmpinfoheader,14);
313 biCompression = GET_4B(bmpinfoheader,16);
314 biXPelsPerMeter = GET_4B(bmpinfoheader,24);
315 biYPelsPerMeter = GET_4B(bmpinfoheader,28);
316 biClrUsed = GET_4B(bmpinfoheader,32);
317 /* biSizeImage, biClrImportant fields are ignored */
318
319 switch (source->bits_per_pixel) {
320 case 8: /* colormapped image */
321 mapentrysize = 4; /* Windows uses RGBQUAD colormap */
322 TRACEMS2(cinfo, 1, JTRC_BMP_MAPPED, (int) biWidth, (int) biHeight);
323 break;
324 case 24: /* RGB image */
325 TRACEMS2(cinfo, 1, JTRC_BMP, (int) biWidth, (int) biHeight);
326 break;
327 default:
328 ERREXIT(cinfo, JERR_BMP_BADDEPTH);
329 break;
330 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000331 if (biCompression != 0)
332 ERREXIT(cinfo, JERR_BMP_COMPRESSED);
333
334 if (biXPelsPerMeter > 0 && biYPelsPerMeter > 0) {
335 /* Set JFIF density parameters from the BMP data */
336 cinfo->X_density = (UINT16) (biXPelsPerMeter/100); /* 100 cm per meter */
337 cinfo->Y_density = (UINT16) (biYPelsPerMeter/100);
338 cinfo->density_unit = 2; /* dots/cm */
339 }
340 break;
341 default:
342 ERREXIT(cinfo, JERR_BMP_BADHEADER);
Guido Vollbeding989630f2010-01-10 00:00:00 +0000343 return;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000344 }
345
Guido Vollbeding989630f2010-01-10 00:00:00 +0000346 if (biWidth <= 0 || biHeight <= 0)
347 ERREXIT(cinfo, JERR_BMP_EMPTY);
348 if (biPlanes != 1)
349 ERREXIT(cinfo, JERR_BMP_BADPLANES);
350
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000351 /* Compute distance to bitmap data --- will adjust for colormap below */
352 bPad = bfOffBits - (headerSize + 14);
353
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000354 /* Read the colormap, if any */
355 if (mapentrysize > 0) {
356 if (biClrUsed <= 0)
357 biClrUsed = 256; /* assume it's 256 */
358 else if (biClrUsed > 256)
359 ERREXIT(cinfo, JERR_BMP_BADCMAP);
360 /* Allocate space to store the colormap */
361 source->colormap = (*cinfo->mem->alloc_sarray)
362 ((j_common_ptr) cinfo, JPOOL_IMAGE,
363 (JDIMENSION) biClrUsed, (JDIMENSION) 3);
364 /* and read it from the file */
365 read_colormap(source, (int) biClrUsed, mapentrysize);
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000366 /* account for size of colormap */
367 bPad -= biClrUsed * mapentrysize;
368 }
369
370 /* Skip any remaining pad bytes */
371 if (bPad < 0) /* incorrect bfOffBits value? */
372 ERREXIT(cinfo, JERR_BMP_BADHEADER);
373 while (--bPad >= 0) {
374 (void) read_byte(source);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000375 }
376
377 /* Compute row width in file, including padding to 4-byte boundary */
378 if (source->bits_per_pixel == 24)
379 row_width = (JDIMENSION) (biWidth * 3);
380 else
381 row_width = (JDIMENSION) biWidth;
382 while ((row_width & 3) != 0) row_width++;
383 source->row_width = row_width;
384
385 /* Allocate space for inversion array, prepare for preload pass */
386 source->whole_image = (*cinfo->mem->request_virt_sarray)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000387 ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000388 row_width, (JDIMENSION) biHeight, (JDIMENSION) 1);
389 source->pub.get_pixel_rows = preload_image;
390 if (cinfo->progress != NULL) {
391 cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
392 progress->total_extra_passes++; /* count file input as separate pass */
393 }
394
395 /* Allocate one-row buffer for returned data */
396 source->pub.buffer = (*cinfo->mem->alloc_sarray)
397 ((j_common_ptr) cinfo, JPOOL_IMAGE,
398 (JDIMENSION) (biWidth * 3), (JDIMENSION) 1);
399 source->pub.buffer_height = 1;
400
401 cinfo->in_color_space = JCS_RGB;
402 cinfo->input_components = 3;
403 cinfo->data_precision = 8;
404 cinfo->image_width = (JDIMENSION) biWidth;
405 cinfo->image_height = (JDIMENSION) biHeight;
406}
407
408
409/*
410 * Finish up at the end of the file.
411 */
412
Thomas G. Lane489583f1996-02-07 00:00:00 +0000413METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000414finish_input_bmp (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
415{
416 /* no work */
417}
418
419
420/*
421 * The module selection routine for BMP format input.
422 */
423
Thomas G. Lane489583f1996-02-07 00:00:00 +0000424GLOBAL(cjpeg_source_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000425jinit_read_bmp (j_compress_ptr cinfo)
426{
427 bmp_source_ptr source;
428
429 /* Create module interface object */
430 source = (bmp_source_ptr)
431 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
432 SIZEOF(bmp_source_struct));
433 source->cinfo = cinfo; /* make back link for subroutines */
434 /* Fill in method ptrs, except get_pixel_rows which start_input sets */
435 source->pub.start_input = start_input_bmp;
436 source->pub.finish_input = finish_input_bmp;
437
438 return (cjpeg_source_ptr) source;
439}
440
441#endif /* BMP_SUPPORTED */