c# writing out a Tiff File using libtiff
by brian on Mar.12, 2008, under GUI, c#, c# coding GUI, coding, graphics
Tiff files are interesting. They are like the all inclusive file format. It is an extremely flexible file format that allows for many types of pixel formats that other image formats couldn’t possibly handle. For instance 16 bits per channel or sample in the Tiff lingo. Event the possibility of 4 or 5 samples per pixel. If you wanted you could have a Red, Green, Blue, IR, Alpha channel. Although software would have a difficult time displaying the image. Anyway I digress — tiff being somewhat flexible is also some what difficult to manage, this is where libtiff comes in handy. It handles most of this for us.
The first step is to add using System.Runtime.InteropServices; to you module. Then add the stubs for the libtiff functions as follows:
static class TiffHandler
{
[DllImport("libtiff3.dll")]
private static extern int TIFFOpen(String image_path, String opts);
[DllImport("libtiff3.dll")]
private static extern void TIFFClose(int handle);
[DllImport("libtiff3.dll")]
private static extern void TIFFGetField(int handle, uint property, ref UInt32 value );
[DllImport("libtiff3.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
private static extern int TIFFSetField(int handle, uint property, uint value);
[DllImport("libtiff3.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
private static extern int TIFFSetField(int handle, uint property, string value);
[DllImport("libtiff3.dll")]
private unsafe static extern int TIFFReadRGBAImage(int handle, UInt32 width, UInt32 height, byte *raster, int Unkown);
[DllImport("libtiff3.dll")]
private unsafe static extern int TIFFReadEncodedStrip(int handle, int strip, byte* buf, int size);
[DllImport("libtiff3.dll")]
private unsafe static extern byte* _TIFFmalloc(UInt32 size);
[DllImport("libtiff3.dll")]
private unsafe static extern void _TIFFfree(byte* pointer);
[DllImport("libtiff3.dll")]
private unsafe static extern void TIFFWriteEncodedStrip(int handle, int offset, byte* buffer, int size);
[DllImport("libtiff3.dll")]
private static extern uint TIFFScanlineSize(int handle);
[DllImport("libtiff3.dll")]
private static extern int TIFFDefaultStripSize(int handle, int size);
[DllImport("libtiff3.dll")]
private unsafe static extern int TIFFWriteScanline(int handle, byte* buffer, int row, int sample);
[DllImport("libtiff3.dll")]
private unsafe static extern int TIFFReadScanline(int handle, byte* data, int row, int sample);
Next we add the constants. I’m not going to add them all there are thousands of them but here is an example of the ones that are used in writting a tiff. If you want more look in the libtiff header files. It seems like it’s in tiff.h
const uint TIFFTAG_IMAGEWIDTH = 256; /* image width in pixels */
const uint TIFFTAG_IMAGELENGTH = 257; /* image height in pixels */
const uint TIFFTAG_BITSPERSAMPLE = 258; /* bits per channel (sample) */
const uint TIFFTAG_SAMPLESPERPIXEL = 277; /* samples per pixel */
const uint TIFFTAG_COMPRESSION = 259; /* data compression technique */
const uint COMPRESSION_DEFLATE = 32946; /* Deflate compression */
const uint PHOTOMETRIC_RGB = 2; /* RGB color model */
const uint TIFFTAG_PLANARCONFIG = 284; /* storage organization */
const uint PLANARCONFIG_CONTIG = 1; /* single image plane */
And then the code to write out the image to a file:
public static unsafe void SaveTiff(string fileName, ImageArray array)
{
int tif = TIFFOpen(fileName, "w");
int Samples = 3;
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint)array.Width);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint)array.Height);
TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE);
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, (uint)Samples);
TIFFSetField(tif, TIFFTAG_ARTIST, "Bee Eee Inventions, LLC");
int cnt = array.Width * array.Height;
int size = (cnt * Samples * sizeof(ushort));
ushort* buffer;
buffer = (ushort*)_TIFFmalloc((uint)size);
// We set the strip size of the file to be size of one row of pixels
//int RowSize = TIFFDefaultStripSize(tif, array.Width*3);
// copy data into buffer
int i,pos,ptr=0;
for (i = 0, pos = 0; i < cnt; i++, pos += Samples, ptr++)
{
buffer[pos] = (ushort)(array.Data[ptr].r);
buffer[pos + 1] = (ushort)(array.Data[ptr].g);
buffer[pos + 2] = (ushort)(array.Data[ptr].b);
}
// Write the information to the file
TIFFWriteEncodedStrip(tif, 0, (byte *)buffer, size);
TIFFClose(tif);
_TIFFfree((byte *)buffer);
}
Enjoy. Next article I’ll show you how to read some tiff files. Opening all tiff files could be difficult. :)
Leave a Reply
You must be logged in to post a comment.

August 11th, 2008 on 9:59 am
Hi !
This is very interesting, thank you !
But I have a problem: the program does not find the libtiff3.dll ! I tried to register it but I could not ! How did you do that ?
Thank you !
August 11th, 2008 on 8:56 pm
FGold,
Because libtiff3.dll isn’t a c# assembly you can’t add it as a reference.
Instead put the dll in the same directory as your executable or within the system search path ie c:\Windows\system32. Another way to do it is to add the dll to your c# project (Add->Existing Item). Then go to it’s properties and set “Copy To Output Directory” to “Always Copy”, that’ll do it too.
Good luck
Brian D.
February 16th, 2009 on 2:36 am
Hi frnd’s …
Im new to c#.net can any one say from where i have to download libtiff dll and how to include in c#.net
Tks in Adv
G.sam….
April 13th, 2009 on 7:47 pm
i have the same problem: no matter where i put the libtiff3.dll, it will not load it. i have tried all the possibilities.
if i use the older version: libtiff.dll, then it works fine. Links, i can load an image, etc. however there is no entrypoint for the _TiffMalloc call, which i need.
so i either have to find the header file for the libtiff.dll, or figure out why the libtiff3.dll will not link.
any ideas?
Thanks
Joe
January 7th, 2010 on 1:49 am
what is ImageArray , i copy the code , but i have not ImageArray define ?
August 23rd, 2010 on 1:13 pm
Nope, not a .Net class. Just a custom defined class.
September 5th, 2010 on 5:04 am
Buy:Viagra Soft Tabs.Soma.VPXL.Viagra Professional.Levitra.Cialis Soft Tabs.Cialis Super Active+.Maxaman.Cialis.Viagra Super Force.Zithromax.Super Active ED Pack.Viagra.Cialis Professional.Propecia.Viagra Super Active+.Tramadol….
September 6th, 2010 on 1:25 am
Buy:Advair.Amoxicillin.Nymphomax.Female Cialis.Ventolin.Wellbutrin SR.Cozaar.Lipitor.Prozac.Aricept.Seroquel.Zetia.SleepWell.Buspar.Lasix.Acomplia.Zocor.Female Pink Viagra.Lipothin.Benicar….
September 6th, 2010 on 8:15 pm
Buy:Female Pink Viagra.Ventolin.Zetia.Female Cialis.Buspar.Lipothin.Prozac.Lasix.Amoxicillin.Nymphomax.Lipitor.Benicar.Advair.Cozaar.Seroquel.Wellbutrin SR.Aricept.SleepWell.Acomplia.Zocor….