Click here to Skip to main content
15,886,518 members
Articles / Desktop Programming / MFC
Article

Bitmap to Tiff conversion using Libtiff

Rate me:
Please Sign up or sign in to vote.
4.35/5 (12 votes)
6 Oct 20041 min read 322.5K   6.7K   46   75
Bitmap to Tiff conversion using Libtiff

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.

//load Bitmap

HBITMAP hImage = (HBITMAP)LoadImage(NULL, "C:\\MyBit.bmp", IMAGE_BITMAP,
0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION|LR_DEFAULTSIZE);

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

//Memory allocation is still 600x600 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,8);
  TIFFSetField(image, TIFFTAG_SAMPLESPERPIXEL,1);

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

  TIFFSetField(image, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
  TIFFSetField(image, TIFFTAG_COMPRESSION, COMPRESSION_PACKBITS); 

// 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

  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);

What's New in this update:

  1. Improved Compression
  2. Multi-page Tiff (Method to add pages in tiff)
  3. 256 colors of bmp

What is Next?

  1. Color in tiff file
  2. 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...

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
India India
Working in Patni Computer Systems, Noida(INDIA). I like to work in C/C++ even from my school time & mostly worked using C++, VC++, COM.

I want to give something bigger than biggest to IT field. Try is going on.

I like to make friends.
That’s all about me.

Ok! See you.
Have a nice Life.

Comments and Discussions

 
General[SK]Re: what's TIFFSetField(image, TIFFTAG_GROUP3OPTIONS, group3options)? Pin
Sumit Kapoor5-Dec-04 17:33
Sumit Kapoor5-Dec-04 17:33 
GeneralRe: [SK]Re: what's TIFFSetField(image, TIFFTAG_GROUP3OPTIONS, group3options)? Pin
Ayato Kamina5-Dec-04 23:05
Ayato Kamina5-Dec-04 23:05 
GeneralNo Image just Black or White Screen? Then Here is Solution Pin
Sumit Kapoor7-Oct-04 21:39
Sumit Kapoor7-Oct-04 21:39 
QuestionIs it possible to crop some part of image with LibTIFF?? Pin
Jigar Mehta10-Aug-04 21:17
Jigar Mehta10-Aug-04 21:17 
AnswerRe: Is it possible to crop some part of image with LibTIFF?? Pin
Sumit Kapoor10-Aug-04 22:15
Sumit Kapoor10-Aug-04 22:15 
QuestionHow to convert BMP to EPS File Format Pin
pubududilena28-Apr-04 22:40
pubududilena28-Apr-04 22:40 
AnswerRe: How to convert BMP to EPS File Format Pin
Sumit Kapoor29-Apr-04 3:07
Sumit Kapoor29-Apr-04 3:07 
GeneralLibTiff for Wince Pin
Tomal6-Apr-04 20:26
Tomal6-Apr-04 20:26 
GeneralRe: LibTiff for Wince Pin
Sumit Kapoor6-Apr-04 20:52
Sumit Kapoor6-Apr-04 20:52 
Generalit dosen't work very well Pin
Neta15-Mar-04 23:11
Neta15-Mar-04 23:11 
GeneralRe: it dosen't work very well Pin
Sumit Kapoor6-Apr-04 20:49
Sumit Kapoor6-Apr-04 20:49 
Generalreg tiff Pin
evangedhivya19-Jan-04 5:22
evangedhivya19-Jan-04 5:22 
GeneralRe: reg tiff Pin
Sumit Kapoor19-Jan-04 19:04
Sumit Kapoor19-Jan-04 19:04 
Generalreg libtiff Pin
evangedhivya8-Jan-04 23:00
evangedhivya8-Jan-04 23:00 
GeneralRe: reg libtiff Pin
Sumit Kapoor9-Jan-04 2:56
Sumit Kapoor9-Jan-04 2:56 
GeneralRe: reg libtiff Pin
evangedhivya9-Jan-04 5:49
evangedhivya9-Jan-04 5:49 
GeneralRe: reg libtiff Pin
Sumit Kapoor9-Jan-04 18:36
Sumit Kapoor9-Jan-04 18:36 
GeneralRe: reg libtiff Pin
evangedhivya11-Jan-04 4:09
evangedhivya11-Jan-04 4:09 
GeneralRe: reg libtiff Pin
Sumit Kapoor11-Jan-04 18:27
Sumit Kapoor11-Jan-04 18:27 
QuestionWhere is &#8216;GlobalFree&#8217; call?? Pin
Anonymous8-Jan-04 8:13
Anonymous8-Jan-04 8:13 
AnswerRe: Where is &#8216;GlobalFree&#8217; call?? Pin
Sumit Kapoor8-Jan-04 17:41
Sumit Kapoor8-Jan-04 17:41 
Generala new year vote Pin
m_harriss30-Dec-03 1:33
m_harriss30-Dec-03 1:33 
GeneralSince I use ImageMagick... Pin
Uwe Keim29-Dec-03 20:43
sitebuilderUwe Keim29-Dec-03 20:43 
GeneralRe: Since I use ImageMagick... Pin
Sumit Kapoor30-Dec-03 17:56
Sumit Kapoor30-Dec-03 17:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.