Image.st
changeset 3911 a3c84de217fe
parent 3908 d39322d668f6
child 3912 a16c7edceb0a
--- a/Image.st	Mon Jul 14 13:35:05 2003 +0200
+++ b/Image.st	Mon Jul 14 13:48:22 2003 +0200
@@ -8649,47 +8649,16 @@
 
 !Image methodsFor:'image manipulations'!
 
-applyPixelValuesTo:pixelFunctionBlock into:newImage
+applyPixelValuesTo:pixelFunctionBlock in:aRectangle into:newImage 
     "helper for withPixelFunctionAppliedToValues:
      enumerate pixelValues and evaluate the block for each.
-     To be redefined by subclasses for better performance."
-
-    |w  "{Class: SmallInteger }"
-     h  "{Class: SmallInteger }"
-     newPixel newPixelRow pixelRow|
-
-    w := width.
-    h := height - 1.
-
-    newPixelRow := Array new:width.
-    pixelRow := self pixelArraySpecies new:width.
-
-    0 to:h do:[:y |
-	self rowAt:y into:pixelRow.
-	1 to:w do:[:col |
-	    newPixel := pixelFunctionBlock
-			    value:self
-			    value:(pixelRow at:col)
-			    value:(col-1)
-			    value:y.
-	    newPixelRow at:col put:newPixel.
-	].
-	newImage rowAt:y putAll:newPixelRow
-    ].
-
-    "Modified: 24.4.1997 / 16:18:31 / cg"
-!
-
-applyPixelValuesTo:pixelFunctionBlock into:newImage in:aRectangle
-    "helper for withPixelFunctionAppliedToValues:
-     enumerate pixelValues and evaluate the block for each.
-     To be redefined by subclasses for better performance."
-
-    |w  "{Class: SmallInteger }"
-     h  "{Class: SmallInteger }"
+     Could be redefined by subclasses for better performance."
+
+    |w   "{Class: SmallInteger }"
+     h   "{Class: SmallInteger }"
      x0  "{Class: SmallInteger }"
      y0  "{Class: SmallInteger }"
-     y  "{Class: SmallInteger }"
+     y   "{Class: SmallInteger }"
      newPixel newPixelRow pixelRow|
 
     x0 := aRectangle left.
@@ -8700,25 +8669,54 @@
     newPixelRow := Array new:w.
     pixelRow := self pixelArraySpecies new:width.
 
+    (x0 = 0 and:[w = self width]) ifTrue:[
+        "/ slightly faster
+        y := y0.
+        h timesRepeat:[
+            self rowAt:y into:pixelRow.
+            1 to:w do:[:runCol |
+                newPixel := pixelFunctionBlock
+                                value:self
+                                value:(pixelRow at:runCol)
+                                value:(runCol-1)
+                                value:y.
+                newPixelRow at:runCol put:newPixel.
+            ].
+            newImage rowAt:y putAll:newPixelRow.
+            y := y + 1.
+        ].
+        ^ self.
+    ].
+
     y := y0.
     h timesRepeat:[
-	self rowAt:y into:pixelRow.
-	1 to:w do:[:runCol |
-	    newPixel := pixelFunctionBlock
-			    value:self
-			    value:(pixelRow at:runCol+x0)
-			    value:(runCol+x0-1)
-			    value:y.
-	    newPixelRow at:runCol put:newPixel.
-	].
-	pixelRow replaceFrom:x0+1 to:x0+1+w-1 with:newPixelRow startingAt:1.
-	newImage rowAt:y putAll:newPixelRow.
-	y := y + 1.
+        self rowAt:y into:pixelRow.
+        1 to:w do:[:runCol |
+            newPixel := pixelFunctionBlock
+                            value:self
+                            value:(pixelRow at:runCol+x0)
+                            value:(runCol+x0-1)
+                            value:y.
+            newPixelRow at:runCol put:newPixel.
+        ].
+        pixelRow replaceFrom:x0+1 to:x0+1+w-1 with:newPixelRow startingAt:1.
+        newImage rowAt:y putAll:newPixelRow.
+        y := y + 1.
     ].
 
     "Modified: 24.4.1997 / 16:18:31 / cg"
 !
 
+applyPixelValuesTo:pixelFunctionBlock into:newImage
+    "helper for withPixelFunctionAppliedToValues:
+     enumerate pixelValues and evaluate the block for each."
+
+    ^ self 
+        applyPixelValuesTo:pixelFunctionBlock 
+        in:(0@0 corner:width@height) 
+        into:newImage
+!
+
 blendWith:aColor
     "return a new image which is blended with some color.
      The receiver must be a palette image (currently).
