fixed d24 -> d8 conversion;
authorClaus Gittinger <cg@exept.de>
Thu, 01 Oct 1998 12:46:03 +0200
changeset 1008 327f93d57c7b
parent 1007 98f4db3e2d23
child 1009 b153d1426731
fixed d24 -> d8 conversion; added sortColorMap function
ImageEditor.st
--- a/ImageEditor.st	Tue Sep 29 12:03:40 1998 +0200
+++ b/ImageEditor.st	Thu Oct 01 12:46:03 1998 +0200
@@ -1123,6 +1123,13 @@
                           #translateLabel: true
                           #value: #compressColorMap
                           #activeHelpKey: #compressColormap
+                          #enabled: #hasColormap
+                      )
+                       #(#MenuItem
+                          #label: 'Sort colormap'
+                          #translateLabel: true
+                          #value: #sortColorMap
+                          #enabled: #hasColormap
                       )
                     ) nil
                     nil
@@ -1170,7 +1177,7 @@
           nil
       )
 
-    "Modified: / 7.9.1998 / 18:21:27 / cg"
+    "Modified: / 30.9.1998 / 23:53:12 / cg"
 !
 
 menuMouseButtonColors
@@ -1391,6 +1398,12 @@
     "Created: / 31.7.1998 / 02:02:22 / cg"
 !
 
