为什么我把BMP转TIF(传真压缩)格式不成功呢?看代码

WJZMLSH 2005-07-02 04:11:09
HBITMAP hImage = (HBITMAP)LoadImage(NULL, "mybit2.bmp", IMAGE_BITMAP,
0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION|LR_DEFAULTSIZE);

CBitmap* m_Bitmap = CBitmap::FromHandle(hImage);

// Sumit: memory allocation is still 1800x1800 in your code..
BYTE* bmpBuffer=(BYTE*)GlobalAlloc(GPTR, 600*600);//allocate memory


// Size of bitmap as I draw by using x,y points...
m_Bitmap->GetBitmapBits(600*600 ,bmpBuffer);


TIFF *image;

// Open the TIFF file
if((image = TIFFOpen("C:\\output.tif", "w")) == NULL)
{
printf("Could not open output.tif for writing\n");
}

TIFFSetField(image, TIFFTAG_IMAGEWIDTH,600);
TIFFSetField(image, TIFFTAG_IMAGELENGTH,600);
TIFFSetField(image, TIFFTAG_BITSPERSAMPLE,1);
TIFFSetField(image, TIFFTAG_SAMPLESPERPIXEL,1);

uint32 rowsperstrip = TIFFDefaultStripSize(image, -1);
//<REC> gives better compression

TIFFSetField(image, TIFFTAG_ROWSPERSTRIP, rowsperstrip);

TIFFSetField(image, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX3);
// TIFFSetField(image, TIFFTAG_COMPRESSION, COMPRESSION_PACKBITS);
// TIFFSetField(image, TIFFTAG_COMPRESSION, COMPRESSION_NONE);


// Start CCITTFAX3 setting

uint32 group3options = GROUP3OPT_FILLBITS+GROUP3OPT_2DENCODING;
TIFFSetField(image, TIFFTAG_GROUP3OPTIONS, group3options);
TIFFSetField(image, TIFFTAG_FAXMODE, FAXMODE_CLASSF);
TIFFSetField(image, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
TIFFSetField(image, TIFFTAG_ROWSPERSTRIP, -1L);


// End CCITTFAX3 setting

//if we comment following line then Tiff will not view in Imaging
//but view in DC
TIFFSetField(image, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);

TIFFSetField(image, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
TIFFSetField(image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);

TIFFSetField(image, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
TIFFSetField(image, TIFFTAG_XRESOLUTION, 100.0);
TIFFSetField(image, TIFFTAG_YRESOLUTION, 100.0);


char page_number[20];
sprintf(page_number, "Page %d", 1);

TIFFSetField(image, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);
TIFFSetField(image, TIFFTAG_PAGENUMBER, 1, 1);
TIFFSetField(image, TIFFTAG_PAGENAME, page_number);



// Write the information to the file
BYTE *bits;
for (int y = 0; y < 600; y++)
{
bits= bmpBuffer + y*600;
if (TIFFWriteScanline(image,bits, y, 0)==-1) MessageBox("Complete or error");
}

// Close the file
TIFFClose(image);

...全文
136 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsvine 2005-07-07
  • 打赏
  • 举报
回复
看你程序的意思,你的bmp图是600X600的灰度图或24位彩色图

for (int y = 0; y < 600; y++)
{
bits= bmpBuffer + y*600;
if (TIFFWriteScanline(image,bits, y, 0)==-1) MessageBox("Complete or error");
}
用到的bits应该是单色的bmp信息,且其调色板为FF FF FF 00 00 00 00 00 。

4,446

社区成员

发帖
与我相关
我的任务
社区描述
图形图像/机器视觉
社区管理员
  • 机器视觉
  • 迪菲赫尔曼
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