Final 350 project

Dependencies:   uzair Camera_LS_Y201 F7_Ethernet LCD_DISCO_F746NG NetworkAPI SDFileSystem mbed

Committer:
shoaib_ahmed
Date:
Mon Jul 31 09:16:35 2017 +0000
Revision:
0:791a779d6220
final project;

Who changed what in which revision?

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