]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/images/images.factor
basis: use lint.vocabs tool to trim using lists
[factor.git] / basis / images / images.factor
index 625627f337027307c47089b27866a04c863dd960..4c392b84d1415ff3b8e270f9e030f153627b0bee 100644 (file)
@@ -1,6 +1,6 @@
 ! Copyright (C) 2009 Doug Coleman, Daniel Ehrenberg.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: combinators kernel accessors sequences math arrays ;
+USING: accessors combinators kernel math sequences ;
 IN: images
 
 SINGLETONS:
@@ -18,7 +18,7 @@ SINGLETONS:
     u-9-9-9-e5-components
     float-11-11-10-components ;
 
-UNION: component-order 
+UNION: component-order
     A L LA BGR RGB BGRA RGBA ABGR ARGB RGBX XRGB BGRX XBGR
     INTENSITY DEPTH DEPTH-STENCIL R RG ;
 
@@ -41,8 +41,8 @@ UNION: unnormalized-integer-components
     int-integer-components uint-integer-components ;
 
 UNION: signed-unnormalized-integer-components
-    byte-integer-components 
-    short-integer-components 
+    byte-integer-components
+    short-integer-components
     int-integer-components ;
 
 UNION: unsigned-unnormalized-integer-components
@@ -62,10 +62,16 @@ UNION: alpha-channel BGRA RGBA ABGR ARGB LA A INTENSITY ;
 
 UNION: alpha-channel-precedes-colors ABGR ARGB XBGR XRGB ;
 
-TUPLE: image dim component-order component-type upside-down? bitmap ;
+TUPLE: image
+    dim component-order component-type
+    upside-down? premultiplied-alpha?
+    bitmap 2x? ;
 
 : <image> ( -- image ) image new ; inline
 
+: image-dim ( image -- dim )
+    [ dim>> ] [ 2x?>> ] bi [ [ 2.0 / ] map ] when ;
+
 : has-alpha? ( image -- ? ) component-order>> alpha-channel? ;
 
 : bytes-per-component ( component-type -- n )
@@ -126,20 +132,48 @@ TUPLE: image dim component-order component-type upside-down? bitmap ;
 : bytes-per-pixel ( image -- n )
     [ component-order>> ] [ component-type>> ] bi (bytes-per-pixel) ;
 
+: bytes-per-image ( image -- n )
+    [ dim>> product ] [ bytes-per-pixel ] bi * ;
+
+: rowstride ( image -- n )
+    [ dim>> first ] [ bytes-per-pixel ] bi * ;
+
 <PRIVATE
 
-: pixel@ ( x y image -- start end bitmap )
-    [ dim>> first * + ]
-    [ bytes-per-pixel [ * dup ] keep + ]
-    [ bitmap>> ] tri ;
+:: pixel@ ( x y w image -- start end bitmap )
+    image dim>> first y * x + :> start
+    start w [ image bytes-per-pixel * ] bi@ :> ( start' w' )
+    start'  start' w' +  image bitmap>> ; inline
 
 : set-subseq ( new-value from to victim -- )
     <slice> 0 swap copy ; inline
 
 PRIVATE>
 
+: pixel-row-at ( x y w image -- pixels )
+    pixel@ subseq ; inline
+
+: pixel-row-slice-at ( x y w image -- pixels )
+    pixel@ <slice> ; inline
+
+: set-pixel-row-at ( pixel x y w image -- )
+    pixel@ set-subseq ; inline
+
 : pixel-at ( x y image -- pixel )
-    pixel@ subseq ;
+    [ 1 ] dip pixel-row-at ; inline
+
+: pixel-slice-at ( x y image -- pixels )
+    [ 1 ] dip pixel-row-slice-at ; inline
 
 : set-pixel-at ( pixel x y image -- )
-    pixel@ set-subseq ;
+    [ 1 ] dip set-pixel-row-at ; inline
+
+:: each-pixel ( ... image quot: ( ... x y pixel -- ... ) -- ... )
+    image dim>> first2 :> ( width height )
+    image bytes-per-pixel :> n
+    height width [ <iota> ] bi@ [| y x |
+        y width * x + :> start
+        start n * :> from
+        from n + :> to
+        x y from to image bitmap>> <slice> quot call
+    ] cartesian-each ; inline