From ModMyiFone - Wiki
Background
The PNG images found on the iPhone cannot be viewed by a standard image viewer. Apple, for some unknown reason, implemented a non-standard PNG format that includes an extra data before the PNG header and compressed image data without the traditional headers and footers. In addition, the red blue pixels are flipped (BGRA instead of RGBA). All iPhone PNG images appear to follow this format.
Discussion
The PNG format consists of a header, followed by a set of data atoms, or chunks. According to the PNG spec, the 'IHDR', or PNG header chunk, should always come first. In Apple's iPhone format, a 'CgBI' chunk appears before the header. This chunk's data is four bytes long and contains a value of 0x40, or 48 decimal and is marked mandatory and private, which means that the data contained in the 'CgBL' chunk a third party extension to the PNG format that must be implemented by the parser. The purpose of this chunk, other than to signify that the PNG in iPhone format, is unknown. It could be a format version identifier.
Compressed image data, stored in the 'IDAT' chunk, contains deflate compressed data without the zlib headers, footers, or checksums that normal PNGs contain. When using zlib to decompress data, a negative value must be passed as the windowSize to use zlib's undocumented 'skip headers and crc' feature. There does not appear to be a good technical reason for using this format instead of the standard.
For some reason, the red and blue pixels are reversed in the deflated data. This could be an optimization to prevent the iPhone from having to do a pixel swap before writing the image to the screen buffer.
To convert an iPhone PNG to a standard PNG:
- Remove the 'CgBl' chunk
- Decompress the data using zlib with the windowSize set to a negative number
- Swap the red pixel with the blue pixel in the data
- Recompress the data using zlib using the default headers and CRC
- Replace the image data chunk with the new compressed data in the PNG, and create a new checksum
Code
Conversion code for both purposes may be retrieved using anonymous svn from svn://svn.poop.nl/pub/iphone-fixpng (guaranteed to be the most-recent), or using the tarball at http://www.poop.nl/files/iphone-fixpng.tgz, whichever you prefer.
Tested using the iPhone wallpaper images (/Library/Wallpaper) on both Intel and PowerPC Macs with OSX. The Makefile in SVN now compiles for both ppc and x86.
You'll need zlib for fixpng.c and libpng for flipchannels.c
if you don't have libpng installed, a nice universal installer can be found here:
http://ethan.tira-thompson.com/Mac%20OS%20X%20Ports.html
Once you have libpng installed, cd to the directory where fixpng is and type "make"
Here is an alternative Makefile for building on darwin. It assumes that fink is installed with the libpng package whereas the included Makefile assumes a binary installation into /usr/local. ZLib is included with MacOS X.
LIBS=-lz -lpng12
LIBPATH=-L/sw/lib
CC=gcc
CFLAGS=-I/sw/include
all: fixpng
clean:
rm fixpng png
fixpng: fixpng.c
$(CC) $(CFLAGS) -o fixpng fixpng.c $(LIBPATH) $(LIBS)
$(CC) $(CFLAGS) -o png png.c $(LIBPATH) $(LIBS)
SOURCE LINK
Alternative Code
To normalize downloaded PNG files from the iPhone and iPod touch you can download and use this python script at:
http://www.axelbrz.com.ar/?mod=iphone-png-images-normalizer (multiplatform)
This script is recommended for Windows users that cannot use the iphone-fixpng.
To normalize PNG online, you can use at : http://zope.release78.org/beta2/iconvert/