 : validate-truecolor-alpha ( loading-png -- loading-png )
     { 8 16 } validate-bit-depth ;
 
-: decode-png ( loading-png -- loading-png ) 
+: png>image ( loading-png -- image )
     dup color-type>> {
         { greyscale [ validate-greyscale decode-greyscale ] }
         { truecolor [ validate-truecolor decode-truecolor ] }

        [ unknown-color-type ]
    } case ;

M: png-image stream>image
    drop [
        <loading-png>
        read-png-header
        read-png-chunks
        parse-ihdr-chunk
        decode-png
    ] with-input-stream ;

: png-image-bytes ( loading-png -- byte-array )
    [ inflate-data ] [ png-group-width ] bi group reverse-png-filter ;

: decode-greyscale ( loading-png -- image )
    unimplemented-color-type ;

: decode-truecolor ( loading-png -- image )
    [ <image> ] dip {
        [ png-image-bytes >>bitmap ]
        [ [ width>> ] [ height>> ] bi 2array >>dim ]
        [ drop RGB >>component-order ubyte-components >>component-type ]
    } cleave ;
    
: decode-indexed-color ( loading-png -- image )
    unimplemented-color-type ;

: decode-greyscale-alpha ( loading-png -- image )
    unimplemented-color-type ;

: decode-truecolor-alpha ( loading-png -- image )
    [ <image> ] dip {
        [ png-image-bytes >>bitmap ]
        [ [ width>> ] [ height>> ] bi 2array >>dim ]
