         [ unhandled-ifd-entry swap 2array ]
     } case ;
 
-: process-ifd ( ifd -- processed-ifd )
-    ifd-entries>> [ process-ifd-entry ] map ;
+: process-ifd ( ifd -- ifd )
+    dup ifd-entries>> [ process-ifd-entry ] map >>processed-tags ;
 
 : (load-tiff) ( path -- tiff )
     binary [
         <tiff>
         read-header
         read-ifds
-        dup ifds>> [ process-ifd ] map
-        >>processed-ifds
+        dup ifds>> [ process-ifd read-strips drop ] each
     ] with-file-reader ;
 
 : load-tiff ( path -- tiff )
     (load-tiff) ;
+
+! TODO: duplicate ifds = error, seeking out of bounds = error

! 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 ;
IN: graphics.tiff

TUPLE: tiff
endianness
the-answer
ifd-offset
ifds
processed-ifds ;

CONSTRUCTOR: tiff ( -- tiff )
    V{ } clone >>ifds ;

TUPLE: ifd count ifd-entries next ;

CONSTRUCTOR: ifd ( count ifd-entries next -- ifd ) ;


        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 ;

