 TUPLE: loading-bitmap 
 size reserved offset header-length width
 height planes bit-count compression size-image
-x-pels y-pels color-used color-important rgb-quads color-index ;
+x-pels y-pels color-used color-important color-palette color-index
+uncompressed-bytes ;
 
 ERROR: bitmap-magic magic ;
 

: reverse-lines ( byte-array width -- byte-array )
    <sliced-groups> <reversed> concat ; inline

: raw-bitmap>seq ( loading-bitmap -- array )
    dup bit-count>>
    {
        { 32 [ color-index>> ] }

        { 8 [ [ 8bit>buffer ] [ width>> 3 * ] bi reverse-lines ] }
        [ bmp-not-supported ]
    } case >byte-array ;

ERROR: unsupported-bitmap-compression compression ;

: uncompress-bitmap ( loading-bitmap -- loading-bitmap' )
    dup compression>> {
        { 0 [ ] }
        { 1 [ "run-length encoding 8" unsupported-bitmap-compression ] }
        { 2 [ "run-length encoding 4" unsupported-bitmap-compression ] }
        { 3 [ "bitfields" unsupported-bitmap-compression ] }
        { 4 [ "jpeg" unsupported-bitmap-compression ] }
        { 5 [ "png" unsupported-bitmap-compression ] }
    } case ;

: loading-bitmap>bytes ( loading-bitmap -- byte-array )
    uncompress-bitmap bitmap>bytes ;

: parse-file-header ( loading-bitmap -- loading-bitmap )
    2 read "BM" assert-sequence=
