Depth8Image.st
changeset 1657 f3d8ceac748a
parent 1634 72ee64fad9fe
child 1658 4c81124d0b37
--- a/Depth8Image.st	Thu Apr 24 17:55:58 1997 +0200
+++ b/Depth8Image.st	Thu Apr 24 17:57:54 1997 +0200
@@ -59,9 +59,9 @@
     "Modified: 20.4.1996 / 23:40:22 / cg"
 ! !
 
-!Depth8Image methodsFor:'accessing'!
+!Depth8Image methodsFor:'accessing - pixels'!
 
-atX:x y:y
+colorAtX:x y:y
     "retrieve a pixel at x/y; return a color.
      Pixels start at x=0 , y=0 for upper left pixel, end at
      x = width-1, y=height-1 for lower right pixel"
@@ -85,9 +85,40 @@
     ^ colorMap at:index
 
     "Modified: 8.6.1996 / 10:52:48 / cg"
+    "Created: 24.4.1997 / 17:33:58 / cg"
 !
 
-atX:x y:y putValue:aPixelValue
+pixelAtX:x y:y
+    "retrieve a pixel at x/y; return a pixelValue.
+     Pixels start at x=0 , y=0 for upper left pixel, end at
+     x = width-1, y=height-1 for lower right pixel"
+
+    |index "{ Class: SmallInteger }"|
+
+%{  /* NOCONTEXT */
+
+    OBJ b = _INST(bytes);
+    OBJ w = _INST(width);
+
+    if (__isByteArray(b) 
+     && __bothSmallInteger(x, y) 
+     && __isSmallInteger(w) ) {
+        int _idx, _pix;
+
+        _idx = (__intVal(w) * __intVal(y)) + __intVal(x);
+        if ((unsigned)_idx < __byteArraySize(b)) {
+            _pix = _ByteArrayInstPtr(b)->ba_element[_idx];
+            RETURN( __MKSMALLINT(_pix) );
+        }
+    }
+%}.
+    "/ should not be reached ...
+
+    index := (width * y) + 1 + x.
+    ^ bytes at:index.
+!
+
+pixelAtX:x y:y put:aPixelValue
     "set the pixel at x/y to aPixelValue.
      Pixels start at x=0 , y=0 for upper left pixel, end at
      x = width-1, y=height-1 for lower right pixel"
@@ -117,34 +148,114 @@
     bytes at:index put:aPixelValue.
 !
 
-valueAtX:x y:y
-    "retrieve a pixel at x/y; return a pixelValue.
-     Pixels start at x=0 , y=0 for upper left pixel, end at
-     x = width-1, y=height-1 for lower right pixel"
+rowAt:rowIndex into:aPixelBuffer startingAt:startIndex
+    "fill aPixelBuffer with pixel values from a single row.
+     Notice: row indexing starts at 0."
+
+    |srcIdx|
+
+    srcIdx := (rowIndex * width) + 1.
+    aPixelBuffer replaceFrom:startIndex to:startIndex+width-1 with:bytes startingAt:srcIdx
+
+    "Modified: 24.4.1997 / 15:47:22 / cg"
+!
+
+rowAt:rowIndex putAll:pixelArray
+    "replace a single rows bits from bits in the argument;
+     Notice: row indexing starts at 0."
+
+    |dstIdx|
+
+    dstIdx := (rowIndex*width) + 1.
+    bytes replaceFrom:dstIdx to:dstIdx+width-1 with:pixelArray startingAt:1.
+    ^ pixelArray
+
+    "Modified: 24.4.1997 / 14:34:23 / cg"
+!
 
-    |index "{ Class: SmallInteger }"|
+rowAt:rowIndex putAll:pixelArray startingAt:startIndex
+    "store a single rows bits from bits in the pixelArray argument;
+     Return the pixelArray.
+     Notice: row indexing starts at 0."
+
+    |dstIdx|
+
+    dstIdx := (rowIndex * width) + 1.
+    bytes replaceFrom:dstIdx to:dstIdx+width-1 with:pixelArray startingAt:startIndex.
+    ^ pixelArray
+
+    "Created: 24.4.1997 / 15:49:42 / cg"
+! !
 
