 CONSTANT: comment-extension HEX: fe
 CONSTANT: application-extension HEX: ff
 CONSTANT: trailer HEX: 3b
+CONSTANT: graphic-control-extension-block-size HEX: 04
+CONSTANT: block-terminator HEX: 00
 
 : <loading-gif> ( -- loading-gif )
     \ loading-gif new

    [ compressed-bytes>> ] bi
    lzw-uncompress ;

: apply-palette ( indexes palette -- bitmap )
    [ nth 255 suffix ] curry V{ } map-as concat ;

: dimensions ( loading-gif -- dim )
    [ image-descriptor>> width>> ] [ image-descriptor>> height>> ] bi 2array ;

: loading-gif>image ( loading-gif -- image )
    [ <image> ] dip
    [ dimensions >>dim ]
    [ drop RGBA >>component-order ubyte-components >>component-type ]
    [
        [ decompress ] [ global-color-table>> ] bi
        apply-palette >>bitmap
    ] tri ;


ERROR: gif-unexpected-eof ;

TUPLE: graphics-control-extension
flags delay-time transparent-color-index ;

TUPLE: image-descriptor
left top width height flags first-code-size ;
