ImageEditView.st
changeset 5492 f3e1ee8eaf5a
parent 5491 a78da5678d29
child 5493 1840ba5cf10b
--- a/ImageEditView.st	Mon Feb 20 17:49:18 2017 +0100
+++ b/ImageEditView.st	Mon Feb 20 18:09:27 2017 +0100
@@ -1055,6 +1055,103 @@
     "Created: / 20-02-2017 / 17:37:23 / cg"
 !
 
+autoCropLeft:doLeft right:doRight top:doTop bottom:doBottom
+    |yMinNew yMaxNew xMinNew xMaxNew
+     pix stillCropping xMax yMax|
+
+    xMax := image width - 1.
+    yMax := image height - 1.
+
+    xMinNew := 0.
+    doLeft ifTrue:[
+        pix := image pixelAtX:xMinNew y:0.
+        stillCropping := true.
+        [stillCropping] whileTrue:[
+            0 to:yMax do:[:y |
+                (image pixelAtX:xMinNew y:y) ~~ pix ifTrue:[
+                    stillCropping := false
+                ]
+            ].
+            stillCropping ifTrue:[
+                xMinNew := xMinNew + 1.
+                xMinNew >= image width ifTrue:[
+                    self warn:(resources string:'Image is all the same color - no crop.').
+                    ^ self
+                ]
+            ].
+        ].
+    ].
+
+    xMaxNew := xMax.
+    doRight ifTrue:[
+        stillCropping := true.
+        pix := image pixelAtX:xMaxNew y:0.
+        [stillCropping] whileTrue:[
+            0 to:yMax do:[:y |
+                (image pixelAtX:xMaxNew y:y) ~~ pix ifTrue:[
+                    stillCropping := false
+                ]
+            ].
+            stillCropping ifTrue:[
+                xMaxNew := xMaxNew - 1.
+            ].
+        ].
+    ].
+
+    yMinNew := 0.
+    doTop ifTrue:[
+        pix := image pixelAtX:xMinNew y:yMinNew.
+        stillCropping := true.
+        [stillCropping] whileTrue:[
+            xMinNew to:xMaxNew do:[:x |
+                (image pixelAtX:x y:yMinNew) ~~ pix ifTrue:[
+                    stillCropping := false
+                ]
+            ].
+            stillCropping ifTrue:[
+                yMinNew := yMinNew + 1.
+            ].
+        ].
+    ].
+
+    yMaxNew := yMax.
+    doRight ifTrue:[
+        stillCropping := true.
+        pix := image pixelAtX:xMaxNew y:yMaxNew.
+        [stillCropping] whileTrue:[
+            xMinNew to:xMaxNew do:[:x |
+                (image pixelAtX:x y:yMaxNew) ~~ pix ifTrue:[
+                    stillCropping := false
+                ]
+            ].
+            stillCropping ifTrue:[
+                yMaxNew := yMaxNew - 1.
+            ].
+        ].
+    ].
+
+    (xMinNew == 0
+    and:[xMaxNew == (image width - 1)
+    and:[yMinNew == 0
+    and:[yMaxNew == (image height - 1)]]]) ifTrue:[
+        self warn:(resources string:'No border found - no crob.').
+        ^ self
+    ].
+
+"/    self warn:'extract subImage ' 
+"/              , (xMinNew @ yMinNew) printString
+"/              , ' -> '
+"/              , (xMaxNew @ yMaxNew) printString.
+
+    self makeUndo.
+    self 
+        makeSubImageX:xMinNew y:yMinNew 
+        width:(xMaxNew - xMinNew + 1)
+        height:(yMaxNew - yMinNew + 1)
+
+    "Created: / 20-02-2017 / 17:59:05 / cg"
+!
+
 copyImageToClipboard
     self class copyImageToClipboard:image.
     self setClipboardObject:image.