Saving colorindexed Tiff using LibTiff.net throws exception

198 Views Asked by At

I'm trying to save a colorindexed Bitmap to Tiff using Libtiff.net. But it throws an OutOfRangeException: Exception

The exception occurs if i set and indexed colormap:

       internal static bool SaveColorPalette(Bitmap source, Tiff destTiff, int bitsPerSample)
    {
        destTiff.SetField(TiffTag.PHOTOMETRIC, Photometric.PALETTE);
        var paletteSize = 1 << bitsPerSample;
        var palette = source.Palette.Entries;
        var reds = new short[paletteSize];
        var greens = new short[paletteSize];
        var blues = new short[paletteSize];
        for (var i = 0; i < palette.Length && i < paletteSize; ++i)
        {
            var color = palette[i];
            reds[i] = color.R;
            greens[i] = color.G;
            blues[i] = color.B;
        }

        destTiff.SetField(TiffTag.COLORMAP, reds, greens, blues);
        return true;
    }

Does anyone have an idea what can cause this?

Thanks.

Edit: The exception occurs when i save the Tiff with

outputTiff.WriteDirectory();
1

There are 1 best solutions below

0
On BEST ANSWER

I've found solution.

First you have to define

TiffTag.BITSPERSAMPLE

The Tiff.SetField function for TiffTag.COLORMAP use this to calculate the array length, instead of relying on C# length property.

See line 231 at TiffTagMethods