1 ! Copyright (C) 2007, 2009 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types arrays byte-arrays columns
4 combinators fry grouping io io.binary io.encodings.binary
5 io.files kernel libc macros math math.bitwise math.functions
6 namespaces opengl opengl.gl prettyprint sequences strings
7 summary ui ui.gadgets.panes images.backend ;
10 TUPLE: bitmap-image < image ;
12 ! Currently can only handle 24/32bit bitmaps.
13 ! Handles row-reversed bitmaps (their height is negative)
15 TUPLE: bitmap magic size reserved offset header-length width
16 height planes bit-count compression size-image
17 x-pels y-pels color-used color-important rgb-quads color-index
20 : array-copy ( bitmap array -- bitmap array' )
21 over size-image>> abs memory>byte-array ;
23 : 8bit>buffer ( bitmap -- array )
24 [ rgb-quads>> 4 <sliced-groups> [ 3 head-slice ] map ]
25 [ color-index>> >array ] bi [ swap nth ] with map concat ;
27 ERROR: bmp-not-supported n ;
29 : raw-bitmap>buffer ( bitmap -- array )
32 { 32 [ color-index>> ] }
33 { 24 [ color-index>> ] }
34 { 16 [ bmp-not-supported ] }
36 { 4 [ bmp-not-supported ] }
37 { 2 [ bmp-not-supported ] }
38 { 1 [ bmp-not-supported ] }
43 M: bitmap-magic summary
44 drop "First two bytes of bitmap stream must be 'BM'" ;
46 : read2 ( -- n ) 2 read le> ;
47 : read4 ( -- n ) 4 read le> ;
49 : parse-file-header ( bitmap -- bitmap )
50 2 read >string dup "BM" = [ bitmap-magic ] unless >>magic
55 : parse-bitmap-header ( bitmap -- bitmap )
66 read4 >>color-important ;
68 : rgb-quads-length ( bitmap -- n )
69 [ offset>> 14 - ] [ header-length>> ] bi - ;
71 : color-index-length ( bitmap -- n )
75 [ bit-count>> * 31 + 32 /i 4 * ]
79 : parse-bitmap ( bitmap -- bitmap )
80 dup rgb-quads-length read >>rgb-quads
81 dup color-index-length read >>color-index ;
83 : load-bitmap-data ( path -- bitmap )
86 parse-file-header parse-bitmap-header parse-bitmap
89 : process-bitmap-data ( bitmap -- bitmap )
90 dup raw-bitmap>buffer >>buffer ;
92 : load-bitmap ( path -- bitmap )
93 load-bitmap-data process-bitmap-data ;
95 ERROR: unknown-component-order bitmap ;
97 : bitmap>component-order ( bitmap -- object )
102 [ unknown-component-order ]
105 M: bitmap >image ( bitmap -- bitmap-image )
107 [ [ width>> ] [ height>> ] bi 2array ]
108 [ bitmap>component-order ]
110 } cleave bitmap-image new-image ;
112 M: bitmap-image load-image* ( path bitmap -- bitmap-image )
113 drop load-bitmap >image ;
115 MACRO: (nbits>bitmap) ( bits -- )
118 2over * _ * >>size-image
121 swap array-copy [ >>buffer ] [ >>color-index ] bi
125 : bgr>bitmap ( array height width -- bitmap )
128 : bgra>bitmap ( array height width -- bitmap )
131 : write2 ( n -- ) 2 >le write ;
132 : write4 ( n -- ) 4 >le write ;
134 : save-bitmap ( bitmap path -- )
136 B{ CHAR: B CHAR: M } write
138 buffer>> length 14 + 40 + write4
146 [ planes>> 1 or write2 ]
147 [ bit-count>> 24 or write2 ]
148 [ compression>> 0 or write4 ]
149 [ size-image>> write4 ]
150 [ x-pels>> 0 or write4 ]
151 [ y-pels>> 0 or write4 ]
152 [ color-used>> 0 or write4 ]
153 [ color-important>> 0 or write4 ]
154 [ rgb-quads>> write ]
155 [ color-index>> write ]