Image.st
changeset 3905 bc5f7af0d9d9
parent 3880 c4c8268a2d9f
child 3906 f77d3b42614a
equal deleted inserted replaced
3904:52925c65a8cc 3905:bc5f7af0d9d9
 10219     "
 10219     "
 10220 
 10220 
 10221     "Modified: 24.4.1997 / 18:33:42 / cg"
 10221     "Modified: 24.4.1997 / 18:33:42 / cg"
 10222 !
 10222 !
 10223 
 10223 
       
 10224 withColorResolutionReducedBy:numBits
       
 10225     "return a new image with the same picture as the receiver, but reduced colorResolution;
       
 10226      that is, the lower numBits are cleared in the r/g/b color components.
       
 10227      If anything fails, return nil."
       
 10228 
       
 10229     |xMax yMax r g b nR nG nB clr pix map revMap n_clr n_pix mask anyChange
       
 10230      newColors newColorArray newImage extMask extBits newPixelValue|
       
 10231 
       
 10232     numBits > 7 ifTrue:[
       
 10233         ^ nil
       
 10234     ].
       
 10235     mask := (16rFF bitShift:numBits) bitAnd:16rFF.
       
 10236     extMask := (1 bitShift:numBits).
       
 10237     extBits := extMask - 1.
       
 10238 
       
 10239     anyChange := false.
       
 10240 
       
 10241     newColors := Set new.
       
 10242     newColorArray := OrderedCollection new.
       
 10243     map := Array new:256.
       
 10244     revMap := OrderedCollection new.
       
 10245 
       
 10246     newImage := self class width:width height:height depth:depth.
       
 10247     newImage photometric:photometric.
       
 10248     newImage colorMap:(self colorMap copy).
       
 10249     newImage bits:(self bits copy).
       
 10250     newImage mask:(self mask copy).
       
 10251 
       
 10252     xMax := width - 1.
       
 10253     yMax := height - 1.
       
 10254 
       
 10255     newPixelValue := 
       
 10256         [:image :pixelValue |
       
 10257             |r g b nR nG nB|
       
 10258 
       
 10259             r := image redBitsOf:pixelValue.
       
 10260             g := image greenBitsOf:pixelValue.
       
 10261             b := image blueBitsOf:pixelValue.
       
 10262             nR := r bitAnd:mask. (nR bitAnd:extMask)~~0 ifTrue:[nR := nR bitOr:extBits].
       
 10263             nG := g bitAnd:mask. (nG bitAnd:extMask)~~0 ifTrue:[nG := nG bitOr:extBits].
       
 10264             nB := b bitAnd:mask. (nB bitAnd:extMask)~~0 ifTrue:[nB := nB bitOr:extBits].
       
 10265             image valueFromRedBits:nR greenBits:nG blueBits:nB.
       
 10266         ].
       
 10267 
       
 10268 
       
 10269     photometric == #palette ifFalse:[
       
 10270         "/ direct manipulation of the pixels
       
 10271         0 to:yMax do:[:y |
       
 10272             0 to:xMax do:[:x |
       
 10273                 pix := self pixelAtX:x y:y.
       
 10274                 n_pix := newPixelValue value:self value:pix.
       
 10275                 n_pix ~= pix ifTrue:[
       
 10276                     newImage pixelAtX:x y:y put:n_pix.
       
 10277                     anyChange := true.
       
 10278                 ]
       
 10279             ]
       
 10280         ].
       
 10281         anyChange ifFalse:[
       
 10282             ^ nil
       
 10283         ].
       
 10284     ] ifTrue:[
       
 10285         "/ manipulate the colormap
       
 10286         0 to:yMax do:[:y |
       
 10287             0 to:xMax do:[:x |
       
 10288                 pix := self pixelAtX:x y:y.
       
 10289                 (n_pix := map at:pix+1) isNil ifTrue:[
       
 10290                     clr := self colorAtX:x y:y.
       
 10291 
       
 10292                     r := clr redByte.
       
 10293                     g := clr greenByte.
       
 10294                     b := clr blueByte.
       
 10295                     nR := r bitAnd:mask. (nR bitAnd:extMask)~~0 ifTrue:[nR := nR bitOr:extBits].
       
 10296                     nG := g bitAnd:mask. (nR bitAnd:extMask)~~0 ifTrue:[nR := nR bitOr:extBits].
       
 10297                     nB := b bitAnd:mask. (nR bitAnd:extMask)~~0 ifTrue:[nR := nR bitOr:extBits].
       
 10298                     n_clr := Color redByte:nR greenByte:nG blueByte:nB.
       
 10299                     (newColors includes:n_clr) ifFalse:[
       
 10300                         newColors add:n_clr.
       
 10301                         newColorArray add:n_clr.
       
 10302                         revMap add:pix.
       
 10303                         map at:pix+1 put:(n_pix := revMap size - 1).
       
 10304                     ] ifTrue:[
       
 10305                         "/ mhmh - multiple pixels mapped to the same color
       
 10306                         n_pix := (newColorArray indexOf:n_clr) - 1.
       
 10307                         map at:pix+1 put:n_pix.
       
 10308                     ]
       
 10309                 ].
       
 10310                 newImage pixelAtX:x y:y put:n_pix.
       
 10311             ]
       
 10312         ].
       
 10313         revMap size == self colorMap size ifTrue:[
       
 10314             revMap = (0 to:revMap size-1) ifTrue:[
       
 10315                 ^ nil
       
 10316             ]
       
 10317         ].
       
 10318 
       
 10319         newImage colorMap:(MappedPalette withColors:newColorArray).
       
 10320     ].
       
 10321 
       
 10322     ^ newImage
       
 10323 !
       
 10324 
 10224 withPixelFunctionApplied:pixelFunctionBlock
 10325 withPixelFunctionApplied:pixelFunctionBlock
 10225     "return a new image from the old one, by applying a pixel processor
 10326     "return a new image from the old one, by applying a pixel processor
 10226      on the pixel colors.
 10327      on the pixel colors.
 10227      Notice: this method is very slow - either apply pixel values
 10328      Notice: this method is very slow - either apply pixel values
 10228      (#withPixelFunctionAppliedToPixels:) or redefine this method in
 10329      (#withPixelFunctionAppliedToPixels:) or redefine this method in
 12475 ! !
 12576 ! !
 12476 
 12577 
 12477 !Image class methodsFor:'documentation'!
 12578 !Image class methodsFor:'documentation'!
 12478 
 12579 
 12479 version
 12580 version
 12480     ^ '$Header: /cvs/stx/stx/libview/Image.st,v 1.344 2003-05-07 14:29:48 cg Exp $'
 12581     ^ '$Header: /cvs/stx/stx/libview/Image.st,v 1.345 2003-07-02 15:22:31 cg Exp $'
 12481 ! !
 12582 ! !
 12482 
 12583 
 12483 Image initialize!
 12584 Image initialize!