0

I am having trouble loading Tiff files in C#. I downloaded some sample tiff files and was able to load them fine, however when I attempt to load any tiff files generated from PCI Geomatica or ArcGIS, the ReadRGBAImage call fails (returns false). Other than IMAGEWIDTH and IMAGELENGTH, all of the other tags I've tried to retrieve have returned null (eg. XRESOLUTION). Does anyone have any ideas as to why this is happening? The relevant code is below:

using (Tiff tif = Tiff.Open(fileName, "r"))
        {
            // Find the width and height of the image
            FieldValue[] value = tif.GetField(TiffTag.IMAGEWIDTH);
            int width = value[0].ToInt();

            value = tif.GetField(TiffTag.IMAGELENGTH);
            int height = value[0].ToInt();

            // Read the image into the memory buffer
            int[] raster = new int[height * width];

            if (!tif.ReadRGBAImage(width, height, raster))
            {
                System.Windows.Forms.MessageBox.Show("Could not read image");
                return null;
            }

        }

Thanks!

BruceDoh
  • 113
  • 6
  • Haven't used libtiff.net myself, but is it possible the files are laid out as strips rather than RGBA pixels? This is pretty common in GIS image types. I see the Tiff class has a `ReadRGBAStrip` method... (Also, tiling is common for GIS image formats.) – adv12 Jan 15 '15 at 17:29
  • 1
    Thanks, adv12. That sounds like a promising answer. I'll try it out and report back. – BruceDoh Jan 15 '15 at 17:38
  • 1
    I was able to successfully load a strip of the tiff using ReadRGBAStrip. Thanks again for this answer, you may have just saved me a lot of banging my head against a wall! PS. I'm new to this site and not 100% sure how it all works, but if you want to submit this as an answer I can mark it as the correct answer. (that gives you some sort of reputation points or something?) – BruceDoh Jan 15 '15 at 17:44
  • Another common TIFF pitfall is endianness. Make sure your lib correctly opens TIFF files created at MacOS. – Yury Schkatula Jan 15 '15 at 17:47
  • If you can read an image using `ReadRGBAStrip` then you should be able to read it using `ReadRGBAImage` too. And LibTiff.Net should read images with any endianness just fine. So it quite interesting why `ReadRGBAImage` fails. It would help if you post all error and warning messages emitted by the library here. – Bobrovsky Jan 15 '15 at 19:54
  • Actually, I misspoke. It was with ReadEncodedStrip that I was able to load a strip. I am still having trouble reading RGBA values from the file, so any help on that front would be appreciated. – BruceDoh Jan 15 '15 at 20:38
  • Feel free to un-mark my answer and pursue this further. It's worth noting that @Bobrovsky is a (the?) developer of LibTiff.Net, so he probably knows what he's talking about ;) – adv12 Jan 15 '15 at 21:18
  • @Bobrovsky I don't see any error messages. When I run ReadRGBAImage or ReadRGBAStrip, it returns false. I've tried to make sure my output is in RGBA format, but I'm still unable to load it. I can load encodedstrips, but I'm not sure how to extract RGBA data from that. For example, my RGBA values are showing as floating point in ArcGIS, but when I read encoded strips, I get values like this: 180 < - R ? 152 < - G? 170 < - B? 61 < - A? But the 4th value (alpha?) is always around 59-65, but in ArcGIS, the alpha value is around 28.0 to 28.9... RGB values are in approximate the range of 0.01-0.8 – BruceDoh Jan 16 '15 at 14:34

1 Answers1

0

Without a file to reproduce the issue, I can be 100% sure, but it looks like your file can not be converted to RGBA raster with help of LibTiff.Net.

It's not an error, it's just you are using some not very popular flavor of TIFF. Some say TIFF is a Thousand of Incompatible File Formats. And there is certainly some truth in that statement.

The library can read (and decode!) your file. You can get decoded raster using ReadEncodedStrip and / or ReadScanline methods. The task of converting your raster to RGBA is left to you.

And don't forget that not every image can be converted to RGBA without loosing some of the image data.

Bobrovsky
  • 13,789
  • 19
  • 80
  • 130
  • I think you're right. I've given up on trying to load tiff files produced by these programs and am now just dealing with simple ascii files. Thanks for the responses. – BruceDoh Jan 16 '15 at 21:54