-
Notifications
You must be signed in to change notification settings - Fork 126
Open
Labels
Description
I am trying to convert a tiff file to PNG with libtiff.net with the code below, and gets that specific error for one file (see attachement). Is the file itself broken (and other software can make sense of it anyway) or is it some kind of limitation in libtiff.net?
public class TiffConverter
{
public void Convert(Stream input, Stream output)
{
Tiff.SetErrorHandler(new TiffExceptionErrorHandler());
using var tifImg = Tiff.ClientOpen(null, "r", input, new TiffStream());
var width = tifImg.GetField(TiffTag.IMAGEWIDTH)[0].ToInt();
var height = tifImg.GetField(TiffTag.IMAGELENGTH)[0].ToInt();
var bitmap = new SKBitmap();
var info = new SKImageInfo(width, height);
var raster = new int[width * height];
var handle = GCHandle.Alloc(raster, GCHandleType.Pinned);
try
{
bitmap.InstallPixels(info, handle.AddrOfPinnedObject());
if (tifImg.ReadRGBAImageOriented(width, height, raster, Orientation.TOPLEFT) == false)
{
throw new FormatException("Could not decode TIFF image.");
}
if (SKImageInfo.PlatformColorType == SKColorType.Bgra8888)
{
SKSwizzle.SwapRedBlue(handle.AddrOfPinnedObject(), raster.Length);
}
bitmap.Encode(output, SKEncodedImageFormat.Png, 0);
}
finally
{
handle.Free();
}
}
private class TiffExceptionErrorHandler : TiffErrorHandler
{
public override void ErrorHandler(Tiff tif, string method, string format, params object[] args)
{
throw new FormatException(string.Format(format, args));
}
}
new TiffConverter().Convert(File.OpenRead("06.tiff"), new MemoryStream());
shibaev