 
 <PRIVATE
 
-: <jpeg-image> ( headers bitstream -- image )
-    jpeg-image new swap >>bitstream swap >>headers ;
+: <loading-jpeg> ( headers bitstream -- image )
+    loading-jpeg new swap >>bitstream swap >>headers ;
 
 SINGLETONS: SOF DHT DAC RST SOI EOI SOS DQT DNL DRI DHP EXP
 APP JPG COM TEM RES ;


QUALIFIED-WITH: bitstreams bs

TUPLE: jpeg-image < image
    { headers }
    { bitstream }
    { color-info initial: { f f f f } }


ERROR: not-a-jpeg-image ;

: loading-jpeg>image ( loading-jpeg -- image )
    dup jpeg-image [
        baseline-parse
        baseline-decompress
    ] with-variable ;

: load-jpeg ( stream -- loading-jpeg )
    [
        parse-marker { SOI } = [ not-a-jpeg-image ] unless
        parse-headers
        unlimited-input contents <loading-jpeg>
    ] with-input-stream ;

PRIVATE>

M: jpeg-image stream>image ( stream jpeg-image -- bitmap )
    drop load-jpeg loading-jpeg>image ;