+hasColormap
+    ^ [self image notNil and:[self image colorMap notNil]]
+
+    "Created: / 30.9.1998 / 23:53:55 / cg"
+!
+
 imageInfoHolder
     |holder|
     (holder := builder bindingAt:#imageInfoHolder) isNil ifTrue:[
@@ -1874,11 +1887,17 @@
 
     |depth numColors newColorMap newImage 
      oldImage image newColors realColorMap oldFileName
-     usedColors useNearest usageCounts tmpBits tmpMap quest| 
+     usedColors useNearest usageCounts tmpBits tmpMap quest
+     prevMode maskThreshold maskImage| 
 
     self withExecuteCursorDo:[
         oldImage := self image.
 
+        prevMode := colorMapMode.
+        oldImage depth > 8 ifTrue:[
+            prevMode := nil
+        ].
+
         newColorMap := self class listOfColorMaps at:aMode.
         depth       := (newColorMap size log: 2) asInteger. 
 
@@ -1886,11 +1905,17 @@
         depth == 1 ifTrue:[
             quest := 'Keep colormap (or use standard B&W)'
         ] ifFalse:[
-            quest := 'Keep colormap (or use standard)'
+            prevMode isNil ifTrue:[
+                quest := 'Compute colormap (or use standard)'
+            ] ifFalse:[
+                quest := 'Keep colormap (or use standard)'
+            ]
         ].
-        ((colorMapMode value = aMode)
+        ((prevMode = aMode)
         or:[self confirm:(resources string:quest)]) ifTrue:[
+
             "/ keep the colormap
+            newColorMap atAllPut:Color black.
             depth > oldImage depth ifTrue:[
                 "/ easy - simply copy the part
                 numColors := 1 bitShift:oldImage depth.
@@ -1904,7 +1929,7 @@
                     "/ yea - just install them
                     usedColors asArray keysAndValuesDo:[:idx :clr |
                         newColorMap at:idx put:clr
-                    ]
+                    ].
                 ] ifFalse:[
                     "/ copy over those that are most often used.
                     oldImage depth < 8 ifTrue:[
@@ -1965,15 +1990,11 @@
         newImage    := (Image implementorForDepth: depth) new.  
         oldFileName := oldImage fileName.
 
-        Object errorSignal handle:
-        [:ex|
-            Object errorSignal handle:
-            [:ex|
+        Image imageErrorSignal handle:[:ex|
+            Color colorErrorSignal handle:[:ex|
                 imageEditView undo.
-                ^self warn:(resources string:'Conversion failed !!')
-            ]
-            do:
-            [
+                ^ self warn:(resources string:'Conversion failed !!')
+            ] do:[
                 newImage := (Image implementorForDepth: depth) new.
                 newImage width:oldImage width height:oldImage height depth:depth.
                 newImage colorMap:newColorMap.
@@ -1987,33 +2008,37 @@
                     (newColorMap includes:clr)
                         ifTrue: [newColor := clr]
                         ifFalse: [
-                            useNearest ifTrue:[
-                                newColor := clr nearestIn:newColorMap
-                            ] ifFalse:[
-                                newColor := oldImage colorMap first
+                            newColor := clr nearestIn:newColorMap.
+                            useNearest ifFalse:[
+                                (newColor deltaFrom:clr) > 0.5 ifTrue:[
+                                    newColor := oldImage colorFromValue:0
+                                ]
                             ]
                         ].
                     newImage colorAtX:x y:y put:newColor.
                 ].
                 image := newImage
             ].
-        ] 
-        do:
-        [ 
-            image := newImage fromImage: oldImage
+        ] do:[ 
+            image := newImage fromImage:oldImage
         ].
 
-        (aMode asString endsWith:'mask')
-        ifTrue:
-        [
-            image mask isNil
-            ifTrue:
-            [
-                image mask: (Depth1Image fromImage: (image asThresholdMonochromeImage: 0.1)). 
+        (aMode asString endsWith:'mask') ifTrue:[
+            image mask isNil ifTrue:[
+                (self confirm:'Generate mask from black ?') ifTrue:[
+                    maskThreshold := 0.1.
+                    maskImage := Depth1Image fromImage:(image asThresholdMonochromeImage:maskThreshold). 
+                ] ifFalse:[
+                    maskImage := Depth1Image extent:image extent.
+                    maskImage data:(ByteArray 
+                                        new:(maskImage bytesPerRow * maskImage height)
+                                        withAll:16rFF).
+
+"/                    maskImage fillRectangle:(image bounds) withColor:(Color colorId:1).
+                ].
+                image mask:maskImage.
             ].
-        ]
-        ifFalse:
-        [ 
+        ] ifFalse:[ 
             image mask: nil.
         ]. 
         realColorMap := OrderedCollection new.
@@ -2041,7 +2066,7 @@
         ]
     ]
 
-    "Modified: / 7.9.1998 / 17:57:55 / cg"
+    "Modified: / 30.9.1998 / 23:44:19 / cg"
 !
 
 compressColorMap
@@ -2144,6 +2169,98 @@
 
     "Created: / 28.7.1998 / 20:03:11 / cg"
     "Modified: / 15.9.1998 / 17:53:32 / cg"
+!
+
+sortColorMap
+    "calculates a new color map for the image, sorting colors"
+
+    |depth newColorMap newImage oldImage usedColors oldToNew oldBits newBits tmpBits| 
+
+    oldImage := self image.
+    depth := oldImage depth.
+
+    oldImage photometric ~~ #palette ifTrue:[
+        self information:'Compress colorMap: Only palette images have colormaps.'.
+        ^ self
+    ].
+
+    usedColors := oldImage realColorMap.
+
+    imageEditView makeUndo.
+
+    self withExecuteCursorDo:[
+"/        newColorMap := Array new:usedColors size.
+
+        "/ translation table
+        oldToNew := ByteArray new:(1 bitShift:depth).
+        newColorMap := usedColors asArray.
+        newColorMap sort:[:a :b |
+                                a redByte == b redByte ifTrue:[
+                                    a greenByte == b greenByte ifTrue:[
+                                        a blueByte < b blueByte
+                                    ] ifFalse:[
+                                        a greenByte < b greenByte 
+                                    ]
+                                ] ifFalse:[
+                                    a redByte < b redByte 
+                                ]
+                          ].
+
+        oldImage colorMap asArray keysAndValuesDo:[:oldIdx :clr |
+            |newPixel|
+
+            (usedColors includes:clr) ifTrue:[
+                newPixel := newColorMap indexOf:clr.
+                oldToNew at:oldIdx put:newPixel-1.
+            ]
+        ].
+
+        oldBits := oldImage bits.
+        newBits := ByteArray new:(oldBits size).
+        depth ~~ 8 ifTrue:[
+            "/ expand/compress can only handle 8bits
+            tmpBits := ByteArray uninitializedNew:(oldImage width*oldImage height).
+            oldBits
+                expandPixels:depth
+                width:oldImage width
+                height:oldImage height 
+                into:tmpBits
+                mapping:oldToNew.
+            tmpBits
+                compressPixels:depth 
+                width:oldImage width 
+                height:oldImage height 
+                into:newBits 
+                mapping:nil
+        ] ifFalse:[
+            oldBits
+                expandPixels:depth
+                width:oldImage width
+                height:oldImage height 
+                into:newBits
+                mapping:oldToNew.
+        ].
+
+        newImage := oldImage species new
+                        width:oldImage width
+                        height:oldImage height
+                        depth:depth
+                        fromArray:newBits.
+
+        newImage colorMap:newColorMap.  
+        newImage fileName:oldImage fileName.
+        newImage mask:(oldImage mask copy).
+
+        (imageEditView image:newImage) notNil ifTrue:
+        [
+            self listOfColors contents: newImage colorMap.
+            self findColorMapMode.
+            self updateLabelsAndHistory.
+        ]
+    ]
+
+    "Modified: / 15.9.1998 / 17:53:32 / cg"
+    "Created: / 30.9.1998 / 23:51:23 / cg"
 ! !
 
 !ImageEditor methodsFor:'user actions - editing'!