@@ -10383,7 +10381,121 @@
     "return a new image from the old one, by applying a pixel processor
      on the pixel values.
      (read `Behond photography, by Gerard J. Holzmann;
-	   ISBM 0-13-074410-7)
+           ISBM 0-13-074410-7)
+     See blurred / oilPointed as examples ...)"
+
+    ^ self 
+        withPixelFunctionAppliedToPixels:pixelFunctionBlock 
+        in:(0@0 corner:width@height)
+
+    "oil painting effect:
+
+     |i w h|
+
+     i := Image fromFile:'goodies/bitmaps/gifImages/claus.gif'.
+     i inspect.
+     w := i width - 1.
+     h := i height - 1.
+     (i withPixelFunctionAppliedToPixels:[:oldImage :oldPixel :x :y |
+                        |b p max xMin xMax yMin yMax|
+
+                        b := Bag identityNew:10.
+                        xMin := x-3 max:0.
+                        xMax := x+3 min:w.
+                        yMin := y-3 max:0.
+                        yMax := y+3 min:h.
+                        xMin to:xMax do:[:tx|
+                          yMin to:yMax do:[:ty|
+                            b add:(oldImage pixelAtX:tx y:ty)
+                          ]
+                        ].
+                        max := 0.
+                        b contents keysAndValuesDo:[:pixel :n |
+                            n > max ifTrue:[
+                                p := pixel.
+                                max := n
+                            ]
+                        ].
+                        p
+                     ]) inspect.
+    "
+
+    "fisheye effect:
+
+     |i w h w2 h2 R white|
+
+     i := Image fromFile:'goodies/bitmaps/gifImages/claus.gif'.
+     i inspect.
+     w := i width - 1.
+     h := i height - 1.
+     w2 := w // 2.
+     h2 := h // 2.
+     R := w2.
+     white := i valueFromColor:Color white.
+     (i withPixelFunctionAppliedToPixels:[:oldImage :oldPixel :x :y |
+                        |p r a nR nP nX nY|
+
+                        p := (x-w2)@(y-h2).
+                        r := p r.
+                        a := p theta.
+                        nR := r * r / R.
+                        nP := Point r:nR theta:a.
+                        nX := ((nP x+w2) rounded max:0) min:w.
+                        nY := ((nP y+h2) rounded max:0) min:h.
+                        (nX > w or:[nX < 0]) ifTrue:[
+                            white
+                        ] ifFalse:[
+                            (nY > h or:[nY < 0]) ifTrue:[
+                                white
+                            ] ifFalse:[
+                                oldImage pixelAtX:nX y:nY
+                            ]
+                        ]
+                     ]) inspect.
+    "
+    "fisheye effect:
+
+     |i w h w2 h2 R white|
+
+     i := Image fromFile:'goodies/bitmaps/gifImages/claus.gif'.
+     i inspect.
+     w := i width - 1.
+     h := i height - 1.
+     w2 := w // 2.
+     h2 := h // 2.
+     R := w2.
+     white := i valueFromColor:Color white.
+     (i withPixelFunctionAppliedToPixels:[:oldImage :oldPixel :x :y |
+                        |p r a nR nP nX nY|
+
+                        p := (x-w2)@(y-h2).
+                        r := p r.
+                        a := p theta.
+                        nR := r * r / R.
+                        nP := Point r:nR theta:a.
+                        nX := (nP x+w2) rounded.
+                        nY := (nP y+h2) rounded.
+                        (nX > w or:[nX < 0]) ifTrue:[
+                            white
+                        ] ifFalse:[
+                            (nY > h or:[nY < 0]) ifTrue:[
+                                white
+                            ] ifFalse:[
+                                oldImage pixelAtX:nX y:nY
+                            ]
+                        ]
+                     ]) inspect.
+    "
+
+    "Created: 24.4.1997 / 18:37:17 / cg"
+    "Modified: 24.4.1997 / 18:40:02 / cg"
+!
+
+withPixelFunctionAppliedToPixels:pixelFunctionBlock in:aRectangle
+    "return a new image from the old one, by applying a pixel processor
+     on the pixel values.
+     (read `Behond photography, by Gerard J. Holzmann;
+           ISBM 0-13-074410-7)
      See blurred / oilPointed as examples ...)"
 
     |newImage newBits newBytesPerRow|
@@ -10400,7 +10512,7 @@
     newImage bitsPerSample:bitsPerSample.
     newImage colorMap:colorMap copy.
 
-    self applyPixelValuesTo:pixelFunctionBlock into:newImage.
+    self applyPixelValuesTo:pixelFunctionBlock in:aRectangle into:newImage.
     ^ newImage
 
     "oil painting effect:
