Introduction
What the article/code snippet does? : It converts 256 color bitmap file to
Black & white tiff file using Libtiff.
Why it's useful? : This Article provides a method to convert Bitmap Image to Tiff Image. The
main target of this demo project is to tell you about writing a tiff file. I was
trying from so long time to get example code that can write tiff file. But no one
can provide one as simple as I wished it to be. Now this code would at least help
you. I've included the Bitmap Image used in this demo.
Where is Libtiff? : You can get Libtiff Library free from http://www.libtiff.org/. Also, I've
included Library files in my demo project. All library contents are as it is
provided & with their owner rights. I have no concern with that Library's bugs.
I am just using this library to show how to convert a bmp to a tiff.
How to use this code
I've collected all conversion code in a single function which converts bmp to tiff, Function is given below or you can fin this code in demo project.
HBITMAP hImage = (HBITMAP)LoadImage(NULL, "C:\\MyBit.bmp", IMAGE_BITMAP,
0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION|LR_DEFAULTSIZE);
CBitmap* m_Bitmap = CBitmap::FromHandle(hImage);
BYTE* bmpBuffer=(BYTE*)GlobalAlloc(GPTR, 600*600);
m_Bitmap->GetBitmapBits(600*600 ,bmpBuffer);
TIFF *image;
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,8);
TIFFSetField(image, TIFFTAG_SAMPLESPERPIXEL,1);
uint32 rowsperstrip = TIFFDefaultStripSize(image, -1);
TIFFSetField(image, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
TIFFSetField(image, TIFFTAG_COMPRESSION, COMPRESSION_PACKBITS);
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);
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);
BYTE *bits;
for (int y = 0; y < 600; y++)
{
bits= bmpBuffer + y*600;
if (TIFFWriteScanline(image,bits, y, 0)==-1) MessageBox("Complete or error");
}
TIFFClose(image);
What's New in this update:
- Improved Compression
- Multi-page Tiff (Method to add pages in tiff)
- 256 colors of bmp
What is Next?
- Color in tiff file
- Other format to tiff
My wish was to provide you one independent function that can be used
anywhere. That's what I did in this article.
What am I expecting from you all?
As I think, I'm not an expert like you, long way yet remain to take over. Please point out my bugs in this article so I
can improve those. Thanks, OK! Good Bye, Have a nice life...