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 * wrbmp.c
shoaib_ahmed 0:791a779d6220 3 *
shoaib_ahmed 0:791a779d6220 4 * Copyright (C) 1994-1996, Thomas G. Lane.
shoaib_ahmed 0:791a779d6220 5 * This file is part of the Independent JPEG Group's software.
shoaib_ahmed 0:791a779d6220 6 * For conditions of distribution and use, see the accompanying README file.
shoaib_ahmed 0:791a779d6220 7 *
shoaib_ahmed 0:791a779d6220 8 * This file contains routines to write output images in Microsoft "BMP"
shoaib_ahmed 0:791a779d6220 9 * format (MS Windows 3.x and OS/2 1.x flavors).
shoaib_ahmed 0:791a779d6220 10 * Either 8-bit colormapped or 24-bit full-color format can be written.
shoaib_ahmed 0:791a779d6220 11 * No compression is supported.
shoaib_ahmed 0:791a779d6220 12 *
shoaib_ahmed 0:791a779d6220 13 * These routines may need modification for non-Unix environments or
shoaib_ahmed 0:791a779d6220 14 * specialized applications. As they stand, they assume output to
shoaib_ahmed 0:791a779d6220 15 * an ordinary stdio stream.
shoaib_ahmed 0:791a779d6220 16 *
shoaib_ahmed 0:791a779d6220 17 * This code contributed by James Arthur Boucher.
shoaib_ahmed 0:791a779d6220 18 */
shoaib_ahmed 0:791a779d6220 19
shoaib_ahmed 0:791a779d6220 20 #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
shoaib_ahmed 0:791a779d6220 21
shoaib_ahmed 0:791a779d6220 22 #ifdef BMP_SUPPORTED
shoaib_ahmed 0:791a779d6220 23
shoaib_ahmed 0:791a779d6220 24
shoaib_ahmed 0:791a779d6220 25 /*
shoaib_ahmed 0:791a779d6220 26 * To support 12-bit JPEG data, we'd have to scale output down to 8 bits.
shoaib_ahmed 0:791a779d6220 27 * This is not yet implemented.
shoaib_ahmed 0:791a779d6220 28 */
shoaib_ahmed 0:791a779d6220 29
shoaib_ahmed 0:791a779d6220 30 #if BITS_IN_JSAMPLE != 8
shoaib_ahmed 0:791a779d6220 31 Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
shoaib_ahmed 0:791a779d6220 32 #endif
shoaib_ahmed 0:791a779d6220 33
shoaib_ahmed 0:791a779d6220 34 /*
shoaib_ahmed 0:791a779d6220 35 * Since BMP stores scanlines bottom-to-top, we have to invert the image
shoaib_ahmed 0:791a779d6220 36 * from JPEG's top-to-bottom order. To do this, we save the outgoing data
shoaib_ahmed 0:791a779d6220 37 * in a virtual array during put_pixel_row calls, then actually emit the
shoaib_ahmed 0:791a779d6220 38 * BMP file during finish_output. The virtual array contains one JSAMPLE per
shoaib_ahmed 0:791a779d6220 39 * pixel if the output is grayscale or colormapped, three if it is full color.
shoaib_ahmed 0:791a779d6220 40 */
shoaib_ahmed 0:791a779d6220 41
shoaib_ahmed 0:791a779d6220 42 /* Private version of data destination object */
shoaib_ahmed 0:791a779d6220 43
shoaib_ahmed 0:791a779d6220 44 typedef struct {
shoaib_ahmed 0:791a779d6220 45 struct djpeg_dest_struct pub; /* public fields */
shoaib_ahmed 0:791a779d6220 46
shoaib_ahmed 0:791a779d6220 47 boolean is_os2; /* saves the OS2 format request flag */
shoaib_ahmed 0:791a779d6220 48
shoaib_ahmed 0:791a779d6220 49 jvirt_sarray_ptr whole_image; /* needed to reverse row order */
shoaib_ahmed 0:791a779d6220 50 JDIMENSION data_width; /* JSAMPLEs per row */
shoaib_ahmed 0:791a779d6220 51 JDIMENSION row_width; /* physical width of one row in the BMP file */
shoaib_ahmed 0:791a779d6220 52 int pad_bytes; /* number of padding bytes needed per row */
shoaib_ahmed 0:791a779d6220 53 JDIMENSION cur_output_row; /* next row# to write to virtual array */
shoaib_ahmed 0:791a779d6220 54 } bmp_dest_struct;
shoaib_ahmed 0:791a779d6220 55
shoaib_ahmed 0:791a779d6220 56 typedef bmp_dest_struct * bmp_dest_ptr;
shoaib_ahmed 0:791a779d6220 57
shoaib_ahmed 0:791a779d6220 58
shoaib_ahmed 0:791a779d6220 59 /* Forward declarations */
shoaib_ahmed 0:791a779d6220 60 LOCAL(void) write_colormap
shoaib_ahmed 0:791a779d6220 61 JPP((j_decompress_ptr cinfo, bmp_dest_ptr dest,
shoaib_ahmed 0:791a779d6220 62 int map_colors, int map_entry_size));
shoaib_ahmed 0:791a779d6220 63
shoaib_ahmed 0:791a779d6220 64
shoaib_ahmed 0:791a779d6220 65 /*
shoaib_ahmed 0:791a779d6220 66 * Write some pixel data.
shoaib_ahmed 0:791a779d6220 67 * In this module rows_supplied will always be 1.
shoaib_ahmed 0:791a779d6220 68 */
shoaib_ahmed 0:791a779d6220 69
shoaib_ahmed 0:791a779d6220 70 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 71 put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
shoaib_ahmed 0:791a779d6220 72 JDIMENSION rows_supplied)
shoaib_ahmed 0:791a779d6220 73 /* This version is for writing 24-bit pixels */
shoaib_ahmed 0:791a779d6220 74 {
shoaib_ahmed 0:791a779d6220 75 bmp_dest_ptr dest = (bmp_dest_ptr) dinfo;
shoaib_ahmed 0:791a779d6220 76 JSAMPARRAY image_ptr;
shoaib_ahmed 0:791a779d6220 77 register JSAMPROW inptr, outptr;
shoaib_ahmed 0:791a779d6220 78 register JDIMENSION col;
shoaib_ahmed 0:791a779d6220 79 int pad;
shoaib_ahmed 0:791a779d6220 80
shoaib_ahmed 0:791a779d6220 81 /* Access next row in virtual array */
shoaib_ahmed 0:791a779d6220 82 image_ptr = (*cinfo->mem->access_virt_sarray)
shoaib_ahmed 0:791a779d6220 83 ((j_common_ptr) cinfo, dest->whole_image,
shoaib_ahmed 0:791a779d6220 84 dest->cur_output_row, (JDIMENSION) 1, TRUE);
shoaib_ahmed 0:791a779d6220 85 dest->cur_output_row++;
shoaib_ahmed 0:791a779d6220 86
shoaib_ahmed 0:791a779d6220 87 /* Transfer data. Note destination values must be in BGR order
shoaib_ahmed 0:791a779d6220 88 * (even though Microsoft's own documents say the opposite).
shoaib_ahmed 0:791a779d6220 89 */
shoaib_ahmed 0:791a779d6220 90 inptr = dest->pub.buffer[0];
shoaib_ahmed 0:791a779d6220 91 outptr = image_ptr[0];
shoaib_ahmed 0:791a779d6220 92 for (col = cinfo->output_width; col > 0; col--) {
shoaib_ahmed 0:791a779d6220 93 outptr[2] = *inptr++; /* can omit GETJSAMPLE() safely */
shoaib_ahmed 0:791a779d6220 94 outptr[1] = *inptr++;
shoaib_ahmed 0:791a779d6220 95 outptr[0] = *inptr++;
shoaib_ahmed 0:791a779d6220 96 outptr += 3;
shoaib_ahmed 0:791a779d6220 97 }
shoaib_ahmed 0:791a779d6220 98
shoaib_ahmed 0:791a779d6220 99 /* Zero out the pad bytes. */
shoaib_ahmed 0:791a779d6220 100 pad = dest->pad_bytes;
shoaib_ahmed 0:791a779d6220 101 while (--pad >= 0)
shoaib_ahmed 0:791a779d6220 102 *outptr++ = 0;
shoaib_ahmed 0:791a779d6220 103 }
shoaib_ahmed 0:791a779d6220 104
shoaib_ahmed 0:791a779d6220 105 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 106 put_gray_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
shoaib_ahmed 0:791a779d6220 107 JDIMENSION rows_supplied)
shoaib_ahmed 0:791a779d6220 108 /* This version is for grayscale OR quantized color output */
shoaib_ahmed 0:791a779d6220 109 {
shoaib_ahmed 0:791a779d6220 110 bmp_dest_ptr dest = (bmp_dest_ptr) dinfo;
shoaib_ahmed 0:791a779d6220 111 JSAMPARRAY image_ptr;
shoaib_ahmed 0:791a779d6220 112 register JSAMPROW inptr, outptr;
shoaib_ahmed 0:791a779d6220 113 register JDIMENSION col;
shoaib_ahmed 0:791a779d6220 114 int pad;
shoaib_ahmed 0:791a779d6220 115
shoaib_ahmed 0:791a779d6220 116 /* Access next row in virtual array */
shoaib_ahmed 0:791a779d6220 117 image_ptr = (*cinfo->mem->access_virt_sarray)
shoaib_ahmed 0:791a779d6220 118 ((j_common_ptr) cinfo, dest->whole_image,
shoaib_ahmed 0:791a779d6220 119 dest->cur_output_row, (JDIMENSION) 1, TRUE);
shoaib_ahmed 0:791a779d6220 120 dest->cur_output_row++;
shoaib_ahmed 0:791a779d6220 121
shoaib_ahmed 0:791a779d6220 122 /* Transfer data. */
shoaib_ahmed 0:791a779d6220 123 inptr = dest->pub.buffer[0];
shoaib_ahmed 0:791a779d6220 124 outptr = image_ptr[0];
shoaib_ahmed 0:791a779d6220 125 for (col = cinfo->output_width; col > 0; col--) {
shoaib_ahmed 0:791a779d6220 126 *outptr++ = *inptr++; /* can omit GETJSAMPLE() safely */
shoaib_ahmed 0:791a779d6220 127 }
shoaib_ahmed 0:791a779d6220 128
shoaib_ahmed 0:791a779d6220 129 /* Zero out the pad bytes. */
shoaib_ahmed 0:791a779d6220 130 pad = dest->pad_bytes;
shoaib_ahmed 0:791a779d6220 131 while (--pad >= 0)
shoaib_ahmed 0:791a779d6220 132 *outptr++ = 0;
shoaib_ahmed 0:791a779d6220 133 }
shoaib_ahmed 0:791a779d6220 134
shoaib_ahmed 0:791a779d6220 135
shoaib_ahmed 0:791a779d6220 136 /*
shoaib_ahmed 0:791a779d6220 137 * Startup: normally writes the file header.
shoaib_ahmed 0:791a779d6220 138 * In this module we may as well postpone everything until finish_output.
shoaib_ahmed 0:791a779d6220 139 */
shoaib_ahmed 0:791a779d6220 140
shoaib_ahmed 0:791a779d6220 141 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 142 start_output_bmp (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
shoaib_ahmed 0:791a779d6220 143 {
shoaib_ahmed 0:791a779d6220 144 /* no work here */
shoaib_ahmed 0:791a779d6220 145 }
shoaib_ahmed 0:791a779d6220 146
shoaib_ahmed 0:791a779d6220 147
shoaib_ahmed 0:791a779d6220 148 /*
shoaib_ahmed 0:791a779d6220 149 * Finish up at the end of the file.
shoaib_ahmed 0:791a779d6220 150 *
shoaib_ahmed 0:791a779d6220 151 * Here is where we really output the BMP file.
shoaib_ahmed 0:791a779d6220 152 *
shoaib_ahmed 0:791a779d6220 153 * First, routines to write the Windows and OS/2 variants of the file header.
shoaib_ahmed 0:791a779d6220 154 */
shoaib_ahmed 0:791a779d6220 155
shoaib_ahmed 0:791a779d6220 156 LOCAL(void)
shoaib_ahmed 0:791a779d6220 157 write_bmp_header (j_decompress_ptr cinfo, bmp_dest_ptr dest)
shoaib_ahmed 0:791a779d6220 158 /* Write a Windows-style BMP file header, including colormap if needed */
shoaib_ahmed 0:791a779d6220 159 {
shoaib_ahmed 0:791a779d6220 160 char bmpfileheader[14];
shoaib_ahmed 0:791a779d6220 161 char bmpinfoheader[40];
shoaib_ahmed 0:791a779d6220 162 #define PUT_2B(array,offset,value) \
shoaib_ahmed 0:791a779d6220 163 (array[offset] = (char) ((value) & 0xFF), \
shoaib_ahmed 0:791a779d6220 164 array[offset+1] = (char) (((value) >> 8) & 0xFF))
shoaib_ahmed 0:791a779d6220 165 #define PUT_4B(array,offset,value) \
shoaib_ahmed 0:791a779d6220 166 (array[offset] = (char) ((value) & 0xFF), \
shoaib_ahmed 0:791a779d6220 167 array[offset+1] = (char) (((value) >> 8) & 0xFF), \
shoaib_ahmed 0:791a779d6220 168 array[offset+2] = (char) (((value) >> 16) & 0xFF), \
shoaib_ahmed 0:791a779d6220 169 array[offset+3] = (char) (((value) >> 24) & 0xFF))
shoaib_ahmed 0:791a779d6220 170 INT32 headersize, bfSize;
shoaib_ahmed 0:791a779d6220 171 int bits_per_pixel, cmap_entries;
shoaib_ahmed 0:791a779d6220 172
shoaib_ahmed 0:791a779d6220 173 /* Compute colormap size and total file size */
shoaib_ahmed 0:791a779d6220 174 if (cinfo->out_color_space == JCS_RGB) {
shoaib_ahmed 0:791a779d6220 175 if (cinfo->quantize_colors) {
shoaib_ahmed 0:791a779d6220 176 /* Colormapped RGB */
shoaib_ahmed 0:791a779d6220 177 bits_per_pixel = 8;
shoaib_ahmed 0:791a779d6220 178 cmap_entries = 256;
shoaib_ahmed 0:791a779d6220 179 } else {
shoaib_ahmed 0:791a779d6220 180 /* Unquantized, full color RGB */
shoaib_ahmed 0:791a779d6220 181 bits_per_pixel = 24;
shoaib_ahmed 0:791a779d6220 182 cmap_entries = 0;
shoaib_ahmed 0:791a779d6220 183 }
shoaib_ahmed 0:791a779d6220 184 } else {
shoaib_ahmed 0:791a779d6220 185 /* Grayscale output. We need to fake a 256-entry colormap. */
shoaib_ahmed 0:791a779d6220 186 bits_per_pixel = 8;
shoaib_ahmed 0:791a779d6220 187 cmap_entries = 256;
shoaib_ahmed 0:791a779d6220 188 }
shoaib_ahmed 0:791a779d6220 189 /* File size */
shoaib_ahmed 0:791a779d6220 190 headersize = 14 + 40 + cmap_entries * 4; /* Header and colormap */
shoaib_ahmed 0:791a779d6220 191 bfSize = headersize + (INT32) dest->row_width * (INT32) cinfo->output_height;
shoaib_ahmed 0:791a779d6220 192
shoaib_ahmed 0:791a779d6220 193 /* Set unused fields of header to 0 */
shoaib_ahmed 0:791a779d6220 194 MEMZERO(bmpfileheader, SIZEOF(bmpfileheader));
shoaib_ahmed 0:791a779d6220 195 MEMZERO(bmpinfoheader, SIZEOF(bmpinfoheader));
shoaib_ahmed 0:791a779d6220 196
shoaib_ahmed 0:791a779d6220 197 /* Fill the file header */
shoaib_ahmed 0:791a779d6220 198 bmpfileheader[0] = 0x42; /* first 2 bytes are ASCII 'B', 'M' */
shoaib_ahmed 0:791a779d6220 199 bmpfileheader[1] = 0x4D;
shoaib_ahmed 0:791a779d6220 200 PUT_4B(bmpfileheader, 2, bfSize); /* bfSize */
shoaib_ahmed 0:791a779d6220 201 /* we leave bfReserved1 & bfReserved2 = 0 */
shoaib_ahmed 0:791a779d6220 202 PUT_4B(bmpfileheader, 10, headersize); /* bfOffBits */
shoaib_ahmed 0:791a779d6220 203
shoaib_ahmed 0:791a779d6220 204 /* Fill the info header (Microsoft calls this a BITMAPINFOHEADER) */
shoaib_ahmed 0:791a779d6220 205 PUT_2B(bmpinfoheader, 0, 40); /* biSize */
shoaib_ahmed 0:791a779d6220 206 PUT_4B(bmpinfoheader, 4, cinfo->output_width); /* biWidth */
shoaib_ahmed 0:791a779d6220 207 PUT_4B(bmpinfoheader, 8, cinfo->output_height); /* biHeight */
shoaib_ahmed 0:791a779d6220 208 PUT_2B(bmpinfoheader, 12, 1); /* biPlanes - must be 1 */
shoaib_ahmed 0:791a779d6220 209 PUT_2B(bmpinfoheader, 14, bits_per_pixel); /* biBitCount */
shoaib_ahmed 0:791a779d6220 210 /* we leave biCompression = 0, for none */
shoaib_ahmed 0:791a779d6220 211 /* we leave biSizeImage = 0; this is correct for uncompressed data */
shoaib_ahmed 0:791a779d6220 212 if (cinfo->density_unit == 2) { /* if have density in dots/cm, then */
shoaib_ahmed 0:791a779d6220 213 PUT_4B(bmpinfoheader, 24, (INT32) (cinfo->X_density*100)); /* XPels/M */
shoaib_ahmed 0:791a779d6220 214 PUT_4B(bmpinfoheader, 28, (INT32) (cinfo->Y_density*100)); /* XPels/M */
shoaib_ahmed 0:791a779d6220 215 }
shoaib_ahmed 0:791a779d6220 216 PUT_2B(bmpinfoheader, 32, cmap_entries); /* biClrUsed */
shoaib_ahmed 0:791a779d6220 217 /* we leave biClrImportant = 0 */
shoaib_ahmed 0:791a779d6220 218
shoaib_ahmed 0:791a779d6220 219 if (JFWRITE(dest->pub.output_file, bmpfileheader, 14) != (size_t) 14)
shoaib_ahmed 0:791a779d6220 220 ERREXIT(cinfo, JERR_FILE_WRITE);
shoaib_ahmed 0:791a779d6220 221 if (JFWRITE(dest->pub.output_file, bmpinfoheader, 40) != (size_t) 40)
shoaib_ahmed 0:791a779d6220 222 ERREXIT(cinfo, JERR_FILE_WRITE);
shoaib_ahmed 0:791a779d6220 223
shoaib_ahmed 0:791a779d6220 224 if (cmap_entries > 0)
shoaib_ahmed 0:791a779d6220 225 write_colormap(cinfo, dest, cmap_entries, 4);
shoaib_ahmed 0:791a779d6220 226 }
shoaib_ahmed 0:791a779d6220 227
shoaib_ahmed 0:791a779d6220 228
shoaib_ahmed 0:791a779d6220 229 LOCAL(void)
shoaib_ahmed 0:791a779d6220 230 write_os2_header (j_decompress_ptr cinfo, bmp_dest_ptr dest)
shoaib_ahmed 0:791a779d6220 231 /* Write an OS2-style BMP file header, including colormap if needed */
shoaib_ahmed 0:791a779d6220 232 {
shoaib_ahmed 0:791a779d6220 233 char bmpfileheader[14];
shoaib_ahmed 0:791a779d6220 234 char bmpcoreheader[12];
shoaib_ahmed 0:791a779d6220 235 INT32 headersize, bfSize;
shoaib_ahmed 0:791a779d6220 236 int bits_per_pixel, cmap_entries;
shoaib_ahmed 0:791a779d6220 237
shoaib_ahmed 0:791a779d6220 238 /* Compute colormap size and total file size */
shoaib_ahmed 0:791a779d6220 239 if (cinfo->out_color_space == JCS_RGB) {
shoaib_ahmed 0:791a779d6220 240 if (cinfo->quantize_colors) {
shoaib_ahmed 0:791a779d6220 241 /* Colormapped RGB */
shoaib_ahmed 0:791a779d6220 242 bits_per_pixel = 8;
shoaib_ahmed 0:791a779d6220 243 cmap_entries = 256;
shoaib_ahmed 0:791a779d6220 244 } else {
shoaib_ahmed 0:791a779d6220 245 /* Unquantized, full color RGB */
shoaib_ahmed 0:791a779d6220 246 bits_per_pixel = 24;
shoaib_ahmed 0:791a779d6220 247 cmap_entries = 0;
shoaib_ahmed 0:791a779d6220 248 }
shoaib_ahmed 0:791a779d6220 249 } else {
shoaib_ahmed 0:791a779d6220 250 /* Grayscale output. We need to fake a 256-entry colormap. */
shoaib_ahmed 0:791a779d6220 251 bits_per_pixel = 8;
shoaib_ahmed 0:791a779d6220 252 cmap_entries = 256;
shoaib_ahmed 0:791a779d6220 253 }
shoaib_ahmed 0:791a779d6220 254 /* File size */
shoaib_ahmed 0:791a779d6220 255 headersize = 14 + 12 + cmap_entries * 3; /* Header and colormap */
shoaib_ahmed 0:791a779d6220 256 bfSize = headersize + (INT32) dest->row_width * (INT32) cinfo->output_height;
shoaib_ahmed 0:791a779d6220 257
shoaib_ahmed 0:791a779d6220 258 /* Set unused fields of header to 0 */
shoaib_ahmed 0:791a779d6220 259 MEMZERO(bmpfileheader, SIZEOF(bmpfileheader));
shoaib_ahmed 0:791a779d6220 260 MEMZERO(bmpcoreheader, SIZEOF(bmpcoreheader));
shoaib_ahmed 0:791a779d6220 261
shoaib_ahmed 0:791a779d6220 262 /* Fill the file header */
shoaib_ahmed 0:791a779d6220 263 bmpfileheader[0] = 0x42; /* first 2 bytes are ASCII 'B', 'M' */
shoaib_ahmed 0:791a779d6220 264 bmpfileheader[1] = 0x4D;
shoaib_ahmed 0:791a779d6220 265 PUT_4B(bmpfileheader, 2, bfSize); /* bfSize */
shoaib_ahmed 0:791a779d6220 266 /* we leave bfReserved1 & bfReserved2 = 0 */
shoaib_ahmed 0:791a779d6220 267 PUT_4B(bmpfileheader, 10, headersize); /* bfOffBits */
shoaib_ahmed 0:791a779d6220 268
shoaib_ahmed 0:791a779d6220 269 /* Fill the info header (Microsoft calls this a BITMAPCOREHEADER) */
shoaib_ahmed 0:791a779d6220 270 PUT_2B(bmpcoreheader, 0, 12); /* bcSize */
shoaib_ahmed 0:791a779d6220 271 PUT_2B(bmpcoreheader, 4, cinfo->output_width); /* bcWidth */
shoaib_ahmed 0:791a779d6220 272 PUT_2B(bmpcoreheader, 6, cinfo->output_height); /* bcHeight */
shoaib_ahmed 0:791a779d6220 273 PUT_2B(bmpcoreheader, 8, 1); /* bcPlanes - must be 1 */
shoaib_ahmed 0:791a779d6220 274 PUT_2B(bmpcoreheader, 10, bits_per_pixel); /* bcBitCount */
shoaib_ahmed 0:791a779d6220 275
shoaib_ahmed 0:791a779d6220 276 if (JFWRITE(dest->pub.output_file, bmpfileheader, 14) != (size_t) 14)
shoaib_ahmed 0:791a779d6220 277 ERREXIT(cinfo, JERR_FILE_WRITE);
shoaib_ahmed 0:791a779d6220 278 if (JFWRITE(dest->pub.output_file, bmpcoreheader, 12) != (size_t) 12)
shoaib_ahmed 0:791a779d6220 279 ERREXIT(cinfo, JERR_FILE_WRITE);
shoaib_ahmed 0:791a779d6220 280
shoaib_ahmed 0:791a779d6220 281 if (cmap_entries > 0)
shoaib_ahmed 0:791a779d6220 282 write_colormap(cinfo, dest, cmap_entries, 3);
shoaib_ahmed 0:791a779d6220 283 }
shoaib_ahmed 0:791a779d6220 284
shoaib_ahmed 0:791a779d6220 285
shoaib_ahmed 0:791a779d6220 286 /*
shoaib_ahmed 0:791a779d6220 287 * Write the colormap.
shoaib_ahmed 0:791a779d6220 288 * Windows uses BGR0 map entries; OS/2 uses BGR entries.
shoaib_ahmed 0:791a779d6220 289 */
shoaib_ahmed 0:791a779d6220 290
shoaib_ahmed 0:791a779d6220 291 LOCAL(void)
shoaib_ahmed 0:791a779d6220 292 write_colormap (j_decompress_ptr cinfo, bmp_dest_ptr dest,
shoaib_ahmed 0:791a779d6220 293 int map_colors, int map_entry_size)
shoaib_ahmed 0:791a779d6220 294 {
shoaib_ahmed 0:791a779d6220 295 JSAMPARRAY colormap = cinfo->colormap;
shoaib_ahmed 0:791a779d6220 296 int num_colors = cinfo->actual_number_of_colors;
shoaib_ahmed 0:791a779d6220 297 FILE * outfile = dest->pub.output_file;
shoaib_ahmed 0:791a779d6220 298 int i;
shoaib_ahmed 0:791a779d6220 299
shoaib_ahmed 0:791a779d6220 300 if (colormap != NULL) {
shoaib_ahmed 0:791a779d6220 301 if (cinfo->out_color_components == 3) {
shoaib_ahmed 0:791a779d6220 302 /* Normal case with RGB colormap */
shoaib_ahmed 0:791a779d6220 303 for (i = 0; i < num_colors; i++) {
shoaib_ahmed 0:791a779d6220 304 putc(GETJSAMPLE(colormap[2][i]), outfile);
shoaib_ahmed 0:791a779d6220 305 putc(GETJSAMPLE(colormap[1][i]), outfile);
shoaib_ahmed 0:791a779d6220 306 putc(GETJSAMPLE(colormap[0][i]), outfile);
shoaib_ahmed 0:791a779d6220 307 if (map_entry_size == 4)
shoaib_ahmed 0:791a779d6220 308 putc(0, outfile);
shoaib_ahmed 0:791a779d6220 309 }
shoaib_ahmed 0:791a779d6220 310 } else {
shoaib_ahmed 0:791a779d6220 311 /* Grayscale colormap (only happens with grayscale quantization) */
shoaib_ahmed 0:791a779d6220 312 for (i = 0; i < num_colors; i++) {
shoaib_ahmed 0:791a779d6220 313 putc(GETJSAMPLE(colormap[0][i]), outfile);
shoaib_ahmed 0:791a779d6220 314 putc(GETJSAMPLE(colormap[0][i]), outfile);
shoaib_ahmed 0:791a779d6220 315 putc(GETJSAMPLE(colormap[0][i]), outfile);
shoaib_ahmed 0:791a779d6220 316 if (map_entry_size == 4)
shoaib_ahmed 0:791a779d6220 317 putc(0, outfile);
shoaib_ahmed 0:791a779d6220 318 }
shoaib_ahmed 0:791a779d6220 319 }
shoaib_ahmed 0:791a779d6220 320 } else {
shoaib_ahmed 0:791a779d6220 321 /* If no colormap, must be grayscale data. Generate a linear "map". */
shoaib_ahmed 0:791a779d6220 322 for (i = 0; i < 256; i++) {
shoaib_ahmed 0:791a779d6220 323 putc(i, outfile);
shoaib_ahmed 0:791a779d6220 324 putc(i, outfile);
shoaib_ahmed 0:791a779d6220 325 putc(i, outfile);
shoaib_ahmed 0:791a779d6220 326 if (map_entry_size == 4)
shoaib_ahmed 0:791a779d6220 327 putc(0, outfile);
shoaib_ahmed 0:791a779d6220 328 }
shoaib_ahmed 0:791a779d6220 329 }
shoaib_ahmed 0:791a779d6220 330 /* Pad colormap with zeros to ensure specified number of colormap entries */
shoaib_ahmed 0:791a779d6220 331 if (i > map_colors)
shoaib_ahmed 0:791a779d6220 332 ERREXIT1(cinfo, JERR_TOO_MANY_COLORS, i);
shoaib_ahmed 0:791a779d6220 333 for (; i < map_colors; i++) {
shoaib_ahmed 0:791a779d6220 334 putc(0, outfile);
shoaib_ahmed 0:791a779d6220 335 putc(0, outfile);
shoaib_ahmed 0:791a779d6220 336 putc(0, outfile);
shoaib_ahmed 0:791a779d6220 337 if (map_entry_size == 4)
shoaib_ahmed 0:791a779d6220 338 putc(0, outfile);
shoaib_ahmed 0:791a779d6220 339 }
shoaib_ahmed 0:791a779d6220 340 }
shoaib_ahmed 0:791a779d6220 341
shoaib_ahmed 0:791a779d6220 342
shoaib_ahmed 0:791a779d6220 343 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 344 finish_output_bmp (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
shoaib_ahmed 0:791a779d6220 345 {
shoaib_ahmed 0:791a779d6220 346 bmp_dest_ptr dest = (bmp_dest_ptr) dinfo;
shoaib_ahmed 0:791a779d6220 347 register FILE * outfile = dest->pub.output_file;
shoaib_ahmed 0:791a779d6220 348 JSAMPARRAY image_ptr;
shoaib_ahmed 0:791a779d6220 349 register JSAMPROW data_ptr;
shoaib_ahmed 0:791a779d6220 350 JDIMENSION row;
shoaib_ahmed 0:791a779d6220 351 register JDIMENSION col;
shoaib_ahmed 0:791a779d6220 352 cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
shoaib_ahmed 0:791a779d6220 353
shoaib_ahmed 0:791a779d6220 354 /* Write the header and colormap */
shoaib_ahmed 0:791a779d6220 355 if (dest->is_os2)
shoaib_ahmed 0:791a779d6220 356 write_os2_header(cinfo, dest);
shoaib_ahmed 0:791a779d6220 357 else
shoaib_ahmed 0:791a779d6220 358 write_bmp_header(cinfo, dest);
shoaib_ahmed 0:791a779d6220 359
shoaib_ahmed 0:791a779d6220 360 /* Write the file body from our virtual array */
shoaib_ahmed 0:791a779d6220 361 for (row = cinfo->output_height; row > 0; row--) {
shoaib_ahmed 0:791a779d6220 362 if (progress != NULL) {
shoaib_ahmed 0:791a779d6220 363 progress->pub.pass_counter = (long) (cinfo->output_height - row);
shoaib_ahmed 0:791a779d6220 364 progress->pub.pass_limit = (long) cinfo->output_height;
shoaib_ahmed 0:791a779d6220 365 (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
shoaib_ahmed 0:791a779d6220 366 }
shoaib_ahmed 0:791a779d6220 367 image_ptr = (*cinfo->mem->access_virt_sarray)
shoaib_ahmed 0:791a779d6220 368 ((j_common_ptr) cinfo, dest->whole_image, row-1, (JDIMENSION) 1, FALSE);
shoaib_ahmed 0:791a779d6220 369 data_ptr = image_ptr[0];
shoaib_ahmed 0:791a779d6220 370 for (col = dest->row_width; col > 0; col--) {
shoaib_ahmed 0:791a779d6220 371 putc(GETJSAMPLE(*data_ptr), outfile);
shoaib_ahmed 0:791a779d6220 372 data_ptr++;
shoaib_ahmed 0:791a779d6220 373 }
shoaib_ahmed 0:791a779d6220 374 }
shoaib_ahmed 0:791a779d6220 375 if (progress != NULL)
shoaib_ahmed 0:791a779d6220 376 progress->completed_extra_passes++;
shoaib_ahmed 0:791a779d6220 377
shoaib_ahmed 0:791a779d6220 378 /* Make sure we wrote the output file OK */
shoaib_ahmed 0:791a779d6220 379 fflush(outfile);
shoaib_ahmed 0:791a779d6220 380 if (ferror(outfile))
shoaib_ahmed 0:791a779d6220 381 ERREXIT(cinfo, JERR_FILE_WRITE);
shoaib_ahmed 0:791a779d6220 382 }
shoaib_ahmed 0:791a779d6220 383
shoaib_ahmed 0:791a779d6220 384
shoaib_ahmed 0:791a779d6220 385 /*
shoaib_ahmed 0:791a779d6220 386 * The module selection routine for BMP format output.
shoaib_ahmed 0:791a779d6220 387 */
shoaib_ahmed 0:791a779d6220 388
shoaib_ahmed 0:791a779d6220 389 GLOBAL(djpeg_dest_ptr)
shoaib_ahmed 0:791a779d6220 390 jinit_write_bmp (j_decompress_ptr cinfo, boolean is_os2)
shoaib_ahmed 0:791a779d6220 391 {
shoaib_ahmed 0:791a779d6220 392 bmp_dest_ptr dest;
shoaib_ahmed 0:791a779d6220 393 JDIMENSION row_width;
shoaib_ahmed 0:791a779d6220 394
shoaib_ahmed 0:791a779d6220 395 /* Create module interface object, fill in method pointers */
shoaib_ahmed 0:791a779d6220 396 dest = (bmp_dest_ptr)
shoaib_ahmed 0:791a779d6220 397 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 398 SIZEOF(bmp_dest_struct));
shoaib_ahmed 0:791a779d6220 399 dest->pub.start_output = start_output_bmp;
shoaib_ahmed 0:791a779d6220 400 dest->pub.finish_output = finish_output_bmp;
shoaib_ahmed 0:791a779d6220 401 dest->is_os2 = is_os2;
shoaib_ahmed 0:791a779d6220 402
shoaib_ahmed 0:791a779d6220 403 if (cinfo->out_color_space == JCS_GRAYSCALE) {
shoaib_ahmed 0:791a779d6220 404 dest->pub.put_pixel_rows = put_gray_rows;
shoaib_ahmed 0:791a779d6220 405 } else if (cinfo->out_color_space == JCS_RGB) {
shoaib_ahmed 0:791a779d6220 406 if (cinfo->quantize_colors)
shoaib_ahmed 0:791a779d6220 407 dest->pub.put_pixel_rows = put_gray_rows;
shoaib_ahmed 0:791a779d6220 408 else
shoaib_ahmed 0:791a779d6220 409 dest->pub.put_pixel_rows = put_pixel_rows;
shoaib_ahmed 0:791a779d6220 410 } else {
shoaib_ahmed 0:791a779d6220 411 ERREXIT(cinfo, JERR_BMP_COLORSPACE);
shoaib_ahmed 0:791a779d6220 412 }
shoaib_ahmed 0:791a779d6220 413
shoaib_ahmed 0:791a779d6220 414 /* Calculate output image dimensions so we can allocate space */
shoaib_ahmed 0:791a779d6220 415 jpeg_calc_output_dimensions(cinfo);
shoaib_ahmed 0:791a779d6220 416
shoaib_ahmed 0:791a779d6220 417 /* Determine width of rows in the BMP file (padded to 4-byte boundary). */
shoaib_ahmed 0:791a779d6220 418 row_width = cinfo->output_width * cinfo->output_components;
shoaib_ahmed 0:791a779d6220 419 dest->data_width = row_width;
shoaib_ahmed 0:791a779d6220 420 while ((row_width & 3) != 0) row_width++;
shoaib_ahmed 0:791a779d6220 421 dest->row_width = row_width;
shoaib_ahmed 0:791a779d6220 422 dest->pad_bytes = (int) (row_width - dest->data_width);
shoaib_ahmed 0:791a779d6220 423
shoaib_ahmed 0:791a779d6220 424 /* Allocate space for inversion array, prepare for write pass */
shoaib_ahmed 0:791a779d6220 425 dest->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, cinfo->output_height, (JDIMENSION) 1);
shoaib_ahmed 0:791a779d6220 428 dest->cur_output_row = 0;
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 /* Create decompressor output buffer. */
shoaib_ahmed 0:791a779d6220 435 dest->pub.buffer = (*cinfo->mem->alloc_sarray)
shoaib_ahmed 0:791a779d6220 436 ((j_common_ptr) cinfo, JPOOL_IMAGE, row_width, (JDIMENSION) 1);
shoaib_ahmed 0:791a779d6220 437 dest->pub.buffer_height = 1;
shoaib_ahmed 0:791a779d6220 438
shoaib_ahmed 0:791a779d6220 439 return (djpeg_dest_ptr) dest;
shoaib_ahmed 0:791a779d6220 440 }
shoaib_ahmed 0:791a779d6220 441
shoaib_ahmed 0:791a779d6220 442 #endif /* BMP_SUPPORTED */