support pasing 24bit rgb image into an 8-bit palette image
authorClaus Gittinger <cg@exept.de>
Wed, 01 Aug 2001 13:01:06 +0200
changeset 1986 630db3277f10
parent 1985 1e21a6928dfb
child 1987 eb9f2f7f1450
support pasing 24bit rgb image into an 8-bit palette image
ImageEditView.st
--- a/ImageEditView.st	Fri Jul 27 23:13:13 2001 +0200
+++ b/ImageEditView.st	Wed Aug 01 13:01:06 2001 +0200
@@ -1130,7 +1130,8 @@
 pasteAt: aPoint modeUnder:modeUnder
     "paste the image in the clipboard at aPoint"
 
-    |answer|
+    |answer anyColorMissing choosedBox imagePoint imgX imgY copiedImage imageBox newColorMap
+     existingColors newColors allColors currentColorMap newColormap|
 
     ClipboardMagnification isNil ifTrue: [
         "/ ^self
@@ -1145,8 +1146,6 @@
         self warn: 'Paste failed !!\Increasing the images depth might help.' withCRs. 
     ] do: [   
         windowGroup withExecuteCursorDo:[
-            |anyColorMissing choosedBox imagePoint imgX imgY copiedImage imageBox presentClr newColorMap|
-
             choosedBox := self dragRectangleStartingAt: aPoint emphasis: #inverseFilledBox.
 
             imagePoint := choosedBox origin//magnification.
@@ -1195,23 +1194,84 @@
                                 default:1.
 
                     answer isNil ifTrue:[^ self].
+
+                    currentColorMap := image colorMap.
+
                     answer == #nearest ifTrue:[
                         0 to:copiedImage height-1 do:[:y |
                             0 to:copiedImage width-1 do:[:x |
                                 |clr n_clr|
 
                                 clr := copiedImage atX:x y:y.
-                                n_clr := clr nearestIn:image colorMap.
+                                n_clr := clr nearestIn:currentColorMap.
                                 copiedImage atX:x y:y put:n_clr
                             ]
                         ].
                     ] ifFalse:[
+                        answer == #new ifTrue:[
+                            existingColors := image usedValues asIdentitySet.
+                            newColors := copiedImage usedValues.
+                            allColors := existingColors addAll:newColors.
+                            allColors size > (1 bitShift:image depth) ifTrue:[
+                                self warn:'Sorry: too many colors - unimplemented function'.
+                                self undo.
+                                ^ self.
+                            ].
+
+                            newColormap := OrderedCollection new.
+                            newColormap addAll:image usedColors.
+                            newColormap addAll:copiedImage usedColors.
+                            "/ translate image to use new colorMap...
+
+                            "/ translate image to use new colors ...
+                            0 to:image height-1 do:[:y |
+                                0 to:image width-1 do:[:x |
+                                    |clr n_idx|
+
+                                    (image maskAtX:x y:y) == 1 ifTrue:[
+                                        clr := image atX:x y:y.
+                                        n_idx := newColormap indexOf:clr.
+                                        image pixelAtX:x y:y put:n_idx-1.
+                                    ]
+                                ]
+                            ].
+                            image colorMap:newColormap.
+                            currentColorMap := newColormap.    
+
+                            "/ paste new image...    
+                            imgX := imagePoint x.
+                            imgY := imagePoint y.
+
+                            0 to:copiedImage height-1 do:[:y |
+                                0 to:copiedImage width-1 do:[:x |
+                                    |clr idx|
+
+                                    (copiedImage maskAtX:x y:y) == 1
+                                    ifTrue:[
+                                        clr := copiedImage atX:x y:y.
+                                        idx := currentColorMap indexOf:clr.
+                                        idx == 0 ifTrue:[
+                                            currentColorMap add:clr.
+                                            idx := currentColorMap size.
+                                            idx > (1 bitShift:image depth) ifTrue:[
+                                                self halt.
+                                            ].
+                                        ].
+                                        image pixelAtX:imgX+x y:imgY+y put:idx-1.
+                                        image maskAtX:imgX+x y:imgY+y put:1.
+                                    ]
+                                ]
+                            ].
+                            image restored.
+                            self redraw: (imageBox := (imagePoint * magnification extent: (Clipboard extent * magnification)) expandedBy: 1@1).
+                            modified := true.
+                            ^ self.
+                        ].
                         self warn:'Sorry: unimplemented function'.
                         self undo.
                         ^ self.
                     ].
                 ].
-
                 newColorMap notNil ifTrue:[
                     image colorMap:(Colormap fromColors:newColorMap).
                 ].
@@ -1252,11 +1312,11 @@
                                            y:dstY
                                          put:(copiedImage colorAtX:x y:y).
                                 wasMasked ifTrue:[
-                                    image mask pixelAtX:dstX y:dstY put:1
+                                    image maskAtX:dstX y:dstY put:1
                                 ].
                             ] ifTrue:[
                                 wasMasked ifFalse:[
-                                    image mask pixelAtX:dstX y:dstY put:0
+                                    image maskAtX:dstX y:dstY put:0
                                 ]
                             ].
                         ]
@@ -2014,6 +2074,6 @@
 !ImageEditView class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libwidg2/ImageEditView.st,v 1.145 2001-07-23 15:23:31 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libwidg2/ImageEditView.st,v 1.146 2001-08-01 11:01:06 cg Exp $'
 ! !
 ImageEditView initialize!