rdbmp.c 15 KB

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