rdbmp.c 15 KB

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