diff --git a/basis/images/png/authors.txt b/basis/images/png/authors.txt new file mode 100755 index 0000000000..7c1b2f2279 --- /dev/null +++ b/basis/images/png/authors.txt @@ -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 index 0000000000..0effa3c3d1 --- /dev/null +++ b/basis/images/png/png-tests.factor @@ -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 index 0000000000..0965a13ad6 --- /dev/null +++ b/basis/images/png/png.factor @@ -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 ) + + 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-info size>> ] bi stream-throws [ + + 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 index 0000000000..d34914a5ec Binary files /dev/null and b/basis/images/test-images/rgb.png differ