ImageEditor.st
changeset 1935 27b19cd0274b
parent 1934 294d0e102881
child 1936 e2ce1a4139a2
equal deleted inserted replaced
1934:294d0e102881 1935:27b19cd0274b
  3840             ]
  3840             ]
  3841         ].
  3841         ].
  3842         ((prevMode = aMode)
  3842         ((prevMode = aMode)
  3843         or:[depth > oldImage depth
  3843         or:[depth > oldImage depth
  3844         or:[self confirm:(resources string:quest)]]) ifTrue:[
  3844         or:[self confirm:(resources string:quest)]]) ifTrue:[
  3845 
  3845             (newColorMap isFixedPalette) ifFalse:[
  3846             "/ keep the colormap
  3846 
  3847             newColorMap atAllPut:Color black.
  3847                 "/ keep the colormap
  3848             depth > oldImage depth ifTrue:[
  3848                 newColorMap atAllPut:Color black.
  3849                 "/ easy - simply copy the part
  3849                 depth > oldImage depth ifTrue:[
  3850                 numColors := 1 bitShift:oldImage depth.
  3850                     "/ easy - simply copy the part
  3851                 0 to:numColors-1 do:[:pixel |
  3851                     numColors := 1 bitShift:oldImage depth.
  3852                     newColorMap at:(pixel+1) put:(oldImage colorFromValue:pixel)
  3852                     0 to:numColors-1 do:[:pixel |
  3853                 ].
  3853                         newColorMap at:(pixel+1) put:(oldImage colorFromValue:pixel)
  3854             ] ifFalse:[
       
  3855                 "/ see if all used color fit the new colormap
       
  3856                 usedColors := oldImage usedColorsMax:(1 bitShift:depth).
       
  3857                 (usedColors notNil and:[usedColors size > (1 bitShift:depth)]) ifTrue:[
       
  3858                     usedColors := oldImage realUsedColors
       
  3859                 ].
       
  3860                 (usedColors notNil and:[usedColors size <= (1 bitShift:depth)]) ifTrue:[
       
  3861                     "/ yea - just install them
       
  3862                     usedColors asArray keysAndValuesDo:[:idx :clr |
       
  3863                         newColorMap at:idx put:clr
       
  3864                     ].
  3854                     ].
  3865                 ] ifFalse:[
  3855                 ] ifFalse:[
  3866                     "/ copy over those that are most often used.
  3856                     "/ see if all used color fit the new colormap
  3867                     oldImage depth < 8 ifTrue:[
  3857                     usedColors := oldImage usedColorsMax:(1 bitShift:depth).
  3868                         tmpBits := ByteArray uninitializedNew:(oldImage width*oldImage height).
  3858                     (usedColors notNil and:[usedColors size > (1 bitShift:depth)]) ifTrue:[
  3869                         oldImage bits
  3859                         usedColors := oldImage realUsedColors
  3870                             expandPixels:(oldImage depth)
  3860                     ].
  3871                             width:oldImage width
  3861                     (usedColors notNil and:[usedColors size <= (1 bitShift:depth)]) ifTrue:[
  3872                             height:oldImage height 
  3862                         "/ yea - just install them
  3873                             into:tmpBits
  3863                         usedColors asArray keysAndValuesDo:[:idx :clr |
  3874                             mapping:nil.
  3864                             newColorMap at:idx put:clr
       
  3865                         ].
  3875                     ] ifFalse:[
  3866                     ] ifFalse:[
  3876                         oldImage depth == 8 ifTrue:[
  3867                         "/ copy over those that are most often used.
  3877                             tmpBits := oldImage bits
  3868                         oldImage depth < 8 ifTrue:[
       
  3869                             tmpBits := ByteArray uninitializedNew:(oldImage width*oldImage height).
       
  3870                             oldImage bits
       
  3871                                 expandPixels:(oldImage depth)
       
  3872                                 width:oldImage width
       
  3873                                 height:oldImage height 
       
  3874                                 into:tmpBits
       
  3875                                 mapping:nil.
  3878                         ] ifFalse:[
  3876                         ] ifFalse:[
       
  3877                             oldImage depth == 8 ifTrue:[
       
  3878                                 tmpBits := oldImage bits
       
  3879                             ] ifFalse:[
       
  3880                                 colorMapMode value:prevMode.
       
  3881                                 self findColorMapMode.    
       
  3882                                 self warn:('Too many used colors in image (', oldImage usedColors size printString , ').').
       
  3883                                 ^ self
       
  3884     "/                            (self confirm:('Too many used colors in image (', oldImage usedColors size printString , ').\\Dither ?' withCRs))
       
  3885     "/                            ifFalse:[.
       
  3886     "/                                ^ self
       
  3887     "/                            ].
       
  3888     "/                            self image: (Image newForDepth:depth) fromImage:oldImage.
       
  3889     "/                            ^ self.
       
  3890                             ]
       
  3891                         ].
       
  3892                         usageCounts := tmpBits usageCounts.
       
  3893                         tmpMap := Array new:usageCounts size.
       
  3894                         oldImage colorMap asArray keysAndValuesDo:[:i :clr |
       
  3895                             tmpMap at:i put:clr
       
  3896                         ].
       
  3897                         usageCounts sort:[:a :b | a > b] with:tmpMap.
       
  3898 
       
  3899                         1 to:(1 bitShift:depth) do:[:idx |
       
  3900                             newColorMap at:idx put:(tmpMap at:idx)
       
  3901                         ].
       
  3902 
       
  3903                         useNearest := Dialog 
       
  3904                                             confirmWithCancel:(resources 
       
  3905                                                                   stringWithCRs:'Image requires %1 colors.\ColorMap has only space for %2\\Use nearest (or map to first color) ?'
       
  3906                                                                   with:usedColors size
       
  3907                                                                   with:(1 bitShift:depth))
       
  3908                                             labels:(resources array:#('Cancel' 'First' 'Nearest')).
       
  3909                         useNearest isNil ifTrue:[
  3879                             colorMapMode value:prevMode.
  3910                             colorMapMode value:prevMode.
  3880                             self findColorMapMode.    
  3911                             ^ self   "/ cancel
  3881                             self warn:('Too many used colors in image (', oldImage usedColors size printString , ').').
  3912                         ].
  3882                             ^ self
  3913                     ]
  3883 "/                            (self confirm:('Too many used colors in image (', oldImage usedColors size printString , ').\\Dither ?' withCRs))
       
  3884 "/                            ifFalse:[.
       
  3885 "/                                ^ self
       
  3886 "/                            ].
       
  3887 "/                            self image: (Image newForDepth:depth) fromImage:oldImage.
       
  3888 "/                            ^ self.
       
  3889                         ]
       
  3890                     ].
       
  3891                     usageCounts := tmpBits usageCounts.
       
  3892                     tmpMap := Array new:usageCounts size.
       
  3893                     oldImage colorMap asArray keysAndValuesDo:[:i :clr |
       
  3894                         tmpMap at:i put:clr
       
  3895                     ].
       
  3896                     usageCounts sort:[:a :b | a > b] with:tmpMap.
       
  3897 
       
  3898                     1 to:(1 bitShift:depth) do:[:idx |
       
  3899                         newColorMap at:idx put:(tmpMap at:idx)
       
  3900                     ].
       
  3901 
       
  3902                     useNearest := Dialog 
       
  3903                                         confirmWithCancel:(resources 
       
  3904                                                               stringWithCRs:'Image requires %1 colors.\ColorMap has only space for %2\\Use nearest (or map to first color) ?'
       
  3905                                                               with:usedColors size
       
  3906                                                               with:(1 bitShift:depth))
       
  3907                                         labels:(resources array:#('Cancel' 'First' 'Nearest')).
       
  3908                     useNearest isNil ifTrue:[
       
  3909                         colorMapMode value:prevMode.
       
  3910                         ^ self   "/ cancel
       
  3911                     ].
       
  3912                 ]
  3914                 ]
  3913             ]
  3915             ]
  3914         ] ifFalse:[
  3916         ] ifFalse:[
  3915             "/ standard colormap
  3917             "/ standard colormap
  3916             usedColors := oldImage usedColors.
  3918             usedColors := oldImage usedColors.
  3983                 image mask:maskImage.
  3985                 image mask:maskImage.
  3984             ].
  3986             ].
  3985         ] ifFalse:[ 
  3987         ] ifFalse:[ 
  3986             image mask: nil.
  3988             image mask: nil.
  3987         ]. 
  3989         ]. 
  3988         realColorMap := OrderedCollection new.
  3990 
  3989         image realColorMap do:[:clr|
  3991         newColorMap isFixedPalette ifFalse:[
  3990             (realColorMap includes: clr) ifFalse: [realColorMap add: clr]
  3992             realColorMap := OrderedCollection new.
       
  3993             image realColorMap do:[:clr|
       
  3994                 (realColorMap includes: clr) ifFalse: [realColorMap add: clr]
       
  3995             ].
       
  3996             newColors := realColorMap copyFrom: 1 to: (newColorMap size min: realColorMap size).
       
  3997             newColorMap do:[:clr|
       
  3998                 ((newColors size < newColorMap size) and: [(newColors includes: clr) not]) 
       
  3999                 ifTrue:[      
       
  4000                     newColors add: clr
       
  4001                 ]
       
  4002             ].                  
       
  4003             image colorMap: newColors.   
  3991         ].
  4004         ].
  3992         newColors := realColorMap copyFrom: 1 to: (newColorMap size min: realColorMap size).
       
  3993         newColorMap do:[:clr|
       
  3994             ((newColors size < newColorMap size) and: [(newColors includes: clr) not]) 
       
  3995             ifTrue:[      
       
  3996                 newColors add: clr
       
  3997             ]
       
  3998         ].                  
       
  3999         image colorMap: newColors.   
       
  4000         image fileName: oldFileName.
  4005         image fileName: oldFileName.
  4001 
  4006 
  4002         (imageEditView image: image) notNil ifTrue:[
  4007         (imageEditView image: image) notNil ifTrue:[
  4003             self fetchImageData.
  4008             self fetchImageData.
  4004         ]
  4009         ]