Move threshold logic to CFFI again

This commit is contained in:
Joshua Granick
2016-01-08 14:51:38 -08:00
parent 0bf3017ce8
commit 97fb0dcccc
4 changed files with 97 additions and 84 deletions

View File

@@ -1102,13 +1102,13 @@ class ImageDataUtil {
if (srcData == null || destData == null) return 0;
#if (false && (cpp || neko) && !disable_cffi && !macro)
if (CFFI.enabled) return lime_image_data_util_threshold (image, sourceImage, sourceRect, destPoint, operation, threshold, color, mastk, copySource, format); else
var hits = 0;
#if ((cpp || neko) && !disable_cffi && !macro)
if (CFFI.enabled) hits = lime_image_data_util_threshold (image, sourceImage, sourceRect, destPoint, _operation, (_threshold >> 16) & 0xFFFF, (_threshold) & 0xFFFF, (_color >> 16) & 0xFFFF, (_color) & 0xFFFF, (_mask >> 16) & 0xFFFF, (_mask) & 0xFFFF, copySource); else
#end
{
var hits = 0;
var srcView = new ImageDataView (sourceImage, sourceRect);
var destView = new ImageDataView (image, new Rectangle (destPoint.x, destPoint.y, srcView.width, srcView.height));
@@ -1130,7 +1130,7 @@ class ImageDataUtil {
pixelMask = srcPixel & _mask;
value = __ucompare (pixelMask, _threshold);
value = __pixelCompare (pixelMask, _threshold);
test = switch (_operation) {
@@ -1149,6 +1149,10 @@ class ImageDataUtil {
_color.writeUInt8 (destData, destPosition, destFormat, destPremultiplied);
hits++;
} else if (copySource) {
srcPixel.writeUInt8 (destData, destPosition, destFormat, destPremultiplied);
}
srcPosition += 4;
@@ -1158,16 +1162,16 @@ class ImageDataUtil {
}
if (hits > 0) {
}
image.dirty = true;
if (hits > 0) {
}
return hits;
image.dirty = true;
}
return hits;
}
@@ -1200,7 +1204,7 @@ class ImageDataUtil {
}
private static inline function __ucompare (n1:Int, n2:Int):Int {
private static inline function __pixelCompare (n1:Int, n2:Int):Int {
var tmp1:Int;
var tmp2:Int;
@@ -1273,7 +1277,7 @@ class ImageDataUtil {
@:cffi private static function lime_image_data_util_resize (image:Dynamic, buffer:Dynamic, width:Int, height:Int):Void;
@:cffi private static function lime_image_data_util_set_format (image:Dynamic, format:Int):Void;
@:cffi private static function lime_image_data_util_set_pixels (image:Dynamic, rect:Dynamic, bytes:Dynamic, format:Int):Void;
@:cffi private static function lime_image_data_util_threshold_inner_loop (image:Dynamic, sourceImage:Image, sourceRect:Dynamic, mask:Int, threshold:Int, operation:Int, color:Int, destRect:Dynamic):Int;
@:cffi private static function lime_image_data_util_threshold (image:Dynamic, sourceImage:Image, sourceRect:Dynamic, destPoint:Dynamic, operation:Int, thresholdRG:Int, thresholdBA:Int, colorRG:Int, colorBA:Int, maskRG:Int, maskBA:Int, copySource:Bool):Int;
@:cffi private static function lime_image_data_util_unmultiply_alpha (image:Dynamic):Void;
#end

View File

@@ -32,8 +32,7 @@ namespace lime {
static void Resize (Image* image, ImageBuffer* buffer, int width, int height);
static void SetFormat (Image* image, PixelFormat format);
static void SetPixels (Image* image, Rectangle* rect, Bytes* bytes, PixelFormat format);
static int ThresholdInnerLoop (Image* image, Image* sourceImage, Rectangle* sourceRect, int mask, int threshold, int operation, int color, Rectangle* destRect);
static int Ucompare (int n1, int n2);
static int Threshold (Image* image, Image* sourceImage, Rectangle* sourceRect, Vector2* destPoint, int operation, int32_t threshold, int32_t color, int32_t mask, bool copySource);
static void UnmultiplyAlpha (Image* image);

View File

@@ -769,13 +769,16 @@ namespace lime {
}
int lime_image_data_util_threshold_inner_loop (value image, value sourceImage, value sourceRect, int mask, int threshold, int operation, int color, value destRect) {
int lime_image_data_util_threshold (value image, value sourceImage, value sourceRect, value destPoint, int operation, int thresholdRG, int thresholdBA, int colorRG, int colorBA, int maskRG, int maskBA, bool copySource) {
Image _image = Image (image);
Image _sourceImage = Image (sourceImage);
Rectangle _sourceRect = Rectangle (sourceRect);
Rectangle _destRect = Rectangle (destRect);
return ImageDataUtil::ThresholdInnerLoop (&_image, &_sourceImage, &_sourceRect, mask, threshold, operation, color, &_destRect);
Vector2 _destPoint = Vector2 (destPoint);
int32_t threshold = (thresholdRG << 16) | thresholdBA;
int32_t color = (colorRG << 16) | colorBA;
int32_t mask = (maskRG << 16) | maskBA;
return ImageDataUtil::Threshold (&_image, &_sourceImage, &_sourceRect, &_destPoint, operation, threshold, color, mask, copySource);
}
@@ -1441,7 +1444,7 @@ namespace lime {
DEFINE_PRIME4v (lime_image_data_util_resize);
DEFINE_PRIME2v (lime_image_data_util_set_format);
DEFINE_PRIME4v (lime_image_data_util_set_pixels);
DEFINE_PRIME8v (lime_image_data_util_threshold_inner_loop);
DEFINE_PRIME12 (lime_image_data_util_threshold);
DEFINE_PRIME1v (lime_image_data_util_unmultiply_alpha);
DEFINE_PRIME3 (lime_image_encode);
DEFINE_PRIME1 (lime_image_load);

View File

@@ -606,70 +606,7 @@ namespace lime {
}
int ImageDataUtil::ThresholdInnerLoop (Image* image, Image* sourceImage, Rectangle* sourceRect, int mask, int threshold, int operation, int color, Rectangle* destRect) {
int startX = (int)destRect->x;
int startY = (int)destRect->y;
int destWidth = (int)destRect->width;
int destHeight = (int)destRect->height;
ImageDataView srcView = ImageDataView (sourceImage, sourceRect);
RGBA srcPixel;
int srcPosition = 0;
PixelFormat srcFormat = image->buffer->format;
bool srcPremultiplied = image->buffer->premultiplied;
uint8_t* srcData = (uint8_t*)sourceImage->buffer->data->Data ();
RGBA colorRGBA = RGBA (color);
int pixelMask = 0;
int i = 0;
bool test = false;
int hits = 0;
for(int yy = 0; yy < destHeight; yy++) {
srcPosition = srcView.Row (yy + startY);
srcPosition += (4 * startX);
for(int xx = 0; xx < destWidth; xx++) {
srcPixel.ReadUInt8 (srcData, srcPosition, srcFormat, srcPremultiplied);
pixelMask = srcPixel.Get() & mask;
i = Ucompare (pixelMask, threshold);
switch(operation) {
case 0: test = (i == 0); //EQUALS
break;
case 1: test = (i == -1); //LESS_THAN
break;
case 2: test = (i == 1); //GREATER_THAN
break;
case 3: test = (i != 0); //NOT_EQUALS
break;
case 4: test = (i == 0 || i == -1); //LESS_THAN_OR_EQUAL_TO
break;
case 5: test = (i == 0 || i == 1); //GREATER_THAN_OR_EQUAL_TO
break;
}
if (test) {
colorRGBA.WriteUInt8 (srcData, srcPosition, srcFormat, srcPremultiplied);
hits++;
}
srcPosition += 4;
}
}
return hits;
}
int ImageDataUtil::Ucompare (int n1, int n2) {
int __pixelCompare (int32_t n1, int32_t n2) {
int tmp1;
int tmp2;
@@ -724,6 +661,76 @@ namespace lime {
}
int ImageDataUtil::Threshold (Image* image, Image* sourceImage, Rectangle* sourceRect, Vector2* destPoint, int operation, int32_t threshold, int32_t color, int32_t mask, bool copySource) {
RGBA _color (color);
int hits = 0;
uint8_t* srcData = (uint8_t*)sourceImage->buffer->data->Data ();
uint8_t* destData = (uint8_t*)image->buffer->data->Data ();
ImageDataView srcView = ImageDataView (sourceImage, sourceRect);
Rectangle destRect = Rectangle (destPoint->x, destPoint->y, srcView.width, srcView.height);
ImageDataView destView = ImageDataView (image, &destRect);
PixelFormat srcFormat = sourceImage->buffer->format;
PixelFormat destFormat = image->buffer->format;
bool srcPremultiplied = sourceImage->buffer->premultiplied;
bool destPremultiplied = image->buffer->premultiplied;
int srcPosition, destPosition, value;
RGBA srcPixel, destPixel;
int32_t pixelMask;
bool test;
for (int y = 0; y < destView.height; y++) {
srcPosition = srcView.Row (y);
destPosition = destView.Row (y);
for (int x = 0; x < destView.width; x++) {
srcPixel.ReadUInt8 (srcData, srcPosition, srcFormat, srcPremultiplied);
pixelMask = srcPixel.Get () & mask;
value = __pixelCompare (pixelMask, threshold);
switch (operation) {
case 0: test = (value != 0); break;
case 1: test = (value == 0); break;
case 2: test = (value == -1); break;
case 3: test = (value == 0 || value == -1); break;
case 4: test = (value == 1); break;
case 5: test = (value == 0 || value == 1); break;
}
if (test) {
_color.WriteUInt8 (destData, destPosition, destFormat, destPremultiplied);
hits++;
} else if (copySource) {
srcPixel.WriteUInt8 (destData, destPosition, destFormat, destPremultiplied);
}
srcPosition += 4;
destPosition += 4;
}
}
return hits;
}
void ImageDataUtil::UnmultiplyAlpha (Image* image) {
PixelFormat format = image->buffer->format;