Depth2Image.st
changeset 81 4ba554473294
parent 54 29a6b2f8e042
child 89 ea2bf46eb669
--- a/Depth2Image.st	Thu Nov 17 15:24:54 1994 +0100
+++ b/Depth2Image.st	Thu Nov 17 15:31:38 1994 +0100
@@ -1,6 +1,6 @@
 "
  COPYRIGHT (c) 1993 by Claus Gittinger
-              All Rights Reserved
+	      All Rights Reserved
 
  This software is furnished under a license and may be used
  only in accordance with the terms of that license and with the
@@ -11,17 +11,17 @@
 "
 
 Image subclass:#Depth2Image
-         instanceVariableNames:''
-         classVariableNames:''
-         poolDictionaries:''
-         category:'Graphics-Display Objects'
+	 instanceVariableNames:''
+	 classVariableNames:''
+	 poolDictionaries:''
+	 category:'Graphics-Display Objects'
 !
 
 Depth2Image comment:'
 COPYRIGHT (c) 1993 by Claus Gittinger
-              All Rights Reserved
+	      All Rights Reserved
 
-$Header: /cvs/stx/stx/libview/Depth2Image.st,v 1.7 1994-08-05 01:13:55 claus Exp $
+$Header: /cvs/stx/stx/libview/Depth2Image.st,v 1.8 1994-11-17 14:29:10 claus Exp $
 '!
 
 !Depth2Image class methodsFor:'documentation'!
@@ -29,7 +29,7 @@
 copyright
 "
  COPYRIGHT (c) 1993 by Claus Gittinger
-              All Rights Reserved
+	      All Rights Reserved
 
  This software is furnished under a license and may be used
  only in accordance with the terms of that license and with the
@@ -42,7 +42,7 @@
 
 version
 "
-$Header: /cvs/stx/stx/libview/Depth2Image.st,v 1.7 1994-08-05 01:13:55 claus Exp $
+$Header: /cvs/stx/stx/libview/Depth2Image.st,v 1.8 1994-11-17 14:29:10 claus Exp $
 "
 !
 
@@ -55,7 +55,7 @@
 "
 ! !
 
-!Depth2Image methodsFor:'accessing'!
+!Depth2Image methodsFor:'queries'!
 
 bitsPerPixel
     "return the number of bits per pixel"
@@ -83,7 +83,7 @@
 
     nbytes := width // 4.
     ((width \\ 4) ~~ 0) ifTrue:[
-        ^ nbytes + 1
+	^ nbytes + 1
     ].
     ^ nbytes
 !
@@ -92,7 +92,9 @@
     "return the number of samples per pixel in the image."
 
     ^ 1
-!
+! !
+
+!Depth2Image methodsFor:'accessing'!
 
 valueAtX:x y:y
     "retrieve a pixel at x/y; return a pixel value.
@@ -128,37 +130,37 @@
     shift := #(-6 -4 -2 0) at:((x \\ 4) + 1).
     value := (byte bitShift:shift) bitAnd:3.
     photometric == #whiteIs0 ifTrue:[
-        (value == 0) ifTrue:[
-            ^ Color white
-        ].
-        (value == 1) ifTrue:[
-            ^ Color grey:67
-        ].
-        (value == 2) ifTrue:[
-            ^ Color grey:33
-        ].
-        ^ Color black
+	(value == 0) ifTrue:[
+	    ^ Color white
+	].
+	(value == 1) ifTrue:[
+	    ^ Color grey:67
+	].
+	(value == 2) ifTrue:[
+	    ^ Color grey:33
+	].
+	^ Color black
     ].
     photometric == #blackIs0 ifTrue:[
-        (value == 0) ifTrue:[
-            ^ Color black
-        ].
-        (value == 1) ifTrue:[
-            ^ Color grey:33
-        ].
-        (value == 2) ifTrue:[
-            ^ Color grey:67
-        ].
-        ^ Color white
+	(value == 0) ifTrue:[
+	    ^ Color black
+	].
+	(value == 1) ifTrue:[
+	    ^ Color grey:33
+	].
+	(value == 2) ifTrue:[
+	    ^ Color grey:67
+	].
+	^ Color white
     ].
     photometric ~~ #palette ifTrue:[
