最終更新:2014-04-23 (水) 10:32:39 (2059d)  

PNG はてなブックマークを見る
Top / PNG

Portable Network Graphics

最適化

iPhone/iOS

なんかiPhoneのリソース用のPNGは形式が変なので他の用途に使うには変換する必要がある。

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:

  • 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