Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using LibTiff from C# (to access tiled TIFF images)

I'd like to use LibTiff to access very large TIFF files. I need functions like multiple pages and tiles, and so LibTiff seems to be the right way to go. Can anyone help me on how to use LibTiff from C#? I've found some links (like blog.bee-ee which contained partial code. But I couln't get beyond getting a version. I've looked at FreeImage but found it not suitable (pictures are approx. 800 MPixel 8 or 16 bit grayscale --> 800-1600 MByte) size and I can't load it in memory in a 32-bit environment)

I'm very experienced in C/C++, but not yet in C#. Can anyone help me to a wrapper or some hints?

Note: I need pages to access pyramidical planes (multi-resolution) in a tiff, and tiles of 256x256 to have fast access to different parts of the image without loading it at once.

[Edit] The LibTIFF.NET solution seemed most practical for me. I'm now integrating it in a product development, and it will propably save me a lot of headaches from going in/out of managed memory. I have not yet tried the 'callback' functionality, which seems to be solved nicely in a .net way. Thanks for the help on stackoverflow [/Edit]

like image 559
Adriaan Avatar asked Jan 11 '10 12:01

Adriaan


People also ask

Is TIFF a video format?

Tag Image File Format, abbreviated TIFF or TIF, is an image file format for storing raster graphics images, popular among graphic artists, the publishing industry, and photographers.


3 Answers

You can try our LibTiff.Net. It is free and open source version of LibTiff written using managed C#. API of our implementation kept very similar to original one's.

https://bitmiracle.com/libtiff/

We've just released it, so there may be bugs. But full source code comes with a number of test, so most obvious bugs should be fixed.

like image 159
Bobrovsky Avatar answered Sep 29 '22 09:09

Bobrovsky


WIC provides support for very large image files. There's a nice wrapper for it in the .NET framework: TiffBitmapDecoder.

like image 30
Hans Passant Avatar answered Oct 01 '22 09:10

Hans Passant


My initial solution was to use a C#.NET wrapper for the LibTIFF DLL. I have found it on libtiff ftp. It seems to contain all important methods of the LIBTiff library. It keeps memory in unmanaged LibTIFF memory; this means that the C# code needs to be aware of this and copy data when it needs to be processed in safe code.

My 'tile reading code' to test it read like

if (LibTIFF.IsTiled(tiff))
{
    if (LibTIFF.GetField(tiff, (int)TIFFTags.TIFFTAG_TILEWIDTH, out tileWidth) &&
        LibTIFF.GetField(tiff, (int)TIFFTags.TIFFTAG_TILELENGTH, out tileLength))
    {
        uint tiles = 0;
        IntPtr pTile = LibTIFF._malloc((int)(tileLength*tileWidth*bitsPerSample/2));
        for (uint y = 0; y < imageLength; y += tileLength)
        {
            for (uint x = 0; x < imageWidth; x += tileWidth)
            {
                LibTIFF.ReadTile(tiff, pTile, x, y, 0, 0);
                tiles++;
            }
        }                        
        LibTIFF._free(pTile);   
    }
}

I'll go for the .NET port of LibTIFF though, but this solution may be the right one for other people, so I leave it on StackOverflow.

like image 38
Adriaan Avatar answered Oct 02 '22 09:10

Adriaan