The ModMyTM Family of Sites:
ModMyMotoModMyiModMyGphone





 
 

Discuss AppStore Apps | Browse / Search Cydia | MMi Cydia Stats



Personal tools

Iphone PNG images

From ModMyi - Wiki

Contents

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 0x30, 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:

  1. Remove the 'CgBl' chunk
  2. Decompress the data using zlib with the windowSize set to a negative number
  3. Swap the red pixel with the blue pixel in the data
  4. Recompress the data using zlib using the default headers and CRC
  5. 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

You only need to make a small modification to be able to compile the code on Linux, here is the Makefile that I've tested on Kubuntu GNU/Linux 7.10, you'll need to have the libpng package installed, the zlib package is automatically installed.

LIBS_FIX=-lz
LIBS_FLIP=-lpng -lz -L/usr/local/lib
INCLUDES_FLIP=-I/usr/local/include
CC=gcc

all: fixpng flipchannels

clean:
        $(RM) -f fixpng flipchannels

fixpng: fixpng.c
        $(CC) -o fixpng fixpng.c $(LIBS_FIX)

flipchannels: flipchannels.c
        $(CC) -o flipchannels flipchannels.c $(LIBS_FLIP) $(INCLUDES_FLIP)

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/

atPurpose (http://www.atpurpose.com) have created 2 QuickLook plugins for Mac OS X that can preview PNG crushed images and PVR textures right from the Finder. They can be downloaded here http://atpeek.download.atpurpose.com/PngUncrush.zip and here http://atpeek.download.atpurpose.com/PvrUncrush.zip


Go to Top
ModMyI

All times are GMT -6. The time now is 12:41 PM. Powered by vBulletin® Version 3.8.6
If you need Dedicated Server Hosting, you should check out SingleHop. | Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Powered by vbWiki Pro 1.3 RC4. Copyright ©2006-2007, NuHit, LLC

Owned and Operated by ModMy, LLC Copyright © 2007-10. All rights reserved.

iPhone News / iPhone Forums / Apple News / Apple Forums / iPad News / iPad Forums / Cydia Hosting /
RSS / Contact Us / / Top