]> gitweb.factorcode.org Git - factor.git/commitdiff
initial support for parsing in png files and a test image
authorDoug Coleman <doug.coleman@gmail.com>
Tue, 3 Mar 2009 22:24:41 +0000 (16:24 -0600)
committerDoug Coleman <doug.coleman@gmail.com>
Tue, 3 Mar 2009 22:24:41 +0000 (16:24 -0600)
basis/images/png/authors.txt [new file with mode: 0755]
basis/images/png/png-tests.factor [new file with mode: 0755]
basis/images/png/png.factor [new file with mode: 0755]
basis/images/test-images/rgb.png [new file with mode: 0755]

diff --git a/basis/images/png/authors.txt b/basis/images/png/authors.txt
new file mode 100755 (executable)
index 0000000..7c1b2f2
--- /dev/null
@@ -0,0 +1 @@
+Doug Coleman
diff --git a/basis/images/png/png-tests.factor b/basis/images/png/png-tests.factor
new file mode 100755 (executable)
index 0000000..0effa3c
--- /dev/null
@@ -0,0 +1,7 @@
+! Copyright (C) 2009 Doug Coleman.
+! See http://factorcode.org/license.txt for BSD license.
+USING: tools.test images.png ;
+IN: images.png.tests
+
+: png-test-path ( -- path )
+    "vocab:images/test-images/rgb.png" ;
\ No newline at end of file
diff --git a/basis/images/png/png.factor b/basis/images/png/png.factor
new file mode 100755 (executable)
index 0000000..0965a13
--- /dev/null
@@ -0,0 +1,41 @@
+! Copyright (C) 2009 Doug Coleman.
+! See http://factorcode.org/license.txt for BSD license.
+USING: accessors constructors images io io.binary io.encodings.ascii
+io.encodings.binary io.encodings.string io.files io.files.info kernel
+sequences io.streams.limited ;
+IN: images.png
+
+TUPLE: png-image < image chunks ;
+
+CONSTRUCTOR: png-image ( -- image )
+V{ } clone >>chunks ;
+
+TUPLE: png-chunk length type data crc ;
+
+CONSTRUCTOR: png-chunk ( -- png-chunk ) ;
+
+CONSTANT: png-header B{ HEX: 89 HEX: 50 HEX: 4e HEX: 47 HEX: 0d HEX: 0a HEX: 1a HEX: 0a }
+
+ERROR: bad-png-header header ;
+
+: read-png-header ( -- )
+    8 read dup png-header sequence= [
+        bad-png-header
+    ] unless drop ;
+
+: read-png-chunks ( image -- image )
+    <png-chunk>
+    4 read be> >>length
+    4 read ascii decode >>type
+    dup length>> read >>data
+    4 read >>crc
+    [ over chunks>> push ] 
+    [ type>> ] bi "IEND" =
+    [ read-png-chunks ] unless ;
+
+: load-png ( path -- image )
+    [ binary <file-reader> ] [ file-info size>> ] bi stream-throws <limited-stream> [
+        <png-image>
+        read-png-header
+        read-png-chunks
+    ] with-input-stream ;
diff --git a/basis/images/test-images/rgb.png b/basis/images/test-images/rgb.png
new file mode 100755 (executable)
index 0000000..d34914a
Binary files /dev/null and b/basis/images/test-images/rgb.png differ