Skip to content

Decoding malformed iPhone generated PNG throws 'Bad compression method for ZLIB header: cmf=237' #410

Description

@zeus82

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

Try loading the attached image and see that it generates a 'Bad compression method for ZLIB header: cmf=237' error. System.Drawing can open this image.

Steps to Reproduce

Image.Load(File.OpenRead("1.png"))

System Configuration

.NET Standard 2.0 Dll hosted in a .NET Framework 4.6.1 windows service

  • ImageSharp version: 1.0.0-beta0002
  • Other ImageSharp packages and versions: Sizlabors.Core 1.0.0-beta0004
  • Environment (Operating system, version and so on): Windows 10
  • .NET Framework version: 4.6.1
  • Additional information:

1

Activity

  1. JimBobSquarePants commented on Dec 23, 2017

    @JimBobSquarePants
    Member

    Hi @zeus82

    Thanks for the detail and the input image. It turns out that image is not a valid png.

    cgbi

    http://iphonedevwiki.net/index.php/CgBI_file_format

    There's quite a few differences between the format

    • extra critical chunk (CgBI)
    • byteswapped (RGBA -> BGRA) pixel data, presumably for high-speed direct blitting to the framebuffer
    • zlib header, footer, and CRC removed from the IDAT chunk
    • premultiplied alpha (color' = color * alpha / 255)

    @antonfirsov @tocsoft @dlemstra

    Theoretically it looks like we could potentially handle these changes with some updates to our decoder based on the following, it looks like a lot of work though for all our different color types:

    http://www.axelbrz.com.ar/?mod=iphone-png-images-normalizer

    I don't know if we should attempt this for V1 though. What do you think?

  2. tocsoft commented on Dec 23, 2017

    @tocsoft
    Member

    seeing as chrome doesn't seem to even seem to render it then i'm inclined to think we should defiantly push it past v1 and even then see how much of an issue its causing in the future and make a another call then.

  3. JimBobSquarePants commented on Dec 23, 2017

    @JimBobSquarePants
    Member

    Ha! Doesn't it! I only have Edge installed at the mo cos I had to get my laptop replaced. That renders something but it ain't pretty. Yeah let's leave it for now then.

  4. antonfirsov commented on Dec 23, 2017

    @antonfirsov
    Member

    Only Windows + GDI based decoders seem do decode it properly. I consider this issue as low-priority one, definitely not a show-stopper for V 1.0.

  5. added this to the Future milestone on Dec 24, 2017
  6. JimBobSquarePants commented on Dec 24, 2017

    @JimBobSquarePants
    Member

    Ok, have moved it to futures. Gonna do a little cleanup in the png decoder around the zlib headers to remove an allocation I saw that we don't need anyway.

  7. vpenades commented on Jan 3, 2018

    @vpenades
    Contributor

    I would detect this specific format, but just to throw a more specific exception, like "malformed iPhone PNG detected", I think it's Apple's responsability to write proper PNGs, not the responsability of others to read their malformed files.

  8. changed the title [-]Image.Load<Rgba32> Results in 'Bad compression method for ZLIB header: cmf=237' Exception[/-] [+]Decoding malformed iPhone generated PNG throws 'Bad compression method for ZLIB header: cmf=237'[/+] on May 26, 2018
  9. qmfrederik commented on Jun 13, 2018

    @qmfrederik

    These files have been processed with an Apple-variation of pngcrush.

    You can use PNGDecrush (source) to decrush the image, which can then be fed to other image libraries.

    E.g.:

    using (Stream input = File.OpenRead("34303888-73d65ca6-e705-11e7-88cf-6c16ad871172.png"))
    using (MemoryStream decrushed = new MemoryStream())
    {
        PNGDecrusher.Decrush(input, decrushed);
        decrushed.Position = 0;
    
        var image = Image.Load(decrushed);
    }
  10. JimBobSquarePants commented on Jun 13, 2018

    @JimBobSquarePants
    Member

    @qmfrederik Thanks for the input; that a really useful tool! 👍

    I'm going to get around to natively handling this one day, need to do some cleanup of the decoder first though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions