ImageEditor.st
changeset 2054 56352366d94a
parent 2044 830fea482888
child 2058 584f6f6bb171
--- a/ImageEditor.st	Wed Jun 14 11:14:54 2006 +0200
+++ b/ImageEditor.st	Fri Jul 07 13:45:23 2006 +0200
@@ -2162,6 +2162,11 @@
                         itemValue: reduceNumberOfColors
                         translateLabel: true
                       )
+                     (MenuItem
+                        label: 'Dither to Depth...'
+                        itemValue: ditherToDepth
+                        translateLabel: true
+                      )
                      )
                     nil
                     nil
@@ -2282,6 +2287,8 @@
         nil
         nil
       )
+
+    "Modified: / 07-07-2006 / 13:23:01 / cg"
 !
 
 menuMouseButtonColors
@@ -2997,10 +3004,12 @@
         listOfColors removeAll.
         image colorMap isEmptyOrNil ifTrue:[
             someOrAllUsedColors := image usedColorsMax:10000.
-            someOrAllUsedColors := someOrAllUsedColors asArray.
-            someOrAllUsedColors sort:self sortBlockForColors.
-            listOfColors addAll:someOrAllUsedColors.
-            "/ listOfColors add:Color black; add:Color white.
+            someOrAllUsedColors notNil ifTrue:[
+                someOrAllUsedColors := someOrAllUsedColors asArray.
+                someOrAllUsedColors sort:self sortBlockForColors.
+                listOfColors addAll:someOrAllUsedColors.
+                "/ listOfColors add:Color black; add:Color white.
+            ]
         ] ifFalse:[
             listOfColors addAll:(image colorMap).
         ].
@@ -3009,24 +3018,28 @@
             self colorMapMode: colorMapMode value.
             image := self image.
         ].                               
-    ].                               
-    drawColor1 := listOfColors at:1.
-    drawColor2 := listOfColors at:2 ifAbsent:drawColor1.
-
-    self hasMask ifTrue: [             
-        (listOfColors contains: [:clr| clr = (Color colorId:0)]) 
-        ifFalse:[
-            listOfColors addFirst:(Color colorId:0).
-            drawColor1 := listOfColors at:2. 
-            drawColor2 := listOfColors at:3 ifAbsent:drawColor1.
-        ]
+    ].  
+    listOfColors notEmptyOrNil ifTrue:[
+        drawColor1 := listOfColors at:1.
+        drawColor2 := listOfColors at:2 ifAbsent:drawColor1.
+
+        self hasMask ifTrue: [             
+            (listOfColors contains: [:clr| clr = (Color colorId:0)]) 
+            ifFalse:[
+                listOfColors addFirst:(Color colorId:0).
+                drawColor1 := listOfColors at:2. 
+                drawColor2 := listOfColors at:3 ifAbsent:drawColor1.
+            ]
+        ].
+
+        imageEditView drawingColors:(Array with: drawColor1 with: drawColor2).
+
+        self selectionOfColor 
+            setValue: 0;
+            value: (listOfColors indexOf: imageEditView selectedColor).
     ].
 
-    imageEditView drawingColors:(Array with: drawColor1 with: drawColor2).
-
-    self selectionOfColor 
-        setValue: 0;
-        value: (listOfColors indexOf: imageEditView selectedColor).
+    "Modified: / 07-07-2006 / 12:27:43 / cg"
 !
 
 update:something with:aParameter from:changedObject
@@ -4230,6 +4243,59 @@
     MaskClipboard := mask subImageIn: (0@0 extent:mask extent).
 !
 
+ditherToDepth
+    |depth|
+
+    depth := Dialog request:'New depth ?'.
+    depth isEmptyOrNil ifTrue:[^ self].
+    depth := Number readFrom:depth onError:nil.
+    depth isNil ifTrue:[^ self].
+
+    self ditherToDepth:depth
+
+    "Created: / 07-07-2006 / 13:22:10 / cg"
+!
+
+ditherToDepth:depth
+    |ditherColors newImage|
+
+    depth = 1 ifTrue:[
+        ditherColors := Array with:(Color black) with:(Color white).
+    ] ifFalse:[ depth = 2 ifTrue:[
+        ditherColors := Array 
+                            with:(Color black) 
+                            with:(Color darkGrey)
+                            with:(Color lightGrey)
+                            with:(Color white).
+    ] ifFalse:[ depth = 3 ifTrue:[
+        ditherColors := Color colorCubeWithRed:2 green:2 blue:2. 
+    ] ifFalse:[ depth = 4 ifTrue:[
+        ditherColors := Color vgaColors. 
+    ] ifFalse:[ depth = 5 ifTrue:[
+        ditherColors := Color colorCubeWithRed:4 green:4 blue:2. 
+    ] ifFalse:[ depth = 5 ifTrue:[
+        ditherColors := Color colorCubeWithRed:4 green:4 blue:3. 
+    ] ifFalse:[ depth <= 8 ifTrue:[
+        ditherColors := Color colorCubeWithRed:6 green:6 blue:5. 
+    ] ifFalse:[ 
+        self halt.
+    ]]]]]]].                           
+
+    self withExecuteCursorDo:[
+        newImage := self image asDitheredImageUsing:ditherColors depth:8.
+
+        imageEditView makeUndo.
+        imageEditView image:newImage.
+        imageEditView setModified.
+        self updateImage.
+        self updateImagePreView.
+
+        self fetchImageData.
+    ].
+
+    "Created: / 07-07-2006 / 13:20:56 / cg"
+!
+
 editSelectedColor
     self processSelectedColorWith:[:clr | 
         |editor|
@@ -4472,7 +4538,7 @@
 
     self withExecuteCursorDo:[
         image := self image.
-        usedColors := image usedColors.
+        "/ usedColors := image usedColorsMax:4096.
         imageEditView makeUndo.
 
         newImage := image copy.
@@ -4505,6 +4571,8 @@
             Dialog information:(usedColors size printString , ' colors used.')
         ]
     ].
+
+    "Modified: / 07-07-2006 / 13:10:42 / cg"
 !
 
 selectedColorIndexOrNil