Image.st
changeset 3905 bc5f7af0d9d9
parent 3880 c4c8268a2d9f
child 3906 f77d3b42614a
--- a/Image.st	Tue Jul 01 23:19:58 2003 +0200
+++ b/Image.st	Wed Jul 02 17:22:31 2003 +0200
@@ -10221,6 +10221,107 @@
     "Modified: 24.4.1997 / 18:33:42 / cg"
 !
 
+withColorResolutionReducedBy:numBits
+    "return a new image with the same picture as the receiver, but reduced colorResolution;
+     that is, the lower numBits are cleared in the r/g/b color components.
+     If anything fails, return nil."
+
+    |xMax yMax r g b nR nG nB clr pix map revMap n_clr n_pix mask anyChange
+     newColors newColorArray newImage extMask extBits newPixelValue|
+
+    numBits > 7 ifTrue:[
+        ^ nil
+    ].
+    mask := (16rFF bitShift:numBits) bitAnd:16rFF.
+    extMask := (1 bitShift:numBits).
+    extBits := extMask - 1.
+
+    anyChange := false.
+
+    newColors := Set new.
+    newColorArray := OrderedCollection new.
+    map := Array new:256.
+    revMap := OrderedCollection new.
+
+    newImage := self class width:width height:height depth:depth.
+    newImage photometric:photometric.
+    newImage colorMap:(self colorMap copy).
+    newImage bits:(self bits copy).
+    newImage mask:(self mask copy).
+
+    xMax := width - 1.
+    yMax := height - 1.
+
+    newPixelValue := 
+        [:image :pixelValue |
+            |r g b nR nG nB|
+
+            r := image redBitsOf:pixelValue.
+            g := image greenBitsOf:pixelValue.
+            b := image blueBitsOf:pixelValue.
+            nR := r bitAnd:mask. (nR bitAnd:extMask)~~0 ifTrue:[nR := nR bitOr:extBits].
+            nG := g bitAnd:mask. (nG bitAnd:extMask)~~0 ifTrue:[nG := nG bitOr:extBits].
+            nB := b bitAnd:mask. (nB bitAnd:extMask)~~0 ifTrue:[nB := nB bitOr:extBits].
+            image valueFromRedBits:nR greenBits:nG blueBits:nB.
+        ].
+
+
+    photometric == #palette ifFalse:[
+        "/ direct manipulation of the pixels
+        0 to:yMax do:[:y |
+            0 to:xMax do:[:x |
+                pix := self pixelAtX:x y:y.
+                n_pix := newPixelValue value:self value:pix.
+                n_pix ~= pix ifTrue:[
+                    newImage pixelAtX:x y:y put:n_pix.
+                    anyChange := true.
+                ]
+            ]
+        ].
+        anyChange ifFalse:[
+            ^ nil
+        ].
+    ] ifTrue:[
+        "/ manipulate the colormap
+        0 to:yMax do:[:y |
+            0 to:xMax do:[:x |
+                pix := self pixelAtX:x y:y.
+                (n_pix := map at:pix+1) isNil ifTrue:[
+                    clr := self colorAtX:x y:y.
+
+                    r := clr redByte.
+                    g := clr greenByte.
+                    b := clr blueByte.
+                    nR := r bitAnd:mask. (nR bitAnd:extMask)~~0 ifTrue:[nR := nR bitOr:extBits].
+                    nG := g bitAnd:mask. (nR bitAnd:extMask)~~0 ifTrue:[nR := nR bitOr:extBits].
+                    nB := b bitAnd:mask. (nR bitAnd:extMask)~~0 ifTrue:[nR := nR bitOr:extBits].
+                    n_clr := Color redByte:nR greenByte:nG blueByte:nB.
+                    (newColors includes:n_clr) ifFalse:[
+                        newColors add:n_clr.
+                        newColorArray add:n_clr.
+                        revMap add:pix.
+                        map at:pix+1 put:(n_pix := revMap size - 1).
+                    ] ifTrue:[
+                        "/ mhmh - multiple pixels mapped to the same color
+                        n_pix := (newColorArray indexOf:n_clr) - 1.
+                        map at:pix+1 put:n_pix.
+                    ]
+                ].
+                newImage pixelAtX:x y:y put:n_pix.
+            ]
+        ].
+        revMap size == self colorMap size ifTrue:[
+            revMap = (0 to:revMap size-1) ifTrue:[
+                ^ nil
+            ]
+        ].
+
+        newImage colorMap:(MappedPalette withColors:newColorArray).
+    ].
+
+    ^ newImage
+!
+
 withPixelFunctionApplied:pixelFunctionBlock
     "return a new image from the old one, by applying a pixel processor
      on the pixel colors.
@@ -12477,7 +12578,7 @@
 !Image class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libview/Image.st,v 1.344 2003-05-07 14:29:48 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libview/Image.st,v 1.345 2003-07-02 15:22:31 cg Exp $'
 ! !
 
 Image initialize!