-        self error:'format not supported'.
-        ^ nil
+	self error:'format not supported'.
+	^ nil
     ].
     value := value + 1.
     ^ Color red:(((colorMap at:1) at:value) * (100.0 / 255.0))
-          green:(((colorMap at:2) at:value) * (100.0 / 255.0))
-           blue:(((colorMap at:3) at:value) * (100.0 / 255.0))
+	  green:(((colorMap at:2) at:value) * (100.0 / 255.0))
+	   blue:(((colorMap at:3) at:value) * (100.0 / 255.0))
 !
 
 atX:x y:y putValue:aPixelValue
@@ -179,6 +181,47 @@
     shift := #(6 4 2 0) at:((x \\ 4) + 1).
     byte := (byte bitAnd:(3 bitShift:shift) bitInvert) bitOr:(aPixelValue bitShift:shift).
     bytes at:index put:byte
+! !
+
+!Depth2Image methodsFor:'enumeration'!
+
+valueAtY:y from:xLow to:xHigh do:aBlock
+    "perform aBlock for each pixelValue from x1 to x2 in row y.
+     The block is passed the x coordinate and the pixelValue at each pixel.
+     This method allows slighly faster processing of an
+     image than using valueAtX:y:, since some processing can be
+     avoided when going from pixel to pixel. However, for
+     real image processing, specialized methods should be written."
+
+    |srcIndex "{ Class: SmallInteger }"
+     byte     "{ Class: SmallInteger }"
+     shift    "{ Class: SmallInteger }"
+     value    "{ Class: SmallInteger }"
+     x1       "{ Class: SmallInteger }"
+     x2       "{ Class: SmallInteger }"|
+
+    "left pixel in high bits"
+
+    x1 := xLow.
+    x2 := xHigh.
+
+    srcIndex := (self bytesPerRow * y) + 1.
+    srcIndex := srcIndex + (x1 // 4).
+    shift := #(-6 -4 -2 0) at:((x1 \\ 4) + 1).
+
+    x1 to:x2 do:[:x |
+	byte := bytes at:srcIndex.
+	value := (byte bitShift:shift) bitAnd:3.
+
+	aBlock value:x value:value.
+
+	shift == 0 ifTrue:[
+	    shift := -6.
+	    srcIndex := srcIndex + 1
+	] ifFalse:[
+	    shift := shift + 2.
+	]
+    ].
 !
 
 atY:y from:xLow to:xHigh do:aBlock
@@ -199,38 +242,38 @@
      redMap greenMap blueMap|
 
     photometric == #whiteIs0 ifTrue:[
-        color0 := Color white.
-        color1 := Color grey:67.
-        color2 := Color grey:33.
-        color3 := Color black
+	color0 := Color white.
+	color1 := Color grey:67.
+	color2 := Color grey:33.
+	color3 := Color black
     ] ifFalse:[
-        photometric == #blackIs0 ifTrue:[
-            color0 := Color black.
-            color1 := Color grey:33.
-            color2 := Color grey:67.
-            color3 := Color white
-        ] ifFalse:[
-            photometric == #palette ifTrue:[
-                redMap := colorMap at:1.
-                greenMap := colorMap at:2.
-                blueMap := colorMap at:3.
-                color0 := Color red:((redMap at:1) * 100 / 255)
-                              green:((greenMap at:1) * 100 / 255)
-                               blue:((blueMap at:1) * 100 / 255).
-                color1 := Color red:((redMap at:2) * 100 / 255)
-                              green:((greenMap at:2) * 100 / 255)
-                               blue:((blueMap at:2) * 100 / 255).
-                color2 := Color red:((redMap at:3) * 100 / 255)
-                              green:((greenMap at:3) * 100 / 255)
-                               blue:((blueMap at:3) * 100 / 255).
-                color3 := Color red:((redMap at:4) * 100 / 255)
-                              green:((greenMap at:4) * 100 / 255)
-                               blue:((blueMap at:4) * 100 / 255).
-            ] ifFalse:[
-                self error:'format not supported'.
-                ^ nil
-            ]
-        ]
+	photometric == #blackIs0 ifTrue:[
+	    color0 := Color black.
+	    color1 := Color grey:33.
+	    color2 := Color grey:67.
+	    color3 := Color white
+	] ifFalse:[
+	    photometric == #palette ifTrue:[
+		redMap := colorMap at:1.
+		greenMap := colorMap at:2.
+		blueMap := colorMap at:3.
+		color0 := Color red:((redMap at:1) * 100 / 255)
+			      green:((greenMap at:1) * 100 / 255)
+			       blue:((blueMap at:1) * 100 / 255).
+		color1 := Color red:((redMap at:2) * 100 / 255)
+			      green:((greenMap at:2) * 100 / 255)
+			       blue:((blueMap at:2) * 100 / 255).
+		color2 := Color red:((redMap at:3) * 100 / 255)
+			      green:((greenMap at:3) * 100 / 255)
+			       blue:((blueMap at:3) * 100 / 255).
+		color3 := Color red:((redMap at:4) * 100 / 255)
+			      green:((greenMap at:4) * 100 / 255)
+			       blue:((blueMap at:4) * 100 / 255).
+	    ] ifFalse:[
+		self error:'format not supported'.
+		^ nil
+	    ]
+	]
     ].
 
     "left pixel in high bits"
@@ -243,30 +286,30 @@
     shift := #(-6 -4 -2 0) at:((x1 \\ 4) + 1).
 
     x1 to:x2 do:[:x |
-        byte := bytes at:srcIndex.
-        value := (byte bitShift:shift) bitAnd:3.
+	byte := bytes at:srcIndex.
+	value := (byte bitShift:shift) bitAnd:3.
 
-        (value == 0) ifTrue:[
-            color := color0
-        ] ifFalse:[
-            (value == 1) ifTrue:[
-                color := color1
-            ] ifFalse:[
-                (value == 2) ifTrue:[
-                    color := color2
-                ] ifFalse:[
-                    color := color3
-                ]
-            ]
-        ].
-        aBlock value:x value:color.
+	(value == 0) ifTrue:[
+	    color := color0
+	] ifFalse:[
+	    (value == 1) ifTrue:[
+		color := color1
+	    ] ifFalse:[
+		(value == 2) ifTrue:[
+		    color := color2
+		] ifFalse:[
+		    color := color3
+		]
+	    ]
+	].
+	aBlock value:x value:color.
 
-        shift == 0 ifTrue:[
-            shift := -6.
-            srcIndex := srcIndex + 1
-        ] ifFalse:[
-            shift := shift + 2.
-        ]
+	shift == 0 ifTrue:[
+	    shift := -6.
+	    srcIndex := srcIndex + 1
+	] ifFalse:[
+	    shift := shift + 2.
+	]
     ].
 ! !
 
@@ -296,7 +339,7 @@
     bytesPerRow := self bytesPerRow.
     bytesPerMonoRow := w // 8.
     ((w \\ 8) ~~ 0) ifTrue:[
-        bytesPerMonoRow := bytesPerMonoRow + 1
+	bytesPerMonoRow := bytesPerMonoRow + 1
     ].
     monoData := ByteArray uninitializedNew:(bytesPerMonoRow * h).
 
