为什么我把BMP转TIF(传真压缩)格式不成功呢?看代码
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);