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.
the png file starts signature , contains sequence of chunks. interested in first chunk, ihdr
.
so, code should along lines of:
- skip first 8 bytes (signature)
- skip 4 bytes (ihdr chunk length)
- skip 4 bytes (ihdr chunk type = "ihdr")
- 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:
- read chunk length (4 bytes)
- read chunk type (4 bytes)
- check chunk type whether type looking for
- skip chunk length bytes
- skip 4 bytes of crc
- repeat
edit:
to find info uiimage
instance, cgimage
, use 1 of cgimageget...
functions.
Comments
Post a Comment