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
21 : array-copy ( bitmap array -- bitmap array' )
22 over size-image>> abs memory>byte-array ;
24 MACRO: (nbits>bitmap) ( bits -- )
27 2over * _ * >>size-image
30 swap array-copy [ >>buffer ] [ >>color-index ] bi
34 : bgr>bitmap ( array height width -- bitmap )
37 : bgra>bitmap ( array height width -- bitmap )
40 : 8bit>buffer ( bitmap -- array )
41 [ rgb-quads>> 4 <sliced-groups> [ 3 head-slice ] map ]
42 [ color-index>> >array ] bi [ swap nth ] with map concat ;
44 ERROR: bmp-not-supported n ;
46 : raw-bitmap>buffer ( bitmap -- array )
49 { 32 [ color-index>> ] }
50 { 24 [ color-index>> ] }
51 { 16 [ bmp-not-supported ] }
53 { 4 [ bmp-not-supported ] }
54 { 2 [ bmp-not-supported ] }
55 { 1 [ bmp-not-supported ] }
60 M: bitmap-magic summary
61 drop "First two bytes of bitmap stream must be 'BM'" ;
63 : read2 ( -- n ) 2 read le> ;
64 : read4 ( -- n ) 4 read le> ;
66 : parse-file-header ( bitmap -- bitmap )
67 2 read >string dup "BM" = [ bitmap-magic ] unless >>magic
72 : parse-bitmap-header ( bitmap -- bitmap )
83 read4 >>color-important ;
85 : rgb-quads-length ( bitmap -- n )
86 [ offset>> 14 - ] [ header-length>> ] bi - ;
88 : color-index-length ( bitmap -- n )
92 [ bit-count>> * 31 + 32 /i 4 * ]
96 : parse-bitmap ( bitmap -- bitmap )
97 dup rgb-quads-length read >>rgb-quads
98 dup color-index-length read >>color-index ;
100 : load-bitmap-data ( path -- bitmap )
103 parse-file-header parse-bitmap-header parse-bitmap
106 : alpha-channel-zero? ( bitmap -- ? )
107 buffer>> 4 <sliced-groups> 3 <column> [ 0 = ] all? ;
109 : process-bitmap-data ( bitmap -- bitmap )
110 dup raw-bitmap>buffer >>buffer
111 dup alpha-channel-zero? >>alpha-channel-zero? ;
113 : load-bitmap ( path -- bitmap )
114 load-bitmap-data process-bitmap-data ;
116 : bitmap>image ( bitmap -- bitmap-image )
117 { [ width>> ] [ height>> ] [ bit-count>> ] [ buffer>> ] } cleave
118 bitmap-image new-image ;
120 M: bitmap-image load-image* ( path bitmap -- bitmap-image )
124 : write2 ( n -- ) 2 >le write ;
125 : write4 ( n -- ) 4 >le write ;
127 : save-bitmap ( bitmap path -- )
129 B{ CHAR: B CHAR: M } write
131 buffer>> length 14 + 40 + write4
139 [ planes>> 1 or write2 ]
140 [ bit-count>> 24 or write2 ]
141 [ compression>> 0 or write4 ]
142 [ size-image>> write4 ]
143 [ x-pels>> 0 or write4 ]
144 [ y-pels>> 0 or write4 ]
145 [ color-used>> 0 or write4 ]
146 [ color-important>> 0 or write4 ]
147 [ rgb-quads>> write ]
148 [ color-index>> write ]