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