Skip to content

Error Read error at scanline -1; got 145365 bytes, expected 969285, file opens fine in Windows, Paint.Net #128

@osjoberg

Description

@osjoberg

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?

06.tiff

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());

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions