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