support reading 8bit bitmaps, 4bit is blocking on bit-streams

db4
Doug Coleman 2008-11-07 22:03:27 -06:00
parent a7595665fc
commit 6182a161db
1 changed files with 22 additions and 12 deletions

View File

@ -5,7 +5,7 @@ USING: alien arrays byte-arrays combinators summary
io.backend graphics.viewer io io.binary io.files kernel libc io.backend graphics.viewer io io.binary io.files kernel libc
math math.functions namespaces opengl opengl.gl prettyprint math math.functions namespaces opengl opengl.gl prettyprint
sequences strings ui ui.gadgets.panes io.encodings.binary sequences strings ui ui.gadgets.panes io.encodings.binary
accessors ; accessors grouping ;
IN: graphics.bitmap IN: graphics.bitmap
! Currently can only handle 24bit bitmaps. ! Currently can only handle 24bit bitmaps.
@ -23,16 +23,25 @@ TUPLE: bitmap magic size reserved offset header-length width
swap [ >>array ] [ >>color-index ] bi swap [ >>array ] [ >>color-index ] bi
24 >>bit-count ; 24 >>bit-count ;
: raw-bitmap>string ( str n -- str ) : 8bit>array ( bitmap -- array )
[ rgb-quads>> 4 <sliced-groups> [ 3 head-slice ] map ]
[ color-index>> >array ] bi [ swap nth ] with map concat ;
: 4bit>array ( bitmap -- array )
[ rgb-quads>> 4 <sliced-groups> [ 3 head-slice ] map ]
[ color-index>> >array ] bi [ swap nth ] with map concat ;
: raw-bitmap>array ( bitmap -- array )
dup bit-count>>
{ {
{ 32 [ "32bit" throw ] } { 32 [ "32bit" throw ] }
{ 24 [ ] } { 24 [ color-index>> ] }
{ 16 [ "16bit" throw ] } { 16 [ "16bit" throw ] }
{ 8 [ "8bit" throw ] } { 8 [ 8bit>array ] }
{ 4 [ "4bit" throw ] } { 4 [ 4bit>array ] }
{ 2 [ "2bit" throw ] } { 2 [ "2bit" throw ] }
{ 1 [ "1bit" throw ] } { 1 [ "1bit" throw ] }
} case ; } case >byte-array ;
ERROR: bitmap-magic ; ERROR: bitmap-magic ;
@ -72,13 +81,12 @@ M: bitmap-magic summary
: load-bitmap ( path -- bitmap ) : load-bitmap ( path -- bitmap )
normalize-path binary [ normalize-path binary [
T{ bitmap } clone bitmap new
dup parse-file-header dup parse-file-header
dup parse-bitmap-header dup parse-bitmap-header
dup parse-bitmap dup parse-bitmap
] with-file-reader ] with-file-reader
dup color-index>> over bit-count>> dup raw-bitmap>array >>array ;
raw-bitmap>string >byte-array >>array ;
: save-bitmap ( bitmap path -- ) : save-bitmap ( bitmap path -- )
binary [ binary [
@ -118,6 +126,8 @@ M: bitmap draw-image ( bitmap -- )
bit-count>> { bit-count>> {
! { 32 [ GL_BGRA GL_UNSIGNED_INT_8_8_8_8 ] } ! broken ! { 32 [ GL_BGRA GL_UNSIGNED_INT_8_8_8_8 ] } ! broken
{ 24 [ GL_BGR GL_UNSIGNED_BYTE ] } { 24 [ GL_BGR GL_UNSIGNED_BYTE ] }
{ 8 [ GL_BGR GL_UNSIGNED_BYTE ] }
{ 4 [ GL_BGR GL_UNSIGNED_BYTE ] }
} case } case
] keep array>> glDrawPixels ; ] keep array>> glDrawPixels ;