// 将1bpp的位图转换为 1bit/2bit tiff
/**
参数:BYTE *src 二值图像的像素数据,不包含头部信息, 1bpp, int src_width 原图的宽度, in pixles,int src_height 原图的高度, in pixlesint bpp 转换tiff指定的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*dst_height;}// Image File Headerconst unsigned char tif_ifh[10]={'I','I', //intel顺序0x2a,0x00, //tiff版本号0x08,0x00,0x00,0x00, //IFD(图形文卷目录 Image File Diretory)偏移, 8个字节0x0F,0x00, // 这两个字节属于IFD了,指定有多少个Directory Entry};// Directory Entry ,每个都是12个字节的 , TIFF 6.0规定,标签要以升序的方式排列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 scanlines) in the image.0x00030102, 0x01, bpp, // BitPerSample 2580x00030103, 0x01, 0x001, // Compression, 1无压缩0x00030106, 0x01, 0x000, // PhotometricInterpretation, 对于bilevel图像,值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 PlanarConfiguration equal to 2// 从字面上看来, StripOffsets是个数组,小于64kb, 这里是0xce, 就是头部的206个字节了0x00030112, 0x01, 0x001, // Orientation0x00030115, 0x01, 0x001, // SamplesPerPixel0x00030116, 0x01, 0xffff, // RowsPerStrip, The number of rows in each strip (except possibly the last strip.)// RowsPerStrip and ImageLength together tell us the number of strips in the entire image. default is 2^32-1, 推荐每个strip是8k数据// image. The equation is: StripsPerImage= floor ((ImageLength + RowsPerStrip - 1) / RowsPerStrip).0x00040117, 0x01, 0x07F, // StripByteCounts0x0005011A, 0x01, 0x0BE, // XResolution0x0005011B, 0x01, 0x0C6, // YResolution0x0003011C, 0x01, 0x001, // PlanarConfiguration , 1是单平面格式, 2多平面格式0x00030128, 0x01, 0x002, // ResolutionUnit, 0x002 = inch0x60031280, 0x01000000, // X分辨率ofset=1640x60031280, 0x01000000, // Y分辨率ofset=16C};//tif_ifd[ 5] = dst_width;tif_ifd[ 8] = dst_height;tif_ifd[29] = (tif_size + 0x0f) & 0xfffffff0; // 简单的做成一个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(curr, tif_ifh, sizeof(tif_ifh));curr += sizeof(tif_ifh);memcpy(curr, tif_ifd, sizeof(tif_ifd));curr += sizeof(tif_ifd);// 当src图像的宽度是整数个字节的时候,可以直接复制数据,如果多出几个bit,就不好搞了//memcpy(curr, src, tif_size);// gray2bmp_to_tif.c 中程序,是把每个像素用一个字节来存储了,而这里不是的// 所以不能照着写程序了int width_align = src_width & 0xffffFFF8;int nBytes = width_align/8;if (src_width - width_align) // 有边角bit位nBytes += 1;LPBYTE src2 = src; LPBYTE dst2 = curr;int x,y;for (y = 0; y < src_height; y++){src2 = src + y * src_pitch;dst2 = curr + y * nBytes; //写入tiff的数据流,不要自己补充空白数据memcpy(dst2, src2, nBytes);src2 += nBytes;dst2 += nBytes;}return dst;
}// 这个是测试代码
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* bmpBuffer=(BYTE*)bm.bmBits; // GlobalAlloc(GPTR, bm.bmWidthBytes * bm.bmHeight); //allocate memoryDWORD tifSize;BYTE *tif = BW2Tif(bmpBuffer, bm.bmWidth, bm.bmHeight, 2, tifSize);CFile cf("C:\\bw2tif_out.tif", CFile::modeCreate | CFile::modeWrite);cf.Write(tif, tifSize);cf.Close();delete tif;}