rdbmp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * rdbmp.c
  3. *
  4. * Copyright (C) 1994-1996, Thomas G. Lane.
  5. * Modified 2009-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 read input images in Microsoft "BMP"
  10. * format (MS Windows 3.x, OS/2 1.x, and OS/2 2.x flavors).
  11. * Currently, only 8-, 24-, and 32-bit images are supported, not 1-bit or
  12. * 4-bit (feeding such low-depth images into JPEG would be silly anyway).
  13. * Also, we don't support RLE-compressed files.
  14. *
  15. * These routines may need modification for non-Unix environments or
  16. * specialized applications. As they stand, they assume input from
  17. * an ordinary stdio stream. They further assume that reading begins
  18. * at the start of the file; start_input may need work if the
  19. * user interface has already read some data (e.g., to determine that
  20. * the file is indeed BMP format).
  21. *
  22. * This code contributed by James Arthur Boucher.
  23. */
  24. #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
  25. #ifdef BMP_SUPPORTED
  26. /* Macros to deal with unsigned chars as efficiently as compiler allows */
  27. #ifdef HAVE_UNSIGNED_CHAR
  28. typedef unsigned char U_CHAR;
  29. #define UCH(x) ((int) (x))
  30. #else /* !HAVE_UNSIGNED_CHAR */
  31. typedef char U_CHAR;
  32. #ifdef CHAR_IS_UNSIGNED
  33. #define UCH(x) ((int) (x))
  34. #else
  35. #define UCH(x) ((int) (x) & 0xFF)
  36. #endif
  37. #endif /* HAVE_UNSIGNED_CHAR */
  38. #define ReadOK(file,buffer,len) (JFREAD(file,buffer,len) == ((size_t) (len)))
  39. /* Private version of data source object */
  40. typedef struct _bmp_source_struct * bmp_source_ptr;
  41. typedef struct _bmp_source_struct {
  42. struct cjpeg_source_struct pub; /* public fields */
  43. j_compress_ptr cinfo; /* back link saves passing separate parm */
  44. JSAMPARRAY colormap; /* BMP colormap (converted to my format) */
  45. jvirt_sarray_ptr whole_image; /* Needed to reverse row order */
  46. JDIMENSION source_row; /* Current source row number */
  47. JDIMENSION row_width; /* Physical width of scanlines in file */
  48. int bits_per_pixel; /* remembers 8-, 24-, or 32-bit format */
  49. int cmap_length; /* colormap length */
  50. } bmp_source_struct;
  51. LOCAL(int)
  52. read_byte (bmp_source_ptr sinfo)
  53. /* Read next byte from BMP file */
  54. {
  55. register FILE *infile = sinfo->pub.input_file;
  56. register int c;
  57. if ((c = getc(infile)) == EOF)
  58. ERREXIT(sinfo->cinfo, JERR_INPUT_EOF);
  59. return c;
  60. }
  61. LOCAL(void)
  62. read_colormap (bmp_source_ptr sinfo, int cmaplen, int mapentrysize)
  63. /* Read the colormap from a BMP file */
  64. {
  65. int i;
  66. switch (mapentrysize) {
  67. case 3:
  68. /* BGR format (occurs in OS/2 files) */
  69. for (i = 0; i < cmaplen; i++) {
  70. sinfo->colormap[2][i] = (JSAMPLE) read_byte(sinfo);
  71. sinfo->colormap[1][i] = (JSAMPLE) read_byte(sinfo);
  72. sinfo->colormap[0][i] = (JSAMPLE) read_byte(sinfo);
  73. }
  74. break;
  75. case 4:
  76. /* BGR0 format (occurs in MS Windows files) */
  77. for (i = 0; i < cmaplen; i++) {
  78. sinfo->colormap[2][i] = (JSAMPLE) read_byte(sinfo);
  79. sinfo->colormap[1][i] = (JSAMPLE) read_byte(sinfo);
  80. sinfo->colormap[0][i] = (JSAMPLE) read_byte(sinfo);
  81. (void) read_byte(sinfo);
  82. }
  83. break;
  84. default:
  85. ERREXIT(sinfo->cinfo, JERR_BMP_BADCMAP);
  86. }
  87. }
  88. /*
  89. * Read one row of pixels.
  90. * The image has been read into the whole_image array, but is otherwise
  91. * unprocessed. We must read it out in top-to-bottom row order, and if
  92. * it is an 8-bit image, we must expand colormapped pixels to 24bit format.
  93. */
  94. METHODDEF(JDIMENSION)
  95. get_8bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  96. /* This version is for reading 8-bit colormap indexes */
  97. {
  98. bmp_source_ptr source = (bmp_source_ptr) sinfo;
  99. register JSAMPROW inptr, outptr;
  100. register JSAMPARRAY colormap;
  101. register JDIMENSION col;
  102. register int t;
  103. int cmaplen;
  104. /* Fetch next row from virtual array */
  105. source->source_row--;
  106. inptr = * (*cinfo->mem->access_virt_sarray) ((j_common_ptr) cinfo,
  107. source->whole_image, source->source_row, (JDIMENSION) 1, FALSE);
  108. /* Expand the colormap indexes to real data */
  109. outptr = source->pub.buffer[0];
  110. colormap = source->colormap;
  111. cmaplen = source->cmap_length;
  112. for (col = cinfo->image_width; col > 0; col--) {
  113. t = GETJSAMPLE(*inptr++);
  114. if (t >= cmaplen)
  115. ERREXIT(cinfo, JERR_BMP_OUTOFRANGE);
  116. *outptr++ = colormap[0][t]; /* can omit GETJSAMPLE() safely */
  117. *outptr++ = colormap[1][t];
  118. *outptr++ = colormap[2][t];
  119. }
  120. return 1;
  121. }
  122. METHODDEF(JDIMENSION)
  123. get_24bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  124. /* This version is for reading 24-bit pixels */
  125. {
  126. bmp_source_ptr source = (bmp_source_ptr) sinfo;
  127. register JSAMPROW inptr, outptr;
  128. register JDIMENSION col;
  129. /* Fetch next row from virtual array */
  130. source->source_row--;
  131. inptr = * (*cinfo->mem->access_virt_sarray) ((j_common_ptr) cinfo,
  132. source->whole_image, source->source_row, (JDIMENSION) 1, FALSE);
  133. /* Transfer data. Note source values are in BGR order
  134. * (even though Microsoft's own documents say the opposite).
  135. */
  136. outptr = source->pub.buffer[0];
  137. for (col = cinfo->image_width; col > 0; col--) {
  138. outptr[2] = *inptr++; /* can omit GETJSAMPLE() safely */
  139. outptr[1] = *inptr++;
  140. outptr[0] = *inptr++;
  141. outptr += 3;
  142. }
  143. return 1;
  144. }
  145. METHODDEF(JDIMENSION)
  146. get_32bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  147. /* This version is for reading 32-bit pixels */
  148. {
  149. bmp_source_ptr source = (bmp_source_ptr) sinfo;
  150. register JSAMPROW inptr, outptr;
  151. register JDIMENSION col;
  152. /* Fetch next row from virtual array */
  153. source->source_row--;
  154. inptr = * (*cinfo->mem->access_virt_sarray) ((j_common_ptr) cinfo,
  155. source->whole_image, source->source_row, (JDIMENSION) 1, FALSE);
  156. /* Transfer data. Note source values are in BGR order
  157. * (even though Microsoft's own documents say the opposite).
  158. */
  159. outptr = source->pub.buffer[0];
  160. for (col = cinfo->image_width; col > 0; col--) {
  161. outptr[2] = *inptr++; /* can omit GETJSAMPLE() safely */
  162. outptr[1] = *inptr++;
  163. outptr[0] = *inptr++;
  164. inptr++; /* skip the 4th byte (Alpha channel) */
  165. outptr += 3;
  166. }
  167. return 1;
  168. }
  169. /*
  170. * This method loads the image into whole_image during the first call on
  171. * get_pixel_rows. The get_pixel_rows pointer is then adjusted to call
  172. * get_8bit_row, get_24bit_row, or get_32bit_row on subsequent calls.
  173. */
  174. METHODDEF(JDIMENSION)
  175. preload_image (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  176. {
  177. bmp_source_ptr source = (bmp_source_ptr) sinfo;
  178. register FILE *infile = source->pub.input_file;
  179. register int c;
  180. register JSAMPROW out_ptr;
  181. JDIMENSION row, col;
  182. cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
  183. /* Read the data into a virtual array in input-file row order. */
  184. for (row = 0; row < cinfo->image_height; row++) {
  185. if (progress != NULL) {
  186. progress->pub.pass_counter = (long) row;
  187. progress->pub.pass_limit = (long) cinfo->image_height;
  188. (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
  189. }
  190. out_ptr = * (*cinfo->mem->access_virt_sarray) ((j_common_ptr) cinfo,
  191. source->whole_image, row, (JDIMENSION) 1, TRUE);
  192. for (col = source->row_width; col > 0; col--) {
  193. /* inline copy of read_byte() for speed */
  194. if ((c = getc(infile)) == EOF)
  195. ERREXIT(cinfo, JERR_INPUT_EOF);
  196. *out_ptr++ = (JSAMPLE) c;
  197. }
  198. }
  199. if (progress != NULL)
  200. progress->completed_extra_passes++;
  201. /* Set up to read from the virtual array in top-to-bottom order */
  202. switch (source->bits_per_pixel) {
  203. case 8:
  204. source->pub.get_pixel_rows = get_8bit_row;
  205. break;
  206. case 24:
  207. source->pub.get_pixel_rows = get_24bit_row;
  208. break;
  209. case 32:
  210. source->pub.get_pixel_rows = get_32bit_row;
  211. break;
  212. default:
  213. ERREXIT(cinfo, JERR_BMP_BADDEPTH);
  214. }
  215. source->source_row = cinfo->image_height;
  216. /* And read the first row */
  217. return (*source->pub.get_pixel_rows) (cinfo, sinfo);
  218. }
  219. /*
  220. * Read the file header; return image size and component count.
  221. */
  222. METHODDEF(void)
  223. start_input_bmp (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  224. {
  225. bmp_source_ptr source = (bmp_source_ptr) sinfo;
  226. U_CHAR bmpfileheader[14];
  227. U_CHAR bmpinfoheader[64];
  228. #define GET_2B(array, offset) ((unsigned int) UCH(array[offset]) + \
  229. (((unsigned int) UCH(array[offset+1])) << 8))
  230. #define GET_4B(array, offset) ((INT32) UCH(array[offset]) + \
  231. (((INT32) UCH(array[offset+1])) << 8) + \
  232. (((INT32) UCH(array[offset+2])) << 16) + \
  233. (((INT32) UCH(array[offset+3])) << 24))
  234. INT32 bfOffBits;
  235. INT32 headerSize;
  236. INT32 biWidth;
  237. INT32 biHeight;
  238. unsigned int biPlanes;
  239. INT32 biCompression;
  240. INT32 biXPelsPerMeter,biYPelsPerMeter;
  241. INT32 biClrUsed = 0;
  242. int mapentrysize = 0; /* 0 indicates no colormap */
  243. INT32 bPad;
  244. JDIMENSION row_width;
  245. /* Read and verify the bitmap file header */
  246. if (! ReadOK(source->pub.input_file, bmpfileheader, 14))
  247. ERREXIT(cinfo, JERR_INPUT_EOF);
  248. if (GET_2B(bmpfileheader, 0) != 0x4D42) /* 'BM' */
  249. ERREXIT(cinfo, JERR_BMP_NOT);
  250. bfOffBits = GET_4B(bmpfileheader, 10);
  251. /* We ignore the remaining fileheader fields */
  252. /* The infoheader might be 12 bytes (OS/2 1.x), 40 bytes (Windows),
  253. * or 64 bytes (OS/2 2.x). Check the first 4 bytes to find out which.
  254. */
  255. if (! ReadOK(source->pub.input_file, bmpinfoheader, 4))
  256. ERREXIT(cinfo, JERR_INPUT_EOF);
  257. headerSize = GET_4B(bmpinfoheader, 0);
  258. if (headerSize < 12 || headerSize > 64)
  259. ERREXIT(cinfo, JERR_BMP_BADHEADER);
  260. if (! ReadOK(source->pub.input_file, bmpinfoheader + 4, headerSize - 4))
  261. ERREXIT(cinfo, JERR_INPUT_EOF);
  262. switch ((int) headerSize) {
  263. case 12:
  264. /* Decode OS/2 1.x header (Microsoft calls this a BITMAPCOREHEADER) */
  265. biWidth = (INT32) GET_2B(bmpinfoheader, 4);
  266. biHeight = (INT32) GET_2B(bmpinfoheader, 6);
  267. biPlanes = GET_2B(bmpinfoheader, 8);
  268. source->bits_per_pixel = (int) GET_2B(bmpinfoheader, 10);
  269. switch (source->bits_per_pixel) {
  270. case 8: /* colormapped image */
  271. mapentrysize = 3; /* OS/2 uses RGBTRIPLE colormap */
  272. TRACEMS2(cinfo, 1, JTRC_BMP_OS2_MAPPED, (int) biWidth, (int) biHeight);
  273. break;
  274. case 24: /* RGB image */
  275. case 32: /* RGB image + Alpha channel */
  276. TRACEMS3(cinfo, 1, JTRC_BMP_OS2, (int) biWidth, (int) biHeight,
  277. source->bits_per_pixel);
  278. break;
  279. default:
  280. ERREXIT(cinfo, JERR_BMP_BADDEPTH);
  281. }
  282. break;
  283. case 40:
  284. case 64:
  285. /* Decode Windows 3.x header (Microsoft calls this a BITMAPINFOHEADER) */
  286. /* or OS/2 2.x header, which has additional fields that we ignore */
  287. biWidth = GET_4B(bmpinfoheader, 4);
  288. biHeight = GET_4B(bmpinfoheader, 8);
  289. biPlanes = GET_2B(bmpinfoheader, 12);
  290. source->bits_per_pixel = (int) GET_2B(bmpinfoheader, 14);
  291. biCompression = GET_4B(bmpinfoheader, 16);
  292. biXPelsPerMeter = GET_4B(bmpinfoheader, 24);
  293. biYPelsPerMeter = GET_4B(bmpinfoheader, 28);
  294. biClrUsed = GET_4B(bmpinfoheader, 32);
  295. /* biSizeImage, biClrImportant fields are ignored */
  296. switch (source->bits_per_pixel) {
  297. case 8: /* colormapped image */
  298. mapentrysize = 4; /* Windows uses RGBQUAD colormap */
  299. TRACEMS2(cinfo, 1, JTRC_BMP_MAPPED, (int) biWidth, (int) biHeight);
  300. break;
  301. case 24: /* RGB image */
  302. case 32: /* RGB image + Alpha channel */
  303. TRACEMS3(cinfo, 1, JTRC_BMP, (int) biWidth, (int) biHeight,
  304. source->bits_per_pixel);
  305. break;
  306. default:
  307. ERREXIT(cinfo, JERR_BMP_BADDEPTH);
  308. }
  309. if (biCompression != 0)
  310. ERREXIT(cinfo, JERR_BMP_COMPRESSED);
  311. if (biXPelsPerMeter > 0 && biYPelsPerMeter > 0) {
  312. /* Set JFIF density parameters from the BMP data */
  313. cinfo->X_density = (UINT16) (biXPelsPerMeter/100); /* 100 cm per meter */
  314. cinfo->Y_density = (UINT16) (biYPelsPerMeter/100);
  315. cinfo->density_unit = 2; /* dots/cm */
  316. }
  317. break;
  318. default:
  319. ERREXIT(cinfo, JERR_BMP_BADHEADER);
  320. return; /* avoid compiler warnings for uninitialized variables */
  321. }
  322. if (biPlanes != 1)
  323. ERREXIT(cinfo, JERR_BMP_BADPLANES);
  324. /* Sanity check for buffer allocation below */
  325. if (biWidth <= 0 || biHeight <= 0 || (biWidth >> 24) || (biHeight >> 24))
  326. ERREXIT(cinfo, JERR_BMP_OUTOFRANGE);
  327. /* Compute distance to bitmap data --- will adjust for colormap below */
  328. bPad = bfOffBits - (headerSize + 14);
  329. /* Read the colormap, if any */
  330. if (mapentrysize > 0) {
  331. if (biClrUsed <= 0)
  332. biClrUsed = 256; /* assume it's 256 */
  333. else if (biClrUsed > 256)
  334. ERREXIT(cinfo, JERR_BMP_BADCMAP);
  335. /* Allocate space to store the colormap */
  336. source->colormap = (*cinfo->mem->alloc_sarray) ((j_common_ptr) cinfo,
  337. JPOOL_IMAGE, (JDIMENSION) biClrUsed, (JDIMENSION) 3);
  338. source->cmap_length = (int) biClrUsed;
  339. /* and read it from the file */
  340. read_colormap(source, (int) biClrUsed, mapentrysize);
  341. /* account for size of colormap */
  342. bPad -= biClrUsed * mapentrysize;
  343. }
  344. /* Skip any remaining pad bytes */
  345. if (bPad < 0) /* incorrect bfOffBits value? */
  346. ERREXIT(cinfo, JERR_BMP_BADHEADER);
  347. while (--bPad >= 0) {
  348. (void) read_byte(source);
  349. }
  350. /* Compute row width in file, including padding to 4-byte boundary */
  351. if (source->bits_per_pixel == 24)
  352. row_width = (JDIMENSION) (biWidth * 3);
  353. else if (source->bits_per_pixel == 32)
  354. row_width = (JDIMENSION) (biWidth * 4);
  355. else
  356. row_width = (JDIMENSION) biWidth;
  357. while ((row_width & 3) != 0) row_width++;
  358. source->row_width = row_width;
  359. /* Allocate space for inversion array, prepare for preload pass */
  360. source->whole_image = (*cinfo->mem->request_virt_sarray)
  361. ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
  362. row_width, (JDIMENSION) biHeight, (JDIMENSION) 1);
  363. source->pub.get_pixel_rows = preload_image;
  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. /* Allocate one-row buffer for returned data */
  369. source->pub.buffer = (*cinfo->mem->alloc_sarray) ((j_common_ptr) cinfo,
  370. JPOOL_IMAGE, (JDIMENSION) (biWidth * 3), (JDIMENSION) 1);
  371. source->pub.buffer_height = 1;
  372. cinfo->in_color_space = JCS_RGB;
  373. cinfo->input_components = 3;
  374. cinfo->data_precision = 8;
  375. cinfo->image_width = (JDIMENSION) biWidth;
  376. cinfo->image_height = (JDIMENSION) biHeight;
  377. }
  378. /*
  379. * Finish up at the end of the file.
  380. */
  381. METHODDEF(void)
  382. finish_input_bmp (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  383. {
  384. /* no work */
  385. }
  386. /*
  387. * The module selection routine for BMP format input.
  388. */
  389. GLOBAL(cjpeg_source_ptr)
  390. jinit_read_bmp (j_compress_ptr cinfo)
  391. {
  392. bmp_source_ptr source;
  393. /* Create module interface object */
  394. source = (bmp_source_ptr) (*cinfo->mem->alloc_small)
  395. ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(bmp_source_struct));
  396. source->cinfo = cinfo; /* make back link for subroutines */
  397. /* Fill in method ptrs, except get_pixel_rows which start_input sets */
  398. source->pub.start_input = start_input_bmp;
  399. source->pub.finish_input = finish_input_bmp;
  400. return &source->pub;
  401. }
  402. #endif /* BMP_SUPPORTED */