Skip to content

Re-saving interlaced PNG results in corrupt image #1211

@wagich

Description

@wagich

Prerequisites

  • I have written a descriptive issue title
  • I have verified that I am running the latest version of ImageSharp
  • I have verified if the problem exist in both DEBUG and RELEASE mode
  • I have searched open and closed issues to ensure it has not already been reported

Description

Saving a PNG with PngColorType.Palette and PngInterlaceMode.Adam7 combined with any of the filter modes PngFilterMethod.Average, PngFilterMethod.Paeth and PngFilterMethod.Up results in a corrupt image. Unfortunately, even if this combination of settings doesn't make any sense (?), it can be arrived at using a suitable PNG input image without specifying any encoder options…

The issue seems to be that the settings from the source image are copied over in the PngEncoderCore.Encode method which is called after having already set FilterMethod independently of the source image settings in the PngEncoderOptions constructor, defaulting it to PngFilterMethod.Paeth.
Maybe PngEncoderOptionsHelpers.AdjustOptions could be extended to also validate/change the PngFilterMethod based on PngColorType, same as in PngEncoderOptions ctor.

Steps to Reproduce

var image = Image.Load("anyimage.ext");
var encoder = (PngEncoder)image.GetConfiguration().ImageFormatsManager.FindEncoder(PngFormat.Instance);

encoder.ColorType = PngColorType.Palette;
encoder.InterlaceMethod = PngInterlaceMode.Adam7;
encoder.FilterMethod = PngFilterMethod.Paeth;
// values resulting in corrupt image
//encoder.FilterMethod = PngFilterMethod.Average;
//encoder.FilterMethod = PngFilterMethod.Paeth;
//encoder.FilterMethod = PngFilterMethod.Up;
// values resulting in ok image
//encoder.FilterMethod = PngFilterMethod.Adaptive;
//encoder.FilterMethod = PngFilterMethod.None;
//encoder.FilterMethod = PngFilterMethod.Sub;

image.Save("output.png", encoder);

System Configuration

  • ImageSharp version: 1.0.0-rc0001
  • Environment (Operating system, version and so on): Windows 10 1909, Intel x64
  • .NET Framework version: .NET Core 3.1.300

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions