 : tiff>image ( image -- image )
     ifds>> [ ifd>image ] map first ;
 
-: load-tiff ( path -- parsed-tiff )
+: with-tiff-endianness ( parsed-tiff quot -- )
+    [ dup endianness>> ] dip with-endianness ; inline
+
+: load-tiff-ifds ( path -- parsed-tiff )
     binary [
         <parsed-tiff>
-        read-header dup endianness>> [
-            read-ifds
-            dup ifds>> [
-                process-ifd read-strips
-                uncompress-strips
-                strips>bitmap
-                fix-bitmap-endianness
-                strips-predictor
-                dup extra-samples tag? [ handle-alpha-data ] when
-                drop
-            ] each
-        ] with-endianness
+        read-header [
+            dup ifd-offset>> read-ifds
+            process-ifds
+        ] with-tiff-endianness
     ] with-file-reader ;
+
+: process-tif-ifds ( parsed-tiff -- parsed-tiff )
+    dup ifds>> [
+        read-strips
+        uncompress-strips
+        strips>bitmap
+        fix-bitmap-endianness
+        strips-predictor
+        dup extra-samples tag? [ handle-alpha-data ] when
+        drop
+    ] each ;
+
+: load-tiff ( path -- parsed-tiff )
+    [ load-tiff-ifds ] [
+        binary [
+            [ process-tif-ifds ] with-tiff-endianness
+        ] with-file-reader
+    ] bi ;
 
 ! tiff files can store several images -- we just take the first for now
 M: tiff-image load-image* ( path tiff-image -- image )

photometric-interpretation-white-is-zero
photometric-interpretation-black-is-zero
photometric-interpretation-rgb
photometric-interpretation-palette-color ;
ERROR: bad-photometric-interpretation n ;
: lookup-photometric-interpretation ( n -- singleton )
    {

: lookup-sample-format ( sequence -- object )
    [
        {
            { 0 [ sample-format-none ] }
            { 1 [ sample-format-unsigned-integer ] }
            { 2 [ sample-format-signed-integer ] }
            { 3 [ sample-format-ieee-float ] }
