ios - Find out if PNG is 8 or 24 bits and alpha -


given uiimage/nsimage or nsdata instance, how find out programatically if png 8 bits or 24 bits? , if has alpha channel?

is there cocoa/cocoa touch api helps this?

to avoid duplication, here non-programatic answer question, , here's way find out if image png.

as person programmed long time on j2me platform, know png format well. if have raw data nsdata instance, looking information easy since png format straightforward.

see png specification

the png file starts signature , contains sequence of chunks. interested in first chunk, ihdr.

so, code should along lines of:

  1. skip first 8 bytes (signature)
  2. skip 4 bytes (ihdr chunk length)
  3. skip 4 bytes (ihdr chunk type = "ihdr")
  4. read ihdr

width: 4 bytes
height: 4 bytes
bit depth: 1 byte
color type: 1 byte
compression method: 1 byte
filter method: 1 byte
interlace method: 1 byte

for alpha, should check if there trns (transparency) chunk in file. find chunk, can go following algorithm:

  1. read chunk length (4 bytes)
  2. read chunk type (4 bytes)
  3. check chunk type whether type looking for
  4. skip chunk length bytes
  5. skip 4 bytes of crc
  6. repeat

edit:

to find info uiimage instance, cgimage , use 1 of cgimageget... functions.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -