Some work on pixel formats

This commit is contained in:
Joshua Granick
2015-05-04 15:54:50 -07:00
parent ddd77134d2
commit 2b75564951
13 changed files with 270 additions and 8 deletions

View File

@@ -3,6 +3,7 @@
#include <hx/CFFI.h>
#include <graphics/PixelFormat.h>
#include <utils/ByteArray.h>
@@ -24,6 +25,7 @@ namespace lime {
int bpp;
ByteArray *data;
PixelFormat format;
int height;
int width;
bool transparent;

View File

@@ -8,7 +8,8 @@ namespace lime {
enum PixelFormat {
RGBA,
ARGB
ARGB,
BGRA
};

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 SetFormat (Image* image, PixelFormat format);
static void SetPixels (Image* image, Rectangle* rect, ByteArray* bytes, PixelFormat format);
static void UnmultiplyAlpha (Image* image);

View File

@@ -549,6 +549,16 @@ namespace lime {
}
value lime_image_data_util_set_format (value image, value format) {
Image _image = Image (image);
PixelFormat _format = (PixelFormat)val_int (format);
ImageDataUtil::SetFormat (&_image, _format);
return alloc_null ();
}
value lime_image_data_util_set_pixels (value image, value rect, value bytes, value format) {
Image _image = Image (image);
@@ -985,6 +995,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_format, 2);
DEFINE_PRIM (lime_image_data_util_set_pixels, 4);
DEFINE_PRIM (lime_image_data_util_unmultiply_alpha, 1);
DEFINE_PRIM (lime_image_encode, 3);

View File

@@ -8,6 +8,7 @@ namespace lime {
static int id_bpp;
static int id_buffer;
static int id_data;
static int id_format;
static int id_height;
static int id_width;
static int id_transparent;
@@ -19,6 +20,7 @@ namespace lime {
width = 0;
height = 0;
bpp = 4;
format = RGBA;
data = 0;
transparent = false;
@@ -36,6 +38,7 @@ namespace lime {
id_width = val_id ("width");
id_height = val_id ("height");
id_data = val_id ("data");
id_format = val_id ("format");
init = true;
}
@@ -43,6 +46,7 @@ namespace lime {
width = val_int (val_field (imageBuffer, id_width));
height = val_int (val_field (imageBuffer, id_height));
bpp = val_int (val_field (imageBuffer, id_bitsPerPixel));
format = (PixelFormat)val_int (val_field (imageBuffer, id_format));
transparent = val_bool (val_field (imageBuffer, id_transparent));
value data_value = val_field (imageBuffer, id_data);
value buffer_value = val_field (data_value, id_buffer);
@@ -103,6 +107,7 @@ namespace lime {
id_width = val_id ("width");
id_height = val_id ("height");
id_data = val_id ("data");
id_format = val_id ("format");
init = true;
}
@@ -113,6 +118,7 @@ namespace lime {
alloc_field (mValue, id_bpp, alloc_int (bpp));
alloc_field (mValue, id_transparent, alloc_bool (transparent));
alloc_field (mValue, id_data, data->mValue);
alloc_field (mValue, id_format, alloc_int (format));
return mValue;
}

View File

@@ -478,6 +478,90 @@ namespace lime {
}
void ImageDataUtil::SetFormat (Image* image, PixelFormat format) {
int index, a16;
int length = image->buffer->data->Size () / 4;
int r1, g1, b1, a1, r2, g2, b2, a2;
int r, g, b, a;
switch (image->buffer->format) {
case RGBA:
r1 = 0;
g1 = 1;
b1 = 2;
a1 = 3;
break;
case ARGB:
r1 = 1;
g1 = 2;
b1 = 3;
a1 = 0;
break;
case BGRA:
r1 = 2;
g1 = 1;
b1 = 0;
a1 = 3;
break;
}
switch (format) {
case RGBA:
r2 = 0;
g2 = 1;
b2 = 2;
a2 = 3;
break;
case ARGB:
r2 = 1;
g2 = 2;
b2 = 3;
a2 = 0;
break;
case BGRA:
r2 = 2;
g2 = 1;
b2 = 0;
a2 = 3;
break;
}
unsigned char* data = image->buffer->data->Bytes ();
for (int i = 0; i < length; i++) {
index = i * 4;
r = data[index + r1];
g = data[index + g1];
b = data[index + b1];
a = data[index + a1];
data[index + r2] = r;
data[index + g2] = g;
data[index + b2] = b;
data[index + a2] = a;
}
}
void ImageDataUtil::SetPixels (Image* image, Rectangle* rect, ByteArray* bytes, PixelFormat format) {
if (format == RGBA && rect->width == image->buffer->width && rect->height == image->buffer->height && rect->x == 0 && rect->y == 0) {

View File

@@ -138,7 +138,7 @@ namespace lime {
if (!val_is_null (bytes.mValue)) {
return alloc_int ((intptr_t)bytes.Bytes ());
return alloc_float ((intptr_t)bytes.Bytes ());
}
@@ -146,6 +146,7 @@ namespace lime {
}
value lime_byte_array_init (value inFactory, value inLen, value inResize, value inBytes) {
gByteArrayCreate = new AutoGCRoot (inFactory);