From 1f222697b3a1a6dd4b6f91335bb04f87d65ffe01 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sat, 26 Sep 2009 20:09:16 -0500 Subject: [PATCH 1/3] move the png filtering code to images.png --- basis/compression/inflate/inflate.factor | 42 +++--------------------- basis/images/png/png.factor | 35 +++++++++++++++++--- 2 files changed, 35 insertions(+), 42 deletions(-) diff --git a/basis/compression/inflate/inflate.factor b/basis/compression/inflate/inflate.factor index fa3f4d1284..26b851cc1e 100644 --- a/basis/compression/inflate/inflate.factor +++ b/basis/compression/inflate/inflate.factor @@ -1,10 +1,9 @@ ! Copyright (C) 2009 Marc Fauconneau. ! See http://factorcode.org/license.txt for BSD license. -USING: accessors arrays assocs byte-arrays -byte-vectors combinators fry grouping hashtables -compression.huffman images io.binary kernel locals -math math.bitwise math.order math.ranges multiline sequences -sorting ; +USING: accessors arrays assocs byte-vectors combinators +compression.huffman fry hashtables io.binary kernel locals math +math.bitwise math.order math.ranges sequences sorting ; +QUALIFIED-WITH: bitstreams bs IN: compression.inflate QUALIFIED-WITH: bitstreams bs @@ -177,42 +176,9 @@ CONSTANT: dist-table case ] [ produce ] keep call suffix concat ; - - ! [ produce ] keep dip swap suffix - -:: paeth ( a b c -- p ) - a b + c - { a b c } [ [ - abs ] keep 2array ] with map - sort-keys first second ; - -:: png-unfilter-line ( prev curr filter -- curr' ) - prev :> c - prev 3 tail-slice :> b - curr :> a - curr 3 tail-slice :> x - x length [0,b) - filter { - { 0 [ drop ] } - { 1 [ [| n | n x nth n a nth + 256 wrap n x set-nth ] each ] } - { 2 [ [| n | n x nth n b nth + 256 wrap n x set-nth ] each ] } - { 3 [ [| n | n x nth n a nth n b nth + 2/ + 256 wrap n x set-nth ] each ] } - { 4 [ [| n | n x nth n a nth n b nth n c nth paeth + 256 wrap n x set-nth ] each ] } - } case - curr 3 tail ; PRIVATE> -: reverse-png-filter' ( lines -- byte-array ) - [ first ] [ 1 tail ] [ map ] bi-curry@ bi nip - concat [ 128 + ] B{ } map-as ; - -: reverse-png-filter ( lines -- byte-array ) - dup first length 0 prefix - [ { 0 0 } prepend ] map - 2 clump [ - first2 dup [ third ] [ [ 0 2 ] dip set-nth ] bi - png-unfilter-line - ] map B{ } concat-as ; - : zlib-inflate ( bytes -- bytes ) bs: [ check-zlib-header ] [ inflate-loop ] bi diff --git a/basis/images/png/png.factor b/basis/images/png/png.factor index 8dde02687d..d0a001f3f6 100755 --- a/basis/images/png/png.factor +++ b/basis/images/png/png.factor @@ -1,9 +1,9 @@ ! Copyright (C) 2009 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: accessors images io io.binary io.encodings.ascii -io.encodings.binary io.encodings.string io.files io.files.info kernel -sequences io.streams.limited fry combinators arrays math checksums -checksums.crc32 compression.inflate grouping byte-arrays images.loader ; +USING: accessors arrays checksums checksums.crc32 combinators +compression.inflate fry grouping images images.loader io +io.binary io.encodings.ascii io.encodings.string kernel locals +math math.bitwise math.ranges sequences sorting ; IN: images.png SINGLETON: png-image @@ -90,6 +90,33 @@ ERROR: unknown-filter-method image ; [ unknown-color-type ] } case ; +:: paeth ( a b c -- p ) + a b + c - { a b c } [ [ - abs ] keep 2array ] with map + sort-keys first second ; + +:: png-unfilter-line ( prev curr filter -- curr' ) + prev :> c + prev 3 tail-slice :> b + curr :> a + curr 3 tail-slice :> x + x length [0,b) + filter { + { filter-none [ drop ] } + { filter-sub [ [| n | n x nth n a nth + 256 wrap n x set-nth ] each ] } + { filter-up [ [| n | n x nth n b nth + 256 wrap n x set-nth ] each ] } + { filter-average [ [| n | n x nth n a nth n b nth + 2/ + 256 wrap n x set-nth ] each ] } + { filter-paeth [ [| n | n x nth n a nth n b nth n c nth paeth + 256 wrap n x set-nth ] each ] } + } case + curr 3 tail ; + +: reverse-png-filter ( lines -- byte-array ) + dup first length 0 prefix + [ { 0 0 } prepend ] map + 2 clump [ + first2 dup [ third ] [ [ 0 2 ] dip set-nth ] bi + png-unfilter-line + ] map B{ } concat-as ; + : filter-png ( groups loading-png -- byte-array ) filter-method>> { { filter-none [ reverse-png-filter ] } From 3c2d85c4076ee76e03bea933ab97e0761b992070 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sat, 26 Sep 2009 20:34:10 -0500 Subject: [PATCH 2/3] remove some misguided code,oops --- basis/images/png/png.factor | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/basis/images/png/png.factor b/basis/images/png/png.factor index d0a001f3f6..99228f58e1 100755 --- a/basis/images/png/png.factor +++ b/basis/images/png/png.factor @@ -78,7 +78,6 @@ ERROR: bad-checksum ; ERROR: unknown-color-type n ; ERROR: unimplemented-color-type image ; -ERROR: unknown-filter-method image ; : inflate-data ( loading-png -- bytes ) find-compressed-bytes zlib-inflate ; @@ -117,15 +116,8 @@ ERROR: unknown-filter-method image ; png-unfilter-line ] map B{ } concat-as ; -: filter-png ( groups loading-png -- byte-array ) - filter-method>> { - { filter-none [ reverse-png-filter ] } - [ unknown-filter-method ] - } case ; - : png-image-bytes ( loading-png -- byte-array ) - [ [ inflate-data ] [ png-group-width ] bi group ] - [ filter-png ] bi ; + [ inflate-data ] [ png-group-width ] bi group reverse-png-filter ; : decode-greyscale ( loading-png -- loading-png ) unimplemented-color-type ; From 3dc8834704cce032ce973610f27a59d06780d7ef Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sat, 26 Sep 2009 22:24:14 -0500 Subject: [PATCH 3/3] minor refactoring --- basis/images/png/png.factor | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/basis/images/png/png.factor b/basis/images/png/png.factor index 99228f58e1..7e8f69d555 100755 --- a/basis/images/png/png.factor +++ b/basis/images/png/png.factor @@ -82,17 +82,23 @@ ERROR: unimplemented-color-type image ; : inflate-data ( loading-png -- bytes ) find-compressed-bytes zlib-inflate ; -: png-group-width ( loading-png -- n ) +: scale-bit-depth ( loading-png -- n ) bit-depth>> 8 / ; inline + +: png-bytes-per-pixel ( loading-png -- n ) dup color-type>> { - { 2 [ [ bit-depth>> 8 / 3 * ] [ width>> ] bi * 1 + ] } - { 6 [ [ bit-depth>> 8 / 4 * ] [ width>> ] bi * 1 + ] } + { 2 [ scale-bit-depth 3 * ] } + { 6 [ scale-bit-depth 4 * ] } [ unknown-color-type ] - } case ; + } case ; inline + +: png-group-width ( loading-png -- n ) + ! 1 + is for the filter type, 1 byte preceding each line + [ png-bytes-per-pixel ] [ width>> ] bi * 1 + ; :: paeth ( a b c -- p ) a b + c - { a b c } [ [ - abs ] keep 2array ] with map sort-keys first second ; - + :: png-unfilter-line ( prev curr filter -- curr' ) prev :> c prev 3 tail-slice :> b