Skip to content

Instantly share code, notes, and snippets.

@Oldes
Last active October 30, 2020 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Oldes/2c7960bc4b1093330d10fac8e1547bf3 to your computer and use it in GitHub Desktop.
Save Oldes/2c7960bc4b1093330d10fac8e1547bf3 to your computer and use it in GitHub Desktop.
Read PNG size from raw binary data
Rebol [
name: png-size
needs: 3.2.0
usage: [
png-size http://avatars-04.gitter.im/gh/uv/4/oldes
]
]
png-size: func[
"Read PNG size from raw binary data"
bin [binary! file! url!]
][
;- there must be at least 24 bytes, but as IHDR does not
;- have to be the first chunk, it is safer to read more..
unless binary? bin [ bin: read/binary/part bin 256 ]
if all [
bin: find/match bin #{89504E470D0A1A0A} ;- PNG magic number
bin: find/tail bin #{49484452} ;- IHDR chunk
8 <= length? bin
][
to pair! binary/read bin [UI32 UI32]
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment