]> gitweb.factorcode.org Git - factor.git/commitdiff
Merge branch 'master' into new_ui
authorSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Wed, 11 Feb 2009 00:52:26 +0000 (18:52 -0600)
committerSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Wed, 11 Feb 2009 00:52:26 +0000 (18:52 -0600)
basis/bit-arrays/bit-arrays.factor
core/combinators/combinators-tests.factor
core/combinators/combinators.factor
extra/images/backend/backend.factor
extra/images/bitmap/bitmap-tests.factor
extra/images/bitmap/bitmap.factor
extra/images/images.factor
extra/images/tiff/tiff.factor
extra/images/viewer/viewer.factor

index f1ba71ce1e02861bd79af83aeaff13b9639636b0..3da22e09d65854b49ea65cb8869133da7bef547c 100644 (file)
@@ -1,7 +1,7 @@
 ! Copyright (C) 2007, 2008 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: alien.c-types accessors math alien.accessors kernel
-kernel.private locals sequences sequences.private byte-arrays
+kernel.private sequences sequences.private byte-arrays
 parser prettyprint.custom fry ;
 IN: bit-arrays
 
@@ -70,16 +70,15 @@ M: bit-array byte-length length 7 + -3 shift ;
 
 : ?{ \ } [ >bit-array ] parse-literal ; parsing
 
-:: integer>bit-array ( n -- bit-array ) 
-    n zero? [ 0 <bit-array> ] [
-        [let | out [ n log2 1+ <bit-array> ] i! [ 0 ] n'! [ n ] |
-            [ n' zero? ] [
-                n' out underlying>> i set-alien-unsigned-1
-                n' -8 shift n'!
-                i 1+ i!
-            ] [ ] until
-            out
-        ]
+: integer>bit-array ( n -- bit-array )
+    dup 0 = [
+        <bit-array>
+    ] [
+        [ log2 1+ <bit-array> 0 ] keep
+        [ dup 0 = ] [
+            [ pick underlying>> pick set-alien-unsigned-1 ] keep
+            [ 1+ ] [ -8 shift ] bi*
+        ] [ ] until 2drop
     ] if ;
 
 : bit-array>integer ( bit-array -- n )
index 1a73e22e313ac10ed136c5277840f014143162ee..beb50f1162ac7a69626d92bbbcbffa8a0a042622 100644 (file)
@@ -323,4 +323,18 @@ DEFER: corner-case-1
 [ t ] [ \ corner-case-1 optimized>> ] unit-test
 [ 4 ] [ 2 corner-case-1 ] unit-test
 
-[ 4 ] [ 2 2 [ + ] curry 1array case ] unit-test
\ No newline at end of file
+[ 4 ] [ 2 2 [ + ] curry 1array case ] unit-test
+
+: test-case-8 ( n -- )
+    {
+        { 1 [ "foo" ] }
+    } case ;
+
+[ 3 test-case-8 ]
+[ object>> 3 = ] must-fail-with
+
+[
+    3 {
+        { 1 [ "foo" ] }
+    } case
+] [ object>> 3 = ] must-fail-with
index e356a6d246016db91d7f7cee5e9e35bb33ca219b..daf247d678b438b9e0c24c54daace8d17a642451 100755 (executable)
@@ -49,7 +49,7 @@ ERROR: no-cond ;
     reverse [ no-cond ] swap alist>quot ;
 
 ! case
-ERROR: no-case ;
+ERROR: no-case object ;
 
 : case-find ( obj assoc -- obj' )
     [
@@ -66,7 +66,7 @@ ERROR: no-case ;
     case-find {
         { [ dup array? ] [ nip second call ] }
         { [ dup callable? ] [ call ] }
-        { [ dup not ] [ no-case ] }
+        { [ dup not ] [ drop no-case ] }
     } cond ;
 
 : linear-case-quot ( default assoc -- quot )
index ef2a9a4248cf5ef03f4f2cf6a8babcb964c6d7fa..796e9a3a664df348f90518778a33e9f949402eac 100644 (file)
@@ -1,18 +1,57 @@
 ! Copyright (C) 2009 Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: accessors kernel ;
+USING: accessors kernel grouping fry sequences combinators
+images.bitmap math ;
 IN: images.backend
 
-TUPLE: image width height depth pitch buffer ;
+SINGLETONS: BGR RGB BGRA RGBA ABGR ARGB RGBX XRGB BGRX XBGR ;
+! RGBA
+
+TUPLE: image dim component-order bitmap ;
+
+TUPLE: normalized-image < image ;
 
 GENERIC: load-image* ( path tuple -- image )
 
-: load-image ( path class -- image )
-    new load-image* ;
+GENERIC: >image ( object -- image )
+
+: no-op ( -- ) ;
+
+: normalize-component-order ( image -- image )
+    dup component-order>>
+    {
+        { RGBA [ no-op ] }
+        { BGRA [
+            [
+                [ 4 <sliced-groups> [ [ 0 3 ] dip <slice> reverse-here ] each ]
+                [ RGBA >>component-order ] bi
+            ] change-bitmap
+        ] }
+        { RGB [
+            [ 3 <sliced-groups> [ 255 suffix ] map concat ] change-bitmap
+        ] }
+        { BGR [
+            [
+                3 <sliced-groups> dup [ [ 0 3 ] dip <slice> reverse-here ] each
+                [ 255 suffix ] map concat
+            ] change-bitmap
+        ] }
+    } case RGBA >>component-order ;
+
+GENERIC: normalize-scan-line-order ( image -- image )
+
+M: image normalize-scan-line-order ;
+M: bitmap-image normalize-scan-line-order
+    dup
+    [ bitmap>> ] [ dim>> first 4 * ] bi <sliced-groups> reverse concat
+    >>bitmap ;
+    
+: normalize-image ( image -- image )
+    normalize-component-order
+    normalize-scan-line-order ;
 
-: new-image ( width height depth buffer class -- image )
+: new-image ( dim component-order bitmap class -- image )
     new 
-        swap >>buffer
-        swap >>depth
-        swap >>height
-        swap >>width ; inline
+        swap >>bitmap
+        swap >>component-order
+        swap >>dim ; inline
index a2b31887497320c87acdce2d896ec9c1535b3e0a..a7deae31782838c571e839d63b58aaadef2b7145 100644 (file)
@@ -5,9 +5,6 @@ IN: images.bitmap.tests
 : test-bitmap24 ( -- path )
     "resource:extra/images/test-images/thiswayup24.bmp" ;
 
-: test-bitmap16 ( -- path )
-    "resource:extra/images/test-images/rgb16bit.bmp" ;
-
 : test-bitmap8 ( -- path )
     "resource:extra/images/test-images/rgb8bit.bmp" ;
 
index 50975b2bb35736b4be0cc49ef6a3efbf75bdce26..7b59827d028688f570f3be120efdd1e639cf6700 100755 (executable)
@@ -15,7 +15,6 @@ TUPLE: bitmap-image < image ;
 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?
 buffer ;
 
 : array-copy ( bitmap array -- bitmap array' )
@@ -87,23 +86,31 @@ M: bitmap-magic summary
         parse-file-header parse-bitmap-header parse-bitmap
     ] with-file-reader ;
 
-: alpha-channel-zero? ( bitmap -- ? )
-    buffer>> 4 <sliced-groups> 3 <column> [ 0 = ] all? ;
-
 : process-bitmap-data ( bitmap -- bitmap )
-    dup raw-bitmap>buffer >>buffer
-    dup alpha-channel-zero? >>alpha-channel-zero? ;
+    dup raw-bitmap>buffer >>buffer ;
 
 : load-bitmap ( path -- bitmap )
     load-bitmap-data process-bitmap-data ;
 
-: bitmap>image ( bitmap -- bitmap-image )
-    { [ width>> ] [ height>> ] [ bit-count>> ] [ buffer>> ] } cleave
-    bitmap-image new-image ;
+ERROR: unknown-component-order bitmap ;
+
+: bitmap>component-order ( bitmap -- object )
+    bit-count>> {
+        { 32 [ BGRA ] }
+        { 24 [ BGR ] }
+        { 8 [ BGR ] }
+        [ unknown-component-order ]
+    } case ;
+
+M: bitmap >image ( bitmap -- bitmap-image )
+    {
+        [ [ width>> ] [ height>> ] bi 2array ]
+        [ bitmap>component-order ]
+        [ buffer>> ]
+    } cleave bitmap-image new-image ;
 
 M: bitmap-image load-image* ( path bitmap -- bitmap-image )
-    drop load-bitmap
-    bitmap>image ;
+    drop load-bitmap >image ;
 
 MACRO: (nbits>bitmap) ( bits -- )
     [ -3 shift ] keep '[
@@ -112,7 +119,7 @@ MACRO: (nbits>bitmap) ( bits -- )
             swap >>height
             swap >>width
             swap array-copy [ >>buffer ] [ >>color-index ] bi
-            _ >>bit-count bitmap>image
+            _ >>bit-count >image
     ] ;
 
 : bgr>bitmap ( array height width -- bitmap )
index eb4fc63feecbbe50493577877b9c95e08766c2e8..3df7b5d2d193db3d9c3014b8188ab70d7f1845fb 100644 (file)
@@ -5,9 +5,17 @@ accessors images.bitmap images.tiff images.backend io.backend
 io.pathnames ;
 IN: images
 
-: <image> ( path -- image )
-    normalize-path dup "." split1-last nip >lower
-    {
-        { "bmp" [ bitmap-image load-image ] }
-        { "tiff" [ tiff-image load-image ] }
+ERROR: unknown-image-extension extension ;
+
+: image-class ( path -- class )
+    file-extension >lower {
+        { "bmp" [ bitmap-image ] }
+        { "tiff" [ tiff-image ] }
+        [ unknown-image-extension ]
     } case ;
+
+: load-image ( path -- image )
+    dup image-class new load-image* ;
+
+: <image> ( path -- image )
+    load-image normalize-image ;
index 4be81af095b59258d18a48f791078a0bd32cdd7c..dc40f648cc0f650bad77d7563febcd3c3a05f124 100755 (executable)
@@ -1,7 +1,7 @@
 ! Copyright (C) 2009 Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: accessors combinators io io.encodings.binary io.files
-kernel pack endian tools.hexdump constructors sequences arrays
+kernel pack endian constructors sequences arrays
 sorting.slots math.order math.parser prettyprint classes
 io.binary assocs math math.bitwise byte-arrays grouping
 images.backend ;
@@ -13,7 +13,7 @@ TUPLE: parsed-tiff endianness the-answer ifd-offset ifds ;
 CONSTRUCTOR: parsed-tiff ( -- tiff ) V{ } clone >>ifds ;
 
 TUPLE: ifd count ifd-entries next
-processed-tags strips buffer ;
+processed-tags strips bitmap ;
 CONSTRUCTOR: ifd ( count ifd-entries next -- ifd ) ;
 
 TUPLE: ifd-entry tag type count offset/value ;
@@ -257,29 +257,37 @@ ERROR: bad-small-ifd-type n ;
     dup ifd-entries>>
     [ process-ifd-entry swap ] H{ } map>assoc >>processed-tags ;
 
-: strips>buffer ( ifd -- ifd )
-    dup strips>> concat >>buffer ;
+: strips>bitmap ( ifd -- ifd )
+    dup strips>> concat >>bitmap ;
 
-: ifd>image ( ifd -- image )
+ERROR: unknown-component-order ifd ;
+
+: ifd-component-order ( ifd -- byte-order )
+    bits-per-sample find-tag sum {
+        { 32 [ RGBA ] }
+        { 24 [ RGB ] }
+        [ unknown-component-order ]
+    } case ;
+
+M: ifd >image ( ifd -- image )
     {
-        [ image-width find-tag ]
-        [ image-length find-tag ]
-        [ bits-per-sample find-tag sum ]
-        [ buffer>> ]
+        [ [ image-width find-tag ] [ image-length find-tag ] bi 2array ]
+        [ ifd-component-order ]
+        [ bitmap>> ]
     } cleave tiff-image new-image ;
 
-: parsed-tiff>images ( tiff -- sequence )
-    ifds>> [ ifd>image ] map ;
+M: parsed-tiff >image ( image -- image )
+    ifds>> [ >image ] map first ;
 
 : load-tiff ( path -- parsed-tiff )
     binary [
         <parsed-tiff>
         read-header dup endianness>> [
             read-ifds
-            dup ifds>> [ process-ifd read-strips strips>buffer drop ] each
+            dup ifds>> [ process-ifd read-strips strips>bitmap drop ] each
         ] with-endianness
     ] with-file-reader ;
 
 ! tiff files can store several images -- we just take the first for now
 M: tiff-image load-image* ( path tiff-image -- image )
-    drop load-tiff parsed-tiff>images first ;
+    drop load-tiff >image ;
index 4d5df4874a5a057e77ac1c1b023c417f4a7b6240..f99c34f50982e799c37392990a9c01cc144df4bc 100644 (file)
@@ -1,19 +1,19 @@
 ! Copyright (C) 2007 Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: accessors arrays combinators images.bitmap kernel math
-math.functions namespaces opengl opengl.gl ui ui.gadgets
-ui.gadgets.panes ui.render images.tiff sequences multiline
-images.backend images io.pathnames strings ;
+USING: accessors images images.backend io.pathnames kernel
+namespaces opengl opengl.gl sequences strings ui ui.gadgets
+ui.gadgets.panes ui.render ;
 IN: images.viewer
 
 TUPLE: image-gadget < gadget { image image } ;
 
-GENERIC: draw-image ( image -- )
-
 M: image-gadget pref-dim*
-    image>>
-    [ width>> ] [ height>> ] bi
-    [ abs ] bi@ 2array ;
+    image>> dim>> ;
+
+: draw-image ( tiff -- )
+    0 0 glRasterPos2i 1.0 -1.0 glPixelZoom
+    [ dim>> first2 GL_RGBA GL_UNSIGNED_BYTE ]
+    [ bitmap>> ] bi glDrawPixels ;
 
 M: image-gadget draw-gadget* ( gadget -- )
     origin get [ image>> draw-image ] with-translation ;
@@ -22,44 +22,9 @@ M: image-gadget draw-gadget* ( gadget -- )
     \ image-gadget new-gadget
         swap >>image ;
 
-: bits>gl-params ( n -- gl-bgr gl-format )
-    {
-        { 32 [ GL_BGRA GL_UNSIGNED_BYTE ] }
-        { 24 [ GL_BGR GL_UNSIGNED_BYTE ] }
-        { 8 [ GL_BGR GL_UNSIGNED_BYTE ] }
-        { 4 [ GL_BGR GL_UNSIGNED_BYTE ] }
-    } case ;
-
-M: bitmap-image draw-image ( bitmap -- )
-    {
-        [
-            height>> dup 0 < [
-                drop
-                0 0 glRasterPos2i
-                1.0 -1.0 glPixelZoom
-            ] [
-                0 swap abs glRasterPos2i
-                1.0 1.0 glPixelZoom
-            ] if
-        ]
-        [ width>> abs ]
-        [ height>> abs ]
-        [ depth>> bits>gl-params ]
-        [ buffer>> ]
-    } cleave glDrawPixels ;
-
 : image-window ( path -- gadget )
     [ <image> <image-gadget> dup ] [ open-window ] bi ;
 
-M: tiff-image draw-image ( tiff -- )
-    0 0 glRasterPos2i 1.0 -1.0 glPixelZoom
-    {
-        [ height>> ]
-        [ width>> ]
-        [ depth>> bits>gl-params ]
-        [ buffer>> ]
-    } cleave glDrawPixels ;
-
 GENERIC: image. ( image -- )
 
 M: string image. ( image -- ) <image> <image-gadget> gadget. ;