-%{  /* NOCONTEXT */
+!Depth8Image methodsFor:'converting'!
+
+fromImage:anImage
+    "setup the receiver from another image.
+     The code here is tuned for depth 1, 2 and 4 source images;
+     other conversions are done in the superclasses fallBack method."
 
-    OBJ b = _INST(bytes);
-    OBJ w = _INST(width);
+    |srcBytesPerRow srcBuffer dstBuffer srcBytes srcIdx dstIdx 
+     srcDepth map|
+
+    srcDepth := anImage depth.
+    (#(1 2 4) includes:srcDepth) ifFalse:[
+        ^ super fromImage:anImage
+    ].
 
-    if (__isByteArray(b) 
-     && __bothSmallInteger(x, y) 
-     && __isSmallInteger(w) ) {
-        int _idx, _pix;
+    width := anImage width.
+    height := anImage height.
+    bytes := ByteArray uninitializedNew:(width * height).
+    bitsPerSample := self bitsPerSample.
+    samplesPerPixel := self samplesPerPixel.
+    self colormapFromImage:anImage.
+
+    colorMap isNil ifTrue:[
+        "/ if source has no colorMap, more work is needed ...
+        map := #(
+                #[0 16rFF]
+                #[0 16r55 16rAA 16rFF]
+                nil
+                #[16r00 16r11 16r22 16r33 16r44 16r55 16r66 16r77
+                  16r88 16r99 16rAA 16rBB 16rCC 16rDD 16rEE 16rFF]
+               ) at:srcDepth.
+    ].
+
+    self mask:anImage mask.
+
+    "/ only expand & translate pixels
+
+    srcBytes := anImage bits.
+    srcBytesPerRow := anImage bytesPerRow.
+    srcBuffer := ByteArray new:srcBytesPerRow.
 
-        _idx = (__intVal(w) * __intVal(y)) + __intVal(x);
-        if ((unsigned)_idx < __byteArraySize(b)) {
-            _pix = _ByteArrayInstPtr(b)->ba_element[_idx];
-            RETURN( __MKSMALLINT(_pix) );
-        }
-    }
-%}.
-    "/ should not be reached ...
+    dstBuffer := ByteArray new:width.
+    srcIdx := 1.
+    dstIdx := 1.
+    1 to:height do:[:hi |
+        srcBuffer replaceFrom:1 to:srcBytesPerRow with:srcBytes startingAt:srcIdx.
+        srcBuffer expandPixels:srcDepth width:width height:1 into:dstBuffer
+                       mapping:map. 
+        bytes replaceFrom:dstIdx to:dstIdx+width-1 with:dstBuffer startingAt:1.
+        dstIdx := dstIdx + width.
+        srcIdx := srcIdx + srcBytesPerRow.
+    ]
 
-    index := (width * y) + 1 + x.
-    ^ bytes at:index.
+    "
+     |i1 i2 i4 i8 i16 i24|
+
+     i1 := Image fromFile:'bitmaps/SBrowser.xbm'.
+     i2 := Depth2Image fromImage:i1.
+     i4 := Depth4Image fromImage:i1.
+
+     i8 := Depth8Image fromImage:i1.
+     i8 inspect.
+     i8 := Depth8Image fromImage:i2.
+     i8 inspect.
+     i8 := Depth8Image fromImage:i4.
+     i8 inspect.
+    "
+
+    "Modified: 24.4.1997 / 14:01:14 / cg"
 ! !
 
 !Depth8Image methodsFor:'converting palette images'!
@@ -203,7 +314,7 @@
      in color reduction, when not all colors can be aquired."
 
     |pseudoBits f gcRound has8BitImage deviceDepth
-     imgMap newImage pxl dColors
+     imgMap newImage pixelRow dColors
      usedColors usageCounts maxIndex map
      fit scale lastOK error 
      div 
