First pass at refactoring native CFFI Bytes

This commit is contained in:
Joshua Granick
2018-06-15 23:21:03 -07:00
parent ecd7876c4b
commit 6d666b0eeb
29 changed files with 441 additions and 988 deletions

View File

@@ -12,17 +12,6 @@ namespace lime {
static bool init = false;
Image::Image () {
buffer = 0;
height = 0;
offsetX = 0;
offsetY = 0;
width = 0;
}
Image::Image (value image) {
if (!init) {
@@ -45,20 +34,13 @@ namespace lime {
}
Image::Image (HL_Image* image) {
width = image->width;
height = image->height;
buffer = new ImageBuffer (image->buffer);
offsetX = image->offsetX;
offsetY = image->offsetY;
}
Image::~Image () {
delete buffer;
if (buffer) {
delete buffer;
}
}

View File

@@ -14,21 +14,6 @@ namespace lime {
static bool init = false;
ImageBuffer::ImageBuffer () {
width = 0;
height = 0;
bitsPerPixel = 32;
format = RGBA32;
data = 0;
premultiplied = false;
transparent = false;
_buffer = 0;
_value = 0;
}
ImageBuffer::ImageBuffer (value imageBuffer) {
if (!init) {
@@ -66,40 +51,6 @@ namespace lime {
}
_buffer = 0;
_value = imageBuffer;
}
ImageBuffer::ImageBuffer (HL_ImageBuffer* imageBuffer) {
if (imageBuffer) {
width = imageBuffer->width;
height = imageBuffer->height;
bitsPerPixel = imageBuffer->bitsPerPixel;
format = imageBuffer->format;
transparent = imageBuffer->transparent;
premultiplied = imageBuffer->premultiplied;
data = new ArrayBufferView (imageBuffer->data);
_buffer = imageBuffer;
} else {
width = 0;
height = 0;
bitsPerPixel = 32;
format = RGBA32;
data = 0;
premultiplied = false;
transparent = false;
_buffer = 0;
}
_value = 0;
}
@@ -123,7 +74,7 @@ namespace lime {
}
int stride = Stride ();
unsigned char *bytes = this->data->buffer->Data ();
unsigned char *bytes = this->data->buffer->b;
for (int i = 0; i < height; i++) {
@@ -144,7 +95,7 @@ namespace lime {
if (!this->data) {
this->data = new ArrayBufferView (height * stride);
//this->data = new ArrayBufferView (height * stride);
} else {
@@ -162,51 +113,37 @@ namespace lime {
}
void* ImageBuffer::Value () {
value ImageBuffer::Value () {
if (_buffer) {
return Value (alloc_empty_object ());
}
value ImageBuffer::Value (value imageBuffer) {
if (!init) {
_buffer->width = width;
_buffer->height = height;
_buffer->bitsPerPixel = bitsPerPixel;
_buffer->format = format;
_buffer->transparent = transparent;
_buffer->premultiplied = premultiplied;
//_buffer->data
return _buffer;
} else {
if (!init) {
id_bitsPerPixel = val_id ("bitsPerPixel");
id_transparent = val_id ("transparent");
id_data = val_id ("data");
id_width = val_id ("width");
id_height = val_id ("height");
id_format = val_id ("format");
id_premultiplied = val_id ("premultiplied");
init = true;
}
if (val_is_null (_value)) {
_value = alloc_empty_object ();
}
alloc_field (_value, id_width, alloc_int (width));
alloc_field (_value, id_height, alloc_int (height));
alloc_field (_value, id_bitsPerPixel, alloc_int (bitsPerPixel));
alloc_field (_value, id_data, data ? data->Value () : alloc_null ());
alloc_field (_value, id_transparent, alloc_bool (transparent));
alloc_field (_value, id_format, alloc_int (format));
alloc_field (_value, id_premultiplied, alloc_bool (premultiplied));
return _value;
id_bitsPerPixel = val_id ("bitsPerPixel");
id_transparent = val_id ("transparent");
id_data = val_id ("data");
id_width = val_id ("width");
id_height = val_id ("height");
id_format = val_id ("format");
id_premultiplied = val_id ("premultiplied");
init = true;
}
alloc_field (imageBuffer, id_width, alloc_int (width));
alloc_field (imageBuffer, id_height, alloc_int (height));
alloc_field (imageBuffer, id_bitsPerPixel, alloc_int (bitsPerPixel));
alloc_field (imageBuffer, id_data, data ? data->Value (val_field (imageBuffer, id_data)) : alloc_null ());
alloc_field (imageBuffer, id_transparent, alloc_bool (transparent));
alloc_field (imageBuffer, id_format, alloc_int (format));
alloc_field (imageBuffer, id_premultiplied, alloc_bool (premultiplied));
return imageBuffer;
}

View File

@@ -241,15 +241,16 @@ namespace lime {
} else {
data = new Bytes (resource->path);
manager = new MySrcManager (data->Data (), data->Length ());
data = (Bytes*)malloc (sizeof (Bytes));
data->ReadFile (resource->path);
manager = new MySrcManager (data->b, data->length);
cinfo.src = &manager->pub;
}
} else {
manager = new MySrcManager (resource->data->Data (), resource->data->Length ());
manager = new MySrcManager (resource->data->b, resource->data->length);
cinfo.src = &manager->pub;
}
@@ -266,7 +267,7 @@ namespace lime {
int components = cinfo.output_components;
imageBuffer->Resize (cinfo.output_width, cinfo.output_height, 32);
unsigned char *bytes = imageBuffer->data->Data ();
unsigned char *bytes = imageBuffer->data->buffer->b;
unsigned char *scanline = new unsigned char [imageBuffer->width * components];
while (cinfo.output_scanline < cinfo.output_height) {
@@ -363,7 +364,7 @@ namespace lime {
jpeg_start_compress (&cinfo, TRUE);
JSAMPROW row_pointer = &row_buf[0];
unsigned char* imageData = imageBuffer->data->Data();
unsigned char* imageData = imageBuffer->data->buffer->b;
int stride = imageBuffer->Stride ();
while (cinfo.next_scanline < cinfo.image_height) {
@@ -387,7 +388,14 @@ namespace lime {
jpeg_finish_compress (&cinfo);
bytes->Set (dest.mOutput);
int size = dest.mOutput.size ();
if (size > 0) {
bytes->Resize (size);
memcpy (bytes->b, &dest.mOutput[0], size);
}
return true;

View File

@@ -82,8 +82,8 @@ namespace lime {
png_uint_32 width, height;
int bit_depth, color_type, interlace_type;
FILE_HANDLE *file = NULL;
Bytes *data = NULL;
FILE_HANDLE* file = NULL;
Bytes* data = NULL;
if (resource->path) {
@@ -101,7 +101,7 @@ namespace lime {
} else {
if (png_sig_cmp (resource->data->Data (), 0, PNG_SIG_SIZE)) {
if (png_sig_cmp (resource->data->b, 0, PNG_SIG_SIZE)) {
return false;
@@ -142,15 +142,16 @@ namespace lime {
} else {
data = new Bytes (resource->path);
ReadBuffer buffer (data->Data (), data->Length ());
data = (Bytes*)malloc (sizeof (Bytes));
data->ReadFile (resource->path);
ReadBuffer buffer (data->b, data->length);
png_set_read_fn (png_ptr, &buffer, user_read_data_fn);
}
} else {
ReadBuffer buffer (resource->data->Data (), resource->data->Length ());
ReadBuffer buffer (resource->data->b, resource->data->length);
png_set_read_fn (png_ptr, &buffer, user_read_data_fn);
}
@@ -184,7 +185,7 @@ namespace lime {
imageBuffer->Resize (width, height, 32);
const unsigned int stride = imageBuffer->Stride ();
unsigned char *bytes = imageBuffer->data->Data ();
unsigned char *bytes = imageBuffer->data->buffer->b;
int number_of_passes = png_set_interlace_handling (png_ptr);
@@ -218,7 +219,7 @@ namespace lime {
}
bool PNG::Encode (ImageBuffer *imageBuffer, Bytes *bytes) {
bool PNG::Encode (ImageBuffer *imageBuffer, Bytes* bytes) {
png_structp png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, user_error_fn, user_warning_fn);
@@ -258,7 +259,7 @@ namespace lime {
png_write_info (png_ptr, info_ptr);
bool do_alpha = (color_type == PNG_COLOR_TYPE_RGBA);
unsigned char* imageData = imageBuffer->data->Data();
unsigned char* imageData = imageBuffer->data->buffer->b;
int stride = imageBuffer->Stride ();
{
@@ -296,7 +297,14 @@ namespace lime {
png_write_end (png_ptr, NULL);
bytes->Set (out_buffer);
int size = out_buffer.size ();
if (size > 0) {
bytes->Resize (size);
memcpy (bytes->b, &out_buffer[0], size);
}
return true;

View File

@@ -17,7 +17,7 @@ namespace lime {
PixelFormat format = image->buffer->format;
bool premultiplied = image->buffer->premultiplied;
uint8_t* data = (uint8_t*)image->buffer->data->Data ();
uint8_t* data = (uint8_t*)image->buffer->data->buffer->b;
ImageDataView dataView = ImageDataView (image, rect);
@@ -50,8 +50,8 @@ namespace lime {
void ImageDataUtil::CopyChannel (Image* image, Image* sourceImage, Rectangle* sourceRect, Vector2* destPoint, int srcChannel, int destChannel) {
uint8_t* srcData = (uint8_t*)sourceImage->buffer->data->Data ();
uint8_t* destData = (uint8_t*)image->buffer->data->Data ();
uint8_t* srcData = (uint8_t*)sourceImage->buffer->data->buffer->b;
uint8_t* destData = (uint8_t*)image->buffer->data->buffer->b;
ImageDataView srcView = ImageDataView (sourceImage, sourceRect);
Rectangle destRect = Rectangle (destPoint->x, destPoint->y, srcView.width, srcView.height);
@@ -108,8 +108,8 @@ namespace lime {
void ImageDataUtil::CopyPixels (Image* image, Image* sourceImage, Rectangle* sourceRect, Vector2* destPoint, Image* alphaImage, Vector2* alphaPoint, bool mergeAlpha) {
uint8_t* sourceData = (uint8_t*)sourceImage->buffer->data->Data ();
uint8_t* destData = (uint8_t*)image->buffer->data->Data ();
uint8_t* sourceData = (uint8_t*)sourceImage->buffer->data->buffer->b;
uint8_t* destData = (uint8_t*)image->buffer->data->buffer->b;
if (!sourceData || !destData) return;
@@ -207,7 +207,7 @@ namespace lime {
} else {
uint8_t* alphaData = (uint8_t*)alphaImage->buffer->data->Data ();
uint8_t* alphaData = (uint8_t*)alphaImage->buffer->data->buffer->b;
PixelFormat alphaFormat = alphaImage->buffer->format;
bool alphaPremultiplied = alphaImage->buffer->premultiplied;
int alphaPosition;
@@ -291,10 +291,9 @@ namespace lime {
void ImageDataUtil::FillRect (Image* image, Rectangle* rect, int32_t color) {
uint8_t* data = (uint8_t*)image->buffer->data->Data ();
uint8_t* data = (uint8_t*)image->buffer->data->buffer->b;
PixelFormat format = image->buffer->format;
bool premultiplied = image->buffer->premultiplied;
RGBA fillColor (color);
if (rect->x == 0 && rect->y == 0 && rect->width == image->width && rect->height == image->height) {
@@ -330,7 +329,7 @@ namespace lime {
void ImageDataUtil::FloodFill (Image* image, int x, int y, int32_t color) {
uint8_t* data = (uint8_t*)image->buffer->data->Data ();
uint8_t* data = (uint8_t*)image->buffer->data->buffer->b;
PixelFormat format = image->buffer->format;
bool premultiplied = image->buffer->premultiplied;
@@ -405,8 +404,8 @@ namespace lime {
int length = int (rect->width * rect->height);
pixels->Resize (length * 4);
uint8_t* data = (uint8_t*)image->buffer->data->Data ();
uint8_t* destData = (uint8_t*)pixels->Data ();
uint8_t* data = (uint8_t*)image->buffer->data->buffer->b;
uint8_t* destData = (uint8_t*)pixels->b;
PixelFormat sourceFormat = image->buffer->format;
bool premultiplied = image->buffer->premultiplied;
@@ -440,8 +439,8 @@ namespace lime {
Rectangle destRect = Rectangle (destPoint->x, destPoint->y, sourceView.width, sourceView.height);
ImageDataView destView = ImageDataView (image, &destRect);
uint8_t* sourceData = (uint8_t*)sourceImage->buffer->data->Data ();
uint8_t* destData = (uint8_t*)image->buffer->data->Data ();
uint8_t* sourceData = (uint8_t*)sourceImage->buffer->data->buffer->b;
uint8_t* destData = (uint8_t*)image->buffer->data->buffer->b;
PixelFormat sourceFormat = sourceImage->buffer->format;
PixelFormat destFormat = image->buffer->format;
bool sourcePremultiplied = sourceImage->buffer->premultiplied;
@@ -480,8 +479,8 @@ namespace lime {
void ImageDataUtil::MultiplyAlpha (Image* image) {
PixelFormat format = image->buffer->format;
uint8_t* data = (uint8_t*)image->buffer->data->Data ();
int length = int (image->buffer->data->Length () / 4);
uint8_t* data = (uint8_t*)image->buffer->data->buffer->b;
int length = int (image->buffer->data->length / 4);
RGBA pixel;
for (int i = 0; i < length; i++) {
@@ -499,8 +498,8 @@ namespace lime {
int imageWidth = image->width;
int imageHeight = image->height;
uint8_t* data = (uint8_t*)image->buffer->data->Data ();
uint8_t* newData = (uint8_t*)buffer->data->Data ();
uint8_t* data = (uint8_t*)image->buffer->data->buffer->b;
uint8_t* newData = (uint8_t*)buffer->data->buffer->b;
int sourceIndex, sourceIndexX, sourceIndexY, sourceIndexXY, index;
int sourceX, sourceY;
@@ -554,7 +553,7 @@ namespace lime {
void ImageDataUtil::SetFormat (Image* image, PixelFormat format) {
int index;
int length = image->buffer->data->Length () / 4;
int length = image->buffer->data->length / 4;
int r1, g1, b1, a1, r2, g2, b2, a2;
int r, g, b, a;
@@ -614,7 +613,7 @@ namespace lime {
}
unsigned char* data = image->buffer->data->Data ();
unsigned char* data = image->buffer->data->buffer->b;
for (int i = 0; i < length; i++) {
@@ -637,14 +636,14 @@ namespace lime {
void ImageDataUtil::SetPixels (Image* image, Rectangle* rect, Bytes* bytes, int offset, PixelFormat format, Endian endian) {
uint8_t* data = (uint8_t*)image->buffer->data->Data ();
uint8_t* data = (uint8_t*)image->buffer->data->buffer->b;
PixelFormat sourceFormat = image->buffer->format;
bool premultiplied = image->buffer->premultiplied;
ImageDataView dataView = ImageDataView (image, rect);
int row;
RGBA pixel;
uint8_t* byteArray = (uint8_t*)bytes->Data ();
uint8_t* byteArray = (uint8_t*)bytes->b;
int srcPosition = offset;
bool transparent = image->buffer->transparent;
@@ -729,8 +728,8 @@ namespace lime {
RGBA _color (color);
int hits = 0;
uint8_t* srcData = (uint8_t*)sourceImage->buffer->data->Data ();
uint8_t* destData = (uint8_t*)image->buffer->data->Data ();
uint8_t* srcData = (uint8_t*)sourceImage->buffer->data->buffer->b;
uint8_t* destData = (uint8_t*)image->buffer->data->buffer->b;
ImageDataView srcView = ImageDataView (sourceImage, sourceRect);
Rectangle destRect = Rectangle (destPoint->x, destPoint->y, srcView.width, srcView.height);
@@ -796,8 +795,8 @@ namespace lime {
void ImageDataUtil::UnmultiplyAlpha (Image* image) {
PixelFormat format = image->buffer->format;
uint8_t* data = (uint8_t*)image->buffer->data->Data ();
int length = int (image->buffer->data->Length () / 4);
uint8_t* data = (uint8_t*)image->buffer->data->buffer->b;
int length = int (image->buffer->data->length / 4);
RGBA pixel;
for (int i = 0; i < length; i++) {