support 8-bit rgb displays
authorClaus Gittinger <cg@exept.de>
Fri, 14 Jun 1996 17:59:42 +0200
changeset 856 9f802d4d0c1a
parent 855 9b5aa569679f
child 857 df5125310d86
support 8-bit rgb displays
Depth24Image.st
--- a/Depth24Image.st	Fri Jun 14 17:58:34 1996 +0200
+++ b/Depth24Image.st	Fri Jun 14 17:59:42 1996 +0200
@@ -678,7 +678,7 @@
 
     |bestFormat usedDeviceDepth usedDeviceBitsPerPixel depth
      myDepth form imageBits destIndex srcIndex 
-     rightShiftR rightShiftG rightShiftB shiftRed shiftGreen shiftBlue ok|
+     rightShiftR rightShiftG rightShiftB shiftRed shiftGreen shiftBlue|
 
     bestFormat := self bestSupportedImageFormatFor:aDevice.
     usedDeviceDepth := bestFormat at:#depth.
@@ -711,9 +711,7 @@
 
             "/ now, walk over the image and compose 16bit values from the r/g/b triples
 
-            ok := false.
-
-%{          /* OPTIONAL */
+%{
             if (__bothSmallInteger(_INST(height),_INST(width))
              && __bothSmallInteger(rightShiftR, shiftRed)
              && __bothSmallInteger(rightShiftG, shiftGreen)
@@ -772,55 +770,18 @@
                         }
                     }
                 }
-                ok = true;
             }
 %}.
-            ok ifFalse:[
-                "/ this fallback is only executed if type is not
-                "/ what the primitive expects; for example, if the bytes-instvar
-                "/ is not a ByteArray
-
-                rightShiftR := rightShiftR negated.
-                rightShiftG := rightShiftG negated.
-                rightShiftB := rightShiftB negated.
-
-                destIndex := 1.
-                srcIndex := 1.
-
-                0 to:height-1 do:[:y |
-                    0 to:width-1 do:[:x |
-                        |r g b v|
-
-                        r := bytes at:srcIndex.
-                        g := bytes at:(srcIndex + 1).
-                        b := bytes at:(srcIndex + 2).
-
-                        r := r bitShift:rightShiftR.
-                        g := g bitShift:rightShiftG.
-                        b := b bitShift:rightShiftB.
-
-                        v := r bitShift:shiftRed.
-                        v := v bitOr:(g bitShift:shiftGreen).
-                        v := v bitOr:(b bitShift:shiftBlue).
-
-                        imageBits wordAt:destIndex put:v MSB:true.
-                        destIndex := destIndex + 2.
-                        srcIndex := srcIndex + 3.
-                    ]
-                ]
-            ]
         ] ifFalse:[
             "/
-            "/ 32bit pixels
+            "/ 32 bits/pixel ...
             "/
             (usedDeviceBitsPerPixel == 32) ifTrue:[
                 imageBits := ByteArray uninitializedNew:(width * height * 4).
 
                 "/ now, walk over the image and compose 32bit values from the r/g/b triples
 
-                ok := false.
-
-%{              /* OPTIONAL */
+%{       
                 if (__bothSmallInteger(_INST(height), _INST(width))
                  && __bothSmallInteger(rightShiftR, shiftRed)
                  && __bothSmallInteger(rightShiftG, shiftGreen)
@@ -883,50 +844,79 @@
                             }
                         }
                     }
-                    ok = true;
                 }
 %}.
