diff -r 75ff98ae918d -r 62426e8110ae Depth8Image.st --- a/Depth8Image.st Tue May 21 22:49:14 2013 +0200 +++ b/Depth8Image.st Tue May 21 22:50:48 2013 +0200 @@ -75,19 +75,19 @@ %{ /* NOCONTEXT */ - OBJ b = _INST(bytes); - OBJ w = _INST(width); + OBJ b = __INST(bytes); + OBJ w = __INST(width); if (__isByteArrayLike(b) && __bothSmallInteger(x, y) && __isSmallInteger(w) ) { - int _idx, _pix; + int _idx, _pix; - _idx = (__intVal(w) * __intVal(y)) + __intVal(x); - if ((unsigned)_idx < __byteArraySize(b)) { - _pix = __ByteArrayInstPtr(b)->ba_element[_idx]; - RETURN( __MKSMALLINT(_pix) ); - } + _idx = (__intVal(w) * __intVal(y)) + __intVal(x); + if ((unsigned)_idx < __byteArraySize(b)) { + _pix = __ByteArrayInstPtr(b)->ba_element[_idx]; + RETURN( __MKSMALLINT(_pix) ); + } } %}. "/ the code below is only evaluated if the bytes-collection is @@ -106,19 +106,19 @@ %{ /* NOCONTEXT */ - OBJ b = _INST(bytes); - OBJ w = _INST(width); + OBJ b = __INST(bytes); + OBJ w = __INST(width); if (__isByteArrayLike(b) && __bothSmallInteger(x, y) && __bothSmallInteger(w, aPixelValue) ) { - int _idx; + int _idx; - _idx = (__intVal(w) * __intVal(y)) + __intVal(x); - if ((unsigned)_idx < __byteArraySize(b)) { - __ByteArrayInstPtr(b)->ba_element[_idx] = __intVal(aPixelValue); - RETURN( self ); - } + _idx = (__intVal(w) * __intVal(y)) + __intVal(x); + if ((unsigned)_idx < __byteArraySize(b)) { + __ByteArrayInstPtr(b)->ba_element[_idx] = __intVal(aPixelValue); + RETURN( self ); + } } %}. "fall back code for nonByteArray or nonInteger arguments" @@ -134,18 +134,18 @@ |srcIdx endIndex n| bytes notNil ifTrue:[ - srcIdx := (y * width) + 1. - n := width. + srcIdx := (y * width) + 1. + n := width. - endIndex := srcIdx+width-1. - endIndex > bytes size ifTrue:[ - self assert:(bytes size >= endIndex). - endIndex := bytes size. - n := endIndex - srcIdx + 1. - ]. + endIndex := srcIdx+width-1. + endIndex > bytes size ifTrue:[ + self assert:(bytes size >= endIndex). + endIndex := bytes size. + n := endIndex - srcIdx + 1. + ]. - aPixelBuffer replaceFrom:startIndex to:startIndex+n-1 with:bytes startingAt:srcIdx. - ^ self. + aPixelBuffer replaceFrom:startIndex to:startIndex+n-1 with:bytes startingAt:srcIdx. + ^ self. ]. ^ super rowAt:y into:aPixelBuffer startingAt:startIndex @@ -264,36 +264,36 @@ cube nR nG nB ditherColors clr| (cube := aDevice fixColors) notNil ifTrue:[ - nR := aDevice numFixRed. - nG := aDevice numFixGreen. - nB := aDevice numFixBlue. + nR := aDevice numFixRed. + nG := aDevice numFixGreen. + nB := aDevice numFixBlue. - DitherAlgorithm == #floydSteinberg ifTrue:[ - f := self - asFloydSteinbergDitheredDepth8FormOn:aDevice - colors:cube - nRed:nR - nGreen:nG - nBlue:nB. - ] ifFalse:[ - f := self - asNearestPaintDepth8FormOn:aDevice - colors:cube - nRed:nR - nGreen:nG - nBlue:nB. - ]. - f notNil ifTrue:[^ f]. + DitherAlgorithm == #floydSteinberg ifTrue:[ + f := self + asFloydSteinbergDitheredDepth8FormOn:aDevice + colors:cube + nRed:nR + nGreen:nG + nBlue:nB. + ] ifFalse:[ + f := self + asNearestPaintDepth8FormOn:aDevice + colors:cube + nRed:nR + nGreen:nG + nBlue:nB. + ]. + f notNil ifTrue:[^ f]. ]. "find used colors" bytes := self bits. usedColors := bytes usedValues. "gets us an array filled with used values" - "(could use bytes asBag)" + "(could use bytes asBag)" maxIndex := usedColors max + 1. usedColors size > 20 ifTrue:[ - ('Depth8Image [info]: allocating ' , usedColors size printString , ' colors ...') infoPrintCR. + ('Depth8Image [info]: allocating ' , usedColors size printString , ' colors ...') infoPrintCR. ]. "sort by usage" @@ -319,187 +319,187 @@ gcRound := 0. usedColors do:[:aColorIndex | - |devColor color - r "{Class: SmallInteger }" - g "{Class: SmallInteger }" - b "{Class: SmallInteger }" - mapIndex "{Class: SmallInteger }"| + |devColor color + r "{Class: SmallInteger }" + g "{Class: SmallInteger }" + b "{Class: SmallInteger }" + mapIndex "{Class: SmallInteger }"| - fit ifTrue:[ - mapIndex := aColorIndex + 1. - "/ color := colorMap at:mapIndex. + fit ifTrue:[ + mapIndex := aColorIndex + 1. + "/ color := colorMap at:mapIndex. - color := self colorFromValue:aColorIndex. - (color isOnDevice:aDevice) ifTrue:[ - "wow - an immediate hit" - devColor := color - ] ifFalse:[ - devColor := color exactOn:aDevice. - devColor isNil ifTrue:[ - " - could not allocate color - on the first round, do a GC to flush - unused colors - this may help if some colors where locked by - already free images. - " - gcRound == 0 ifTrue:[ - ObjectMemory scavenge; finalize. - devColor := color exactOn:aDevice. - gcRound := 1 - ]. - devColor isNil ifTrue:[ - gcRound == 1 ifTrue:[ - CollectGarbageWhenRunningOutOfColors ifTrue:[ - 'Depth8Image [info]: force GC for possible color reclamation.' infoPrintCR. - ObjectMemory incrementalGC; finalize. - devColor := color exactOn:aDevice. - ]. - gcRound := 2 - ] - ] - ]. - ]. - (devColor notNil and:[devColor colorId notNil]) ifTrue:[ - imgMap at:mapIndex put:devColor. - lastOK := lastOK + 1. - ] ifFalse:[ - fit := false - ] - ] + color := self colorFromValue:aColorIndex. + (color isOnDevice:aDevice) ifTrue:[ + "wow - an immediate hit" + devColor := color + ] ifFalse:[ + devColor := color exactOn:aDevice. + devColor isNil ifTrue:[ + " + could not allocate color - on the first round, do a GC to flush + unused colors - this may help if some colors where locked by + already free images. + " + gcRound == 0 ifTrue:[ + ObjectMemory scavenge; finalize. + devColor := color exactOn:aDevice. + gcRound := 1 + ]. + devColor isNil ifTrue:[ + gcRound == 1 ifTrue:[ + CollectGarbageWhenRunningOutOfColors ifTrue:[ + 'Depth8Image [info]: force GC for possible color reclamation.' infoPrintCR. + ObjectMemory incrementalGC; finalize. + devColor := color exactOn:aDevice. + ]. + gcRound := 2 + ] + ] + ]. + ]. + (devColor notNil and:[devColor colorId notNil]) ifTrue:[ + imgMap at:mapIndex put:devColor. + lastOK := lastOK + 1. + ] ifFalse:[ + fit := false + ] + ] ]. fit ifFalse:[ - ('Depth8Image [info]: got %1 exact colors (out of %2)' bindWith:lastOK with:usedColors size) infoPrintCR. + ('Depth8Image [info]: got %1 exact colors (out of %2)' bindWith:lastOK with:usedColors size) infoPrintCR. - DitherAlgorithm == #floydSteinberg ifTrue:[ - dColors := imgMap collect:[:clr | clr isNil ifTrue:[clr] - ifFalse:[clr nearestOn:aDevice]]. - dColors := dColors select:[:clr | clr notNil] thenCollect:[:clr | clr exactOn:aDevice]. - dColors := dColors asSet. - dColors addAll:(aDevice colorMap collect:[:c|c onDevice:aDevice] thenSelect:[:c | c colorId notNil]). - ditherColors := aDevice availableDitherColors. - ditherColors notNil ifTrue:[ - dColors addAll:ditherColors. - ]. - dColors := dColors asArray. - dColors size > 256 ifTrue:[ - dColors := dColors copyTo:256 - ]. - ^ self asFloydSteinbergDitheredPseudoFormUsing:dColors on:aDevice - ]. + DitherAlgorithm == #floydSteinberg ifTrue:[ + dColors := imgMap collect:[:clr | clr isNil ifTrue:[clr] + ifFalse:[clr nearestOn:aDevice]]. + dColors := dColors select:[:clr | clr notNil] thenCollect:[:clr | clr exactOn:aDevice]. + dColors := dColors asSet. + dColors addAll:(aDevice colorMap collect:[:c|c onDevice:aDevice] thenSelect:[:c | c colorId notNil]). + ditherColors := aDevice availableDitherColors. + ditherColors notNil ifTrue:[ + dColors addAll:ditherColors. + ]. + dColors := dColors asArray. + dColors size > 256 ifTrue:[ + dColors := dColors copyTo:256 + ]. + ^ self asFloydSteinbergDitheredPseudoFormUsing:dColors on:aDevice + ]. - " - again, this time allow wrong colors (loop while increasing allowed error) - " - error := 1. - [fit] whileFalse:[ - fit := true. - usedColors from:(lastOK+1) to:(usedColors size) do:[:aColorIndex | - |devColor color - r "{Class: SmallInteger }" - g "{Class: SmallInteger }" - b "{Class: SmallInteger }" - mapIndex "{Class: SmallInteger }" - rMask "{Class: SmallInteger }" - gMask "{Class: SmallInteger }" - bMask "{Class: SmallInteger }"| + " + again, this time allow wrong colors (loop while increasing allowed error) + " + error := 1. + [fit] whileFalse:[ + fit := true. + usedColors from:(lastOK+1) to:(usedColors size) do:[:aColorIndex | + |devColor color + r "{Class: SmallInteger }" + g "{Class: SmallInteger }" + b "{Class: SmallInteger }" + mapIndex "{Class: SmallInteger }" + rMask "{Class: SmallInteger }" + gMask "{Class: SmallInteger }" + bMask "{Class: SmallInteger }"| - fit ifTrue:[ - gMask := bMask := rMask := m. + fit ifTrue:[ + gMask := bMask := rMask := m. - mapIndex := aColorIndex + 1. - "/ color := colorMap at:mapIndex. - color := self colorFromValue:aColorIndex. - r := (color red * 255 / 100.0) rounded. - g := (color green * 255 / 100.0) rounded. - b := (color blue * 255 / 100.0) rounded. + mapIndex := aColorIndex + 1. + "/ color := colorMap at:mapIndex. + color := self colorFromValue:aColorIndex. + r := (color red * 255 / 100.0) rounded. + g := (color green * 255 / 100.0) rounded. + b := (color blue * 255 / 100.0) rounded. - color := Color red:((r bitShift:shift) bitAnd:rMask) * scale - green:((g bitShift:shift) bitAnd:gMask) * scale - blue:((b bitShift:shift) bitAnd:bMask) * scale. + color := Color red:((r bitShift:shift) bitAnd:rMask) * scale + green:((g bitShift:shift) bitAnd:gMask) * scale + blue:((b bitShift:shift) bitAnd:bMask) * scale. - (color isOnDevice:aDevice) ifTrue:[ - "wow - an immediate hit" - devColor := color. - ] ifFalse:[ - devColor := color nearestOn:aDevice. - (devColor notNil and:[(devColor deltaFrom:color) > error]) ifTrue:[ - devColor := nil - ]. - devColor isNil ifTrue:[ - " - no free color - on the first round, do a GC to flush unused - colors - this may help if some colors where locked by already - free images. - " - gcRound == 0 ifTrue:[ - ObjectMemory scavenge; finalize. - devColor := color nearestOn:aDevice. - (devColor notNil and:[(devColor deltaFrom:color) > error]) ifTrue:[ - devColor := nil - ]. - gcRound := 1 - ]. - devColor isNil ifTrue:[ - gcRound == 1 ifTrue:[ - CollectGarbageWhenRunningOutOfColors ifTrue:[ - 'Depth8Image [info]: force GC for possible color reclamation.' infoPrintCR. - ObjectMemory incrementalGC; finalize. - devColor := color nearestOn:aDevice. - (devColor notNil and:[(devColor deltaFrom:color) > error]) ifTrue:[ - devColor := nil - ]. - ]. - gcRound := 2 - ] - ] - ]. - ]. - (devColor notNil and:[devColor colorId notNil]) ifTrue:[ - imgMap at:mapIndex put:devColor. - lastOK := lastOK + 1. - ] ifFalse:[ - fit := false - ] - ]. - ]. + (color isOnDevice:aDevice) ifTrue:[ + "wow - an immediate hit" + devColor := color. + ] ifFalse:[ + devColor := color nearestOn:aDevice. + (devColor notNil and:[(devColor deltaFrom:color) > error]) ifTrue:[ + devColor := nil + ]. + devColor isNil ifTrue:[ + " + no free color - on the first round, do a GC to flush unused + colors - this may help if some colors where locked by already + free images. + " + gcRound == 0 ifTrue:[ + ObjectMemory scavenge; finalize. + devColor := color nearestOn:aDevice. + (devColor notNil and:[(devColor deltaFrom:color) > error]) ifTrue:[ + devColor := nil + ]. + gcRound := 1 + ]. + devColor isNil ifTrue:[ + gcRound == 1 ifTrue:[ + CollectGarbageWhenRunningOutOfColors ifTrue:[ + 'Depth8Image [info]: force GC for possible color reclamation.' infoPrintCR. + ObjectMemory incrementalGC; finalize. + devColor := color nearestOn:aDevice. + (devColor notNil and:[(devColor deltaFrom:color) > error]) ifTrue:[ + devColor := nil + ]. + ]. + gcRound := 2 + ] + ] + ]. + ]. + (devColor notNil and:[devColor colorId notNil]) ifTrue:[ + imgMap at:mapIndex put:devColor. + lastOK := lastOK + 1. + ] ifFalse:[ + fit := false + ] + ]. + ]. - fit ifTrue:[ - ('Depth8Image [info]: remaining colors with error <= %1' bindWith:error) infoPrintCR. - ]. + fit ifTrue:[ + ('Depth8Image [info]: remaining colors with error <= %1' bindWith:error) infoPrintCR. + ]. - error := error * 2. - error > 100 ifTrue:[ - " - break out, if the error becomes too big. - " - 'Depth8Image [info]: hard color allocation problem - revert to b&w for remaining colors' infoPrintCR. - " - map to b&w as a last fallback. - (should really do a dither here) - " - usedColors from:(lastOK+1) to:(usedColors size) do:[:aColorIndex | - |color - mapIndex "{ Class: SmallInteger }"| + error := error * 2. + error > 100 ifTrue:[ + " + break out, if the error becomes too big. + " + 'Depth8Image [info]: hard color allocation problem - revert to b&w for remaining colors' infoPrintCR. + " + map to b&w as a last fallback. + (should really do a dither here) + " + usedColors from:(lastOK+1) to:(usedColors size) do:[:aColorIndex | + |color + mapIndex "{ Class: SmallInteger }"| - mapIndex := aColorIndex + 1. - "/ color := colorMap at:mapIndex. - color := self colorFromValue:aColorIndex. - color brightness > 0.4 ifTrue:[ - color := Color white. - ] ifFalse:[ - color := Color black. - ]. - imgMap at:mapIndex put:(color onDevice:aDevice). - ]. - fit := true. - ] - ]. + mapIndex := aColorIndex + 1. + "/ color := colorMap at:mapIndex. + color := self colorFromValue:aColorIndex. + color brightness > 0.4 ifTrue:[ + color := Color white. + ] ifFalse:[ + color := Color black. + ]. + imgMap at:mapIndex put:(color onDevice:aDevice). + ]. + fit := true. + ] + ]. - error > 10 ifTrue:[ - 'Depth8Image [info]: not enough colors for a reasonable image' infoPrintCR - ] ifFalse:[ - 'Depth8Image [info]: not enough colors for exact picture' infoPrintCR. - ] + error > 10 ifTrue:[ + 'Depth8Image [info]: not enough colors for a reasonable image' infoPrintCR + ] ifFalse:[ + 'Depth8Image [info]: not enough colors for exact picture' infoPrintCR. + ] ]. " @@ -508,9 +508,9 @@ mapSize := imgMap size. map := ByteArray new:256. 1 to:mapSize do:[:i | - (clr := imgMap at:i) notNil ifTrue:[ - map at:i put:clr colorId - ] + (clr := imgMap at:i) notNil ifTrue:[ + map at:i put:clr colorId + ] ]. " @@ -518,38 +518,38 @@ " deviceDepth := aDevice depth. has8BitImage := (deviceDepth == 8) - or:[ (aDevice supportedImageFormatForDepth:8) notNil ]. + or:[ (aDevice supportedImageFormatForDepth:8) notNil ]. " finally, create a form on the device and copy (& translate) the pixel values " has8BitImage ifTrue:[ - pseudoBits := ByteArray uninitializedNew:(width * height). + pseudoBits := ByteArray uninitializedNew:(width * height). - bytes - expandPixels:8 "xlate only" - width:width height:height - into:pseudoBits - mapping:map. + bytes + expandPixels:8 "xlate only" + width:width height:height + into:pseudoBits + mapping:map. - map := nil. + map := nil. - f := Form width:width height:height depth:deviceDepth onDevice:aDevice. - f isNil ifTrue:[^ nil]. - f colorMap:imgMap. - f initGC. - aDevice - drawBits:pseudoBits - bitsPerPixel:8 - depth:deviceDepth - padding:8 - width:width height:height - x:0 y:0 - into:(f id) x:0 y:0 - width:width height:height - with:(f gcId). - ^ f + f := Form width:width height:height depth:deviceDepth onDevice:aDevice. + f isNil ifTrue:[^ nil]. + f colorMap:imgMap. + f initGC. + aDevice + drawBits:pseudoBits + bitsPerPixel:8 + depth:deviceDepth + padding:8 + width:width height:height + x:0 y:0 + into:(f id) x:0 y:0 + width:width height:height + with:(f gcId). + ^ f ]. " @@ -562,14 +562,14 @@ newImage bits:(ByteArray uninitializedNew:(height * newImage bytesPerRow)). 0 to:height-1 do:[:row | - pixelRow := self rowAt:row. - pixelRow - expandPixels:8 "xlate only" - width:width - height:1 - into:pixelRow - mapping:map. - newImage rowAt:row putAll:pixelRow + 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 onDevice:aDevice. @@ -578,14 +578,14 @@ f initGC. aDevice - drawBits:(newImage bits) - depth:deviceDepth - padding:8 - width:width height:height - x:0 y:0 - into:(f id) x:0 y:0 - width:width height:height - with:(f gcId). + drawBits:(newImage bits) + depth:deviceDepth + padding:8 + width:width height:height + x:0 y:0 + into:(f id) x:0 y:0 + width:width height:height + with:(f gcId). ^ f @@ -611,7 +611,7 @@ bestFormat := self bestSupportedImageFormatFor:aDevice. usedDeviceDepth := bestFormat at:#depth. usedDeviceDepth == 1 ifTrue:[ - ^ self asMonochromeFormOn:aDevice + ^ self asMonochromeFormOn:aDevice ]. usedDeviceBitsPerPixel := bestFormat at:#bitsPerPixel. usedDevicePadding := bestFormat at:#padding. @@ -623,330 +623,330 @@ "/ for now, only support some depths usedDeviceBitsPerPixel == 16 ifTrue:[ - "/ 16 bits/pixel + "/ 16 bits/pixel - "/ now, walk over the image and replace - "/ colorMap indices by color values in the bits array + "/ now, walk over the image and replace + "/ colorMap indices by color values in the bits array %{ - unsigned char *srcPtr = 0; - unsigned char *dstPtr = 0; - OBJ _bytes = __INST(bytes); + unsigned char *srcPtr = 0; + unsigned char *dstPtr = 0; + OBJ _bytes = __INST(bytes); - if (__isByteArrayLike(_bytes)) { - srcPtr = _ByteArrayInstPtr(_bytes)->ba_element; - } else { - if (__isExternalBytesLike(_bytes)) { - srcPtr = __externalBytesAddress(_bytes); - } - } - if (__isByteArray(imageBits)) { - dstPtr = _ByteArrayInstPtr(imageBits)->ba_element; - } else { - if (__isExternalBytesLike(imageBits)) { - dstPtr = __externalBytesAddress(imageBits); - } - } + if (__isByteArrayLike(_bytes)) { + srcPtr = __ByteArrayInstPtr(_bytes)->ba_element; + } else { + if (__isExternalBytesLike(_bytes)) { + srcPtr = __externalBytesAddress(_bytes); + } + } + if (__isByteArray(imageBits)) { + dstPtr = __ByteArrayInstPtr(imageBits)->ba_element; + } else { + if (__isExternalBytesLike(imageBits)) { + dstPtr = __externalBytesAddress(imageBits); + } + } - if (__bothSmallInteger(_INST(height), _INST(width)) - && __isArrayLike(colorValues) - && srcPtr - && dstPtr) { - int r,p; - int x, y, w, h, nPix; - unsigned short pixels[256]; + if (__bothSmallInteger(__INST(height), __INST(width)) + && __isArrayLike(colorValues) + && srcPtr + && dstPtr) { + int r,p; + int x, y, w, h, nPix; + unsigned short pixels[256]; - OBJ *ap = __ArrayInstPtr(colorValues)->a_element; + OBJ *ap = __ArrayInstPtr(colorValues)->a_element; - nPix = __arraySize(colorValues); - for (p=0; p 0) { - unsigned idx, v; + while (nPix > 0) { + unsigned idx, v; - if (((unsigned INT)srcPtr & 3) == 0) { - unsigned INT idx4; + if (((unsigned INT)srcPtr & 3) == 0) { + unsigned INT idx4; - while (r > 4) { + while (r > 4) { #ifdef __MSBFIRST - idx4 = ((unsigned int *)srcPtr)[0]; - v = pixels[(idx4 >> 24) & 0xFF]; - ((short *)dstPtr)[0] = v; - v = pixels[(idx4 >> 16) & 0xFF]; - ((short *)dstPtr)[1] = v; - v = pixels[(idx4 >> 8) & 0xFF]; - ((short *)dstPtr)[2] = v; - v = pixels[idx4 & 0xFF]; - ((short *)dstPtr)[3] = v; + idx4 = ((unsigned int *)srcPtr)[0]; + v = pixels[(idx4 >> 24) & 0xFF]; + ((short *)dstPtr)[0] = v; + v = pixels[(idx4 >> 16) & 0xFF]; + ((short *)dstPtr)[1] = v; + v = pixels[(idx4 >> 8) & 0xFF]; + ((short *)dstPtr)[2] = v; + v = pixels[idx4 & 0xFF]; + ((short *)dstPtr)[3] = v; #else # ifdef __LSBFIRST - idx4 = ((unsigned int *)srcPtr)[0]; - v = pixels[idx4 & 0xFF]; - dstPtr[0] = (v>>8) & 0xFF; - dstPtr[1] = (v) & 0xFF; + idx4 = ((unsigned int *)srcPtr)[0]; + v = pixels[idx4 & 0xFF]; + dstPtr[0] = (v>>8) & 0xFF; + dstPtr[1] = (v) & 0xFF; - v = pixels[(idx4 >> 8) & 0xFF]; - dstPtr[2] = (v>>8) & 0xFF; - dstPtr[3] = (v) & 0xFF; + v = pixels[(idx4 >> 8) & 0xFF]; + dstPtr[2] = (v>>8) & 0xFF; + dstPtr[3] = (v) & 0xFF; - v = pixels[(idx4 >> 16) & 0xFF]; - dstPtr[4] = (v>>8) & 0xFF; - dstPtr[5] = (v) & 0xFF; + v = pixels[(idx4 >> 16) & 0xFF]; + dstPtr[4] = (v>>8) & 0xFF; + dstPtr[5] = (v) & 0xFF; - v = pixels[(idx4 >> 24) & 0xFF]; - dstPtr[6] = (v>>8) & 0xFF; - dstPtr[7] = (v) & 0xFF; + v = pixels[(idx4 >> 24) & 0xFF]; + dstPtr[6] = (v>>8) & 0xFF; + dstPtr[7] = (v) & 0xFF; # else /* unknown/unspecified - code below works on any machine */ - idx = srcPtr[0]; - v = pixels[idx]; - dstPtr[0] = (v>>8) & 0xFF; - dstPtr[1] = (v) & 0xFF; + idx = srcPtr[0]; + v = pixels[idx]; + dstPtr[0] = (v>>8) & 0xFF; + dstPtr[1] = (v) & 0xFF; - idx = srcPtr[1]; - v = pixels[idx]; - dstPtr[2] = (v>>8) & 0xFF; - dstPtr[3] = (v) & 0xFF; + idx = srcPtr[1]; + v = pixels[idx]; + dstPtr[2] = (v>>8) & 0xFF; + dstPtr[3] = (v) & 0xFF; - idx = srcPtr[2]; - v = pixels[idx]; - dstPtr[4] = (v>>8) & 0xFF; - dstPtr[5] = (v) & 0xFF; + idx = srcPtr[2]; + v = pixels[idx]; + dstPtr[4] = (v>>8) & 0xFF; + dstPtr[5] = (v) & 0xFF; - idx = srcPtr[3]; - v = pixels[idx]; - dstPtr[6] = (v>>8) & 0xFF; - dstPtr[7] = (v) & 0xFF; + idx = srcPtr[3]; + v = pixels[idx]; + dstPtr[6] = (v>>8) & 0xFF; + dstPtr[7] = (v) & 0xFF; # endif #endif - r -= 4; - dstPtr += 8; - nPix -= 4; - srcPtr +=4; - } - } + r -= 4; + dstPtr += 8; + nPix -= 4; + srcPtr +=4; + } + } - nPix--; + nPix--; - idx = *srcPtr++; - v = pixels[idx]; + idx = *srcPtr++; + v = pixels[idx]; #ifdef __MSBFIRST - ((short *)dstPtr)[0] = v; + ((short *)dstPtr)[0] = v; #else - dstPtr[0] = (v>>8) & 0xFF; - dstPtr[1] = (v) & 0xFF; + dstPtr[0] = (v>>8) & 0xFF; + dstPtr[1] = (v) & 0xFF; #endif - dstPtr += 2; + dstPtr += 2; - if (--r == 0) { - dstPtr += p; - r = w; - } - } - } + if (--r == 0) { + dstPtr += p; + r = w; + } + } + } %}. ] ifFalse:[ - usedDeviceBitsPerPixel == 32 ifTrue:[ - "/ 32 bits/pixel + usedDeviceBitsPerPixel == 32 ifTrue:[ + "/ 32 bits/pixel - "/ now, walk over the image and replace - "/ colorMap indices by color values in the bits array + "/ now, walk over the image and replace + "/ colorMap indices by color values in the bits array %{ - unsigned char *srcPtr = 0; - unsigned char *dstPtr = 0; - OBJ _bytes = __INST(bytes); + unsigned char *srcPtr = 0; + unsigned char *dstPtr = 0; + OBJ _bytes = __INST(bytes); - if (__isByteArrayLike(_bytes)) { - srcPtr = _ByteArrayInstPtr(_bytes)->ba_element; - } else { - if (__isExternalBytesLike(_bytes)) { - srcPtr = __externalBytesAddress(_bytes); - } - } - if (__isByteArray(imageBits)) { - dstPtr = _ByteArrayInstPtr(imageBits)->ba_element; - } else { - if (__isExternalBytesLike(imageBits)) { - dstPtr = __externalBytesAddress(imageBits); - } - } + if (__isByteArrayLike(_bytes)) { + srcPtr = __ByteArrayInstPtr(_bytes)->ba_element; + } else { + if (__isExternalBytesLike(_bytes)) { + srcPtr = __externalBytesAddress(_bytes); + } + } + if (__isByteArray(imageBits)) { + dstPtr = __ByteArrayInstPtr(imageBits)->ba_element; + } else { + if (__isExternalBytesLike(imageBits)) { + dstPtr = __externalBytesAddress(imageBits); + } + } - if (__bothSmallInteger(_INST(height), _INST(width)) - && __isArrayLike(colorValues) - && srcPtr - && dstPtr) { - int x, y, w, h, nPix; + if (__bothSmallInteger(__INST(height), __INST(width)) + && __isArrayLike(colorValues) + && srcPtr + && dstPtr) { + int x, y, w, h, nPix; - OBJ *ap = __ArrayInstPtr(colorValues)->a_element; + OBJ *ap = __ArrayInstPtr(colorValues)->a_element; - w = __intVal(_INST(width)); - h = __intVal(_INST(height)); - nPix = w * h; - while (nPix > 0) { - unsigned idx, v; - OBJ clr; + w = __intVal(__INST(width)); + h = __intVal(__INST(height)); + nPix = w * h; + while (nPix > 0) { + unsigned idx, v; + OBJ clr; - idx = *srcPtr; - clr = ap[idx]; - v = __intVal(clr); + idx = *srcPtr; + clr = ap[idx]; + v = __intVal(clr); #ifdef __MSBFIRST - ((long *)dstPtr)[0] = v; + ((long *)dstPtr)[0] = v; #else - dstPtr[0] = (v>>24) & 0xFF; - dstPtr[1] = (v>>16) & 0xFF; - dstPtr[2] = (v>>8) & 0xFF; - dstPtr[3] = (v) & 0xFF; + dstPtr[0] = (v>>24) & 0xFF; + dstPtr[1] = (v>>16) & 0xFF; + dstPtr[2] = (v>>8) & 0xFF; + dstPtr[3] = (v) & 0xFF; #endif - dstPtr += 4; - srcPtr += 1; - nPix--; - } - } + dstPtr += 4; + srcPtr += 1; + nPix--; + } + } %}. - ] ifFalse:[ - usedDeviceBitsPerPixel == 8 ifTrue:[ - "/ 8 bits/pixel + ] ifFalse:[ + usedDeviceBitsPerPixel == 8 ifTrue:[ + "/ 8 bits/pixel - "/ now, walk over the image and replace - "/ colorMap indices by color values in the bits array + "/ now, walk over the image and replace + "/ colorMap indices by color values in the bits array %{ - unsigned char *srcPtr = 0; - unsigned char *dstPtr = 0; - OBJ _bytes = __INST(bytes); + unsigned char *srcPtr = 0; + unsigned char *dstPtr = 0; + OBJ _bytes = __INST(bytes); - if (__isByteArrayLike(_bytes)) { - srcPtr = _ByteArrayInstPtr(_bytes)->ba_element; - } else { - if (__isExternalBytesLike(_bytes)) { - srcPtr = __externalBytesAddress(_bytes); - } - } - if (__isByteArray(imageBits)) { - dstPtr = _ByteArrayInstPtr(imageBits)->ba_element; - } else { - if (__isExternalBytesLike(imageBits)) { - dstPtr = __externalBytesAddress(imageBits); - } - } + if (__isByteArrayLike(_bytes)) { + srcPtr = __ByteArrayInstPtr(_bytes)->ba_element; + } else { + if (__isExternalBytesLike(_bytes)) { + srcPtr = __externalBytesAddress(_bytes); + } + } + if (__isByteArray(imageBits)) { + dstPtr = __ByteArrayInstPtr(imageBits)->ba_element; + } else { + if (__isExternalBytesLike(imageBits)) { + dstPtr = __externalBytesAddress(imageBits); + } + } - if (__bothSmallInteger(_INST(height), _INST(width)) - && __isArrayLike(colorValues) - && srcPtr - && dstPtr) { - int x, y, w, h, nPix; - int r,p; + if (__bothSmallInteger(__INST(height), __INST(width)) + && __isArrayLike(colorValues) + && srcPtr + && dstPtr) { + int x, y, w, h, nPix; + int r,p; - OBJ *ap = __ArrayInstPtr(colorValues)->a_element; + OBJ *ap = __ArrayInstPtr(colorValues)->a_element; - w = __intVal(_INST(width)); - h = __intVal(_INST(height)); - r = w; - p = __intVal(padd); + w = __intVal(__INST(width)); + h = __intVal(__INST(height)); + r = w; + p = __intVal(padd); - nPix = w * h; - while (nPix > 0) { - unsigned idx, v; - OBJ clr; + nPix = w * h; + while (nPix > 0) { + unsigned idx, v; + OBJ clr; - idx = *srcPtr; - clr = ap[idx]; - v = __intVal(clr); + idx = *srcPtr; + clr = ap[idx]; + v = __intVal(clr); - dstPtr[0] = v; + dstPtr[0] = v; - dstPtr += 1; - srcPtr += 1; - nPix--; + dstPtr += 1; + srcPtr += 1; + nPix--; - if (--r == 0) { - dstPtr += p; - r = w; - } + if (--r == 0) { + dstPtr += p; + r = w; + } - } - } + } + } %}. - ] ifFalse:[ - usedDeviceBitsPerPixel == 24 ifTrue:[ - "/ 24 bits/pixel + ] ifFalse:[ + usedDeviceBitsPerPixel == 24 ifTrue:[ + "/ 24 bits/pixel - "/ now, walk over the image and replace - "/ colorMap indices by color values in the bits array + "/ now, walk over the image and replace + "/ colorMap indices by color values in the bits array %{ - unsigned char *srcPtr = 0; - unsigned char *dstPtr = 0; - OBJ _bytes = __INST(bytes); + unsigned char *srcPtr = 0; + unsigned char *dstPtr = 0; + OBJ _bytes = __INST(bytes); - if (__isByteArrayLike(_bytes)) { - srcPtr = _ByteArrayInstPtr(_bytes)->ba_element; - } else { - if (__isExternalBytesLike(_bytes)) { - srcPtr = __externalBytesAddress(_bytes); - } - } - if (__isByteArray(imageBits)) { - dstPtr = _ByteArrayInstPtr(imageBits)->ba_element; - } else { - if (__isExternalBytesLike(imageBits)) { - dstPtr = __externalBytesAddress(imageBits); - } - } + if (__isByteArrayLike(_bytes)) { + srcPtr = __ByteArrayInstPtr(_bytes)->ba_element; + } else { + if (__isExternalBytesLike(_bytes)) { + srcPtr = __externalBytesAddress(_bytes); + } + } + if (__isByteArray(imageBits)) { + dstPtr = __ByteArrayInstPtr(imageBits)->ba_element; + } else { + if (__isExternalBytesLike(imageBits)) { + dstPtr = __externalBytesAddress(imageBits); + } + } - if (__bothSmallInteger(_INST(height), _INST(width)) - && __isArrayLike(colorValues) - && srcPtr - && dstPtr) { - int x, y, w, h, nPix; - int r,p; + if (__bothSmallInteger(__INST(height), __INST(width)) + && __isArrayLike(colorValues) + && srcPtr + && dstPtr) { + int x, y, w, h, nPix; + int r,p; - OBJ *ap = __ArrayInstPtr(colorValues)->a_element; + OBJ *ap = __ArrayInstPtr(colorValues)->a_element; - w = __intVal(_INST(width)); - h = __intVal(_INST(height)); - r = w; - p = __intVal(padd); + w = __intVal(__INST(width)); + h = __intVal(__INST(height)); + r = w; + p = __intVal(padd); - nPix = w * h; - while (nPix > 0) { - unsigned idx, v; - OBJ clr; + nPix = w * h; + while (nPix > 0) { + unsigned idx, v; + OBJ clr; - idx = *srcPtr; - clr = ap[idx]; - v = __intVal(clr); + idx = *srcPtr; + clr = ap[idx]; + v = __intVal(clr); - dstPtr[0] = (v>>16) & 0xFF; - dstPtr[1] = (v>>8) & 0xFF; - dstPtr[2] = (v) & 0xFF; + dstPtr[0] = (v>>16) & 0xFF; + dstPtr[1] = (v>>8) & 0xFF; + dstPtr[2] = (v) & 0xFF; - dstPtr += 3; - srcPtr += 1; - nPix--; - if (--r == 0) { - dstPtr += p; - r = w; - } - } - } + dstPtr += 3; + srcPtr += 1; + nPix--; + if (--r == 0) { + dstPtr += p; + r = w; + } + } + } %}. - ] - ] - ] + ] + ] + ] ]. imageBits isNil ifTrue:[ - 'Image [warning]: unimplemented trueColor depth in paletteImageAsTrueColorFormOn: ' errorPrint. usedDeviceBitsPerPixel errorPrintCR. - ^ self asMonochromeFormOn:aDevice + 'Image [warning]: unimplemented trueColor depth in paletteImageAsTrueColorFormOn: ' errorPrint. usedDeviceBitsPerPixel errorPrintCR. + ^ self asMonochromeFormOn:aDevice ]. form := Form width:width height:height depth:usedDeviceDepth onDevice:aDevice. @@ -954,13 +954,13 @@ form initGC. form - copyBitsFrom:imageBits - bitsPerPixel:usedDeviceBitsPerPixel - depth:usedDeviceDepth - padding:usedDevicePadding - width:width height:height - x:0 y:0 - toX:0 y:0. + copyBitsFrom:imageBits + bitsPerPixel:usedDeviceBitsPerPixel + depth:usedDeviceDepth + padding:usedDevicePadding + width:width height:height + x:0 y:0 + toX:0 y:0. ^ form @@ -1094,77 +1094,77 @@ rgbBytes := ByteArray uninitializedNew:256 * 3. photometric == #palette ifTrue:[ - lastColor := colorMap size - 1 + lastColor := colorMap size - 1 ] ifFalse:[ - lastColor := 255. + lastColor := 255. ]. index := 1. 0 to:lastColor do:[:pix | - clr := self colorFromValue:pix. - rgbBytes at:index put:(clr redByte). - rgbBytes at:index+1 put:(clr greenByte). - rgbBytes at:index+2 put:(clr blueByte). + clr := self colorFromValue:pix. + rgbBytes at:index put:(clr redByte). + rgbBytes at:index+1 put:(clr greenByte). + rgbBytes at:index+2 put:(clr blueByte). - index := index + 3. + index := index + 3. ]. "/ collect valid ditherColors ... aMapOrNil isNil ifTrue:[ - ditherColors := colors select:[:clr | clr notNil]. + ditherColors := colors select:[:clr | clr notNil]. ] ifFalse:[ - ditherColors := colors + ditherColors := colors ]. "/ ... and sort by manhatten distance from black qScramble := #( - "/ 2rX00X00X00X00 + "/ 2rX00X00X00X00 - 2r000000000000 "/ 0 - 2r000000000100 "/ 1 - 2r000000100000 "/ 2 - 2r000000100100 "/ 3 - 2r000100000000 "/ 4 - 2r000100000100 "/ 5 - 2r000100100000 "/ 6 - 2r000100100100 "/ 7 - 2r100000000000 "/ 8 - 2r100000000100 "/ 9 - 2r100000100000 "/ a - 2r100000100100 "/ b - 2r100100000000 "/ c - 2r100100000100 "/ d - 2r100100100000 "/ e - 2r100100100100 "/ f - ). + 2r000000000000 "/ 0 + 2r000000000100 "/ 1 + 2r000000100000 "/ 2 + 2r000000100100 "/ 3 + 2r000100000000 "/ 4 + 2r000100000100 "/ 5 + 2r000100100000 "/ 6 + 2r000100100100 "/ 7 + 2r100000000000 "/ 8 + 2r100000000100 "/ 9 + 2r100000100000 "/ a + 2r100000100100 "/ b + 2r100100000000 "/ c + 2r100100000100 "/ d + 2r100100100000 "/ e + 2r100100100100 "/ f + ). ditherColors := ditherColors sort:[:a :b | - |cr "{Class: SmallInteger }" - cg "{Class: SmallInteger }" - cb "{Class: SmallInteger }" - i1 "{Class: SmallInteger }" - i2 "{Class: SmallInteger }"| + |cr "{Class: SmallInteger }" + cg "{Class: SmallInteger }" + cb "{Class: SmallInteger }" + i1 "{Class: SmallInteger }" + i2 "{Class: SmallInteger }"| - cr := a redByte. - cg := a greenByte. - cb := a blueByte. - i1 := qScramble at:((cr bitShift:-4) bitAnd:16r0F) + 1. - i1 := i1 + ((qScramble at:((cg bitShift:-4) bitAnd:16r0F) + 1) bitShift:-1). - i1 := i1 + ((qScramble at:((cb bitShift:-4) bitAnd:16r0F) + 1) bitShift:-2). + cr := a redByte. + cg := a greenByte. + cb := a blueByte. + i1 := qScramble at:((cr bitShift:-4) bitAnd:16r0F) + 1. + i1 := i1 + ((qScramble at:((cg bitShift:-4) bitAnd:16r0F) + 1) bitShift:-1). + i1 := i1 + ((qScramble at:((cb bitShift:-4) bitAnd:16r0F) + 1) bitShift:-2). - cr := b redByte. - cg := b greenByte. - cb := b blueByte. - i2 := qScramble at:((cr bitShift:-4) bitAnd:16r0F) + 1. - i2 := i2 + ((qScramble at:((cg bitShift:-4) bitAnd:16r0F) + 1) bitShift:-1). - i2 := i2 + ((qScramble at:((cb bitShift:-4) bitAnd:16r0F) + 1) bitShift:-2). + cr := b redByte. + cg := b greenByte. + cb := b blueByte. + i2 := qScramble at:((cr bitShift:-4) bitAnd:16r0F) + 1. + i2 := i2 + ((qScramble at:((cg bitShift:-4) bitAnd:16r0F) + 1) bitShift:-1). + i2 := i2 + ((qScramble at:((cb bitShift:-4) bitAnd:16r0F) + 1) bitShift:-2). - i1 < i2 - ]. + i1 < i2 + ]. aMapOrNil isNil ifTrue:[ - ditherIds := (ditherColors asArray collect:[:clr | clr colorId]) asByteArray. + ditherIds := (ditherColors asArray collect:[:clr | clr colorId]) asByteArray. ] ifFalse:[ - ditherIds := aMapOrNil asByteArray + ditherIds := aMapOrNil asByteArray ]. "/ build an index table, for fast lookup from manhatten-r-g-b distance @@ -1173,20 +1173,20 @@ clrLookup := ByteArray new:(4096). index := 0. ditherColors keysAndValuesDo:[:clrPosition :clr | - |r g b i| + |r g b i| - r := clr redByte. - g := clr greenByte. - b := clr blueByte. - i := qScramble at:((r bitShift:-4) bitAnd:16r0F) + 1. - i := i + ((qScramble at:((g bitShift:-4) bitAnd:16r0F) + 1) bitShift:-1). - i := i + ((qScramble at:((b bitShift:-4) bitAnd:16r0F) + 1) bitShift:-2). - lookupPos := i. + r := clr redByte. + g := clr greenByte. + b := clr blueByte. + i := qScramble at:((r bitShift:-4) bitAnd:16r0F) + 1. + i := i + ((qScramble at:((g bitShift:-4) bitAnd:16r0F) + 1) bitShift:-1). + i := i + ((qScramble at:((b bitShift:-4) bitAnd:16r0F) + 1) bitShift:-2). + lookupPos := i. - [index < lookupPos] whileTrue:[ - clrLookup at:(index+1) put:(clrPosition-1-1). - index := index + 1 - ] + [index < lookupPos] whileTrue:[ + clrLookup at:(index+1) put:(clrPosition-1-1). + index := index + 1 + ] ]. clrLookup from:index+1 to:4096 put:(ditherColors size - 1). @@ -1202,16 +1202,16 @@ ditherRGBBytes := ByteArray uninitializedNew:(lastColor * 3). index := 1. 1 to:lastColor do:[:pix | - clr := ditherColors at:pix. - ditherRGBBytes at:index put:(clr redByte). - ditherRGBBytes at:index+1 put:(clr greenByte). - ditherRGBBytes at:index+2 put:(clr blueByte). - aMapOrNil isNil ifTrue:[ - ditherIds at:pix put:clr colorId. - ] ifFalse:[ - ditherIds at:pix put:(aMapOrNil at:pix). - ]. - index := index + 3. + clr := ditherColors at:pix. + ditherRGBBytes at:index put:(clr redByte). + ditherRGBBytes at:index+1 put:(clr greenByte). + ditherRGBBytes at:index+2 put:(clr blueByte). + aMapOrNil isNil ifTrue:[ + ditherIds at:pix put:clr colorId. + ] ifFalse:[ + ditherIds at:pix put:(aMapOrNil at:pix). + ]. + index := index + 3. ]. pseudoBits := ByteArray uninitializedNew:(width * height). @@ -1241,23 +1241,23 @@ int __nColors = __intVal(lastColor); int __wR = -1, __wG, __wB; static int __qScramble[16] = { - 0x000 /* 2r000000000000 0 */, - 0x004 /* 2r000000000100 1 */, - 0x020 /* 2r000000100000 2 */, - 0x024 /* 2r000000100100 3 */, - 0x100 /* 2r000100000000 4 */, - 0x104 /* 2r000100000100 5 */, - 0x120 /* 2r000100100000 6 */, - 0x124 /* 2r000100100100 7 */, - 0x800 /* 2r100000000000 8 */, - 0x804 /* 2r100000000100 9 */, - 0x820 /* 2r100000100000 a */, - 0x824 /* 2r100000100100 b */, - 0x900 /* 2r100100000000 c */, - 0x904 /* 2r100100000100 d */, - 0x920 /* 2r100100100000 e */, - 0x924 /* 2r100100100100 f */, - }; + 0x000 /* 2r000000000000 0 */, + 0x004 /* 2r000000000100 1 */, + 0x020 /* 2r000000100000 2 */, + 0x024 /* 2r000000100100 3 */, + 0x100 /* 2r000100000000 4 */, + 0x104 /* 2r000100000100 5 */, + 0x120 /* 2r000100100000 6 */, + 0x124 /* 2r000100100100 7 */, + 0x800 /* 2r100000000000 8 */, + 0x804 /* 2r100000000100 9 */, + 0x820 /* 2r100000100000 a */, + 0x824 /* 2r100000100100 b */, + 0x900 /* 2r100100000000 c */, + 0x904 /* 2r100100000100 d */, + 0x920 /* 2r100100100000 e */, + 0x924 /* 2r100100100100 f */, + }; if (__isByteArrayLike(__INST(bytes)) && __isByteArray(pseudoBits) @@ -1266,50 +1266,50 @@ && __isByteArray(ditherIds) && __isByteArray(clrLookup) && __isByteArray(error)) { - failed = false; + failed = false; - srcP = __ByteArrayInstPtr(_INST(bytes))->ba_element; - dstP = __ByteArrayInstPtr(pseudoBits)->ba_element; - rgbP = __ByteArrayInstPtr(rgbBytes)->ba_element; - idP = __ByteArrayInstPtr(ditherIds)->ba_element; - __clrLookup = __ByteArrayInstPtr(clrLookup)->ba_element; - errP = (short *) __ByteArrayInstPtr(error)->ba_element; + srcP = __ByteArrayInstPtr(__INST(bytes))->ba_element; + dstP = __ByteArrayInstPtr(pseudoBits)->ba_element; + rgbP = __ByteArrayInstPtr(rgbBytes)->ba_element; + idP = __ByteArrayInstPtr(ditherIds)->ba_element; + __clrLookup = __ByteArrayInstPtr(clrLookup)->ba_element; + errP = (short *) __ByteArrayInstPtr(error)->ba_element; - /* - * clear error accumulator - */ - eP = errP; - bzero(eP, (__w+2) * 2 * 3); + /* + * clear error accumulator + */ + eP = errP; + bzero(eP, (__w+2) * 2 * 3); - for (__y=__h; __y>0; __y--) { - __eR = __eG = __eB = 0; + for (__y=__h; __y>0; __y--) { + __eR = __eG = __eB = 0; - eP = &(errP[3]); - __eR = eP[0]; - __eG = eP[1]; - __eB = eP[2]; + eP = &(errP[3]); + __eR = eP[0]; + __eG = eP[1]; + __eB = eP[2]; - for (__x=__w; __x>0; __x--) { - int __want; - int pix; - int __wantR, __wantG, __wantB; - int idx; - int tR, tG, tB; - int nR, nG, nB; - int dR, dG, dB; - int minDelta, bestIdx; - int cnt; + for (__x=__w; __x>0; __x--) { + int __want; + int pix; + int __wantR, __wantG, __wantB; + int idx; + int tR, tG, tB; + int nR, nG, nB; + int dR, dG, dB; + int minDelta, bestIdx; + int cnt; - pix = *srcP++; + pix = *srcP++; - /* - * wR, wG and wB is the wanted r/g/b value; - */ - idx = pix+pix+pix; /* pix * 3 */ + /* + * wR, wG and wB is the wanted r/g/b value; + */ + idx = pix+pix+pix; /* pix * 3 */ - __wantR = rgbP[idx] + __eR; - __wantG = rgbP[idx+1] + __eG; - __wantB = rgbP[idx+2] + __eB; + __wantR = rgbP[idx] + __eR; + __wantG = rgbP[idx+1] + __eG; + __wantB = rgbP[idx+2] + __eB; #define RED_SCALE 30 #define GREEN_SCALE 59 @@ -1326,236 +1326,236 @@ #define NPROBE 8 #ifndef FAST_LOOKUP - if ((__wantR == __wR) - && (__wantG == __wG) - && (__wantB == __wB)) { - /* - * same color again - reuse last bestMatch - */ - } else + if ((__wantR == __wR) + && (__wantG == __wG) + && (__wantB == __wB)) { + /* + * same color again - reuse last bestMatch + */ + } else #endif - { - __wR = __wantR; - __wG = __wantG; - __wB = __wantB; + { + __wR = __wantR; + __wG = __wantG; + __wB = __wantB; #ifdef FAST_LOOKUP - if(__wR > 255) __wR = 255; - else if (__wR < 0) __wR = 0; - if(__wG > 255) __wG = 255; - else if (__wG < 0) __wG = 0; - if(__wB > 255) __wB = 255; - else if (__wB < 0) __wB = 0; + if(__wR > 255) __wR = 255; + else if (__wR < 0) __wR = 0; + if(__wG > 255) __wG = 255; + else if (__wG < 0) __wG = 0; + if(__wB > 255) __wB = 255; + else if (__wB < 0) __wB = 0; - { - int lookupIndex; - int idx, idx0; - int d, delta; - unsigned char *dp0; + { + int lookupIndex; + int idx, idx0; + int d, delta; + unsigned char *dp0; - dp = __ByteArrayInstPtr(ditherRGBBytes)->ba_element; - lookupIndex = __qScramble[((__wR & 0xF0)>>4)]; - lookupIndex |= __qScramble[((__wG & 0xF0)>>4)] >> 1; - lookupIndex |= __qScramble[((__wB & 0xF0)>>4)] >> 2; - idx = bestIdx =__clrLookup[lookupIndex]; - dp += (idx+idx+idx); + dp = __ByteArrayInstPtr(ditherRGBBytes)->ba_element; + lookupIndex = __qScramble[((__wR & 0xF0)>>4)]; + lookupIndex |= __qScramble[((__wG & 0xF0)>>4)] >> 1; + lookupIndex |= __qScramble[((__wB & 0xF0)>>4)] >> 2; + idx = bestIdx =__clrLookup[lookupIndex]; + dp += (idx+idx+idx); - /* try color at lookupIndex */ + /* try color at lookupIndex */ - d = dp[0]; - delta = (__wR - d) * RED_SCALE; - if (delta < 0) delta = -delta; + d = dp[0]; + delta = (__wR - d) * RED_SCALE; + if (delta < 0) delta = -delta; - d = dp[1]; - if (__wG > d) - delta += (__wG - d) * GREEN_SCALE; - else - delta += (d - __wG) * GREEN_SCALE; - d = dp[2]; - if (__wB > d) - delta += (__wB - d) * BLUE_SCALE; - else - delta += (d - __wB) * BLUE_SCALE; + d = dp[1]; + if (__wG > d) + delta += (__wG - d) * GREEN_SCALE; + else + delta += (d - __wG) * GREEN_SCALE; + d = dp[2]; + if (__wB > d) + delta += (__wB - d) * BLUE_SCALE; + else + delta += (d - __wB) * BLUE_SCALE; - if (delta <= GOOD_DELTA) { - goto foundBest; - } - minDelta = delta; + if (delta <= GOOD_DELTA) { + goto foundBest; + } + minDelta = delta; # ifndef ONE_SHOT - idx0 = idx; dp0 = dp; - cnt = 0; - while ((++cnt <= NPROBE) && (idx > 0)) { - /* try previous color(s) */ + idx0 = idx; dp0 = dp; + cnt = 0; + while ((++cnt <= NPROBE) && (idx > 0)) { + /* try previous color(s) */ - idx--; dp -= 3; - d = dp[0]; - delta = (__wR - d) * RED_SCALE; - if (delta < 0) delta = -delta; - d = dp[1]; - if (__wG > d) - delta += (__wG - d) * GREEN_SCALE; - else - delta += (d - __wG) * GREEN_SCALE; - d = dp[2]; - if (__wB > d) - delta += (__wB - d) * BLUE_SCALE; - else - delta += (d - __wB) * BLUE_SCALE; + idx--; dp -= 3; + d = dp[0]; + delta = (__wR - d) * RED_SCALE; + if (delta < 0) delta = -delta; + d = dp[1]; + if (__wG > d) + delta += (__wG - d) * GREEN_SCALE; + else + delta += (d - __wG) * GREEN_SCALE; + d = dp[2]; + if (__wB > d) + delta += (__wB - d) * BLUE_SCALE; + else + delta += (d - __wB) * BLUE_SCALE; - if (delta < minDelta) { - bestIdx = idx; - if (delta <= GOOD_DELTA) { - goto foundBest; - } - minDelta = delta; - } - } + if (delta < minDelta) { + bestIdx = idx; + if (delta <= GOOD_DELTA) { + goto foundBest; + } + minDelta = delta; + } + } - idx = idx0; dp = dp0; - cnt = 0; - while ((++cnt <= NPROBE) && (++idx < __nColors)) { - /* try next color */ + idx = idx0; dp = dp0; + cnt = 0; + while ((++cnt <= NPROBE) && (++idx < __nColors)) { + /* try next color */ - dp += 3; - d = dp[0]; - delta = (__wR - d) * RED_SCALE; - if (delta < 0) delta = -delta; - d = dp[1]; - if (__wG > d) - delta += (__wG - d) * GREEN_SCALE; - else - delta += (d - __wG) * GREEN_SCALE; - d = dp[2]; - if (__wB > d) - delta += (__wB - d) * BLUE_SCALE; - else - delta += (d - __wB) * BLUE_SCALE; + dp += 3; + d = dp[0]; + delta = (__wR - d) * RED_SCALE; + if (delta < 0) delta = -delta; + d = dp[1]; + if (__wG > d) + delta += (__wG - d) * GREEN_SCALE; + else + delta += (d - __wG) * GREEN_SCALE; + d = dp[2]; + if (__wB > d) + delta += (__wB - d) * BLUE_SCALE; + else + delta += (d - __wB) * BLUE_SCALE; - if (delta < minDelta) { - bestIdx = idx; - if (delta <= GOOD_DELTA) { - goto foundBest; - } - minDelta = delta; - } - } + if (delta < minDelta) { + bestIdx = idx; + if (delta <= GOOD_DELTA) { + goto foundBest; + } + minDelta = delta; + } + } # endif - } - foundBest: ; + } + foundBest: ; #else /* - if(__wR > 255) __wR = 255; - else if (__wR < 0) __wR = 0; - if(__wG > 255) __wG = 255; - else if (__wG < 0) __wG = 0; - if(__wB > 255) __wB = 255; - else if (__wB < 0) __wB = 0; + if(__wR > 255) __wR = 255; + else if (__wR < 0) __wR = 0; + if(__wG > 255) __wG = 255; + else if (__wG < 0) __wG = 0; + if(__wB > 255) __wB = 255; + else if (__wB < 0) __wB = 0; */ - /* find the best matching color */ + /* find the best matching color */ - minDelta = 99999; - bestIdx = -1; - dp = __ByteArrayInstPtr(ditherRGBBytes)->ba_element; - for (idx = 0; idx<__nColors; idx++) { - int d, delta; + minDelta = 99999; + bestIdx = -1; + dp = __ByteArrayInstPtr(ditherRGBBytes)->ba_element; + for (idx = 0; idx<__nColors; idx++) { + int d, delta; - d = dp[0]; - delta = (__wR - d) * RED_SCALE; - if (delta < 0) delta = -delta; - if (delta < minDelta) { - d = dp[1]; - if (__wG > d) - delta += (__wG - d) * GREEN_SCALE; - else - delta += (d - __wG) * GREEN_SCALE; - if (delta < minDelta) { - d = dp[2]; - if (__wB > d) - delta += (__wB - d) * BLUE_SCALE; - else - delta += (d - __wB) * BLUE_SCALE; + d = dp[0]; + delta = (__wR - d) * RED_SCALE; + if (delta < 0) delta = -delta; + if (delta < minDelta) { + d = dp[1]; + if (__wG > d) + delta += (__wG - d) * GREEN_SCALE; + else + delta += (d - __wG) * GREEN_SCALE; + if (delta < minDelta) { + d = dp[2]; + if (__wB > d) + delta += (__wB - d) * BLUE_SCALE; + else + delta += (d - __wB) * BLUE_SCALE; - if (delta < minDelta) { - bestIdx = idx; - if (delta <= GOOD_DELTA) { - break; - } - minDelta = delta; - } - } - } - dp += 3; - } + if (delta < minDelta) { + bestIdx = idx; + if (delta <= GOOD_DELTA) { + break; + } + minDelta = delta; + } + } + } + dp += 3; + } #endif - } - dp = __ByteArrayInstPtr(ditherRGBBytes)->ba_element; - dp += bestIdx * 3; - dR = dp[0]; - dG = dp[1]; - dB = dp[2]; + } + dp = __ByteArrayInstPtr(ditherRGBBytes)->ba_element; + dp += bestIdx * 3; + dR = dp[0]; + dG = dp[1]; + dB = dp[2]; /* console_fprintf(stderr, "want: %d/%d/%d (%d/%d/%d) got: %d/%d/%d\n", - __wantR, __wantG, __wantB, - __wR, __wG, __wB, - dR, dG, dB); + __wantR, __wantG, __wantB, + __wR, __wG, __wB, + dR, dG, dB); */ - /* - * store the corresponding dither colors colorId - */ - *dstP++ = idP[bestIdx]; + /* + * store the corresponding dither colors colorId + */ + *dstP++ = idP[bestIdx]; - /* - * the new error & distribute the error - */ - __eR = __wantR - dR; - if (__eR) { - tR = __eR >> 4; /* 16th of error */ - nR = eP[3] + (tR * 7);/* from accu: error for (x+1 / y) */ - eP[0] = tR*5; /* 5/16th for (x / y+1) */ - eP[-3] = tR*3; /* 3/16th for (x-1 / y+1) */ - eP[3] = __eR - (tR*15); /* 1/16th for (x+1 / y+1) */ - __eR = nR; - } else { - __eR = eP[3]; - eP[0] = eP[-3] = eP[3] = 0; - } + /* + * the new error & distribute the error + */ + __eR = __wantR - dR; + if (__eR) { + tR = __eR >> 4; /* 16th of error */ + nR = eP[3] + (tR * 7);/* from accu: error for (x+1 / y) */ + eP[0] = tR*5; /* 5/16th for (x / y+1) */ + eP[-3] = tR*3; /* 3/16th for (x-1 / y+1) */ + eP[3] = __eR - (tR*15); /* 1/16th for (x+1 / y+1) */ + __eR = nR; + } else { + __eR = eP[3]; + eP[0] = eP[-3] = eP[3] = 0; + } - __eG = __wantG - dG; - if (__eG) { - tG = __eG >> 4; - nG = eP[4] + (tG * 7);/* plus 7/16'th of this error */ - eP[1] = tG*5; - eP[-2] = tG*3; - eP[4] = __eG - (tG*15); - __eG = nG; - } else { - __eG = eP[4]; - eP[1] = eP[-2] = eP[4] = 0; - } + __eG = __wantG - dG; + if (__eG) { + tG = __eG >> 4; + nG = eP[4] + (tG * 7);/* plus 7/16'th of this error */ + eP[1] = tG*5; + eP[-2] = tG*3; + eP[4] = __eG - (tG*15); + __eG = nG; + } else { + __eG = eP[4]; + eP[1] = eP[-2] = eP[4] = 0; + } - __eB = __wantB - dB; - if (__eB) { - tB = __eB >> 4; - nB = eP[5] + (tB * 7); - eP[2] = tB*5; - eP[-1] = tB*3; - eP[5] = __eB - (tB*15); - __eB = nB; - } else { - __eB = eP[5]; - eP[2] = eP[-1] = eP[5] = 0; - } + __eB = __wantB - dB; + if (__eB) { + tB = __eB >> 4; + nB = eP[5] + (tB * 7); + eP[2] = tB*5; + eP[-1] = tB*3; + eP[5] = __eB - (tB*15); + __eB = nB; + } else { + __eB = eP[5]; + eP[2] = eP[-1] = eP[5] = 0; + } - eP += 3; - } - } + eP += 3; + } + } } %}. failed ifTrue:[ - self primitiveFailed. - ^ nil + self primitiveFailed. + ^ nil ]. ^ pseudoBits @@ -1898,17 +1898,17 @@ maxColor := colorArray size. y1 to:y2 do:[:y | - srcNext := srcIndex + bytesPerRow. - x1 to:x2 do:[:x | - value := bytes at:srcIndex ifAbsent:0. - srcIndex := srcIndex + 1. + srcNext := srcIndex + bytesPerRow. + x1 to:x2 do:[:x | + value := bytes at:srcIndex ifAbsent:0. + srcIndex := srcIndex + 1. - value >= maxColor ifTrue:[ - value := 0. - ]. - aBlock value:x value:y value:(colorArray at:value+1) - ]. - srcIndex := srcNext. + value >= maxColor ifTrue:[ + value := 0. + ]. + aBlock value:x value:y value:(colorArray at:value+1) + ]. + srcIndex := srcNext. ]. "Created: / 7.6.1996 / 19:12:35 / cg" @@ -2037,14 +2037,14 @@ mX := mX asFloat. %{ unsigned char *__dstP = __ByteArrayInstPtr(newBytes)->ba_element; - unsigned char *__srcP = __ByteArrayInstPtr(_INST(bytes))->ba_element; + unsigned char *__srcP = __ByteArrayInstPtr(__INST(bytes))->ba_element; unsigned char *__srcRowP; - int __width = __intVal(_INST(width)); + int __width = __intVal(__INST(width)); int __w = __intVal(newWidth) - 1; int __h = __intVal(newHeight) - 1; int __row, __col; - double __mX = _floatVal(mX); - double __mY = _floatVal(mY); + double __mX = __floatVal(mX); + double __mY = __floatVal(mY); for (__row = 0; __row <= __h; __row++) { __srcRowP = __srcP + (__width * (int)((double)__row / __mY)); @@ -2074,7 +2074,7 @@ ! 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. Specially tuned for factors 2,3 and 4." @@ -2086,95 +2086,95 @@ int _mag; REGISTER int i; int _pixels; - OBJ w = _INST(width); + OBJ w = __INST(width); if (__bothSmallInteger(srcStart, dstStart) && __bothSmallInteger(w, mX) && __isByteArrayLike(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); + _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; + switch (_mag) { + case 1: + break; - case 2: - /* special code for common case */ - if (((INT)dstP & 1) == 0) { - while (_pixels--) { - _byte = *srcP++; - _word = (_byte<<8) | _byte; - ((short *)dstP)[0] = _word; - dstP += 2; - } - } else { - while (_pixels--) { - _byte = *srcP++; - *dstP++ = _byte; - *dstP++ = _byte; - } - } - break; + case 2: + /* special code for common case */ + if (((INT)dstP & 1) == 0) { + while (_pixels--) { + _byte = *srcP++; + _word = (_byte<<8) | _byte; + ((short *)dstP)[0] = _word; + dstP += 2; + } + } else { + while (_pixels--) { + _byte = *srcP++; + *dstP++ = _byte; + *dstP++ = _byte; + } + } + break; - case 3: - /* special code for common case */ - while (_pixels--) { - _byte = *srcP++; - *dstP++ = _byte; - *dstP++ = _byte; - *dstP++ = _byte; - } - break; + case 3: + /* special code for common case */ + while (_pixels--) { + _byte = *srcP++; + *dstP++ = _byte; + *dstP++ = _byte; + *dstP++ = _byte; + } + break; - case 4: - /* special code for common case */ - if (((INT)dstP & 3) == 0) { - while (_pixels--) { - _byte = *srcP++; - _word = (_byte<<8) | _byte; - _word = (_word<<16) | _word; - ((int *)dstP)[0] = _word; - dstP += 4; - } - } else { - while (_pixels--) { - _byte = *srcP++; - *dstP++ = _byte; - *dstP++ = _byte; - *dstP++ = _byte; - *dstP++ = _byte; - } - } - break; + case 4: + /* special code for common case */ + if (((INT)dstP & 3) == 0) { + while (_pixels--) { + _byte = *srcP++; + _word = (_byte<<8) | _byte; + _word = (_word<<16) | _word; + ((int *)dstP)[0] = _word; + dstP += 4; + } + } else { + while (_pixels--) { + _byte = *srcP++; + *dstP++ = _byte; + *dstP++ = _byte; + *dstP++ = _byte; + *dstP++ = _byte; + } + } + break; - default: - if ((((INT)dstP & 1) == 0) - && ((_mag & 1) == 0)) { - while (_pixels--) { - _byte = *srcP++; - _word = (_byte<<8) | _byte; - for (i=_mag; i>0; i-=2) { - ((short *)dstP)[0] = _word; - dstP += 2; - } - } - } else { - while (_pixels--) { - _byte = *srcP++; - for (i=_mag; i>0; i--) - *dstP++ = _byte; - } - } - break; - } - RETURN (self); + default: + if ((((INT)dstP & 1) == 0) + && ((_mag & 1) == 0)) { + while (_pixels--) { + _byte = *srcP++; + _word = (_byte<<8) | _byte; + for (i=_mag; i>0; i-=2) { + ((short *)dstP)[0] = _word; + dstP += 2; + } + } + } else { + while (_pixels--) { + _byte = *srcP++; + for (i=_mag; i>0; i--) + *dstP++ = _byte; + } + } + break; + } + RETURN (self); } %}. super - magnifyRowFrom:srcBytes offset:srcStart - into:dstBytes offset:dstStart factor:mX + magnifyRowFrom:srcBytes offset:srcStart + into:dstBytes offset:dstStart factor:mX ! ! !Depth8Image methodsFor:'private'! @@ -2234,7 +2234,7 @@ int _h, _w; int _patternOffset = 0; - _srcP = __ByteArrayInstPtr(_INST(bytes))->ba_element; + _srcP = __ByteArrayInstPtr(__INST(bytes))->ba_element; _dstP = __ByteArrayInstPtr(formBytes)->ba_element; for (_h = __intVal(h); _h; _h--) { _last = -1; @@ -2337,7 +2337,7 @@ int _patternOffset = 0; int _outCount; - _srcP = __ByteArrayInstPtr(_INST(bytes))->ba_element; + _srcP = __ByteArrayInstPtr(__INST(bytes))->ba_element; _dstP = __ByteArrayInstPtr(formBytes)->ba_element; for (_h = __intVal(h); _h; _h--) { _last = -1; @@ -2452,10 +2452,10 @@ !Depth8Image class methodsFor:'documentation'! version - ^ '$Header: /cvs/stx/stx/libview/Depth8Image.st,v 1.119 2013-04-02 12:03:44 stefan Exp $' + ^ '$Header: /cvs/stx/stx/libview/Depth8Image.st,v 1.120 2013-05-21 20:50:48 cg Exp $' ! version_CVS - ^ '$Header: /cvs/stx/stx/libview/Depth8Image.st,v 1.119 2013-04-02 12:03:44 stefan Exp $' + ^ '$Header: /cvs/stx/stx/libview/Depth8Image.st,v 1.120 2013-05-21 20:50:48 cg Exp $' ! !