2009-02-09 21:57:26 -05:00
|
|
|
! Copyright (C) 2007, 2009 Doug Coleman.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
|
|
|
USING: accessors alien alien.c-types arrays byte-arrays columns
|
2009-06-03 00:29:04 -04:00
|
|
|
combinators compression.run-length endian fry grouping images
|
2009-06-06 21:10:40 -04:00
|
|
|
images.bitmap.loading images.loader io io.binary
|
|
|
|
io.encodings.8-bit io.encodings.binary io.encodings.string
|
|
|
|
io.files io.streams.limited kernel locals macros math
|
|
|
|
math.bitwise math.functions namespaces sequences
|
|
|
|
specialized-arrays.uint specialized-arrays.ushort strings
|
|
|
|
summary ;
|
2009-06-03 22:36:03 -04:00
|
|
|
QUALIFIED-WITH: bitstreams b
|
2009-02-09 21:57:26 -05:00
|
|
|
IN: images.bitmap
|
|
|
|
|
2009-06-04 17:09:38 -04:00
|
|
|
SINGLETON: bitmap-image
|
|
|
|
"bmp" bitmap-image register-image-class
|
2009-03-14 16:17:51 -04:00
|
|
|
|
2009-06-03 22:36:03 -04:00
|
|
|
! endpoints-triple is ciexyzX/Y/Z, 3x fixed-point-2.30 aka 3x uint
|
2009-03-02 20:38:28 -05:00
|
|
|
|
2009-06-06 21:10:40 -04:00
|
|
|
: write2 ( n -- ) 2 >le write ;
|
|
|
|
: write4 ( n -- ) 4 >le write ;
|
|
|
|
|
2009-03-02 20:38:28 -05:00
|
|
|
<PRIVATE
|
2009-02-09 21:57:26 -05:00
|
|
|
|
2009-06-03 22:36:03 -04:00
|
|
|
: os2-color-lookup ( loading-bitmap -- seq )
|
|
|
|
[ color-index>> >array ]
|
|
|
|
[ color-palette>> 3 <sliced-groups> ] bi
|
|
|
|
'[ _ nth ] map concat ;
|
|
|
|
|
|
|
|
: os2v2-color-lookup ( loading-bitmap -- seq )
|
|
|
|
[ color-index>> >array ]
|
|
|
|
[ color-palette>> 3 <sliced-groups> ] bi
|
|
|
|
'[ _ nth ] map concat ;
|
|
|
|
|
|
|
|
: v3-color-lookup ( loading-bitmap -- seq )
|
|
|
|
[ color-index>> >array ]
|
|
|
|
[ color-palette>> 4 <sliced-groups> [ 3 head-slice ] map ] bi
|
|
|
|
'[ _ nth ] map concat ;
|
|
|
|
|
|
|
|
: color-lookup ( loading-bitmap -- seq )
|
2009-06-06 21:10:40 -04:00
|
|
|
dup file-header>> header-length>> {
|
2009-06-03 22:36:03 -04:00
|
|
|
{ 12 [ os2-color-lookup ] }
|
|
|
|
{ 64 [ os2v2-color-lookup ] }
|
|
|
|
{ 40 [ v3-color-lookup ] }
|
|
|
|
! { 108 [ v4-color-lookup ] }
|
|
|
|
! { 124 [ v5-color-lookup ] }
|
|
|
|
} case ;
|
2009-02-09 21:57:26 -05:00
|
|
|
|
|
|
|
ERROR: bmp-not-supported n ;
|
|
|
|
|
2009-06-03 22:36:03 -04:00
|
|
|
: uncompress-bitfield ( seq masks -- bytes' )
|
|
|
|
'[
|
|
|
|
_ [
|
|
|
|
[ bitand ] [ bit-count ] [ log2 ] tri - shift
|
|
|
|
] with map
|
|
|
|
] { } map-as B{ } concat-as ;
|
2009-03-14 16:31:59 -04:00
|
|
|
|
2009-06-03 22:36:03 -04:00
|
|
|
: bitmap>bytes ( loading-bitmap -- byte-array )
|
2009-06-06 21:10:40 -04:00
|
|
|
dup header>> bit-count>>
|
2009-02-09 21:57:26 -05:00
|
|
|
{
|
|
|
|
{ 32 [ color-index>> ] }
|
2009-06-03 22:36:03 -04:00
|
|
|
{ 24 [ color-index>> ] }
|
|
|
|
{ 16 [
|
|
|
|
[
|
|
|
|
! byte-array>ushort-array
|
|
|
|
2 group [ le> ] map
|
|
|
|
! 5 6 5
|
|
|
|
! { HEX: f800 HEX: 7e0 HEX: 1f } uncompress-bitfield
|
|
|
|
! 5 5 5
|
|
|
|
{ HEX: 7c00 HEX: 3e0 HEX: 1f } uncompress-bitfield
|
|
|
|
] change-color-index
|
|
|
|
color-index>>
|
|
|
|
] }
|
|
|
|
{ 8 [ color-lookup ] }
|
2009-06-06 21:10:40 -04:00
|
|
|
{ 4 [ B [ 4 b:byte-array-n>seq ] change-color-index color-lookup ] }
|
2009-06-03 22:36:03 -04:00
|
|
|
{ 1 [ [ 1 b:byte-array-n>seq ] change-color-index color-lookup ] }
|
2009-03-14 16:17:51 -04:00
|
|
|
[ bmp-not-supported ]
|
2009-02-09 21:57:26 -05:00
|
|
|
} case >byte-array ;
|
|
|
|
|
2009-06-03 22:36:03 -04:00
|
|
|
: set-bitfield-widths ( loading-bitmap -- loading-bitmap' )
|
2009-06-06 21:10:40 -04:00
|
|
|
dup header>> bit-count>> {
|
2009-06-03 22:36:03 -04:00
|
|
|
{ 16 [ dup color-palette>> 4 group [ le> ] map ] }
|
|
|
|
{ 32 [ { HEX: ff0000 HEX: ff00 HEX: ff } ] }
|
|
|
|
} case reverse >>bitfields ;
|
|
|
|
|
|
|
|
ERROR: unsupported-bitfield-widths n ;
|
|
|
|
|
|
|
|
M: unsupported-bitfield-widths summary
|
|
|
|
drop "Bitmaps only support bitfield compression in 16/32bit images" ;
|
|
|
|
|
|
|
|
: uncompress-bitfield-widths ( loading-bitmap -- loading-bitmap' )
|
|
|
|
set-bitfield-widths
|
2009-06-06 21:10:40 -04:00
|
|
|
dup header>> bit-count>> {
|
2009-06-03 22:36:03 -04:00
|
|
|
{ 16 [
|
|
|
|
dup bitfields>> '[
|
|
|
|
byte-array>ushort-array _ uncompress-bitfield
|
|
|
|
] change-color-index
|
|
|
|
] }
|
|
|
|
{ 32 [
|
|
|
|
dup bitfields>> '[
|
|
|
|
byte-array>uint-array _ uncompress-bitfield
|
|
|
|
] change-color-index
|
|
|
|
] }
|
|
|
|
[ unsupported-bitfield-widths ]
|
|
|
|
} case ;
|
|
|
|
|
2009-06-03 00:20:07 -04:00
|
|
|
ERROR: unsupported-bitmap-compression compression ;
|
|
|
|
|
2009-06-06 21:10:40 -04:00
|
|
|
GENERIC: uncompress-bitmap* ( loading-bitmap header -- loading-bitmap )
|
|
|
|
|
|
|
|
: uncompress-bitmap ( loading-bitmap -- loading-bitmap )
|
|
|
|
dup header>> uncompress-bitmap* ;
|
|
|
|
|
|
|
|
M: os2-header uncompress-bitmap* ( loading-bitmap header -- loading-bitmap' )
|
|
|
|
drop ;
|
|
|
|
|
|
|
|
M: v-header uncompress-bitmap* ( loading-bitmap header -- loading-bitmap' )
|
|
|
|
compression>> {
|
2009-06-03 22:36:03 -04:00
|
|
|
{ f [ ] }
|
2009-06-03 00:20:07 -04:00
|
|
|
{ 0 [ ] }
|
2009-06-03 22:36:03 -04:00
|
|
|
{ 1 [ [ run-length-uncompress ] change-color-index ] }
|
|
|
|
{ 2 [ [ 4 b:byte-array-n>seq run-length-uncompress >byte-array ] change-color-index ] }
|
|
|
|
{ 3 [ uncompress-bitfield-widths ] }
|
2009-06-03 00:20:07 -04:00
|
|
|
{ 4 [ "jpeg" unsupported-bitmap-compression ] }
|
|
|
|
{ 5 [ "png" unsupported-bitmap-compression ] }
|
|
|
|
} case ;
|
|
|
|
|
2009-06-03 22:36:03 -04:00
|
|
|
: bitmap-padding ( width -- n )
|
|
|
|
3 * 4 mod 4 swap - 4 mod ; inline
|
|
|
|
|
2009-06-03 00:20:07 -04:00
|
|
|
: loading-bitmap>bytes ( loading-bitmap -- byte-array )
|
2009-06-03 22:36:03 -04:00
|
|
|
uncompress-bitmap
|
|
|
|
bitmap>bytes ;
|
2009-06-03 00:20:07 -04:00
|
|
|
|
|
|
|
: color-palette-length ( loading-bitmap -- n )
|
2009-06-06 21:10:40 -04:00
|
|
|
file-header>>
|
2009-02-09 21:57:26 -05:00
|
|
|
[ offset>> 14 - ] [ header-length>> ] bi - ;
|
|
|
|
|
2009-06-06 21:10:40 -04:00
|
|
|
: color-index-length ( header -- n )
|
2009-02-09 21:57:26 -05:00
|
|
|
{
|
|
|
|
[ width>> ]
|
|
|
|
[ planes>> * ]
|
|
|
|
[ bit-count>> * 31 + 32 /i 4 * ]
|
|
|
|
[ height>> abs * ]
|
|
|
|
} cleave ;
|
|
|
|
|
2009-06-03 22:36:03 -04:00
|
|
|
ERROR: unsupported-bitmap-file magic ;
|
2009-02-09 21:57:26 -05:00
|
|
|
|
2009-03-02 20:38:28 -05:00
|
|
|
PRIVATE>
|
|
|
|
|
2009-04-08 19:42:01 -04:00
|
|
|
: bitmap>color-index ( bitmap -- byte-array )
|
|
|
|
[
|
|
|
|
bitmap>>
|
|
|
|
4 <sliced-groups>
|
|
|
|
[ 3 head-slice <reversed> ] map
|
|
|
|
B{ } join
|
|
|
|
] [
|
|
|
|
dim>> first dup bitmap-padding dup 0 > [
|
|
|
|
[ 3 * group ] dip '[ _ <byte-array> append ] map
|
|
|
|
B{ } join
|
|
|
|
] [
|
|
|
|
2drop
|
|
|
|
] if
|
|
|
|
] bi ;
|
2009-03-14 15:48:28 -04:00
|
|
|
|
2009-06-03 22:36:03 -04:00
|
|
|
: reverse-lines ( byte-array width -- byte-array )
|
|
|
|
<sliced-groups> <reversed> concat ; inline
|
|
|
|
|
2009-03-14 15:48:28 -04:00
|
|
|
: save-bitmap ( image path -- )
|
2009-02-09 21:57:26 -05:00
|
|
|
binary [
|
|
|
|
B{ CHAR: B CHAR: M } write
|
|
|
|
[
|
2009-04-08 19:42:01 -04:00
|
|
|
bitmap>color-index length 14 + 40 + write4
|
2009-02-09 21:57:26 -05:00
|
|
|
0 write4
|
|
|
|
54 write4
|
|
|
|
40 write4
|
|
|
|
] [
|
|
|
|
{
|
2009-03-14 16:08:50 -04:00
|
|
|
! width height
|
2009-03-14 15:48:28 -04:00
|
|
|
[ dim>> first2 [ write4 ] bi@ ]
|
2009-03-14 16:08:50 -04:00
|
|
|
|
|
|
|
! planes
|
2009-03-14 15:48:28 -04:00
|
|
|
[ drop 1 write2 ]
|
2009-03-14 16:08:50 -04:00
|
|
|
|
|
|
|
! bit-count
|
2009-03-14 15:48:28 -04:00
|
|
|
[ drop 24 write2 ]
|
2009-03-14 16:08:50 -04:00
|
|
|
|
|
|
|
! compression
|
2009-03-14 15:48:28 -04:00
|
|
|
[ drop 0 write4 ]
|
2009-03-14 16:08:50 -04:00
|
|
|
|
2009-06-06 21:10:40 -04:00
|
|
|
! image-size
|
2009-04-08 19:42:01 -04:00
|
|
|
[ bitmap>color-index length write4 ]
|
2009-03-14 16:08:50 -04:00
|
|
|
|
|
|
|
! x-pels
|
2009-03-14 15:48:28 -04:00
|
|
|
[ drop 0 write4 ]
|
2009-03-14 16:08:50 -04:00
|
|
|
|
|
|
|
! y-pels
|
2009-03-14 15:48:28 -04:00
|
|
|
[ drop 0 write4 ]
|
2009-03-14 16:08:50 -04:00
|
|
|
|
|
|
|
! color-used
|
2009-03-14 15:48:28 -04:00
|
|
|
[ drop 0 write4 ]
|
2009-03-14 16:08:50 -04:00
|
|
|
|
|
|
|
! color-important
|
2009-03-14 15:48:28 -04:00
|
|
|
[ drop 0 write4 ]
|
2009-03-14 16:08:50 -04:00
|
|
|
|
2009-06-03 00:20:07 -04:00
|
|
|
! color-palette
|
2009-03-14 16:31:59 -04:00
|
|
|
[
|
2009-04-08 19:42:01 -04:00
|
|
|
[ bitmap>color-index ]
|
|
|
|
[ dim>> first 3 * ]
|
|
|
|
[ dim>> first bitmap-padding + ] tri
|
2009-03-14 16:31:59 -04:00
|
|
|
reverse-lines write
|
|
|
|
]
|
2009-02-09 21:57:26 -05:00
|
|
|
} cleave
|
|
|
|
] bi
|
|
|
|
] with-file-writer ;
|