 io.binary io.encodings.ascii io.encodings.binary
 io.encodings.string io.encodings.utf8 io.files kernel math
 math.bitwise math.order math.parser pack prettyprint sequences
-strings math.vectors specialized-arrays.float ;
+strings math.vectors specialized-arrays.float locals ;
 IN: images.tiff
 
 TUPLE: tiff-image < image ;

software date-time photoshop exif-ifd sub-ifd inter-color-profile
xmp iptc fill-order document-name page-number page-name
x-position y-position host-computer copyright artist
min-sample-value max-sample-value make model cell-width cell-length
gray-response-unit gray-response-curve color-map threshholding
image-description free-offsets free-byte-counts tile-width tile-length
matteing data-type image-depth tile-depth

        ] with-tiff-endianness
    ] with-file-reader ;

: process-chunky-ifd ( ifd -- )
    read-strips
    uncompress-strips
    strips>bitmap
    fix-bitmap-endianness
    strips-predictor
    dup extra-samples tag? [ handle-alpha-data ] when
    drop ;

: process-planar-ifd ( ifd -- )
    "planar ifd not supported" throw ;

: dispatch-planar-configuration ( ifd planar-configuration -- )
    {
        { planar-configuration-chunky [ process-chunky-ifd ] }
        { planar-configuration-planar [ process-planar-ifd ] }
    } case ;

: process-ifd ( ifd -- )
    dup planar-configuration find-tag* [
        dispatch-planar-configuration
    ] [
        drop "no planar configuration" throw
    ] if ;

: process-tif-ifds ( parsed-tiff -- )
    ifds>> [ process-ifd ] each ;

: load-tiff ( path -- parsed-tiff )
    [ load-tiff-ifds dup ] keep
    binary [
        [ process-tif-ifds ] with-tiff-endianness
    ] with-file-reader ;

! tiff files can store several images -- we just take the first for now
M: tiff-image load-image* ( path tiff-image -- image )
