|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* rdbmp.c
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* Copyright (C) 1994-1996, Thomas G. Lane.
|
|
kusano |
7d535a |
* Modified 2009-2010 by Guido Vollbeding.
|
|
kusano |
7d535a |
* This file is part of the Independent JPEG Group's software.
|
|
kusano |
7d535a |
* For conditions of distribution and use, see the accompanying README file.
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* This file contains routines to read input images in Microsoft "BMP"
|
|
kusano |
7d535a |
* format (MS Windows 3.x, OS/2 1.x, and OS/2 2.x flavors).
|
|
kusano |
7d535a |
* Currently, only 8-bit and 24-bit images are supported, not 1-bit or
|
|
kusano |
7d535a |
* 4-bit (feeding such low-depth images into JPEG would be silly anyway).
|
|
kusano |
7d535a |
* Also, we don't support RLE-compressed files.
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* These routines may need modification for non-Unix environments or
|
|
kusano |
7d535a |
* specialized applications. As they stand, they assume input from
|
|
kusano |
7d535a |
* an ordinary stdio stream. They further assume that reading begins
|
|
kusano |
7d535a |
* at the start of the file; start_input may need work if the
|
|
kusano |
7d535a |
* user interface has already read some data (e.g., to determine that
|
|
kusano |
7d535a |
* the file is indeed BMP format).
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* This code contributed by James Arthur Boucher.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#ifdef BMP_SUPPORTED
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Macros to deal with unsigned chars as efficiently as compiler allows */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#ifdef HAVE_UNSIGNED_CHAR
|
|
kusano |
7d535a |
typedef unsigned char U_CHAR;
|
|
kusano |
7d535a |
#define UCH(x) ((int) (x))
|
|
kusano |
7d535a |
#else /* !HAVE_UNSIGNED_CHAR */
|
|
kusano |
7d535a |
#ifdef CHAR_IS_UNSIGNED
|
|
kusano |
7d535a |
typedef char U_CHAR;
|
|
kusano |
7d535a |
#define UCH(x) ((int) (x))
|
|
kusano |
7d535a |
#else
|
|
kusano |
7d535a |
typedef char U_CHAR;
|
|
kusano |
7d535a |
#define UCH(x) ((int) (x) & 0xFF)
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
#endif /* HAVE_UNSIGNED_CHAR */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#define ReadOK(file,buffer,len) (JFREAD(file,buffer,len) == ((size_t) (len)))
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Private version of data source object */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
typedef struct _bmp_source_struct * bmp_source_ptr;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
typedef struct _bmp_source_struct {
|
|
kusano |
7d535a |
struct cjpeg_source_struct pub; /* public fields */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
j_compress_ptr cinfo; /* back link saves passing separate parm */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
JSAMPARRAY colormap; /* BMP colormap (converted to my format) */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
jvirt_sarray_ptr whole_image; /* Needed to reverse row order */
|
|
kusano |
7d535a |
JDIMENSION source_row; /* Current source row number */
|
|
kusano |
7d535a |
JDIMENSION row_width; /* Physical width of scanlines in file */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
int bits_per_pixel; /* remembers 8- or 24-bit format */
|
|
kusano |
7d535a |
} bmp_source_struct;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
LOCAL(int)
|
|
kusano |
7d535a |
read_byte (bmp_source_ptr sinfo)
|
|
kusano |
7d535a |
/* Read next byte from BMP file */
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
register FILE *infile = sinfo->pub.input_file;
|
|
kusano |
7d535a |
register int c;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if ((c = getc(infile)) == EOF)
|
|
kusano |
7d535a |
ERREXIT(sinfo->cinfo, JERR_INPUT_EOF);
|
|
kusano |
7d535a |
return c;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
LOCAL(void)
|
|
kusano |
7d535a |
read_colormap (bmp_source_ptr sinfo, int cmaplen, int mapentrysize)
|
|
kusano |
7d535a |
/* Read the colormap from a BMP file */
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
int i;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
switch (mapentrysize) {
|
|
kusano |
7d535a |
case 3:
|
|
kusano |
7d535a |
/* BGR format (occurs in OS/2 files) */
|
|
kusano |
7d535a |
for (i = 0; i < cmaplen; i++) {
|
|
kusano |
7d535a |
sinfo->colormap[2][i] = (JSAMPLE) read_byte(sinfo);
|
|
kusano |
7d535a |
sinfo->colormap[1][i] = (JSAMPLE) read_byte(sinfo);
|
|
kusano |
7d535a |
sinfo->colormap[0][i] = (JSAMPLE) read_byte(sinfo);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
case 4:
|
|
kusano |
7d535a |
/* BGR0 format (occurs in MS Windows files) */
|
|
kusano |
7d535a |
for (i = 0; i < cmaplen; i++) {
|
|
kusano |
7d535a |
sinfo->colormap[2][i] = (JSAMPLE) read_byte(sinfo);
|
|
kusano |
7d535a |
sinfo->colormap[1][i] = (JSAMPLE) read_byte(sinfo);
|
|
kusano |
7d535a |
sinfo->colormap[0][i] = (JSAMPLE) read_byte(sinfo);
|
|
kusano |
7d535a |
(void) read_byte(sinfo);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
default:
|
|
kusano |
7d535a |
ERREXIT(sinfo->cinfo, JERR_BMP_BADCMAP);
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Read one row of pixels.
|
|
kusano |
7d535a |
* The image has been read into the whole_image array, but is otherwise
|
|
kusano |
7d535a |
* unprocessed. We must read it out in top-to-bottom row order, and if
|
|
kusano |
7d535a |
* it is an 8-bit image, we must expand colormapped pixels to 24bit format.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
METHODDEF(JDIMENSION)
|
|
kusano |
7d535a |
get_8bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
|
|
kusano |
7d535a |
/* This version is for reading 8-bit colormap indexes */
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
bmp_source_ptr source = (bmp_source_ptr) sinfo;
|
|
kusano |
7d535a |
register JSAMPARRAY colormap = source->colormap;
|
|
kusano |
7d535a |
JSAMPARRAY image_ptr;
|
|
kusano |
7d535a |
register int t;
|
|
kusano |
7d535a |
register JSAMPROW inptr, outptr;
|
|
kusano |
7d535a |
register JDIMENSION col;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Fetch next row from virtual array */
|
|
kusano |
7d535a |
source->source_row--;
|
|
kusano |
7d535a |
image_ptr = (*cinfo->mem->access_virt_sarray)
|
|
kusano |
7d535a |
((j_common_ptr) cinfo, source->whole_image,
|
|
kusano |
7d535a |
source->source_row, (JDIMENSION) 1, FALSE);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Expand the colormap indexes to real data */
|
|
kusano |
7d535a |
inptr = image_ptr[0];
|
|
kusano |
7d535a |
outptr = source->pub.buffer[0];
|
|
kusano |
7d535a |
for (col = cinfo->image_width; col > 0; col--) {
|
|
kusano |
7d535a |
t = GETJSAMPLE(*inptr++);
|
|
kusano |
7d535a |
*outptr++ = colormap[0][t]; /* can omit GETJSAMPLE() safely */
|
|
kusano |
7d535a |
*outptr++ = colormap[1][t];
|
|
kusano |
7d535a |
*outptr++ = colormap[2][t];
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
return 1;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
METHODDEF(JDIMENSION)
|
|
kusano |
7d535a |
get_24bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
|
|
kusano |
7d535a |
/* This version is for reading 24-bit pixels */
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
bmp_source_ptr source = (bmp_source_ptr) sinfo;
|
|
kusano |
7d535a |
JSAMPARRAY image_ptr;
|
|
kusano |
7d535a |
register JSAMPROW inptr, outptr;
|
|
kusano |
7d535a |
register JDIMENSION col;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Fetch next row from virtual array */
|
|
kusano |
7d535a |
source->source_row--;
|
|
kusano |
7d535a |
image_ptr = (*cinfo->mem->access_virt_sarray)
|
|
kusano |
7d535a |
((j_common_ptr) cinfo, source->whole_image,
|
|
kusano |
7d535a |
source->source_row, (JDIMENSION) 1, FALSE);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Transfer data. Note source values are in BGR order
|
|
kusano |
7d535a |
* (even though Microsoft's own documents say the opposite).
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
inptr = image_ptr[0];
|
|
kusano |
7d535a |
outptr = source->pub.buffer[0];
|
|
kusano |
7d535a |
for (col = cinfo->image_width; col > 0; col--) {
|
|
kusano |
7d535a |
outptr[2] = *inptr++; /* can omit GETJSAMPLE() safely */
|
|
kusano |
7d535a |
outptr[1] = *inptr++;
|
|
kusano |
7d535a |
outptr[0] = *inptr++;
|
|
kusano |
7d535a |
outptr += 3;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
return 1;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
METHODDEF(JDIMENSION)
|
|
kusano |
7d535a |
get_32bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
|
|
kusano |
7d535a |
/* This version is for reading 32-bit pixels */
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
bmp_source_ptr source = (bmp_source_ptr) sinfo;
|
|
kusano |
7d535a |
JSAMPARRAY image_ptr;
|
|
kusano |
7d535a |
register JSAMPROW inptr, outptr;
|
|
kusano |
7d535a |
register JDIMENSION col;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Fetch next row from virtual array */
|
|
kusano |
7d535a |
source->source_row--;
|
|
kusano |
7d535a |
image_ptr = (*cinfo->mem->access_virt_sarray)
|
|
kusano |
7d535a |
((j_common_ptr) cinfo, source->whole_image,
|
|
kusano |
7d535a |
source->source_row, (JDIMENSION) 1, FALSE);
|
|
kusano |
7d535a |
/* Transfer data. Note source values are in BGR order
|
|
kusano |
7d535a |
* (even though Microsoft's own documents say the opposite).
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
inptr = image_ptr[0];
|
|
kusano |
7d535a |
outptr = source->pub.buffer[0];
|
|
kusano |
7d535a |
for (col = cinfo->image_width; col > 0; col--) {
|
|
kusano |
7d535a |
outptr[2] = *inptr++; /* can omit GETJSAMPLE() safely */
|
|
kusano |
7d535a |
outptr[1] = *inptr++;
|
|
kusano |
7d535a |
outptr[0] = *inptr++;
|
|
kusano |
7d535a |
inptr++; /* skip the 4th byte (Alpha channel) */
|
|
kusano |
7d535a |
outptr += 3;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
return 1;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* This method loads the image into whole_image during the first call on
|
|
kusano |
7d535a |
* get_pixel_rows. The get_pixel_rows pointer is then adjusted to call
|
|
kusano |
7d535a |
* get_8bit_row, get_24bit_row, or get_32bit_row on subsequent calls.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
METHODDEF(JDIMENSION)
|
|
kusano |
7d535a |
preload_image (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
bmp_source_ptr source = (bmp_source_ptr) sinfo;
|
|
kusano |
7d535a |
register FILE *infile = source->pub.input_file;
|
|
kusano |
7d535a |
register int c;
|
|
kusano |
7d535a |
register JSAMPROW out_ptr;
|
|
kusano |
7d535a |
JSAMPARRAY image_ptr;
|
|
kusano |
7d535a |
JDIMENSION row, col;
|
|
kusano |
7d535a |
cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Read the data into a virtual array in input-file row order. */
|
|
kusano |
7d535a |
for (row = 0; row < cinfo->image_height; row++) {
|
|
kusano |
7d535a |
if (progress != NULL) {
|
|
kusano |
7d535a |
progress->pub.pass_counter = (long) row;
|
|
kusano |
7d535a |
progress->pub.pass_limit = (long) cinfo->image_height;
|
|
kusano |
7d535a |
(*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
image_ptr = (*cinfo->mem->access_virt_sarray)
|
|
kusano |
7d535a |
((j_common_ptr) cinfo, source->whole_image,
|
|
kusano |
7d535a |
row, (JDIMENSION) 1, TRUE);
|
|
kusano |
7d535a |
out_ptr = image_ptr[0];
|
|
kusano |
7d535a |
for (col = source->row_width; col > 0; col--) {
|
|
kusano |
7d535a |
/* inline copy of read_byte() for speed */
|
|
kusano |
7d535a |
if ((c = getc(infile)) == EOF)
|
|
kusano |
7d535a |
ERREXIT(cinfo, JERR_INPUT_EOF);
|
|
kusano |
7d535a |
*out_ptr++ = (JSAMPLE) c;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if (progress != NULL)
|
|
kusano |
7d535a |
progress->completed_extra_passes++;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Set up to read from the virtual array in top-to-bottom order */
|
|
kusano |
7d535a |
switch (source->bits_per_pixel) {
|
|
kusano |
7d535a |
case 8:
|
|
kusano |
7d535a |
source->pub.get_pixel_rows = get_8bit_row;
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
case 24:
|
|
kusano |
7d535a |
source->pub.get_pixel_rows = get_24bit_row;
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
case 32:
|
|
kusano |
7d535a |
source->pub.get_pixel_rows = get_32bit_row;
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
default:
|
|
kusano |
7d535a |
ERREXIT(cinfo, JERR_BMP_BADDEPTH);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
source->source_row = cinfo->image_height;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* And read the first row */
|
|
kusano |
7d535a |
return (*source->pub.get_pixel_rows) (cinfo, sinfo);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Read the file header; return image size and component count.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
METHODDEF(void)
|
|
kusano |
7d535a |
start_input_bmp (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
bmp_source_ptr source = (bmp_source_ptr) sinfo;
|
|
kusano |
7d535a |
U_CHAR bmpfileheader[14];
|
|
kusano |
7d535a |
U_CHAR bmpinfoheader[64];
|
|
kusano |
7d535a |
#define GET_2B(array,offset) ((unsigned int) UCH(array[offset]) + \
|
|
kusano |
7d535a |
(((unsigned int) UCH(array[offset+1])) << 8))
|
|
kusano |
7d535a |
#define GET_4B(array,offset) ((INT32) UCH(array[offset]) + \
|
|
kusano |
7d535a |
(((INT32) UCH(array[offset+1])) << 8) + \
|
|
kusano |
7d535a |
(((INT32) UCH(array[offset+2])) << 16) + \
|
|
kusano |
7d535a |
(((INT32) UCH(array[offset+3])) << 24))
|
|
kusano |
7d535a |
INT32 bfOffBits;
|
|
kusano |
7d535a |
INT32 headerSize;
|
|
kusano |
7d535a |
INT32 biWidth;
|
|
kusano |
7d535a |
INT32 biHeight;
|
|
kusano |
7d535a |
unsigned int biPlanes;
|
|
kusano |
7d535a |
INT32 biCompression;
|
|
kusano |
7d535a |
INT32 biXPelsPerMeter,biYPelsPerMeter;
|
|
kusano |
7d535a |
INT32 biClrUsed = 0;
|
|
kusano |
7d535a |
int mapentrysize = 0; /* 0 indicates no colormap */
|
|
kusano |
7d535a |
INT32 bPad;
|
|
kusano |
7d535a |
JDIMENSION row_width;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Read and verify the bitmap file header */
|
|
kusano |
7d535a |
if (! ReadOK(source->pub.input_file, bmpfileheader, 14))
|
|
kusano |
7d535a |
ERREXIT(cinfo, JERR_INPUT_EOF);
|
|
kusano |
7d535a |
if (GET_2B(bmpfileheader,0) != 0x4D42) /* 'BM' */
|
|
kusano |
7d535a |
ERREXIT(cinfo, JERR_BMP_NOT);
|
|
kusano |
7d535a |
bfOffBits = (INT32) GET_4B(bmpfileheader,10);
|
|
kusano |
7d535a |
/* We ignore the remaining fileheader fields */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* The infoheader might be 12 bytes (OS/2 1.x), 40 bytes (Windows),
|
|
kusano |
7d535a |
* or 64 bytes (OS/2 2.x). Check the first 4 bytes to find out which.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
if (! ReadOK(source->pub.input_file, bmpinfoheader, 4))
|
|
kusano |
7d535a |
ERREXIT(cinfo, JERR_INPUT_EOF);
|
|
kusano |
7d535a |
headerSize = (INT32) GET_4B(bmpinfoheader,0);
|
|
kusano |
7d535a |
if (headerSize < 12 || headerSize > 64)
|
|
kusano |
7d535a |
ERREXIT(cinfo, JERR_BMP_BADHEADER);
|
|
kusano |
7d535a |
if (! ReadOK(source->pub.input_file, bmpinfoheader+4, headerSize-4))
|
|
kusano |
7d535a |
ERREXIT(cinfo, JERR_INPUT_EOF);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
switch ((int) headerSize) {
|
|
kusano |
7d535a |
case 12:
|
|
kusano |
7d535a |
/* Decode OS/2 1.x header (Microsoft calls this a BITMAPCOREHEADER) */
|
|
kusano |
7d535a |
biWidth = (INT32) GET_2B(bmpinfoheader,4);
|
|
kusano |
7d535a |
biHeight = (INT32) GET_2B(bmpinfoheader,6);
|
|
kusano |
7d535a |
biPlanes = GET_2B(bmpinfoheader,8);
|
|
kusano |
7d535a |
source->bits_per_pixel = (int) GET_2B(bmpinfoheader,10);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
switch (source->bits_per_pixel) {
|
|
kusano |
7d535a |
case 8: /* colormapped image */
|
|
kusano |
7d535a |
mapentrysize = 3; /* OS/2 uses RGBTRIPLE colormap */
|
|
kusano |
7d535a |
TRACEMS2(cinfo, 1, JTRC_BMP_OS2_MAPPED, (int) biWidth, (int) biHeight);
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
case 24: /* RGB image */
|
|
kusano |
7d535a |
TRACEMS2(cinfo, 1, JTRC_BMP_OS2, (int) biWidth, (int) biHeight);
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
default:
|
|
kusano |
7d535a |
ERREXIT(cinfo, JERR_BMP_BADDEPTH);
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
case 40:
|
|
kusano |
7d535a |
case 64:
|
|
kusano |
7d535a |
/* Decode Windows 3.x header (Microsoft calls this a BITMAPINFOHEADER) */
|
|
kusano |
7d535a |
/* or OS/2 2.x header, which has additional fields that we ignore */
|
|
kusano |
7d535a |
biWidth = GET_4B(bmpinfoheader,4);
|
|
kusano |
7d535a |
biHeight = GET_4B(bmpinfoheader,8);
|
|
kusano |
7d535a |
biPlanes = GET_2B(bmpinfoheader,12);
|
|
kusano |
7d535a |
source->bits_per_pixel = (int) GET_2B(bmpinfoheader,14);
|
|
kusano |
7d535a |
biCompression = GET_4B(bmpinfoheader,16);
|
|
kusano |
7d535a |
biXPelsPerMeter = GET_4B(bmpinfoheader,24);
|
|
kusano |
7d535a |
biYPelsPerMeter = GET_4B(bmpinfoheader,28);
|
|
kusano |
7d535a |
biClrUsed = GET_4B(bmpinfoheader,32);
|
|
kusano |
7d535a |
/* biSizeImage, biClrImportant fields are ignored */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
switch (source->bits_per_pixel) {
|
|
kusano |
7d535a |
case 8: /* colormapped image */
|
|
kusano |
7d535a |
mapentrysize = 4; /* Windows uses RGBQUAD colormap */
|
|
kusano |
7d535a |
TRACEMS2(cinfo, 1, JTRC_BMP_MAPPED, (int) biWidth, (int) biHeight);
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
case 24: /* RGB image */
|
|
kusano |
7d535a |
TRACEMS2(cinfo, 1, JTRC_BMP, (int) biWidth, (int) biHeight);
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
case 32: /* RGB image + Alpha channel */
|
|
kusano |
7d535a |
TRACEMS2(cinfo, 1, JTRC_BMP, (int) biWidth, (int) biHeight);
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
default:
|
|
kusano |
7d535a |
ERREXIT(cinfo, JERR_BMP_BADDEPTH);
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if (biCompression != 0)
|
|
kusano |
7d535a |
ERREXIT(cinfo, JERR_BMP_COMPRESSED);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (biXPelsPerMeter > 0 && biYPelsPerMeter > 0) {
|
|
kusano |
7d535a |
/* Set JFIF density parameters from the BMP data */
|
|
kusano |
7d535a |
cinfo->X_density = (UINT16) (biXPelsPerMeter/100); /* 100 cm per meter */
|
|
kusano |
7d535a |
cinfo->Y_density = (UINT16) (biYPelsPerMeter/100);
|
|
kusano |
7d535a |
cinfo->density_unit = 2; /* dots/cm */
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
default:
|
|
kusano |
7d535a |
ERREXIT(cinfo, JERR_BMP_BADHEADER);
|
|
kusano |
7d535a |
return;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (biWidth <= 0 || biHeight <= 0)
|
|
kusano |
7d535a |
ERREXIT(cinfo, JERR_BMP_EMPTY);
|
|
kusano |
7d535a |
if (biPlanes != 1)
|
|
kusano |
7d535a |
ERREXIT(cinfo, JERR_BMP_BADPLANES);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Compute distance to bitmap data --- will adjust for colormap below */
|
|
kusano |
7d535a |
bPad = bfOffBits - (headerSize + 14);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Read the colormap, if any */
|
|
kusano |
7d535a |
if (mapentrysize > 0) {
|
|
kusano |
7d535a |
if (biClrUsed <= 0)
|
|
kusano |
7d535a |
biClrUsed = 256; /* assume it's 256 */
|
|
kusano |
7d535a |
else if (biClrUsed > 256)
|
|
kusano |
7d535a |
ERREXIT(cinfo, JERR_BMP_BADCMAP);
|
|
kusano |
7d535a |
/* Allocate space to store the colormap */
|
|
kusano |
7d535a |
source->colormap = (*cinfo->mem->alloc_sarray)
|
|
kusano |
7d535a |
((j_common_ptr) cinfo, JPOOL_IMAGE,
|
|
kusano |
7d535a |
(JDIMENSION) biClrUsed, (JDIMENSION) 3);
|
|
kusano |
7d535a |
/* and read it from the file */
|
|
kusano |
7d535a |
read_colormap(source, (int) biClrUsed, mapentrysize);
|
|
kusano |
7d535a |
/* account for size of colormap */
|
|
kusano |
7d535a |
bPad -= biClrUsed * mapentrysize;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Skip any remaining pad bytes */
|
|
kusano |
7d535a |
if (bPad < 0) /* incorrect bfOffBits value? */
|
|
kusano |
7d535a |
ERREXIT(cinfo, JERR_BMP_BADHEADER);
|
|
kusano |
7d535a |
while (--bPad >= 0) {
|
|
kusano |
7d535a |
(void) read_byte(source);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Compute row width in file, including padding to 4-byte boundary */
|
|
kusano |
7d535a |
if (source->bits_per_pixel == 24)
|
|
kusano |
7d535a |
row_width = (JDIMENSION) (biWidth * 3);
|
|
kusano |
7d535a |
else if (source->bits_per_pixel == 32)
|
|
kusano |
7d535a |
row_width = (JDIMENSION) (biWidth * 4);
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
row_width = (JDIMENSION) biWidth;
|
|
kusano |
7d535a |
while ((row_width & 3) != 0) row_width++;
|
|
kusano |
7d535a |
source->row_width = row_width;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Allocate space for inversion array, prepare for preload pass */
|
|
kusano |
7d535a |
source->whole_image = (*cinfo->mem->request_virt_sarray)
|
|
kusano |
7d535a |
((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
|
|
kusano |
7d535a |
row_width, (JDIMENSION) biHeight, (JDIMENSION) 1);
|
|
kusano |
7d535a |
source->pub.get_pixel_rows = preload_image;
|
|
kusano |
7d535a |
if (cinfo->progress != NULL) {
|
|
kusano |
7d535a |
cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
|
|
kusano |
7d535a |
progress->total_extra_passes++; /* count file input as separate pass */
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Allocate one-row buffer for returned data */
|
|
kusano |
7d535a |
source->pub.buffer = (*cinfo->mem->alloc_sarray)
|
|
kusano |
7d535a |
((j_common_ptr) cinfo, JPOOL_IMAGE,
|
|
kusano |
7d535a |
(JDIMENSION) (biWidth * 3), (JDIMENSION) 1);
|
|
kusano |
7d535a |
source->pub.buffer_height = 1;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
cinfo->in_color_space = JCS_RGB;
|
|
kusano |
7d535a |
cinfo->input_components = 3;
|
|
kusano |
7d535a |
cinfo->data_precision = 8;
|
|
kusano |
7d535a |
cinfo->image_width = (JDIMENSION) biWidth;
|
|
kusano |
7d535a |
cinfo->image_height = (JDIMENSION) biHeight;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Finish up at the end of the file.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
METHODDEF(void)
|
|
kusano |
7d535a |
finish_input_bmp (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
/* no work */
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* The module selection routine for BMP format input.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
GLOBAL(cjpeg_source_ptr)
|
|
kusano |
7d535a |
jinit_read_bmp (j_compress_ptr cinfo)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
bmp_source_ptr source;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Create module interface object */
|
|
kusano |
7d535a |
source = (bmp_source_ptr)
|
|
kusano |
7d535a |
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
|
kusano |
7d535a |
SIZEOF(bmp_source_struct));
|
|
kusano |
7d535a |
source->cinfo = cinfo; /* make back link for subroutines */
|
|
kusano |
7d535a |
/* Fill in method ptrs, except get_pixel_rows which start_input sets */
|
|
kusano |
7d535a |
source->pub.start_input = start_input_bmp;
|
|
kusano |
7d535a |
source->pub.finish_input = finish_input_bmp;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
return (cjpeg_source_ptr) source;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#endif /* BMP_SUPPORTED */
|