blob: 50e469c987e27bdafc885757f848414987410914 [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * wrbmp.c
3 *
DRC7f4a81b2014-05-18 18:17:01 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane489583f1996-02-07 00:00:00 +00005 * Copyright (C) 1994-1996, Thomas G. Lane.
DRC7f4a81b2014-05-18 18:17:01 +00006 * libjpeg-turbo Modifications:
DRC78df2e62014-05-12 09:23:57 +00007 * Copyright (C) 2013, Linaro Limited.
DRC1e32fe32015-10-14 17:32:39 -05008 * Copyright (C) 2014-2015, D. R. Commander.
DRC7e3acc02015-10-10 10:25:46 -05009 * For conditions of distribution and use, see the accompanying README.ijg
10 * file.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000011 *
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
DRCe5eaf372014-05-09 18:00:32 +000024#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
DRC1d4c51a2014-08-30 22:01:05 +000025#include "jconfigint.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000026
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
49typedef struct {
DRCe5eaf372014-05-09 18:00:32 +000050 struct djpeg_dest_struct pub; /* public fields */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000051
DRCe5eaf372014-05-09 18:00:32 +000052 boolean is_os2; /* saves the OS2 format request flag */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000053
DRCe5eaf372014-05-09 18:00:32 +000054 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. Lane36a4ccc1994-09-24 00:00:00 +000059} bmp_dest_struct;
60
DRCbd498032016-02-19 08:53:33 -060061typedef bmp_dest_struct *bmp_dest_ptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000062
63
64/* Forward declarations */
Thomas G. Lane489583f1996-02-07 00:00:00 +000065LOCAL(void) write_colormap
DRCbc56b752014-05-16 10:43:44 +000066 (j_decompress_ptr cinfo, bmp_dest_ptr dest, int map_colors,
67 int map_entry_size);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000068
69
DRC1d4c51a2014-08-30 22:01:05 +000070static INLINE boolean is_big_endian(void)
DRC72a3cc02014-08-30 20:37:50 +000071{
72 int test_value = 1;
73 if(*(char *)&test_value != 1)
74 return TRUE;
75 return FALSE;
76}
77
78
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000079/*
80 * Write some pixel data.
81 * In this module rows_supplied will always be 1.
82 */
83
Thomas G. Lane489583f1996-02-07 00:00:00 +000084METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000085put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
DRCe5eaf372014-05-09 18:00:32 +000086 JDIMENSION rows_supplied)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000087/* 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. Lanebc79e061995-08-02 00:00:00 +000097 ((j_common_ptr) cinfo, dest->whole_image,
98 dest->cur_output_row, (JDIMENSION) 1, TRUE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000099 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];
DRC78df2e62014-05-12 09:23:57 +0000106
107 if(cinfo->out_color_space == JCS_RGB565) {
DRC72a3cc02014-08-30 20:37:50 +0000108 boolean big_endian = is_big_endian();
DRC78df2e62014-05-12 09:23:57 +0000109 unsigned short *inptr2 = (unsigned short *)inptr;
110 for (col = cinfo->output_width; col > 0; col--) {
DRC72a3cc02014-08-30 20:37:50 +0000111 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 }
DRC78df2e62014-05-12 09:23:57 +0000120 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. Lane36a4ccc1994-09-24 00:00:00 +0000130 }
131
132 /* Zero out the pad bytes. */
133 pad = dest->pad_bytes;
134 while (--pad >= 0)
135 *outptr++ = 0;
136}
137
Thomas G. Lane489583f1996-02-07 00:00:00 +0000138METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000139put_gray_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
DRCe5eaf372014-05-09 18:00:32 +0000140 JDIMENSION rows_supplied)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000141/* 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. Lanebc79e061995-08-02 00:00:00 +0000151 ((j_common_ptr) cinfo, dest->whole_image,
152 dest->cur_output_row, (JDIMENSION) 1, TRUE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000153 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--) {
DRCe5eaf372014-05-09 18:00:32 +0000159 *outptr++ = *inptr++; /* can omit GETJSAMPLE() safely */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000160 }
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. Lane489583f1996-02-07 00:00:00 +0000174METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000175start_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. Lane489583f1996-02-07 00:00:00 +0000189LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000190write_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) \
DRCe5eaf372014-05-09 18:00:32 +0000196 (array[offset] = (char) ((value) & 0xFF), \
197 array[offset+1] = (char) (((value) >> 8) & 0xFF))
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000198#define PUT_4B(array,offset,value) \
DRCe5eaf372014-05-09 18:00:32 +0000199 (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))
DRC1e32fe32015-10-14 17:32:39 -0500203 long headersize, bfSize;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000204 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 }
DRC78df2e62014-05-12 09:23:57 +0000217 } else if (cinfo->out_color_space == JCS_RGB565) {
218 bits_per_pixel = 24;
219 cmap_entries = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000220 } 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 */
DRC1e32fe32015-10-14 17:32:39 -0500227 bfSize = headersize + (long) dest->row_width * (long) cinfo->output_height;
DRCe5eaf372014-05-09 18:00:32 +0000228
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000229 /* Set unused fields of header to 0 */
DRC5de454b2014-05-18 19:04:03 +0000230 MEMZERO(bmpfileheader, sizeof(bmpfileheader));
231 MEMZERO(bmpinfoheader, sizeof(bmpinfoheader));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000232
233 /* Fill the file header */
DRCe5eaf372014-05-09 18:00:32 +0000234 bmpfileheader[0] = 0x42; /* first 2 bytes are ASCII 'B', 'M' */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000235 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) */
DRCe5eaf372014-05-09 18:00:32 +0000241 PUT_2B(bmpinfoheader, 0, 40); /* biSize */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000242 PUT_4B(bmpinfoheader, 4, cinfo->output_width); /* biWidth */
243 PUT_4B(bmpinfoheader, 8, cinfo->output_height); /* biHeight */
DRCe5eaf372014-05-09 18:00:32 +0000244 PUT_2B(bmpinfoheader, 12, 1); /* biPlanes - must be 1 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000245 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 */
DRC1e32fe32015-10-14 17:32:39 -0500249 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. Lane36a4ccc1994-09-24 00:00:00 +0000251 }
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. Lane489583f1996-02-07 00:00:00 +0000265LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000266write_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];
DRC1e32fe32015-10-14 17:32:39 -0500271 long headersize, bfSize;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000272 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 }
DRC78df2e62014-05-12 09:23:57 +0000285 } else if (cinfo->out_color_space == JCS_RGB565) {
286 bits_per_pixel = 24;
287 cmap_entries = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000288 } 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 */
DRC1e32fe32015-10-14 17:32:39 -0500295 bfSize = headersize + (long) dest->row_width * (long) cinfo->output_height;
DRCe5eaf372014-05-09 18:00:32 +0000296
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000297 /* Set unused fields of header to 0 */
DRC5de454b2014-05-18 19:04:03 +0000298 MEMZERO(bmpfileheader, sizeof(bmpfileheader));
299 MEMZERO(bmpcoreheader, sizeof(bmpcoreheader));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000300
301 /* Fill the file header */
DRCe5eaf372014-05-09 18:00:32 +0000302 bmpfileheader[0] = 0x42; /* first 2 bytes are ASCII 'B', 'M' */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000303 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) */
DRCe5eaf372014-05-09 18:00:32 +0000309 PUT_2B(bmpcoreheader, 0, 12); /* bcSize */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000310 PUT_2B(bmpcoreheader, 4, cinfo->output_width); /* bcWidth */
311 PUT_2B(bmpcoreheader, 6, cinfo->output_height); /* bcHeight */
DRCe5eaf372014-05-09 18:00:32 +0000312 PUT_2B(bmpcoreheader, 8, 1); /* bcPlanes - must be 1 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000313 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. Lane489583f1996-02-07 00:00:00 +0000330LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000331write_colormap (j_decompress_ptr cinfo, bmp_dest_ptr dest,
DRCe5eaf372014-05-09 18:00:32 +0000332 int map_colors, int map_entry_size)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000333{
334 JSAMPARRAY colormap = cinfo->colormap;
335 int num_colors = cinfo->actual_number_of_colors;
DRCbd498032016-02-19 08:53:33 -0600336 FILE *outfile = dest->pub.output_file;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000337 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++) {
DRCe5eaf372014-05-09 18:00:32 +0000343 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. Lane36a4ccc1994-09-24 00:00:00 +0000348 }
349 } else {
350 /* Grayscale colormap (only happens with grayscale quantization) */
351 for (i = 0; i < num_colors; i++) {
DRCe5eaf372014-05-09 18:00:32 +0000352 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. Lane36a4ccc1994-09-24 00:00:00 +0000357 }
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)
DRCe5eaf372014-05-09 18:00:32 +0000366 putc(0, outfile);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000367 }
368 }
DRCe5eaf372014-05-09 18:00:32 +0000369 /* Pad colormap with zeros to ensure specified number of colormap entries */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000370 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. Lane489583f1996-02-07 00:00:00 +0000382METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000383finish_output_bmp (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
384{
385 bmp_dest_ptr dest = (bmp_dest_ptr) dinfo;
DRCbd498032016-02-19 08:53:33 -0600386 register FILE *outfile = dest->pub.output_file;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000387 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. Lanebc79e061995-08-02 00:00:00 +0000407 ((j_common_ptr) cinfo, dest->whole_image, row-1, (JDIMENSION) 1, FALSE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000408 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. Lane489583f1996-02-07 00:00:00 +0000428GLOBAL(djpeg_dest_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000429jinit_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,
DRC5de454b2014-05-18 19:04:03 +0000437 sizeof(bmp_dest_struct));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000438 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;
DRC78df2e62014-05-12 09:23:57 +0000449 } else if(cinfo->out_color_space == JCS_RGB565 ) {
450 dest->pub.put_pixel_rows = put_pixel_rows;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000451 } 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). */
DRC78df2e62014-05-12 09:23:57 +0000459 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. Lane36a4ccc1994-09-24 00:00:00 +0000474
475 /* Allocate space for inversion array, prepare for write pass */
476 dest->whole_image = (*cinfo->mem->request_virt_sarray)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000477 ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
DRC78df2e62014-05-12 09:23:57 +0000478 dest->row_width, cinfo->output_height, (JDIMENSION) 1);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000479 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 */