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