Giter Site home page Giter Site logo

pngdecrush's Introduction

PNGDecrush

PNGDecrush is a C# library for reversing the optimization process that is applied to PNG files in an iOS project.

Background

When building an iOS app using Xcode, PNG files are run through the pngcrush tool with some proprietry Apple modifications (accessed through the -iphone option).

xcrun -sdk iphoneos pngcrush -iphone ...

Specifically, the following steps are performed:

  • The proprietry and non-standard CgBI chunk is added to the start of the PNG.
  • Zlib headers and checksums are removed from all IDAT chunks (leaving just raw deflate data).
  • Pixel data is converted to BGR/BGRA byte order.
  • Pixel color values are pre-multiplied with the alpha.

Existing implementations are broken

There are some existing 'decrush' implementations out there if you google around, all of which are broken for a number of different reasons, including:

  • They perform a very naive byte swap on the raw PNG image bytes without reversing the precompression line filters beforehand. This will cause sometimes subtle, sometimes not-so-subtle artifacts in the resulting image.
  • They do not handle multiple IDAT chunks correctly.
  • Their code quality is lacking and they do not have tests.

This implementation

This implemention performs the following steps:

  • The CgBI chunk is removed
  • Chunk data is inflated and deflated again with zlib headers intact
  • The resulting PNG is read into .NET's standard image manipulation classes where:
    • The byte swap is reversed
    • The premultiplied alpha is reversed

We have unit tests, and everything is stream based.

A simple example of using the library:

using (FileStream input = File.OpenRead('/path/to/input.png'))
using (FileStream output = File.Create('/path/to/output.png'))
{
	try
	{
	    PNGDecrusher.Decrush(input, output);
	}
	catch (InvalidDataException)
    {
        // decrushing failed, either an invalid PNG or it wasn't crushed
    }
}

pngdecrush's People

Contributors

mikeweller avatar

Stargazers

zx avatar Muhammet Ali Köker avatar  avatar  avatar  avatar  avatar  avatar Marcelo Lv Cabral avatar Shehzad C avatar lulu.zhou avatar Tomas McGuinness avatar Guo KK avatar Victor avatar 道哥 avatar Justin Hyland avatar JGTM'2024 avatar Brenton House avatar

Watchers

 avatar James Cloos avatar Muhammet Ali Köker avatar

pngdecrush's Issues

License

Could you identify a license under which this source can be used?

Building under GNU/Linux

Is there a way to compile and run this under GNU/Linux? If not, would it be at least possible to make it work with Wine provided that you make a binary release? Here's the image I would like to revert/decrush (source):

📎 image.png

Online Converter

I put this code work on my own server, feel free to use. I will not save any image on my server.
https://ez.schwarzer.wang/tools/pngdecrush

Source code:

public ActionResult PngDeCrush()
{
    if (Request.Files.Count == 0)
        return View("PngDeCrush");
    else
    {
        try
        {
            Stream macPng = Request.Files["pngfile"].InputStream;
            MemoryStream standardPng = new MemoryStream(); 
            PNGDecrusher.Decrush(macPng, standardPng);
            standardPng.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(standardPng, "image/png")
            {
                FileDownloadName = "decrushed.png"
            };
        }
        catch(Exception)
        {
            return View("PngDeCrush");
        }
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.