]> gitweb.factorcode.org Git - factor.git/blob - basis/images/png/png.factor
Fix conflict in images vocab
[factor.git] / basis / images / png / png.factor
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 ;
6 IN: images.png
7
8 TUPLE: png-image < image chunks ;
9
10 CONSTRUCTOR: png-image ( -- image )
11 V{ } clone >>chunks ;
12
13 TUPLE: png-chunk length type data crc ;
14
15 CONSTRUCTOR: png-chunk ( -- png-chunk ) ;
16
17 CONSTANT: png-header B{ HEX: 89 HEX: 50 HEX: 4e HEX: 47 HEX: 0d HEX: 0a HEX: 1a HEX: 0a }
18
19 ERROR: bad-png-header header ;
20
21 : read-png-header ( -- )
22     8 read dup png-header sequence= [
23         bad-png-header
24     ] unless drop ;
25
26 : read-png-chunks ( image -- image )
27     <png-chunk>
28     4 read be> >>length
29     4 read ascii decode >>type
30     dup length>> read >>data
31     4 read >>crc
32     [ over chunks>> push ] 
33     [ type>> ] bi "IEND" =
34     [ read-png-chunks ] unless ;
35
36 : load-png ( path -- image )
37     [ binary <file-reader> ] [ file-info size>> ] bi stream-throws <limited-stream> [
38         <png-image>
39         read-png-header
40         read-png-chunks
41     ] with-input-stream ;