Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/lime/_internal/graphics/ImageDataUtil.hx
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,23 @@ class ImageDataUtil

StackBlur.blur(image, sourceImage, sourceRect, destPoint, blurX, blurY, quality);

if (strength != 1)
{
// as in Flash, the blurred alpha is multiplied by the strength, over the pixels StackBlur wrote
var data = image.data;
var alphaOffset = image.buffer.format == ARGB32 ? 0 : 3;
var end = Std.int(Math.min(Std.int(sourceRect.width) * Std.int(sourceRect.height) * 4, data.length));
var i = alphaOffset;
var alpha:Float;

while (i < end)
{
alpha = data[i] * strength;
data[i] = alpha >= 0xFF ? 0xFF : (alpha <= 0 ? 0 : Std.int(alpha));
i += 4;
}
}

image.dirty = true;
image.version++;

Expand Down
1 change: 1 addition & 0 deletions tests/unit/src/TestMain.hx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class TestMain extends Application
runner.addCase(new app.ApplicationTest());
runner.addCase(new app.EventTest());
runner.addCase(new app.FutureTest());
runner.addCase(new graphics.ImageDataUtilTest());
runner.addCase(new graphics.OpenGLTest());
runner.addCase(new media.AudioBufferTest());
runner.addCase(new media.AudioInputTest());
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/src/graphics/ImageDataUtilTest.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package graphics;

import lime._internal.graphics.ImageDataUtil;
import lime.graphics.Image;
import lime.math.Vector2;
import utest.Assert;
import utest.Test;

class ImageDataUtilTest extends Test
{
public function new()
{
super();
}

// on Flash, the pixels of an Image are in a flash.display.BitmapData, not in the data this blur reads
#if !flash
public function testGaussianBlurMultipliesTheBlurredAlphaByTheStrength()
{
var plain = blurredDot(1);
var strong = blurredDot(4);

var edge = plain.getPixel32(5, 4) & 0xFF;
Assert.isTrue(edge > 0 && edge < 0x40, "the blur did not spread the dot: " + edge);
Assert.equals(edge * 4, strong.getPixel32(5, 4) & 0xFF);

var far = plain.getPixel32(6, 4) & 0xFF;
Assert.equals(Std.int(Math.min(0xFF, far * 4)), strong.getPixel32(6, 4) & 0xFF);

// the alpha stays within a byte
Assert.equals(0xFF, blurredDot(255).getPixel32(4, 4) & 0xFF);
}

private static function blurredDot(strength:Float):Image
{
var source = new Image(null, 0, 0, 9, 9, 0x00000000);
source.setPixel32(4, 4, 0xFFFFFFFF);
var image = new Image(null, 0, 0, 9, 9, 0x00000000);
ImageDataUtil.gaussianBlur(image, source, source.rect, new Vector2(), 4, 4, 1, strength);
return image;
}
#end
}
Loading