Depth32Image.st
changeset 6841 3f4935787091
parent 6302 d393dae43c28
child 6846 6c9367f0ecb8
equal deleted inserted replaced
6840:3dee5e952e94 6841:3f4935787091
       
     1 "{ Encoding: utf8 }"
       
     2 
     1 "
     3 "
     2  COPYRIGHT (c) 1995 by Claus Gittinger
     4  COPYRIGHT (c) 1995 by Claus Gittinger
     3 	      All Rights Reserved
     5 	      All Rights Reserved
     4 
     6 
     5  This software is furnished under a license and may be used
     7  This software is furnished under a license and may be used
     9  other person.  No title to or ownership of the software is
    11  other person.  No title to or ownership of the software is
    10  hereby transferred.
    12  hereby transferred.
    11 "
    13 "
    12 "{ Package: 'stx:libview' }"
    14 "{ Package: 'stx:libview' }"
    13 
    15 
       
    16 "{ NameSpace: Smalltalk }"
       
    17 
    14 Image subclass:#Depth32Image
    18 Image subclass:#Depth32Image
    15 	instanceVariableNames:''
    19 	instanceVariableNames:''
    16 	classVariableNames:''
    20 	classVariableNames:''
    17 	poolDictionaries:''
    21 	poolDictionaries:''
    18 	category:'Graphics-Images'
    22 	category:'Graphics-Images'
    72     "Created: 24.4.1997 / 19:00:28 / cg"
    76     "Created: 24.4.1997 / 19:00:28 / cg"
    73 ! !
    77 ! !
    74 
    78 
    75 !Depth32Image methodsFor:'accessing-pixels'!
    79 !Depth32Image methodsFor:'accessing-pixels'!
    76 
    80 
       
    81 colorAtX:x y:y
       
    82     "retrieve a pixel at x/y; return a color.
       
    83      Pixels start at x=0 , y=0 for upper left pixel, end at
       
    84      x = width-1, y=height-1 for lower right pixel"
       
    85 
       
    86     |index "{ Class: SmallInteger }"
       
    87      rVal gVal bVal|
       
    88 
       
    89     photometric ~~ #rgb ifTrue:[^ super colorAtX:x y:y].
       
    90 
       
    91     index := 1 + (((width * y) + x) * 4).
       
    92     rVal := bytes at:(index).
       
    93     gVal := bytes at:(index + 1).
       
    94     bVal := bytes at:(index + 2).
       
    95 
       
    96     ^ Color redByte:rVal greenByte:gVal blueByte:bVal
       
    97 !
       
    98 
       
    99 colorAtX:x y:y put:aColor
       
   100     "set the pixel at x/y to aColor.
       
   101      Pixels start at x=0 , y=0 for upper left pixel, end at
       
   102      x = width-1, y=height-1 for lower right pixel."
       
   103 
       
   104     |index "{ Class: SmallInteger }"|
       
   105 
       
   106     photometric ~~ #rgb ifTrue:[^ super colorAtX:x y:y put:aColor].
       
   107 
       
   108     index := 1 + (((width * y) + x) * 4).
       
   109     bytes at:(index) put:(aColor redByte).
       
   110     bytes at:(index + 1) put:(aColor greenByte).
       
   111     bytes at:(index + 2) put:(aColor blueByte).
       
   112     bytes at:(index + 3) put:255.               "alpha channel"
       
   113 !
       
   114 
    77 pixelAtX:x y:y
   115 pixelAtX:x y:y
    78     "retrieve a pixel at x/y; return a pixelValue.
   116     "retrieve a pixel at x/y; return a pixelValue.
    79      Pixels start at x=0 , y=0 for upper left pixel, end at
   117      Pixels start at x=0 , y=0 for upper left pixel, end at
    80      x = width-1, y=height-1 for lower right pixel"
   118      x = width-1, y=height-1 for lower right pixel"
    81 
   119 
    82     |pixelIndex "{ Class: SmallInteger }"|
   120     |pixelIndex "{ Class: SmallInteger }"|
    83 
   121 
    84     pixelFunction notNil ifTrue:[^ pixelFunction value:x value:y].
   122     pixelFunction notNil ifTrue:[^ pixelFunction value:x value:y].
    85 
   123 
    86     pixelIndex := (width * 4 * y) + 1 + (x * 4).
   124     pixelIndex := 1 + (((width * y) + x) * 4).
    87 
   125 
    88     "left pixel in high bits"
   126     "left pixel in high bits"
    89     ^ bytes doubleWordAt:pixelIndex MSB:true.
   127     ^ bytes doubleWordAt:pixelIndex MSB:true.
    90 
   128 
    91     "Created: 24.4.1997 / 19:00:28 / cg"
   129     "Created: 24.4.1997 / 19:00:28 / cg"
    97      Pixels start at x=0 , y=0 for upper left pixel, end at
   135      Pixels start at x=0 , y=0 for upper left pixel, end at
    98      x = width-1, y=height-1 for lower right pixel"
   136      x = width-1, y=height-1 for lower right pixel"
    99 
   137 
   100     |pixelIndex "{ Class: SmallInteger }"|
   138     |pixelIndex "{ Class: SmallInteger }"|
   101 
   139 
   102     pixelIndex := (width * 4 * y) + 1 + (x * 4).
   140     pixelIndex := 1 + (((width * y) + x) * 4).
   103     bytes isNil ifTrue:[
   141     bytes isNil ifTrue:[
   104 	self createPixelStore
   142         self createPixelStore
   105     ].
   143     ].
   106     bytes doubleWordAt:pixelIndex put:aPixelValue MSB:true
   144     bytes doubleWordAt:pixelIndex put:aPixelValue MSB:true
   107 
   145 
   108     "Created: / 24-04-1997 / 19:00:28 / cg"
   146     "Created: / 24-04-1997 / 19:00:28 / cg"
   109     "Modified: / 06-06-2007 / 12:20:57 / cg"
   147     "Modified: / 06-06-2007 / 12:20:57 / cg"
       
   148 !
       
   149 
       
   150 rowAt:y putAll:pixelArray startingAt:startIndex
       
   151     "store a single rows bits from bits in the pixelArray argument;
       
   152      Return the pixelArray.
       
   153      Notice: row coordinate starts at 0."
       
   154 
       
   155     |bytes dstIdx pixel|
       
   156 
       
   157     bytes := self bits.
       
   158     dstIdx := (y * self bytesPerRow) + 1.
       
   159     0 to:width-1 do:[:col |
       
   160         pixel := pixelArray at:(startIndex + col).
       
   161         bytes at:dstIdx put:((pixel bitShift:-24) bitAnd:16rFF).
       
   162         bytes at:dstIdx+1 put:((pixel bitShift:-16) bitAnd:16rFF).
       
   163         bytes at:dstIdx+2 put:((pixel bitShift:-8) bitAnd:16rFF).
       
   164         bytes at:dstIdx+3 put:(pixel bitAnd:16rFF).
       
   165         dstIdx := dstIdx + 4.
       
   166     ].
       
   167     ^ pixelArray
   110 ! !
   168 ! !
   111 
   169 
   112 !Depth32Image methodsFor:'converting rgb images'!
   170 !Depth32Image methodsFor:'converting rgb images'!
   113 
   171 
   114 rgbImageAsTrueColorFormOn:aDevice
   172 rgbImageAsTrueColorFormOn:aDevice
   596 
   654 
   597     ^ form
   655     ^ form
   598 
   656 
   599     "Created: / 27-05-2007 / 16:54:19 / cg"
   657     "Created: / 27-05-2007 / 16:54:19 / cg"
   600     "Modified: / 29-05-2007 / 12:19:04 / cg"
   658     "Modified: / 29-05-2007 / 12:19:04 / cg"
       
   659 ! !
       
   660 
       
   661 !Depth32Image methodsFor:'image manipulations'!
       
   662 
       
   663 negative
       
   664     |bytes index newImage newBytes nBytes r g b|
       
   665 
       
   666     photometric ~~ #rgb ifTrue:[
       
   667         ^ super negative.
       
   668     ].
       
   669     bytes := self bits.
       
   670 
       
   671     newImage := self copy.
       
   672     nBytes := bytes size.
       
   673     newImage bits:(newBytes := ByteArray new:nBytes).
       
   674     index := 1.
       
   675     [index < nBytes] whileTrue:[
       
   676         r := bytes at:index.
       
   677         newBytes at:index put:(255-r).
       
   678         index := index + 1.
       
   679         g := bytes at:index.
       
   680         newBytes at:index put:(255-g).
       
   681         index := index + 1.
       
   682         b := bytes at:index.
       
   683         newBytes at:index put:(255-b).
       
   684         index := index + 1.
       
   685         b := bytes at:index.
       
   686         newBytes at:index put:b.
       
   687         index := index + 1.
       
   688     ].
       
   689     ^ newImage
   601 ! !
   690 ! !
   602 
   691 
   603 !Depth32Image methodsFor:'initialization'!
   692 !Depth32Image methodsFor:'initialization'!
   604 
   693 
   605 initialize
   694 initialize
   673     ^ width * 32
   762     ^ width * 32
   674 
   763 
   675     "Created: 24.4.1997 / 19:00:28 / cg"
   764     "Created: 24.4.1997 / 19:00:28 / cg"
   676 !
   765 !
   677 
   766 
       
   767 bitsPerSample
       
   768     "return the number of bits per sample.
       
   769      The return value is an array of bits-per-plane."
       
   770 
       
   771     bitsPerSample notNil ifTrue:[^ bitsPerSample].
       
   772     ^ #(8 8 8 8)
       
   773 !
       
   774 
       
   775 blueBitsOf:pixel
       
   776     "given a pixel-value, return the blue component as byteValue (0..255)"
       
   777 
       
   778     ^ (pixel bitShift:-8) bitAnd:16rFF.
       
   779 !
       
   780 
       
   781 blueComponentOf:pixel
       
   782     "given a pixel-value, return the blue component in percent (0..100)"
       
   783 
       
   784     ^ (100.0 / 255.0) *  ((pixel bitShift:-8) bitAnd:16rFF).
       
   785 !
       
   786 
       
   787 blueMaskForPixelValue
       
   788     "return the mask used with translation from pixelValues to blueBits"
       
   789 
       
   790     ^ 16rFF
       
   791 !
       
   792 
       
   793 blueShiftForPixelValue
       
   794     "return the shift amount used with translation from pixelValues to blueBits"
       
   795 
       
   796     ^ -8
       
   797 !
       
   798 
   678 bytesPerRow
   799 bytesPerRow
   679     "return the number of bytes in one scanline of the image"
   800     "return the number of bytes in one scanline of the image"
   680 
   801 
   681     ^ width * 4.
   802     ^ width * 4.
   682 
   803 
   683     "Created: 24.4.1997 / 19:00:28 / cg"
   804     "Created: 24.4.1997 / 19:00:28 / cg"
   684 !
   805 !
   685 
   806 
       
   807 greenBitsOf:pixel
       
   808     "given a pixel-value, return the green component as byteValue (0..255)"
       
   809 
       
   810     ^ (pixel bitShift:-16) bitAnd:16rFF.
       
   811 !
       
   812 
       
   813 greenComponentOf:pixel
       
   814     "given a pixel-value, return the green component in percent (0..100)"
       
   815 
       
   816     ^ (100.0 / 255.0) * ((pixel bitShift:-16) bitAnd:16rFF).
       
   817 !
       
   818 
       
   819 greenMaskForPixelValue
       
   820     "return the mask used with translation from pixelValues to redBits"
       
   821 
       
   822     ^ 16rFF
       
   823 !
       
   824 
       
   825 greenShiftForPixelValue
       
   826     "return the shift amount used with translation from pixelValues to greenBits"
       
   827 
       
   828     ^ -16
       
   829 !
       
   830 
   686 hasAlphaChannel
   831 hasAlphaChannel
   687     ^ true
   832     ^ true
       
   833 !
       
   834 
       
   835 redBitsOf:pixel
       
   836     "given a pixel-value, return the red component as byteValue (0..255)"
       
   837 
       
   838     ^ (pixel bitShift:-24) bitAnd:16rFF.
       
   839 !
       
   840 
       
   841 redComponentOf:pixel
       
   842     "given a pixel-value, return the red component in percent (0..100)"
       
   843 
       
   844     ^ (100.0 / 255.0) * ((pixel bitShift:-24) bitAnd:16rFF).
       
   845 !
       
   846 
       
   847 redMaskForPixelValue
       
   848     "return the mask used with translation from pixelValues to redBits"
       
   849 
       
   850     ^ 16rFF
       
   851 !
       
   852 
       
   853 redShiftForPixelValue
       
   854     "return the shift amount used with translation from pixelValues to redBits"
       
   855 
       
   856     ^ -24
       
   857 !
       
   858 
       
   859 rgbFromValue:pixelValue
       
   860     "given a pixel value, return the corresponding 24bit rgbValue (rrggbb, red is MSB)."
       
   861 
       
   862     ^ pixelValue rightShift:8     "lsb is alpha channel"
       
   863 !
       
   864 
       
   865 samplesPerPixel
       
   866     "return the number of samples per pixel in the image."
       
   867 
       
   868     samplesPerPixel notNil ifTrue:[^ samplesPerPixel].
       
   869     ^ 4
       
   870 !
       
   871 
       
   872 valueFromRedBits:redBits greenBits:greenBits blueBits:blueBits
       
   873     ^ (((((redBits bitShift:8) bitOr:greenBits) bitShift:8) bitOr:blueBits) bitShift:8) bitOr:255
   688 ! !
   874 ! !
   689 
   875 
   690 !Depth32Image class methodsFor:'documentation'!
   876 !Depth32Image class methodsFor:'documentation'!
   691 
   877 
   692 version
   878 version
   693     ^ '$Header: /cvs/stx/stx/libview/Depth32Image.st,v 1.13 2014-03-02 13:59:52 cg Exp $'
   879     ^ '$Header: /cvs/stx/stx/libview/Depth32Image.st,v 1.14 2015-04-23 21:31:10 stefan Exp $'
   694 !
   880 !
   695 
   881 
   696 version_CVS
   882 version_CVS
   697     ^ '$Header: /cvs/stx/stx/libview/Depth32Image.st,v 1.13 2014-03-02 13:59:52 cg Exp $'
   883     ^ '$Header: /cvs/stx/stx/libview/Depth32Image.st,v 1.14 2015-04-23 21:31:10 stefan Exp $'
   698 ! !
   884 ! !
   699 
   885