wrbmp.c 14 KB

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