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