Depth4Image.st
author Claus Gittinger <cg@exept.de>
Sat, 08 Jun 1996 13:04:12 +0200
changeset 803 899f7eb32ef2
parent 798 31ed4a1d4b4a
child 806 8d8a58e12c08
permissions -rw-r--r--
support #rgb - specialized code relaxed

"
 COPYRIGHT (c) 1993 by Claus Gittinger
	      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
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

Image subclass:#Depth4Image
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'Graphics-Images'
!

!Depth4Image class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1993 by Claus Gittinger
	      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
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

documentation
"
    this class represents 16-color (4 bit / pixel) images.
    Most images coming from the windows world are represented as Depth4Images.
    It mainly consists of methods already implemented in Image,
    reimplemented here for more performance.

    [author:]
        Claus Gittinger

    [see also:]
        Depth1Image Depth2Image Depth8Image Depth16Image Depth24Image
        ImageReader
"
! !

!Depth4Image class methodsFor:'queries'!

imageDepth
    "return the depth of images represented by instances of
     this class - here we return 4"

    ^ 4

    "Modified: 20.4.1996 / 23:40:18 / cg"
! !

!Depth4Image methodsFor:'accessing'!

atX: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"

    |lineIndex "{ Class: SmallInteger }"
     byte      "{ Class: SmallInteger }"
     value     "{ Class: SmallInteger }" |

    lineIndex := (self bytesPerRow * y) + 1.

    "left pixel in high bits"
    byte := bytes at:(lineIndex + (x // 2)).
    x even ifTrue:[
        value := (byte bitShift:-4) bitAnd:16rF.
    ] ifFalse:[
        value := byte bitAnd:16rF.
    ].
    photometric == #whiteIs0 ifTrue:[
        ^ Color gray:100 - (100 / 15 * value)
    ].
    photometric == #blackIs0 ifTrue:[
        ^ Color gray:(100 / 15 * value)
    ].
    photometric ~~ #palette ifTrue:[
        ^ self colorFromValue:value
    ].
    ^ colorMap at:(value + 1)

    "Modified: 8.6.1996 / 10:52:43 / cg"
!

