11

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]

Bobrovsky
  • 13,789
  • 19
  • 80
  • 130
Adriaan
  • 3,282
  • 1
  • 23
  • 31

3 Answers3

8

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.

Bobrovsky
  • 13,789
  • 19
  • 80
  • 130
  • Thanks. My first tests are looking good. Although I found a wrapper class, it did not have a known author and I would sooner or later get to some issues with the access to managed/unmanaged memory resources. Benchmarks showed me equivalent performance on reading of non-compressed files, and a 50% perfomance hit on Adobe compressed pyramidical tiled tiffs. This is acceptable for me. I may decide not to use compression, and I'll be better of making my software multi-core friendly than solving memory issues. – Adriaan Jan 14 '10 at 08:12
  • Thanks, Adriaan our implementation is definitely slower than original due language of choice :-) – Bobrovsky Jan 14 '10 at 12:24
  • I don't mind. For my project performance means functionality/euro. And development hours are 95% of the cost. I can easily throw in a quadcore to compensate for this. Besides, when I stay away from compression the difference is small. And my files only compress about 10%, so that's only a lousy 100MByte per file ;-) – Adriaan Jan 17 '10 at 21:27
4

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

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 1
    Can you add some links? From what I have seen, TiffBitmapDecoder tries to load an image in memory at once. I can't do that. Pictures > 1 GByte will always fail on a 32-bit machine due to fragmentation. are you referring to windows7-specifics (http://msdn.microsoft.com/en-us/library/ee720061%28VS.85%29.aspx)? – Adriaan Jan 11 '10 at 13:09
  • No, TBD doesn't store an image. Start here: http://msdn.microsoft.com/en-us/library/ms748873.aspx – Hans Passant Jan 11 '10 at 13:41
  • Thanks for your help. Unfortunately my experience is different. The steps outlined in http://msdn.microsoft.com/en-us/library/ms748873.aspx do try to allocate a single block of memory with the size of the 1 frame of the bitmap.I need access to every pixel in the bitmap (so no fit-to-screen memory save option), just not necessarily in one chunk of memory. When I try to run the tiff decoder as given in the example, it generally fails on bitmaps > 10k * 10k. And these are just my 'small test files'. My target is 800MPixel x 16 bit. That's why I look for tiled TIFF files. – Adriaan Jan 13 '10 at 08:04
  • Hmya, you can't get a 400 MB image loaded on a 32-bit operating system. Switch to a 64-bit OS or read the image in portions. – Hans Passant Jan 13 '10 at 10:15
  • Yeah. This is why I was looking into tiles. The switch to a 64 bit OS would solve part of the issues, but still get large latencies to read the entire file before doing any processing. Harddisks are slower than cpu's nowadays. Also I like the possibilities of pyramid tiff files (i.e. having low-res images ready in the same file for previews etc.) – Adriaan Jan 13 '10 at 12:33
  • Is it possible to read a large TIFF image around 3MB, which is a `Tiled TIFF Image` using TiffBitmapDecoder and convert to PDF? with out using 3rd party library? I am allowed to use iTextSharp to create pdf, but no other 3rd party library like Lib2Tiff, etc – Murali Murugesan Feb 27 '14 at 22:13
1

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.

Adriaan
  • 3,282
  • 1
  • 23
  • 31
  • Is `LibTiff` is the only way to process Tiled TIFF? or can we do the same with TiffBitmapDecoder class in .Net Framework? – Murali Murugesan Feb 27 '14 at 22:16
  • From what I can see, the TiffBitMapDecoder does not provide functionality to selectively load parts of the file (only for rendering complete frames). http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.tiffbitmapdecoder(v=vs.110).aspx – Adriaan May 05 '14 at 12:41