TUPLE: lzw input output table code old-code initial-code-size code-size ;
-SYMBOL: table-full
-
: initial-uncompress-table ( -- seq )
end-of-information get 1 + iota [ 1vector ] V{ } map-as ;
initial-uncompress-table >>table
dup initial-code-size>> >>code-size ;
-: <lzw-uncompress> ( code-size input -- obj )
+: <lzw-uncompress> ( input code-size -- obj )
lzw new
- swap >>input
swap >>initial-code-size
dup initial-code-size>> >>code-size
+ swap >>input
BV{ } clone >>output
reset-lzw-uncompress ;
drop
] if* ;
-: register-special-codes ( first-code-size -- )
+: register-special-codes ( first-code-size -- first-code-size )
[
1 - 2^ dup clear-code set
1 + end-of-information set
] keep ;
-: lzw-uncompress ( code-size seq -- byte-array )
- [ register-special-codes ] dip
- bs:<lsb0-bit-reader>
+: lzw-uncompress ( bitstream code-size -- byte-array )
+ register-special-codes
<lzw-uncompress>
[ lzw-uncompress-char ] [ output>> ] bi ;
+
+: lzw-uncompress-msb0 ( seq code-size -- byte-array )
+ [ bs:<msb0-bit-reader> ] dip lzw-uncompress ;
+
+: lzw-uncompress-lsb0 ( seq code-size -- byte-array )
+ [ bs:<lsb0-bit-reader> ] dip lzw-uncompress ;
[ 2 ] [ gif-example3 declared-num-colors ] unit-test
: >index-stream ( gif -- seq )
- [ image-descriptor>> first-code-size>> ]
- [ compressed-bytes>> ] bi
- lzw-uncompress ;
+ [ compressed-bytes>> ]
+ [ image-descriptor>> first-code-size>> ] bi
+ lzw-uncompress-lsb0 ;
[
BV{
] with-input-stream ;
: decompress ( loading-gif -- indexes )
- [ image-descriptor>> first-code-size>> ]
- [ compressed-bytes>> ] bi
- lzw-uncompress ;
+ [ compressed-bytes>> ]
+ [ image-descriptor>> first-code-size>> ] bi
+ lzw-uncompress-lsb0 ;
: colorize ( index palette transparent-index/f -- seq )
pick = [ 2drop B{ 0 0 0 0 } ] [ nth 255 suffix ] if ;