@@ -305,48 +348,48 @@
     srcIndex := 1.
     dstIndex := 1.
     1 to:h do:[:count |
-        nextSrc := srcIndex + bytesPerRow.
-        nextDst := dstIndex + bytesPerMonoRow.
-        bitNumber := 1.
-        [bitNumber <= w] whileTrue:[
-            left4pixel := bytes at:srcIndex.
-            srcIndex := srcIndex + 1.
-            byte := 0.
-            ((left4pixel bitAnd:16r80) ~~ 0) ifTrue:[
-                byte := byte bitOr:2r10000000
-            ].
-            ((left4pixel bitAnd:16r20) ~~ 0) ifTrue:[
-                byte := byte bitOr:2r01000000
-            ].
-            ((left4pixel bitAnd:16r08) ~~ 0) ifTrue:[
-                byte := byte bitOr:2r00100000
-            ].
-            ((left4pixel bitAnd:16r02) ~~ 0) ifTrue:[
-                byte := byte bitOr:2r00010000
-            ].
-            bitNumber := bitNumber + 4.
-            (bitNumber <= w) ifTrue:[
-                right4pixel := bytes at:srcIndex.
-                srcIndex := srcIndex + 1.
-                ((right4pixel bitAnd:16r80) ~~ 0) ifTrue:[
-                    byte := byte bitOr:2r00001000
-                ].
-                ((right4pixel bitAnd:16r20) ~~ 0) ifTrue:[
-                    byte := byte bitOr:2r00000100
-                ].
-                ((right4pixel bitAnd:16r08) ~~ 0) ifTrue:[
-                    byte := byte bitOr:2r00000010
-                ].
-                ((right4pixel bitAnd:16r02) ~~ 0) ifTrue:[
-                    byte := byte bitOr:2r00000001
-                ].
-                bitNumber := bitNumber + 4
-            ].
-            monoData at:dstIndex put:byte.
-            dstIndex := dstIndex + 1
-        ].
-        srcIndex := nextSrc.
-        dstIndex := nextDst
+	nextSrc := srcIndex + bytesPerRow.
+	nextDst := dstIndex + bytesPerMonoRow.
+	bitNumber := 1.
+	[bitNumber <= w] whileTrue:[
+	    left4pixel := bytes at:srcIndex.
+	    srcIndex := srcIndex + 1.
+	    byte := 0.
+	    ((left4pixel bitAnd:16r80) ~~ 0) ifTrue:[
+		byte := byte bitOr:2r10000000
+	    ].
+	    ((left4pixel bitAnd:16r20) ~~ 0) ifTrue:[
+		byte := byte bitOr:2r01000000
+	    ].
+	    ((left4pixel bitAnd:16r08) ~~ 0) ifTrue:[
+		byte := byte bitOr:2r00100000
+	    ].
+	    ((left4pixel bitAnd:16r02) ~~ 0) ifTrue:[
+		byte := byte bitOr:2r00010000
+	    ].
+	    bitNumber := bitNumber + 4.
+	    (bitNumber <= w) ifTrue:[
+		right4pixel := bytes at:srcIndex.
+		srcIndex := srcIndex + 1.
+		((right4pixel bitAnd:16r80) ~~ 0) ifTrue:[
+		    byte := byte bitOr:2r00001000
+		].
+		((right4pixel bitAnd:16r20) ~~ 0) ifTrue:[
+		    byte := byte bitOr:2r00000100
+		].
+		((right4pixel bitAnd:16r08) ~~ 0) ifTrue:[
+		    byte := byte bitOr:2r00000010
+		].
+		((right4pixel bitAnd:16r02) ~~ 0) ifTrue:[
+		    byte := byte bitOr:2r00000001
+		].
+		bitNumber := bitNumber + 4
+	    ].
+	    monoData at:dstIndex put:byte.
+	    dstIndex := dstIndex + 1
+	].
+	srcIndex := nextSrc.
+	dstIndex := nextDst
     ].
     ^ Form width:w height:h fromArray:monoData on:aDevice
 ! !
@@ -354,7 +397,7 @@
 !Depth2Image methodsFor:'magnification'!
 
 magnifyRowFrom:srcBytes offset:srcStart
