Add CFFI setPixels

This commit is contained in:
Joshua Granick
2015-04-14 16:44:26 -07:00
parent ed2ccc89db
commit d88a14a2a0
4 changed files with 131 additions and 44 deletions

View File

@@ -825,6 +825,13 @@ class ImageDataUtil {
public static function setPixels (image:Image, rect:Rectangle, byteArray:ByteArray, format:PixelFormat):Void {
if (image.buffer.data == null) return;
#if ((cpp || neko) && !disable_cffi)
if (!System.disableCFFI) lime_image_data_util_set_pixels (image, rect, byteArray, format == ARGB ? 1 : 0); else
#end
{
var len = Math.round (rect.width * rect.height);
//#if (!js && !flash)
@@ -885,6 +892,8 @@ class ImageDataUtil {
}
}
image.dirty = true;
}
@@ -946,6 +955,7 @@ class ImageDataUtil {
private static var lime_image_data_util_merge = System.load ("lime", "lime_image_data_util_merge", -1);
private static var lime_image_data_util_multiply_alpha = System.load ("lime", "lime_image_data_util_multiply_alpha", 1);
private static var lime_image_data_util_resize = System.load ("lime", "lime_image_data_util_resize", 4);
private static var lime_image_data_util_set_pixels = System.load ("lime", "lime_image_data_util_set_pixels", 4);
private static var lime_image_data_util_unmultiply_alpha = System.load ("lime", "lime_image_data_util_unmultiply_alpha", 1);
#end

View File

@@ -29,6 +29,7 @@ namespace lime {
static void Merge (Image* image, Image* sourceImage, Rectangle* sourceRect, Vector2* destPoint, int redMultiplier, int greenMultiplier, int blueMultiplier, int alphaMultiplier);
static void MultiplyAlpha (Image* image);
static void Resize (Image* image, ImageBuffer* buffer, int width, int height);
static void SetPixels (Image* image, Rectangle* rect, ByteArray* bytes, PixelFormat format);
static void UnmultiplyAlpha (Image* image);

View File

@@ -549,6 +549,18 @@ namespace lime {
}
value lime_image_data_util_set_pixels (value image, value rect, value bytes, value format) {
Image _image = Image (image);
Rectangle _rect = Rectangle (rect);
ByteArray _bytes = ByteArray (bytes);
PixelFormat _format = (PixelFormat)val_int (format);
ImageDataUtil::SetPixels (&_image, &_rect, &_bytes, _format);
return alloc_null ();
}
value lime_image_data_util_unmultiply_alpha (value image) {
Image _image = Image (image);
@@ -958,6 +970,7 @@ namespace lime {
DEFINE_PRIM_MULT (lime_image_data_util_merge);
DEFINE_PRIM (lime_image_data_util_multiply_alpha, 1);
DEFINE_PRIM (lime_image_data_util_resize, 4);
DEFINE_PRIM (lime_image_data_util_set_pixels, 4);
DEFINE_PRIM (lime_image_data_util_unmultiply_alpha, 1);
DEFINE_PRIM (lime_image_encode, 3);
DEFINE_PRIM (lime_image_load, 1);

View File

@@ -466,6 +466,69 @@ namespace lime {
}
void ImageDataUtil::SetPixels (Image* image, Rectangle* rect, ByteArray* bytes, PixelFormat format) {
int len = int (rect->width * rect->height);
if (format == RGBA && rect->width == image->buffer->width && rect->height == image->buffer->height && rect->x == 0 && rect->y == 0) {
memcpy (image->buffer->data->Bytes (), bytes->Bytes (), bytes->Size ());
return;
}
uint8_t* data = (uint8_t*)image->buffer->data->Bytes ();
int* byteArray = (int*)bytes->Bytes ();
int offset = int (image->buffer->width * (rect->y + image->offsetX) + (rect->x + image->offsetY));
int pos = offset * 4;
int boundR = int ((rect->x + rect->width + image->offsetX));
int width = image->buffer->width;
int color;
if (format == ARGB) {
for (int i = 0; i < len; i++) {
if (((pos) % (width * 4)) >= boundR * 4) {
pos += (width - boundR) * 4;
}
color = byteArray[i];
data[pos++] = (color >> 16) & 0xFF;
data[pos++] = (color >> 8) & 0xFF;
data[pos++] = color & 0xFF;
data[pos++] = (color >> 24) & 0xFF;
}
} else {
for (int i = 0; i < len; i++) {
if (((pos) % (width * 4)) >= boundR * 4) {
pos += (width - boundR) * 4;
}
color = byteArray[i];
data[pos++] = (color >> 24) & 0xFF;
data[pos++] = (color >> 16) & 0xFF;
data[pos++] = (color >> 8) & 0xFF;
data[pos++] = color & 0xFF;
}
}
}
void ImageDataUtil::UnmultiplyAlpha (Image* image) {
int length = image->buffer->data->Size () / 4;