diff --git a/extra/graphics/tiff/tiff.factor b/extra/graphics/tiff/tiff.factor index 4676ea2748..34f6c3e4e0 100755 --- a/extra/graphics/tiff/tiff.factor +++ b/extra/graphics/tiff/tiff.factor @@ -1,15 +1,28 @@ ! Copyright (C) 2009 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. USING: accessors combinators io io.encodings.binary io.files -kernel pack endian ; +kernel pack endian tools.hexdump constructors sequences arrays +sorting.slots math.order ; IN: graphics.tiff TUPLE: tiff endianness the-answer ifd-offset +ifds ; +CONSTRUCTOR: tiff ( -- tiff ) + V{ } clone >>ifds ; + +TUPLE: ifd count ifd-entries ; + +CONSTRUCTOR: ifd ( count ifd-entries -- ifd ) ; + +TUPLE: ifd-entry tag type count offset ; + +CONSTRUCTOR: ifd-entry ( tag type count offset -- ifd-entry ) ; + ERROR: bad-tiff-magic bytes ; @@ -20,6 +33,9 @@ ERROR: bad-tiff-magic bytes ; [ bad-tiff-magic ] } case ; +: with-tiff-endianness ( tiff quot -- tiff ) + [ dup endianness>> ] dip with-endianness ; inline + : read-header ( tiff -- tiff ) 2 read tiff-endianness [ >>endianness ] keep [ @@ -27,10 +43,27 @@ ERROR: bad-tiff-magic bytes ; 4 read endian> >>ifd-offset ] with-endianness ; +: push-ifd ( tiff ifd -- tiff ) + over ifds>> push ; + +: read-ifd ( -- ifd ) + 2 read endian> + 2 read endian> + 4 read endian> + 4 read endian> ; + +: read-ifds ( tiff -- tiff ) + [ + dup ifd-offset>> seek + 2 read endian> + dup [ read-ifd ] replicate >>ifds + ] with-tiff-endianness ; + : (load-tiff) ( path -- tiff ) binary [ tiff new read-header + read-ifds ] with-file-reader ; : load-tiff ( path -- tiff )