@@ -10412,27 +10524,27 @@
      w := i width - 1.
      h := i height - 1.
      (i withPixelFunctionAppliedToPixels:[:oldImage :oldPixel :x :y |
-			|b p max xMin xMax yMin yMax|
-
-			b := Bag identityNew:10.
-			xMin := x-3 max:0.
-			xMax := x+3 min:w.
-			yMin := y-3 max:0.
-			yMax := y+3 min:h.
-			xMin to:xMax do:[:tx|
-			  yMin to:yMax do:[:ty|
-			    b add:(oldImage pixelAtX:tx y:ty)
-			  ]
-			].
-			max := 0.
-			b contents keysAndValuesDo:[:pixel :n |
-			    n > max ifTrue:[
-				p := pixel.
-				max := n
-			    ]
-			].
-			p
-		     ]) inspect.
+                        |b p max xMin xMax yMin yMax|
+
+                        b := Bag identityNew:10.
+                        xMin := x-3 max:0.
+                        xMax := x+3 min:w.
+                        yMin := y-3 max:0.
+                        yMax := y+3 min:h.
+                        xMin to:xMax do:[:tx|
+                          yMin to:yMax do:[:ty|
+                            b add:(oldImage pixelAtX:tx y:ty)
+                          ]
+                        ].
+                        max := 0.
+                        b contents keysAndValuesDo:[:pixel :n |
+                            n > max ifTrue:[
+                                p := pixel.
+                                max := n
+                            ]
+                        ].
+                        p
+                     ]) inspect.
     "
 
     "fisheye effect:
@@ -10448,25 +10560,25 @@
      R := w2.
      white := i valueFromColor:Color white.
      (i withPixelFunctionAppliedToPixels:[:oldImage :oldPixel :x :y |
-			|p r a nR nP nX nY|
-
-			p := (x-w2)@(y-h2).
-			r := p r.
-			a := p theta.
-			nR := r * r / R.
-			nP := Point r:nR theta:a.
-			nX := ((nP x+w2) rounded max:0) min:w.
-			nY := ((nP y+h2) rounded max:0) min:h.
-			(nX > w or:[nX < 0]) ifTrue:[
-			    white
-			] ifFalse:[
-			    (nY > h or:[nY < 0]) ifTrue:[
-				white
-			    ] ifFalse:[
-				oldImage pixelAtX:nX y:nY
-			    ]
-			]
-		     ]) inspect.
+                        |p r a nR nP nX nY|
+
+                        p := (x-w2)@(y-h2).
+                        r := p r.
+                        a := p theta.
+                        nR := r * r / R.
+                        nP := Point r:nR theta:a.
+                        nX := ((nP x+w2) rounded max:0) min:w.
+                        nY := ((nP y+h2) rounded max:0) min:h.
+                        (nX > w or:[nX < 0]) ifTrue:[
+                            white
+                        ] ifFalse:[
+                            (nY > h or:[nY < 0]) ifTrue:[
+                                white
+                            ] ifFalse:[
+                                oldImage pixelAtX:nX y:nY
+                            ]
+                        ]
+                     ]) inspect.
     "
     "fisheye effect:
 
@@ -10481,25 +10593,25 @@
      R := w2.
      white := i valueFromColor:Color white.
      (i withPixelFunctionAppliedToPixels:[:oldImage :oldPixel :x :y |
-			|p r a nR nP nX nY|
-
-			p := (x-w2)@(y-h2).
-			r := p r.
-			a := p theta.
-			nR := r * r / R.
-			nP := Point r:nR theta:a.
-			nX := (nP x+w2) rounded.
-			nY := (nP y+h2) rounded.
-			(nX > w or:[nX < 0]) ifTrue:[
-			    white
-			] ifFalse:[
-			    (nY > h or:[nY < 0]) ifTrue:[
-				white
-			    ] ifFalse:[
-				oldImage pixelAtX:nX y:nY
-			    ]
-			]
-		     ]) inspect.
+                        |p r a nR nP nX nY|
+
+                        p := (x-w2)@(y-h2).
+                        r := p r.
+                        a := p theta.
+                        nR := r * r / R.
+                        nP := Point r:nR theta:a.
+                        nX := (nP x+w2) rounded.
+                        nY := (nP y+h2) rounded.
+                        (nX > w or:[nX < 0]) ifTrue:[
+                            white
+                        ] ifFalse:[
+                            (nY > h or:[nY < 0]) ifTrue:[
+                                white
+                            ] ifFalse:[
+                                oldImage pixelAtX:nX y:nY
+                            ]
+                        ]
+                     ]) inspect.
     "
 
     "Created: 24.4.1997 / 18:37:17 / cg"
@@ -10599,6 +10711,14 @@
 
 !Image methodsFor:'obsolete'!
 
+applyPixelValuesTo:pixelFunctionBlock into:newImage in:aRectangle
+    "helper for withPixelFunctionAppliedToValues:
+     enumerate pixelValues and evaluate the block for each.
+     To be redefined by subclasses for better performance."
+
+    ^ self applyPixelValuesTo:pixelFunctionBlock in:aRectangle into:newImage 
+!
+
 magnifyBy:scale
     "obsolete: has been renamed to magnifiedBy: for ST-80 compatibility
      and name consistency ..."
@@ -12554,7 +12674,7 @@
 !Image class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libview/Image.st,v 1.347 2003-07-04 14:19:51 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libview/Image.st,v 1.348 2003-07-14 11:48:22 cg Exp $'
 ! !
 
 Image initialize!