Speed up some operations
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package lime.graphics;
|
||||
|
||||
|
||||
import haxe.io.Bytes;
|
||||
import lime.utils.ByteArray;
|
||||
import lime.utils.UInt8Array;
|
||||
|
||||
#if (js && html5)
|
||||
@@ -83,7 +85,7 @@ class ImageBuffer {
|
||||
buffer.__srcImage = __srcImage;
|
||||
|
||||
}
|
||||
#else
|
||||
#elseif nodejs
|
||||
if (data != null) {
|
||||
|
||||
buffer.data = new UInt8Array (data.byteLength);
|
||||
@@ -92,6 +94,15 @@ class ImageBuffer {
|
||||
|
||||
}
|
||||
buffer.__srcCustom = __srcCustom;
|
||||
#else
|
||||
if (data != null) {
|
||||
|
||||
var bytes = Bytes.alloc (data.byteLength);
|
||||
bytes.blit (0, buffer.data.buffer, 0, data.byteLength);
|
||||
var byteArray = ByteArray.fromBytes (bytes);
|
||||
buffer.data = new UInt8Array (byteArray);
|
||||
|
||||
}
|
||||
#end
|
||||
|
||||
buffer.premultiplied = premultiplied;
|
||||
|
||||
@@ -7,6 +7,7 @@ import lime.graphics.ImageBuffer;
|
||||
import lime.math.ColorMatrix;
|
||||
import lime.math.Rectangle;
|
||||
import lime.math.Vector2;
|
||||
import lime.system.System;
|
||||
import lime.utils.ByteArray;
|
||||
import lime.utils.UInt8Array;
|
||||
|
||||
@@ -501,6 +502,10 @@ class ImageDataUtil {
|
||||
var data = image.buffer.data;
|
||||
if (data == null) return;
|
||||
|
||||
#if ((cpp || neko) && !disable_cffi)
|
||||
lime_image_data_util_multiply_alpha (data.buffer);
|
||||
#else
|
||||
|
||||
var index, a16;
|
||||
var length = Std.int (data.length / 4);
|
||||
|
||||
@@ -515,6 +520,8 @@ class ImageDataUtil {
|
||||
|
||||
}
|
||||
|
||||
#end
|
||||
|
||||
image.buffer.premultiplied = true;
|
||||
image.dirty = true;
|
||||
|
||||
@@ -731,4 +738,16 @@ class ImageDataUtil {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Native Methods
|
||||
|
||||
|
||||
|
||||
|
||||
#if (cpp || neko || nodejs)
|
||||
private static var lime_image_data_util_multiply_alpha = System.load ("lime", "lime_image_data_util_multiply_alpha", 1);
|
||||
#end
|
||||
|
||||
|
||||
}
|
||||
@@ -164,6 +164,7 @@
|
||||
<file name="src/app/UpdateEvent.cpp" />
|
||||
<file name="src/audio/format/WAV.cpp" />
|
||||
<file name="src/audio/AudioBuffer.cpp" />
|
||||
<file name="src/graphics/utils/ImageDataUtil.cpp" />
|
||||
<file name="src/graphics/ImageBuffer.cpp" />
|
||||
<file name="src/graphics/RenderEvent.cpp" />
|
||||
<file name="src/ui/GamepadEvent.cpp" />
|
||||
|
||||
27
project/include/graphics/utils/ImageDataUtil.h
Normal file
27
project/include/graphics/utils/ImageDataUtil.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef LIME_GRAPHICS_UTILS_IMAGE_DATA_UTIL_H
|
||||
#define LIME_GRAPHICS_UTILS_IMAGE_DATA_UTIL_H
|
||||
|
||||
|
||||
#include <hx/CFFI.h>
|
||||
#include <utils/ByteArray.h>
|
||||
|
||||
|
||||
namespace lime {
|
||||
|
||||
|
||||
class ImageDataUtil {
|
||||
|
||||
|
||||
public:
|
||||
|
||||
static void MultiplyAlpha (ByteArray *bytes);
|
||||
static void UnmultiplyAlpha (ByteArray *bytes);
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
@@ -15,6 +15,7 @@
|
||||
#include <audio/AudioBuffer.h>
|
||||
#include <graphics/format/JPEG.h>
|
||||
#include <graphics/format/PNG.h>
|
||||
#include <graphics/utils/ImageDataUtil.h>
|
||||
#include <graphics/ImageBuffer.h>
|
||||
#include <graphics/Renderer.h>
|
||||
#include <graphics/RenderEvent.h>
|
||||
@@ -368,6 +369,15 @@ namespace lime {
|
||||
}
|
||||
|
||||
|
||||
value lime_image_data_util_multiply_alpha (value data) {
|
||||
|
||||
ByteArray bytes = ByteArray (data);
|
||||
ImageDataUtil::MultiplyAlpha (&bytes);
|
||||
return alloc_null ();
|
||||
|
||||
}
|
||||
|
||||
|
||||
value lime_image_encode (value buffer, value type, value quality) {
|
||||
|
||||
ImageBuffer imageBuffer = ImageBuffer (buffer);
|
||||
@@ -837,6 +847,7 @@ namespace lime {
|
||||
DEFINE_PRIM (lime_gamepad_event_manager_register, 2);
|
||||
DEFINE_PRIM (lime_gamepad_get_device_guid, 1);
|
||||
DEFINE_PRIM (lime_gamepad_get_device_name, 1);
|
||||
DEFINE_PRIM (lime_image_data_util_multiply_alpha, 1);
|
||||
DEFINE_PRIM (lime_image_encode, 3);
|
||||
DEFINE_PRIM (lime_image_load, 1);
|
||||
DEFINE_PRIM (lime_jni_getenv, 0);
|
||||
|
||||
50
project/src/graphics/utils/ImageDataUtil.cpp
Normal file
50
project/src/graphics/utils/ImageDataUtil.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
#include <graphics/utils/ImageDataUtil.h>
|
||||
#include <system/System.h>
|
||||
|
||||
|
||||
namespace lime {
|
||||
|
||||
|
||||
static long int __alpha16[256];
|
||||
|
||||
int initValues () {
|
||||
|
||||
for (int i = 0; i < 256; i++) {
|
||||
|
||||
__alpha16[i] = i * ((1 << 16) / 255);
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
static int initValues_ = initValues ();
|
||||
|
||||
|
||||
void ImageDataUtil::MultiplyAlpha (ByteArray* bytes) {
|
||||
|
||||
int a16 = 0;
|
||||
int length = bytes->Size () / 4;
|
||||
uint8_t* data = (uint8_t*)bytes->Bytes ();
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
|
||||
a16 = __alpha16[data[3]];
|
||||
data[0] = (data[0] * a16) >> 16;
|
||||
data[1] = (data[1] * a16) >> 16;
|
||||
data[2] = (data[2] * a16) >> 16;
|
||||
data += 4;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void ImageDataUtil::UnmultiplyAlpha (ByteArray* bytes) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user