1 ! Copyright (C) 2009 Doug Coleman.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: arrays combinators grouping kernel math math.matrices
4 math.order sequences sequences.parser ;
5 IN: compression.run-length
7 : run-length-uncompress ( byte-array -- byte-array' )
8 2 group [ first2 <array> ] map B{ } concat-as ;
10 : 8hi-lo ( byte -- hi lo )
11 [ 0xf0 bitand -4 shift ] [ 0xf bitand ] bi ; inline
13 :: run-length-uncompress-bitmap4 ( byte-array m n -- byte-array' )
14 byte-array <sequence-parser> :> sp
15 m 1 + n <zero-matrix> :> matrix
21 ! i j [ number>string ] bi@ " " glue .
23 sp consume dup 0x03 0xff between? [
24 nip [ sp ] dip dup odd?
25 [ 1 + take-n but-last ] [ take-n ] if
26 [ j matrix i swap nth copy ] [ length j + j! ] bi
29 { 0 [ i 1 + i! 0 j! ] }
31 { 2 [ sp consume j + j!
36 [ sp consume 8hi-lo 2array <repetition> concat ] [ head ] bi
37 [ j matrix i swap nth copy ] [ length j + j! ] bi
40 ! j stride >= [ i 1 + i! 0 j! ] when
41 j stride >= [ 0 j! ] when
44 matrix B{ } concat-as ;
46 :: run-length-uncompress-bitmap8 ( byte-array m n -- byte-array' )
47 byte-array <sequence-parser> :> sp
48 m 1 + n <zero-matrix> :> matrix
54 ! i j [ number>string ] bi@ " " glue .
56 sp consume dup 0x03 0xff between? [
57 nip [ sp ] dip dup odd?
58 [ 1 + take-n but-last ] [ take-n ] if
59 [ j matrix i swap nth copy ] [ length j + j! ] bi
62 { 0 [ i 1 + i! 0 j! ] }
64 { 2 [ sp consume j + j!
69 sp consume <array> [ j matrix i swap nth copy ] [ length j + j! ] bi
72 ! j stride >= [ i 1 + i! 0 j! ] when
73 j stride >= [ 0 j! ] when
76 matrix B{ } concat-as ;