flood-filling of 24-bit images
authorClaus Gittinger <cg@exept.de>
Fri, 05 Aug 2005 16:06:08 +0200
changeset 4420 6b8505b72ae6
parent 4419 a7b3e60e4f0a
child 4421 bcc5886c989c
flood-filling of 24-bit images
Image.st
--- a/Image.st	Fri Aug 05 14:45:17 2005 +0200
+++ b/Image.st	Fri Aug 05 16:06:08 2005 +0200
@@ -8482,7 +8482,7 @@
     "fill a area with aColor like a flood up to surrounded pixels having different colors.
      By using #atImageAndMask:put: it also works on images with mono masks."
 
-    |surroundingPixelsOfDo detectedPixel 
+    |surroundingPixelsOfDo detectedPixel processPixelToFill
      allDetectedPixelCoordinates enumerateDetectedPixelsAndDo
      toDo idx pixel w h|
 
@@ -8490,75 +8490,80 @@
     h := self height.
 
     surroundingPixelsOfDo := 
-	[:pX :pY :fn |
-	    |nX nY|
-
-	    nX := pX + 1.
-	    nY := pY + 1.
-	    (nY < h) ifTrue: [fn value:pX value:nY].
-	    (pY > 0) ifTrue: [fn value:pX value:(pY - 1)].
-	    (nX < w) ifTrue: [fn value:nX value:pY].
-	    (pX > 0) ifTrue: [fn value:(pX - 1) value:pY].
-	].
+        [:pX :pY :fn |
+            |nX nY|
+
+            nX := pX + 1.
+            nY := pY + 1.
+            (nY < h) ifTrue: [fn value:pX value:nY].
+            (pY > 0) ifTrue: [fn value:pX value:(pY - 1)].
+            (nX < w) ifTrue: [fn value:nX value:pY].
+            (pX > 0) ifTrue: [fn value:(pX - 1) value:pY].
+        ].
 
     enumerateDetectedPixelsAndDo := 
-	[:detectedPixels :action |
-	    |idx|
-
-	    idx := 1.
-	    0 to:h-1 do:[:y |
-		0 to:w-1 do:[:x |
-		    (detectedPixels at:idx) ifTrue:[ 
-			action value:x value:y
-		    ].
-		    idx := idx + 1.
-		].
-	    ].
-	].
+        [:detectedPixels :action |
+            |idx|
+
+            idx := 1.
+            0 to:h-1 do:[:y |
+                0 to:w-1 do:[:x |
+                    (detectedPixels at:idx) ifTrue:[ 
+                        action value:x value:y
+                    ].
+                    idx := idx + 1.
+                ].
+            ].
+        ].
+
+    processPixelToFill := [:spX :spY |
+            |sp idx|
+
+            ((self pixelAtX:spX y:spY) == detectedPixel and: [mask isNil or:[(mask pixelAtX:spX y:spY) == 1]])
+            ifTrue: [
+                idx := 1 + spX + (spY * w).
+                (allDetectedPixelCoordinates at:idx) ifFalse:[
+                    allDetectedPixelCoordinates at:idx put:true.
+                    toDo add:spX @ spY.
+                ].
+            ]
+        ].
 
     (mask notNil and: [(mask pixelAt:aPoint) == 0]) ifTrue:[
-	allDetectedPixelCoordinates := mask floodFillAt: aPoint withColor: Color white.
-	enumerateDetectedPixelsAndDo
-		value:allDetectedPixelCoordinates
-		value:[:x :y | self atImageAndMask:(x@y) put: aColor].
-	^ allDetectedPixelCoordinates
+        allDetectedPixelCoordinates := mask floodFillAt: aPoint withColor: Color white.
+        enumerateDetectedPixelsAndDo
+                value:allDetectedPixelCoordinates
+                value:[:x :y | self atImageAndMask:(x@y) put: aColor].
+        ^ allDetectedPixelCoordinates
     ].
 
     detectedPixel := self pixelAt: aPoint.
+
     allDetectedPixelCoordinates := BooleanArray new:(w * h). 
     toDo := OrderedCollection new:1000.
     allDetectedPixelCoordinates at:(1 + aPoint x + (aPoint y * w)) put:true.
     toDo add:aPoint.
 
     [toDo notEmpty] whileTrue:[
-	|p|
-
-	p := toDo removeFirst.
-	surroundingPixelsOfDo value:p x value:p y  value:[:spX :spY |
-	    |sp|
-
-	    ((self pixelAtX:spX y:spY) == detectedPixel and: [mask isNil or:[(mask pixelAtX:spX y:spY) == 1]])
-	    ifTrue: [
-		(allDetectedPixelCoordinates at:(1 + spX + (spY * w)))
-
-		ifFalse:[
-		    allDetectedPixelCoordinates at:(1 + spX + (spY * w)) put:true.
-		    toDo add:spX @ spY.
-		].
-	    ]
-	].
+        |p|
+
+        p := toDo removeFirst.
+        surroundingPixelsOfDo value:p x value:p y value:processPixelToFill.
     ].
 
     idx := 1.
-    pixel := self valueFromColor:aColor.
+    aColor redByte notNil ifTrue:[
+        pixel := self valueFromColor:aColor.
+    ].
+
     pixel isNil ifTrue:[
-	enumerateDetectedPixelsAndDo
-		value:allDetectedPixelCoordinates
-		value:[:x :y | mask pixelAtX:x y:y put:0].
+        enumerateDetectedPixelsAndDo
+                value:allDetectedPixelCoordinates
+                value:[:x :y | mask pixelAtX:x y:y put:0].
     ] ifFalse:[
-	enumerateDetectedPixelsAndDo
-		value:allDetectedPixelCoordinates
-		value:[:x :y | self pixelAtX:x y:y put:pixel].
+        enumerateDetectedPixelsAndDo
+                value:allDetectedPixelCoordinates
+                value:[:x :y | self pixelAtX:x y:y put:pixel].
     ].
     ^ allDetectedPixelCoordinates
 
@@ -12820,7 +12825,7 @@
 !Image class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libview/Image.st,v 1.379 2005-08-05 12:24:10 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libview/Image.st,v 1.380 2005-08-05 14:06:08 cg Exp $'
 ! !
 
 Image initialize!