atX:x y:y putValue: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"

    |lineIndex "{ Class: SmallInteger }"
     index     "{ Class: SmallInteger }"
     byte      "{ Class: SmallInteger }" |

    lineIndex := (self bytesPerRow * y) + 1.

    "left pixel is in high bits"
    index := lineIndex + (x // 2).
    byte := bytes at:index.
    x even ifTrue:[
	byte := (byte bitAnd:16rF) bitOr:(aPixelValue bitShift:4)
    ] ifFalse:[
	byte := (byte bitAnd:16rF0) bitOr:aPixelValue
    ].
    bytes at:index put:byte
!

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"

    |lineIndex "{ Class: SmallInteger }"
     byte      "{ Class: SmallInteger }" |

    lineIndex := (self bytesPerRow * y) + 1.

    "left pixel in high bits"
    byte := bytes at:(lineIndex + (x // 2)).
    x even ifTrue:[
	^ (byte bitShift:-4) bitAnd:16rF.
    ].
    ^ byte bitAnd:16rF.
! !

!Depth4Image methodsFor:'converting'!

orderedDitheredMonochromeBitsWithDitherMatrix:ditherMatrix ditherWidth:dW
    "return the dithered monochrome bits for the receiver image;
     with a constant ditherMatrix, this can be used for thresholding.
     Redefined to make use of knowing that pixels are 4-bit values."

    |f dH nDither   
     greyMap monoBits
     bytesPerMonoRow "{Class: SmallInteger }"
     bytesPerRow     "{Class: SmallInteger }"
     w               "{Class: SmallInteger }"
     h               "{Class: SmallInteger }"|

    nDither := ditherMatrix size.
    dH := nDither / dW.

    w := width.
    h := height.

    bytesPerRow := self bytesPerRow.

    bytesPerMonoRow := w + 7 // 8.
    monoBits := ByteArray uninitializedNew:(bytesPerMonoRow * h).
    (monoBits isNil or:[bytes isNil]) ifTrue:[
        ^ nil
    ].

    greyMap := self greyByteMapForRange:nDither.

%{
    int __dW = __intVal(dW);
    int __dH = __intVal(dH);
    int __byte;
    int __dT;
    int __dstIdx = 0;
    int __srcIdx = 0;
    int __bitCnt;
    int __inByte;
    int __grey;
    int __w = __intVal(w);
    int __h = __intVal(h);
    int __x;
    int __y;
    int __oX, __oY, __dY;
    int __nextDst;
    int __nextSrc;
    int __bytesPerRow = __intVal(bytesPerRow);
    int __bytesPerMonoRow = __intVal(bytesPerMonoRow);

    char *__monoBits = __ByteArrayInstPtr(monoBits)->ba_element;
    char *__ditherMatrix = __ByteArrayInstPtr(ditherMatrix)->ba_element;
    unsigned char *__bytes = __ByteArrayInstPtr(__INST(bytes))->ba_element;
    unsigned char *__greyMap = __ByteArrayInstPtr(greyMap)->ba_element;

    __oY = __dY = 0;
    for (__y=0; __y<__h; __y++) {
        __nextDst = __dstIdx + __bytesPerMonoRow;
        __nextSrc = __srcIdx + __bytesPerRow;

        __byte = 0;
        __bitCnt = 8;

        __oX = 0;

        for (__x=0; __x<__w; __x++) {
            if (__x & 1) {
                /* odd */
                __grey = __inByte & 0xF;
                __srcIdx++;
            } else {
                __inByte = __bytes[__srcIdx];   /* 0..255 */
                __grey = (__inByte >> 4) & 0xF;
            }

            __grey = __greyMap[__grey];
            __dT = __ditherMatrix[__dY + __oX];

            __oX++;
            if (__oX == __dW) __oX = 0;

            __byte = __byte << 1;
            if (__grey > __dT) {
                __byte = __byte | 1;       /* white */
            }

            __bitCnt--;
            if (__bitCnt == 0) {
                __monoBits[__dstIdx] = __byte;
                __dstIdx++;
                __byte = 0;
                __bitCnt = 8;
            }
        }

        if (__bitCnt != 8) {
            __byte = __byte << __bitCnt;
            __monoBits[__dstIdx] = __byte;
        }

        __oY++; __dY += __dW;
        if (__oY == __dH) {
            __oY = 0;
            __dY = 0;
        }

        __srcIdx = __nextSrc;
        __dstIdx = __nextDst;
    }
%}.

    ^ monoBits
! !

!Depth4Image methodsFor:'enumerating'!

colorsAtY:y from:xLow to:xHigh do:aBlock
    "perform aBlock for each pixel from x1 to x2 in row y.
     The block is passed the color at each pixel.
     This method allows slighly faster processing of an
     image than using atX: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 }"
     colors p |

    (p := photometric) == #palette ifTrue:[
        colors := colorMap.
    ] ifFalse:[
        (p == #whiteIs0 or:[p == #blackIs0]) ifTrue:[
            colors := Array new:16.
            0 to:15 do:[:i |
                colors at:(i+1) put:(Color gray:100 - (100 / 15 * i))
            ].
            p == #blackIs0 ifTrue:[
                colors reverse
            ]
        ] ifFalse:[
            ^ super colorsAtY:y from:xLow to:xHigh do:aBlock.
        ]
    ].

    x1 := xLow.
    x2 := xHigh.
    srcIndex := (self bytesPerRow * y) + 1.
    srcIndex := srcIndex + (x1 // 2).
    x1 even ifTrue:[
        shift := -4
    ] ifFalse:[
        shift := 0
    ].

    x1 to:x2 do:[:x |
        shift == 0 ifTrue:[
            byte := bytes at:srcIndex.
            value := byte bitAnd:16rF.
            shift := -4.
            srcIndex := srcIndex + 1.
        ] ifFalse:[
            byte := bytes at:srcIndex.
            value := (byte bitShift:-4) bitAnd:16rF.
            shift := 0
        ].
        aBlock value:x value:(colors at:(value + 1)).
    ]

    "Created: 7.6.1996 / 19:12:33 / cg"
    "Modified: 8.6.1996 / 10:16:05 / cg"
!

valuesAtY:y from:xLow to:xHigh do:aBlock
    "perform aBlock for each pixelValue from x1 to x2 in row y.
     The block is passed 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 }"
     pixelValue "{ Class: SmallInteger }"
     x1         "{ Class: SmallInteger }"
     x2         "{ Class: SmallInteger }"
     |

    x1 := xLow.
    x2 := xHigh.
    srcIndex := (self bytesPerRow * y) + 1.
    srcIndex := srcIndex + (x1 // 2).
    x1 even ifTrue:[
        shift := -4
    ] ifFalse:[
        shift := 0
    ].

    x1 to:x2 do:[:x |
        shift == 0 ifTrue:[
            byte := bytes at:srcIndex.
            pixelValue := byte bitAnd:16rF.
            shift := -4.
            srcIndex := srcIndex + 1.
        ] ifFalse:[
            byte := bytes at:srcIndex.
            pixelValue := (byte bitShift:-4) bitAnd:16rF.
            shift := 0
        ].
        aBlock value:x value:pixelValue.
    ]

    "Created: 7.6.1996 / 19:09:45 / cg"
! !

!Depth4Image methodsFor:'magnification'!

magnifyRowFrom:srcBytes offset:srcStart
	  into:dstBytes offset:dstStart factor:mX

    "magnify a single pixel row - can only magnify by integer factors.
     Specially tuned for factor 2."

%{
    unsigned char *srcP, *dstP;
    int _mag;
    REGISTER int i;
    REGISTER unsigned char _byte;
    int _pixels;
    REGISTER int outcnt, bits, bit;
    OBJ w = _INST(width);

    if (__bothSmallInteger(srcStart, dstStart)
     && __bothSmallInteger(w, 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(w);

	switch (_mag) {
	    case 1:
		break;

	    case 2:
		_byte = *srcP++;
		while (_pixels) {
		    bit = (_byte >> 4) & 0xF;
		    bits = (bit << 4) | bit;
		    *dstP++ = bits;

		    if (--_pixels) {
			bit = _byte & 0xF;
			bits = (bit << 4) | bit;
			*dstP++ = bits;
			_byte = *srcP++;
			_pixels--;
		    }
		}
		break;

	    default:
		bits = 0, outcnt = 0;
		_byte = *srcP++;
		while (_pixels) {
		    bit = (_byte >> 4) & 0xF;

		    for (i=_mag; i>0; i--) {
			bits = (bits << 4) | bit;
			outcnt++;
			if (outcnt == 2) {
			    *dstP++ = bits;
			    bits = 0;
			    outcnt = 0;
			}
		    }

		    if (--_pixels) {
			bit = _byte & 0xF;
			for (i=_mag; i>0; i--) {
			    bits = (bits << 4) | bit;
			    outcnt++;
			    if (outcnt == 2) {
				*dstP++ = bits;
				bits = 0;
				outcnt = 0;
			    }
			}
			_byte = *srcP++;
			_pixels--;
		    }
		}

		break;
	}
	RETURN (self);
    }
%}
.
    self primitiveFailed
! !

!Depth4Image methodsFor:'queries'!

bitsPerPixel
    "return the number of bits per pixel"

    ^ 4
!

bitsPerRow
    "return the number of bits in one scanline of the image"

    ^  width * 4
!

bytesPerRow
    "return the number of bytes in one scanline of the image"

    |nbytes|

    nbytes := width // 2.
    width odd ifTrue:[
	^ nbytes + 1
    ].
    ^ nbytes
!

usedValues
    "return a collection of color values used in the receiver."

    |useFlags usedValues|

    useFlags := Array new:16 withAll:false.
    width even ifFalse:[
        0 to:self height - 1 do:[:y |
            self valuesAtY:y from:0 to:self width - 1 do:[:x :pixel |
                useFlags at:(pixel + 1) put:true
            ]
        ].
    ] ifTrue:[
        bytes usedValues do:[:byte |
            useFlags at:(byte bitShift:-4)+1 put:true.
            useFlags at:(byte bitAnd:2r1111)+1 put:true.
        ].
    ].
    usedValues := OrderedCollection new.
    1 to:16 do:[:i | (useFlags at:i) ifTrue:[usedValues add:(i-1)]].
    ^ usedValues

    "Modified: 7.6.1996 / 19:09:14 / cg"
! !

!Depth4Image class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libview/Depth4Image.st,v 1.19 1996-06-08 11:03:32 cg Exp $'
! !