-          into:dstBytes offset:dstStart factor:mX
+	  into:dstBytes offset:dstStart factor:mX
 
     "magnify a single pixel row - can only magnify by integer factors"
 
@@ -369,61 +412,61 @@
 
     /* helper for two-plane magnification by 2 */
     static unsigned char mag2[16] = {0x00, 0x05, 0x0a, 0x0f, 0x50, 0x55, 0x5a, 0x5f, 
-                                     0xa0, 0xa5, 0xaa, 0xaf, 0xf0, 0xf5, 0xfa, 0xff};
+				     0xa0, 0xa5, 0xaa, 0xaf, 0xf0, 0xf5, 0xfa, 0xff};
 
     if (_isSmallInteger(srcStart) && _isSmallInteger(dstStart)
      && _isSmallInteger(_INST(width)) && _isSmallInteger(mX)
      && __isByteArray(srcBytes) && __isByteArray(dstBytes)) {
-        _mag = _intVal(mX);
-        srcP = _ByteArrayInstPtr(srcBytes)->ba_element - 1 + _intVal(srcStart);
-        dstP = _ByteArrayInstPtr(dstBytes)->ba_element - 1 + _intVal(dstStart);
-        _pixels = _intVal(_INST(width));
+	_mag = _intVal(mX);
+	srcP = _ByteArrayInstPtr(srcBytes)->ba_element - 1 + _intVal(srcStart);
+	dstP = _ByteArrayInstPtr(dstBytes)->ba_element - 1 + _intVal(dstStart);
+	_pixels = _intVal(_INST(width));
 
-         switch (_mag) {
-             case 1:
-                break;
+	 switch (_mag) {
+	     case 1:
+		break;
 
-             case 2:
-                 /* tuned for this common case */
-                 while (_pixels > 0) {
-                     _byte = *srcP++;
-                     *dstP++ = mag2[ _byte >> 4 ];
-                     if (_pixels > 2) {
-                         *dstP++ = mag2[ _byte & 0x0F ];
-                     }
-                     _pixels -= 4;
-                 }
-                 break;
+	     case 2:
+		 /* tuned for this common case */
+		 while (_pixels > 0) {
+		     _byte = *srcP++;
+		     *dstP++ = mag2[ _byte >> 4 ];
+		     if (_pixels > 2) {
+			 *dstP++ = mag2[ _byte & 0x0F ];
+		     }
+		     _pixels -= 4;
+		 }
+		 break;
 
-             default:
-                 bits = 0, incnt = 0, outcnt = 0;
-                 shift = 6;
-                 _byte = *srcP++;
-                 while (_pixels--) {
-                     bit = (_byte >> shift) & 3;
-                     incnt++;
-                     if (incnt == 4) {
-                         incnt = 0;
-                         shift = 6;
-                         _byte = *srcP++;
-                     } else {
-                         shift -= 2;
-                     }
+	     default:
+		 bits = 0, incnt = 0, outcnt = 0;
+		 shift = 6;
+		 _byte = *srcP++;
+		 while (_pixels--) {
+		     bit = (_byte >> shift) & 3;
+		     incnt++;
+		     if (incnt == 4) {
+			 incnt = 0;
+			 shift = 6;
+			 _byte = *srcP++;
+		     } else {
+			 shift -= 2;
+		     }
 
-                     for (i=_mag; i>0; i--) {
-                         bits = (bits << 2) | bit;
-                         outcnt++;
-                         if (outcnt == 4) {
-                             *dstP++ = bits;
-                             bits = 0;
-                             outcnt = 0;
-                         }
-                     }
-                 }
-                 break;
+		     for (i=_mag; i>0; i--) {
+			 bits = (bits << 2) | bit;
+			 outcnt++;
+			 if (outcnt == 4) {
+			     *dstP++ = bits;
+			     bits = 0;
+			     outcnt = 0;
+			 }
+		     }
+		 }
+		 break;
 
-        }
-        RETURN (self);
+	}
+	RETURN (self);
     }
 %}
 .