#FEATURE by cg
class: Image
added: #createMaskForPixelValue:
comment/format in: #createMask
changed: #createMaskForIndex:
--- a/Image.st Thu Feb 16 20:42:11 2017 +0100
+++ b/Image.st Fri Feb 17 11:23:19 2017 +0100
@@ -10538,6 +10538,8 @@
!
createMask
+ "create a mask filled with one's (i.e. all pixels opaque)"
+
|maskArray bytesPerMaskRow|
bytesPerMaskRow := (width+7) // 8.
@@ -10545,6 +10547,35 @@
maskArray := ByteArray new:(bytesPerMaskRow * height) withAll:2r11111111.
mask := ImageMask width:width height:height fromArray:maskArray.
^ mask
+
+ "Modified (comment): / 17-02-2017 / 07:59:07 / cg"
+!
+
+createMaskForPixelValue:pixelValue
+ "create or modify the mask to be off wherever the pixelValue appears.
+ If there is already a mask, pixels which are already masked remain so.
+ This is a helper for image readers which use a pixel index as mask,
+ instead of a separate mask plane.
+
+ This is a slow fallback, if it turns out to be timing relevant,
+ redefine in concrete image classes (especially Depth8Image)"
+
+ |mr|
+
+ mask isNil ifTrue:[
+ mask := self createMask.
+ ].
+ 0 to:height-1 do:[:y |
+ mr := mask rowAt:y.
+ 0 to:width-1 do:[:x |
+ (self pixelAtX:x y:y) == pixelValue ifTrue:[
+ mr at:x+1 put:0
+ ].
+ ].
+ mask rowAt:y putAll:mr
+ ].
+
+ "Created: / 17-02-2017 / 11:19:09 / cg"
!
darkened