Last active
October 30, 2020 23:43
-
-
Save Oldes/2c7960bc4b1093330d10fac8e1547bf3 to your computer and use it in GitHub Desktop.
Read PNG size from raw binary data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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