diff --git a/extra/graphics/bitmap/bitmap-tests.factor b/extra/graphics/bitmap/bitmap-tests.factor index 15e960084a..ca8be85e12 100644 --- a/extra/graphics/bitmap/bitmap-tests.factor +++ b/extra/graphics/bitmap/bitmap-tests.factor @@ -1,15 +1,27 @@ -USING: graphics.bitmap graphics.viewer ; +USING: graphics.bitmap graphics.viewer io.encodings.binary +io.files io.files.unique kernel tools.test ; IN: graphics.bitmap.tests -: test-bitmap24 ( -- ) - "resource:extra/graphics/bitmap/test-images/thiswayup24.bmp" bitmap. ; +: test-bitmap32-alpha ( -- path ) + "resource:extra/graphics/bitmap/test-images/32alpha.bmp" ; -: test-bitmap8 ( -- ) - "resource:extra/graphics/bitmap/test-images/rgb8bit.bmp" bitmap. ; +: test-bitmap24 ( -- path ) + "resource:extra/graphics/bitmap/test-images/thiswayup24.bmp" ; -: test-bitmap4 ( -- ) - "resource:extra/graphics/bitmap/test-images/rgb4bit.bmp" bitmap. ; +: test-bitmap8 ( -- path ) + "resource:extra/graphics/bitmap/test-images/rgb8bit.bmp" ; -: test-bitmap1 ( -- ) - "resource:extra/graphics/bitmap/test-images/1bit.bmp" bitmap. ; +: test-bitmap4 ( -- path ) + "resource:extra/graphics/bitmap/test-images/rgb4bit.bmp" ; +: test-bitmap1 ( -- path ) + "resource:extra/graphics/bitmap/test-images/1bit.bmp" ; + +[ t ] +[ + test-bitmap24 + [ binary file-contents ] [ load-bitmap ] bi + + "test-bitmap24" unique-file + [ save-bitmap ] [ binary file-contents ] bi = +] unit-test diff --git a/extra/graphics/bitmap/bitmap.factor b/extra/graphics/bitmap/bitmap.factor index bd34a9ee41..a1cf37c8a1 100755 --- a/extra/graphics/bitmap/bitmap.factor +++ b/extra/graphics/bitmap/bitmap.factor @@ -1,11 +1,10 @@ -! Copyright (C) 2007 Doug Coleman. +! Copyright (C) 2007, 2009 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. - -USING: alien arrays byte-arrays combinators summary -io io.binary io.files kernel libc math -math.functions math.bitwise namespaces opengl opengl.gl -prettyprint sequences strings ui ui.gadgets.panes fry -io.encodings.binary accessors grouping macros alien.c-types ; +USING: accessors alien alien.c-types arrays byte-arrays columns +combinators fry grouping io io.binary io.encodings.binary +io.files kernel libc macros math math.bitwise math.functions +namespaces opengl opengl.gl prettyprint sequences strings +summary ui ui.gadgets.panes ; IN: graphics.bitmap ! Currently can only handle 24/32bit bitmaps. @@ -14,6 +13,7 @@ IN: graphics.bitmap TUPLE: bitmap magic size reserved offset header-length width height planes bit-count compression size-image x-pels y-pels color-used color-important rgb-quads color-index +alpha-channel-zero? array ; : array-copy ( bitmap array -- bitmap array' ) @@ -97,12 +97,19 @@ M: bitmap-magic summary dup rgb-quads-length read >>rgb-quads dup color-index-length read >>color-index ; -: load-bitmap ( path -- bitmap ) +: (load-bitmap) ( path -- bitmap ) binary [ bitmap new parse-file-header parse-bitmap-header parse-bitmap - ] with-file-reader - dup raw-bitmap>array >>array ; + ] with-file-reader ; + +: alpha-channel-zero? ( bitmap -- ? ) + array>> 4 3 [ 0 = ] all? ; + +: load-bitmap ( path -- bitmap ) + (load-bitmap) + dup raw-bitmap>array >>array + dup alpha-channel-zero? >>alpha-channel-zero? ; : write2 ( n -- ) 2 >le write ; : write4 ( n -- ) 4 >le write ;