wrbmp.c 13 KB

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