]> gitweb.factorcode.org Git - factor.git/blob - extra/tools/image-analyzer/utils/utils.factor
tools.image-analyzer: new set of vocabs for reading and analyzing factor images
[factor.git] / extra / tools / image-analyzer / utils / utils.factor
1 USING: accessors alien.c-types alien.data arrays bit-arrays classes
2 continuations destructors fry io io.streams.throwing kernel locals
3 math namespaces sequences ;
4 IN: tools.image-analyzer.utils
5
6 : class-heap-size ( instance -- n )
7     class-of heap-size ;
8
9 : read-bytes>array ( nbytes type -- seq )
10     [ read ] dip cast-array >array ;
11
12 : read-array ( count type -- seq )
13     [ heap-size * ] keep read-bytes>array ;
14
15 : byte-array>bit-array ( byte-array -- bit-array )
16     [ integer>bit-array 8 f pad-tail ] { } map-as concat ;
17
18 : until-eof-reader ( reader-quot -- reader-quot' )
19     '[
20         [ [ @ ] throw-on-eof ] [
21             dup stream-exhausted? [ drop f ] [ throw ] if
22         ] recover
23     ] ; inline
24
25 : save-io-excursion ( quot -- )
26     tell-input '[ _ seek-absolute seek-input ] [ ] cleanup ; inline
27
28 : consume-stream>sequence ( reader-quot: ( -- item )  -- seq )
29     until-eof-reader '[ drop @ ] t swap follow rest ; inline
30
31 TUPLE: backwards-reader stream ;
32
33 M: backwards-reader dispose stream>> dispose ;
34
35 M: backwards-reader stream-element-type
36     stream>> stream-element-type ;
37
38 M: backwards-reader stream-length
39     stream>> stream-length ;
40
41 : backwards-seek ( ofs -- )
42     dup 0 < [ seek-end ] [ seek-absolute ] if seek-input ;
43
44 M:: backwards-reader stream-read-unsafe ( n buf stream -- count )
45     stream stream>> [
46         tell-input n + :> pos-after
47         pos-after neg backwards-seek
48         n buf input-stream get stream-read-unsafe
49         pos-after backwards-seek
50     ] with-input-stream* ;
51
52 : <backwards-reader> ( stream -- stream' )
53     backwards-reader boa ;