-                ok ifFalse:[
-                    "/ this fallback is only executed if type is not
-                    "/ what the primitive expects; for example, if the bytes-instvar
-                    "/ is not a ByteArray; or, if this class is autoload on non-compiling
-                    "/ systems.
+            ] ifFalse:[
+                "/
+                "/ 8 bits/pixel ...
+                "/
+                (usedDeviceBitsPerPixel == 8) ifTrue:[
+                    imageBits := ByteArray uninitializedNew:(width * height).
+
+                    "/ now, walk over the image and compose 8bit values from the r/g/b triples
 
-                    rightShiftR := rightShiftR negated.
-                    rightShiftG := rightShiftG negated.
-                    rightShiftB := rightShiftB negated.
+%{              
+                    if (__bothSmallInteger(_INST(height), _INST(width))
+                     && __bothSmallInteger(rightShiftR, shiftRed)
+                     && __bothSmallInteger(rightShiftG, shiftGreen)
+                     && __bothSmallInteger(rightShiftB, shiftBlue)
+                     && __isByteArray(_INST(bytes))
+                     && __isByteArray(imageBits)) {
+                        int rShRed = __intVal(rightShiftR),
+                            rShGreen = __intVal(rightShiftG),
+                            rShBlue = __intVal(rightShiftB),
+                            lShRed = __intVal(shiftRed),
+                            lShGreen = __intVal(shiftGreen),
+                            lShBlue = __intVal(shiftBlue);
+                        int x, y, w;
 
-                    destIndex := 1.
-                    srcIndex := 1.
-
-                    0 to:height-1 do:[:y |
-                        0 to:width-1 do:[:x |
-                            |r g b v|
+                        unsigned char *srcPtr = _ByteArrayInstPtr(_INST(bytes))->ba_element;
+                        char *dstPtr = _ByteArrayInstPtr(imageBits)->ba_element;
 
-                            r := bytes at:srcIndex.
-                            g := bytes at:(srcIndex + 1).
-                            b := bytes at:(srcIndex + 2).
+                        w = __intVal(_INST(width));
+                        if ((rShRed == 0) && (rShGreen == 0) && (rShBlue == 0)) {
+                            for (y=__intVal(_INST(height)); y > 0; y--) {
+                                for (x=w; x > 0; x--) {
+                                    unsigned v;
 
-                            r := r bitShift:rightShiftR.
-                            g := g bitShift:rightShiftG.
-                            b := b bitShift:rightShiftB.
+                                    v = srcPtr[0] << lShRed;
+                                    v |= (srcPtr[1] << lShGreen);
+                                    v |= (srcPtr[2] << lShBlue);
+                                    dstPtr[0] = v;
 
-                            v := r bitShift:shiftRed.
-                            v := v bitOr:(g bitShift:shiftGreen).
-                            v := v bitOr:(b bitShift:shiftBlue).
+                                    dstPtr += 1;
+                                    srcPtr += 3;
+                                }
+                            }
+                        } else {
+                            for (y=__intVal(_INST(height)); y > 0; y--) {
+                                for (x=w; x > 0; x--) {
+                                    unsigned r, g, b, v;
 
-                            imageBits doubleWordAt:destIndex put:v MSB:true.
-                            destIndex := destIndex + 4.
-                            srcIndex := srcIndex + 3.
-                        ]
-                    ]
+                                    r = srcPtr[0] >> rShRed;
+                                    g = srcPtr[1] >> rShGreen;
+                                    b = srcPtr[2] >> rShBlue;
+                                    v = r << lShRed;
+                                    v |= (g << lShGreen);
+                                    v |= (b << lShBlue);
+
+                                    dstPtr[0] = v;
+
+                                    dstPtr += 1;
+                                    srcPtr += 3;
+                                }
+                            }
+                        }
+                    }
+%}.
                 ]
             ].
         ]
     ].
 
     imageBits isNil ifTrue:[            
-        'IMAGE: unimplemented trueColor depth in rgbImageAsTrueColorFormOn:' errorPrintNL.
+        'IMAGE: unimplemented trueColor depth in #rgbImageAsTrueColorFormOn: ' errorPrint.
+        usedDeviceBitsPerPixel errorPrintCR.
         ^ self asMonochromeFormOn:aDevice
     ].
 
@@ -1718,5 +1708,5 @@
 !Depth24Image class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libview/Depth24Image.st,v 1.33 1996-06-12 18:06:53 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libview/Depth24Image.st,v 1.34 1996-06-14 15:59:42 cg Exp $'
 ! !