 ! See http://factorcode.org/license.txt for BSD license.
 USING: accessors combinators io io.encodings.binary io.files
 kernel pack endian tools.hexdump constructors sequences arrays
-sorting.slots math.order math.parser prettyprint ;
+sorting.slots math.order math.parser prettyprint classes ;
 IN: graphics.tiff
 
 TUPLE: tiff
 endianness
 the-answer
 ifd-offset
-ifds
-processed-ifds ;
+ifds ;
 
 CONSTRUCTOR: tiff ( -- tiff )
     V{ } clone >>ifds ;
 
-TUPLE: ifd count ifd-entries next ;
+TUPLE: ifd count ifd-entries next processed-tags strips ;
 
 CONSTRUCTOR: ifd ( count ifd-entries next -- ifd ) ;
 

        [ unhandled-ifd-entry swap 2array ]
    } case ;

: process-ifd ( ifd -- processed-ifd )
    ifd-entries>> [ process-ifd-entry ] map ;

: (load-tiff) ( path -- tiff )
    binary [
        <tiff>
        read-header
        read-ifds
        dup ifds>> [ process-ifd ] map
        >>processed-ifds
    ] with-file-reader ;

: load-tiff ( path -- tiff )
    (load-tiff) ;

        4 read endian>
        [ <ifd> push-ifd ] [ 0 = [ read-ifds ] unless ] bi
    ] with-tiff-endianness ;

: read-strips ( ifd -- ifd )
    dup processed-tags>>
    [ [ strip-byte-counts instance? ] find nip n>> ]
    [ [ strip-offsets instance? ] find nip n>> ] bi
    [ seek-absolute seek-input read ] { } 2map-as >>strips ;

! ERROR: unhandled-ifd-entry data n ;

