Depth32Image.st
changeset 8744 d9c17cf6f9dd
parent 8728 289cdc983b33
child 8746 0338514ca23d
--- a/Depth32Image.st	Mon Jul 22 13:56:23 2019 +0200
+++ b/Depth32Image.st	Mon Jul 22 13:56:47 2019 +0200
@@ -164,11 +164,11 @@
     g := aColor greenByte.
     b := aColor blueByte.
     
-    ((photometric == #rgb) or:[(photometric == #xrgb)]) ifTrue:[
-        "/ byteorder is: ignored-alpha,r,g,b
-        bytes at:(index + 1) put:r.
-        bytes at:(index + 2) put:g.
-        bytes at:(index + 3) put:b.
+    ((photometric == #rgb) or:[photometric == #rgbx]) ifTrue:[
+        "/ byteorder is: r,g,b,ignored-alpha
+        bytes at:(index) put:r.
+        bytes at:(index + 1) put:g.
+        bytes at:(index + 2) put:b.
         ^ self
     ].
     (photometric == #rgba) ifTrue:[
@@ -187,17 +187,18 @@
         bytes at:(index + 3) put:b.             
         ^ self
     ].
-    (photometric == #rgbx) ifTrue:[
-        "/ byteorder is: <ignoredAlpha>,r,g,b
-        bytes at:(index + 0) put:r.
-        bytes at:(index + 1) put:g.
-        bytes at:(index + 2) put:b.
+    (photometric == #xrgb) ifTrue:[
+        "/ byteorder is: ignored-alpha,r,g,b
+        bytes at:(index + 1) put:r.
+        bytes at:(index + 2) put:g.
+        bytes at:(index + 3) put:b.
         ^ self
     ].
 
     super colorAtX:x y:y put:aColor.
 
     "Modified: / 22-08-2017 / 18:17:14 / cg"
+    "Modified: / 22-07-2019 / 13:07:48 / Claus Gittinger"
 !
 
 pixelAtX:x y:y
@@ -323,6 +324,79 @@
     ].
 
     "Modified (comment): / 21-02-2017 / 15:03:24 / cg"
+!
+
+valueFromColor:aColor
+    "get a color's pixel value."
+
+    |r g b a|
+
+    r := aColor redByte.
+    g := aColor greenByte.
+    b := aColor blueByte.
+    
+    ((photometric == #rgb) or:[photometric == #rgbx]) ifTrue:[
+        "/ byteorder is: r,g,b,ignored-alpha
+        "/ but the pixelvalue is assumed to be MSB (i.e. red at high byte)
+        ^ ((((r bitShift:8) bitOr:g) bitShift:8) bitOr:b) bitShift:8.
+    ].
+    a := aColor alphaByte.
+    (photometric == #rgba) ifTrue:[
+        "/ byteorder is: r,g,b,a
+        "/ but the pixelvalue is assumed to be MSB (i.e. red at high byte)
+        ^ (((((r bitShift:8) bitOr:g) bitShift:8) bitOr:b) bitShift:8) bitOr:a.
+    ].
+    (photometric == #argb) ifTrue:[
+        "/ byteorder is: a,r,g,b
+        "/ but the pixelvalue is assumed to be MSB (i.e. red at high byte)
+        ^ (((((a bitShift:8) bitOr:r) bitShift:8) bitOr:g) bitShift:8) bitOr:b.
+    ].
+    (photometric == #xrgb) ifTrue:[
+        "/ byteorder is: ignored-alpha,r,g,b
+        "/ but the pixelvalue is assumed to be MSB (i.e. red at high byte)
+        ^ (((r bitShift:8) bitOr:g) bitShift:8) bitOr:b.
+    ].
+
+    ^ super valueFromColor:aColor.
+
+    "Created: / 22-07-2019 / 13:43:20 / Claus Gittinger"
+!
+
+valueFromRGB:anRGBValue
+    "get the pixel value corresponding to an RGB value."
+
+    |"r g b" a|
+    
+    "/
+    "/ r := (anRGBValue bitShift:-16) bitAnd:16rFF.
+    "/ g := (anRGBValue bitShift:-8) bitAnd:16rFF.
+    "/ b := anRGBValue bitAnd:16rFF.
+    a := 255.
+
+    ((photometric == #rgb) or:[photometric == #rgbx]) ifTrue:[
+        "/ byteorder is: r,g,b,ignored-alpha
+        "/ but the pixelvalue is assumed to be MSB (i.e. red at high byte)
+        ^ anRGBValue bitShift:8.
+    ].
+    (photometric == #rgba) ifTrue:[
+        "/ byteorder is: r,g,b,a
+        "/ but the pixelvalue is assumed to be MSB (i.e. red at high byte)
+        ^ (anRGBValue bitShift:8) bitOr:a.
+    ].
+    (photometric == #argb) ifTrue:[
+        "/ byteorder is: a,r,g,b
+        "/ but the pixelvalue is assumed to be MSB (i.e. red at high byte)
+        ^ anRGBValue bitOr:(a bitShift:24).
+    ].
+    (photometric == #xrgb) ifTrue:[
+        "/ byteorder is: ignored-alpha,r,g,b
+        "/ but the pixelvalue is assumed to be MSB (i.e. red at high byte)
+        ^ anRGBValue.
+    ].
+
+    ^ super valueFromRGB:anRGBValue.
+
+    "Created: / 22-07-2019 / 13:47:11 / Claus Gittinger"
 ! !
 
 !Depth32Image methodsFor:'converting rgb images'!
@@ -1377,15 +1451,17 @@
 !
 
 numAlphaBits
-    photometric == #argb ifTrue:[
-        ^ 8.
-    ].
-    photometric == #rgba ifTrue:[
-        ^ 8.
-    ].
-    ^ 0
+    ^ 8 "/ always 8 - even if ignored
+"/    photometric == #argb ifTrue:[
+"/        ^ 8.
+"/    ].
+"/    photometric == #rgba ifTrue:[
+"/        ^ 8.
+"/    ].
+"/    ^ 0
 
     "Created: / 22-08-2017 / 17:38:59 / cg"
+    "Modified: / 22-07-2019 / 13:36:02 / Claus Gittinger"
 !
 
 numBlueBits
@@ -1490,7 +1566,11 @@
 !
 
 valueFromRedBits:redBits greenBits:greenBits blueBits:blueBits
-    (photometric == #rgba) ifTrue:[
+    "given the rgb bits, each in 0..maxXXXValue (i.e. according to
+     r/g/b channels number of bits, return the corresponding pixel value.
+     For now, only useful with RGB images"
+
+     (photometric == #rgba) ifTrue:[
         "/ alpha in low byte
         ^ (((((redBits bitShift:8) bitOr:greenBits) bitShift:8) bitOr:blueBits) bitShift:8) bitOr:16rFF
     ].
@@ -1505,6 +1585,7 @@
     ^ super valueFromRedBits:redBits greenBits:greenBits blueBits:blueBits.
 
     "Modified: / 22-08-2017 / 18:34:30 / cg"
+    "Modified (comment): / 22-07-2019 / 13:51:14 / Claus Gittinger"
 !
 
 valueFromRedBits:redBits greenBits:greenBits blueBits:blueBits alphaBits:alphaBits