                 [ drop 0 write4 ]
 
                 ! rgb-quads
-                [ bitmap>> bitmap>color-index write ]
+                [
+                    [ bitmap>> bitmap>color-index ] [ dim>> first ] bi
+                    reverse-lines write
+                ]
             } cleave
         ] bi
     ] with-file-writer ;


ERROR: bmp-not-supported n ;

: raw-bitmap>seq ( bitmap -- array )
    dup bit-count>>
    {
        { 32 [ color-index>> ] }
        { 24 [ color-index>> ] }
        { 8 [ 8bit>buffer ] }
        [ bmp-not-supported ]
    } case >byte-array ;

: parse-file-header ( bitmap -- bitmap )
    2 read "BM" assert-sequence=
    read4 >>size
    read4 >>reserved
    read4 >>offset ;

: parse-bitmap-header ( bitmap -- bitmap )
    read4 >>header-length
    read4 >>width
    read4 >>height


M: bitmap-image load-image* ( path loading-bitmap -- bitmap )
    drop loading-bitmap new
    load-bitmap-data
    loading-bitmap>bitmap-image ;

PRIVATE>

