Giter Site home page Giter Site logo

ibcla / pngdecrush Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mikeweller/pngdecrush

0.0 0.0 0.0 160 KB

A C# library for reversing the 'pngcrush' optimization process that is applied to PNG files in an iOS app.

License: MIT License

C# 100.00%

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
    }
}

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.