1 ! Copyright (C) 2009 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors constructors images io io.binary io.encodings.ascii
4 io.encodings.binary io.encodings.string io.files io.files.info kernel
5 sequences io.streams.limited ;
8 TUPLE: png-image < image chunks ;
10 CONSTRUCTOR: png-image ( -- image )
13 TUPLE: png-chunk length type data crc ;
15 CONSTRUCTOR: png-chunk ( -- png-chunk ) ;
17 CONSTANT: png-header B{ HEX: 89 HEX: 50 HEX: 4e HEX: 47 HEX: 0d HEX: 0a HEX: 1a HEX: 0a }
19 ERROR: bad-png-header header ;
21 : read-png-header ( -- )
22 8 read dup png-header sequence= [
26 : read-png-chunks ( image -- image )
29 4 read ascii decode >>type
30 dup length>> read >>data
32 [ over chunks>> push ]
33 [ type>> ] bi "IEND" =
34 [ read-png-chunks ] unless ;
36 : load-png ( path -- image )
37 [ binary <file-reader> ] [ file-info size>> ] bi stream-throws <limited-stream> [