Save the BMP image of 1bpp to the TIFF Format of 1bpp or 2bpp.

Source: Internet
Author: User
Tags bmp image

// Convert the 1bpp bitmap to the pixel data of the 1bit/2bit tiff/** parameter: byte * SRC binary image, excluding the header information, 1bpp, int src_width, in pixles, int src_height: the height of the source image. In pixlesint BPP converts the BPP */static byte * bw2tif (byte * SRC, int src_width, int src_height, int bpp, DWORD & totalsize) {int dst_width = (bpp = 1 )? Src_width: src_width/2; int dst_height = src_height; // get SRC and DST scan widthint src_pitch = (src_width + 7)/8 + 3 &~ 3; int dst_pitch = (dst_width * BPP + 31)/32*4; // get SRC and DST sizeunsigned int tif_size, BMP _size; If (0x80000000 & dst_height) {BMP _size = src_pitch * (0-src_height); tif_size = dst_pitch * (0-dst_height);} else {BMP _size = src_pitch * src_height; tif_size = dst_pitch * dstheight _height ;} // image file headerconst unsigned char tif_ifh [10] = {'I', 'I', // intel sequence 0x2a, 0x00, // tiff version 0 x, 0 x, 0 x, 0x00, // IFD (image volume directory IMA GE file diretory) offset, 8 bytes 0x0f, 0x00, // these two bytes belong to IFD, specify how many directory entries}; // directory entry, each is 12 bytes. Tiff 6.0 requires that tags should be sorted in ascending order by unsigned long tif_ifd [0x0f * 3 + 4] = {0x000400fe, 0x01,0x000, // newsubfiletype, 0x00030100, 0x01,0x018, // imagewidth, the number of columns in the image, I. E ., the number of pixels per scanline0x00030101, 0x01,0x018, // imagelength, the number of rows (sometimes described as SCA Nlines) in the image.0x00030102, 0x01, bpp, // bitpersample 2580x00030103, 0x01,0x001, // compression, 1 no compression 0x00030106, 0x01,0x000, // photometricinterpretation. For bilevel images, the value 0 = whiteiszero, 1 = blackiszero0x00040111, 0x01, 0x0ce, // stripoffsets, for each strip, the Byte offset of that strip, // n = stripsperimage for planarconfiguration equal to 1; // n = samplesperpixel * stripsperimage for planarconfigura Tion equal to 2 // literally, stripoffsets is an array smaller than 64kb. Here it is 0xce, that is, the first 206 bytes in the header are 0x00030112, 0x01,0x001, // orientation0x00030115, 0x01,0x001, // percentile, 0x01, 0 xFFFF, // rowsperstrip, the number of rows in each strip (partition t possibly the last strip .) // rowsperstrip and imagelength together tell us the number of strips in the entire image. default is 2 ^ 32-1. We recommend that each strip is 8 k Data // image. the equat Ion is: stripsperimage = floor (imagelength + rowsperstrip-1)/rowsperstrip ). 0x00040117, 0x01, 0x07f, // random, 0x01, 0x0be, // xresolution0x0005011b, 0x01, 0x0c6, // yresolution0x0003011c, 0x01,0x001, // planarconfiguration, 1 is a single plane format, 2 multiple plane formats 0x00030128, 0x01,0x002, // resolutionunit, 0x002 = inch0x60031280, 0x01000000, // X resolution ofset = 1640x60031280, 0x01000000, // y resolution ofset = 16C}; // tif_if D [5] = dst_width; tif_ifd [8] = dst_height; tif_ifd [29] = (tif_size + 0x0f) & 0xfffff0; // A striptif_ifd [32] = tif_size; // allocate DST image hpglunsigned int total_size = sizeof (tif_ifh) + sizeof (tif_ifd) + tif_size; trace1 ("tiff size = 0x % x \ n", sizeof (tif_ifh) + sizeof (tif_ifd); byte * DST = new byte [total_size]; If (null = DST) return false; byte * curr = DST; totalsize = total_size; // memcpy (Cu RR, tif_ifh, sizeof (tif_ifh); curr + = sizeof (tif_ifh); memcpy (curr, tif_ifd, sizeof (tif_ifd); curr + = sizeof (tif_ifd ); // when the width of the SRC image is an integer byte, you can directly copy the data. If there are several more bits, it is hard to do it // memcpy (curr, SRC, tif_size ); // The program in gray2bmp _to_tif.c stores each pixel in one byte, but it is not // so it cannot be written according to the program int width_align = src_width & 0xfffffff8; int nbytes = width_align/8; If (src_width-width_align) // The bitwise nbytes + = 1; lpbyte src2 = SRC; lpbyte DS T2 = curr; int X, Y; For (y = 0; y <src_height; y ++) {src2 = SRC + y * src_pitch; dst2 = curr + y * nbytes; // do not add the blank data memcpy (dst2, src2, nbytes); src2 + = nbytes; dst2 + = nbytes;} return DST ;} // The test code void cpagept: onbnclickedbuttoncv () {cfiledialog FD (true, _ T ("BMP"), null, null, _ T ("all files (*. *) | *. * | wibdows Bitmap (*. BMP) | *. BMP | "); If (FD. domodal ()! = Idok) return; cstring filename = FD. getpathname (); hbitmap himage = (hbitmap) LoadImage (null, filename, image_bitmap, 0, 0, lr_loadfromfile | lr_createdibsection | lr_defaultsize); // cbitmap * m_bitmap = cbitmap :: fromhandle (himage); bitmap bm; // GetObject (himage, sizeof (Bitmap), & BM); // sumit: memory Allocation is still 1800x1800 in your code .. byte * BMP buffer = (byte *) BM. bmbits; // globalalloc (gptr, BM. bmwidthbytes * BM. bmheight); // allocate memorydword tifsize; byte * TIF = bw2tif (BMP buffer, BM. bmwidth, BM. bmheight, 2, tifsize); cfile CF ("C: \ bw2tif_out.tif", cfile: modecreate | cfile: modewrite); cf. write (TIF, tifsize); cf. close (); Delete TIF ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.