@@ -510,10 +621,14 @@
     newImage bits:(ByteArray uninitializedNew:(height * newImage bytesPerRow)).
 
     0 to:height-1 do:[:row |
-        0 to:width-1 do:[:col |
-            pxl := self valueAtX:col y:row.
-            newImage atX:col y:row putValue:(map at:pxl+1)
-        ]
+        pixelRow := self rowAt:row.
+        pixelRow
+            expandPixels:8         "xlate only"
+            width:width 
+            height:1
+            into:pixelRow
+            mapping:map.
+        newImage rowAt:row putAll:pixelRow
     ].
 
     f := Form width:width height:height depth:deviceDepth on:aDevice.
@@ -528,7 +643,7 @@
 
     ^ f
 
-    "Modified: 22.4.1997 / 11:26:10 / cg"
+    "Modified: 24.4.1997 / 16:13:41 / cg"
 !
 
 paletteImageAsTrueColorFormOn:aDevice
@@ -1193,92 +1308,6 @@
 
 !Depth8Image methodsFor:'image manipulations'!
 
-applyPixelValuesTo:pixelFunctionBlock into:destImage
-    "helper for withPixelFunctionAppliedToValues:
-     enumerate pixelValues and evaluate the block for each.
-     To be redefined by subclasses for better performance."
-
-    |w  "{Class: SmallInteger }"
-     h  "{Class: SmallInteger }"
-     bpr          "{Class: SmallInteger }"
-     pixelIndex   "{Class: SmallInteger }"
-     nextRowIndex "{Class: SmallInteger }"
-     destBytes|
-
-    w := width - 1.
-    h := height - 1.
-    nextRowIndex := 1.
-    bpr := self bytesPerRow.
-
-    destImage bytesPerRow == bpr ifTrue:[
-        destImage depth == self depth ifTrue:[
-            destBytes := destImage bits.
-            0 to:h do:[:y |
-                pixelIndex := nextRowIndex.
-                nextRowIndex := nextRowIndex + bpr.
-
-                0 to:w do:[:x |
-                    destBytes 
-                        at:pixelIndex
-                        put:(pixelFunctionBlock
-                                value:self
-                                value:(bytes at:pixelIndex)
-                                value:x
-                                value:y).
-                    pixelIndex := pixelIndex + 1.
-                ]
-            ].
-            ^ self.
-        ]
-    ].
-
-    0 to:h do:[:y |
-        pixelIndex := nextRowIndex.
-        nextRowIndex := nextRowIndex + bpr.
-
-        0 to:w do:[:x |
-            destImage atX:x y:y putValue:(pixelFunctionBlock
-                                                value:self
-                                                value:(bytes at:pixelIndex)
-                                                value:x
-                                                value:y).
-            pixelIndex := pixelIndex + 1.
-        ]
-    ].
-
-    "Created: 10.4.1997 / 16:30:10 / cg"
-    "Modified: 10.4.1997 / 16:44:51 / cg"
-!
-
-flipHorizontal
-    "inplace horizontal flip"
-
-    |index  "{Class: SmallInteger }"
-     h      "{Class: SmallInteger }"
-     w      "{Class: SmallInteger }"
-     buffer |
-
-    w := width - 1.
-    h := height - 1.
-
-    buffer := ByteArray new:width.
-
-    index := 1.
-    0 to:h do:[:row |
-        buffer replaceFrom:1 to:width with:bytes startingAt:index.
-        buffer reverse.
-        bytes replaceFrom:index to:index+w with:buffer startingAt:1.
-        index := index + w + 1.
-    ].
-    mask notNil ifTrue:[
-        mask flipHorizontal
-    ].
-    "flush device info"
-    self restored
-
-    "Modified: 22.4.1997 / 12:31:58 / cg"
-!
-
 hardMagnifiedBy:scalePoint
     "return a new image magnified by scalePoint, aPoint.
      This is the general magnification method, handling non-integral values"
@@ -1677,5 +1706,5 @@
 !Depth8Image class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libview/Depth8Image.st,v 1.69 1997-04-22 19:12:04 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libview/Depth8Image.st,v 1.70 1997-04-24 15:57:02 cg Exp $'
 ! !