Fix image encode

This commit is contained in:
Joshua Granick
2015-06-24 16:15:03 -07:00
parent da134fee6e
commit 5f3acf4bc3
6 changed files with 121 additions and 62 deletions

View File

@@ -1,6 +1,7 @@
package lime.graphics.format;
import haxe.io.Bytes;
import lime.graphics.Image;
import lime.graphics.ImageBuffer;
import lime.system.System;
@@ -58,7 +59,16 @@ class JPEG {
#elseif (sys && (!disable_cffi || !format))
return lime_image_encode (image.buffer, 1, quality);
var data = lime_image_encode (image.buffer, 1, quality);
#if neko
var bytes = @:privateAccess (new Bytes (data.length, data.b));
#else
var bytes = Bytes.ofString (data.b);
@:privateAccess (bytes).length = data.length;
#end
return ByteArray.fromBytes (bytes);
#end

View File

@@ -1,6 +1,7 @@
package lime.graphics.format;
import haxe.io.Bytes;
import lime.graphics.Image;
import lime.system.System;
import lime.utils.ByteArray;
@@ -67,7 +68,16 @@ class PNG {
if (!System.disableCFFI) {
return lime_image_encode (image.buffer, 0, 0);
var data = lime_image_encode (image.buffer, 0, 0);
#if neko
var bytes = @:privateAccess (new Bytes (data.length, data.b));
#else
var bytes = Bytes.ofString (data.b);
@:privateAccess (bytes).length = data.length;
#end
return byteArray;
}

View File

@@ -23,6 +23,8 @@ namespace lime {
const unsigned char *Data () const;
int Length () const;
void Resize (int size);
void Set (value bytes);
void Set (const QuickVec<unsigned char> data);
value Value ();
unsigned char *_data;

View File

@@ -361,7 +361,7 @@ namespace lime {
jpeg_finish_compress (&cinfo);
*bytes = Bytes (dest.mOutput);
bytes->Set (dest.mOutput);
return true;

View File

@@ -295,7 +295,7 @@ namespace lime {
png_write_end (png_ptr, NULL);
*bytes = Bytes (out_buffer);
bytes->Set (out_buffer);
return true;

View File

@@ -30,46 +30,7 @@ namespace lime {
Bytes::Bytes (value bytes) {
if (!init) {
id_b = val_id ("b");
id_length = val_id ("length");
init = true;
}
if (val_is_null (bytes)) {
_length = 0;
_data = 0;
_value = 0;
} else {
_value = bytes;
_length = val_int (val_field (bytes, id_length));
if (_length > 0) {
value b = val_field (bytes, id_b);
if (val_is_string (b)) {
_data = (unsigned char*)val_string (b);
} else {
_data = (unsigned char*)buffer_data (val_to_buffer (b));
}
} else {
_data = 0;
}
}
Set (bytes);
}
@@ -101,20 +62,7 @@ namespace lime {
Bytes::Bytes (const QuickVec<unsigned char> data) {
_length = data.size ();
if (_length > 0) {
_data = (unsigned char*)malloc (_length);
memcpy (_data, &data[0], _length);
} else {
_data = 0;
}
_value = 0;
Set (data);
}
@@ -208,6 +156,84 @@ namespace lime {
}
void Bytes::Set (value bytes) {
if (!init) {
id_b = val_id ("b");
id_length = val_id ("length");
init = true;
}
if (!_value && _data) {
free (_data);
}
if (val_is_null (bytes)) {
_length = 0;
_data = 0;
_value = 0;
} else {
_value = bytes;
_length = val_int (val_field (bytes, id_length));
if (_length > 0) {
value b = val_field (bytes, id_b);
if (val_is_string (b)) {
_data = (unsigned char*)val_string (b);
} else {
_data = (unsigned char*)buffer_data (val_to_buffer (b));
}
} else {
_data = 0;
}
}
}
void Bytes::Set (const QuickVec<unsigned char> data) {
if (!_value && _data) {
free (_data);
}
_length = data.size ();
if (_length > 0) {
_data = (unsigned char*)malloc (_length);
memcpy (_data, &data[0], _length);
} else {
_data = 0;
}
_value = 0;
}
value Bytes::Value () {
if (_value) {
@@ -225,10 +251,21 @@ namespace lime {
}
value object = alloc_empty_object ();
value newString = alloc_raw_string (_length);
memcpy ((char*)val_string (newString), _data, _length);
alloc_field (object, id_b, newString);
alloc_field (object, id_length, alloc_int (_length));
if (_length > 0 && _data) {
value newString = alloc_raw_string (_length);
memcpy ((char*)val_string (newString), _data, _length);
alloc_field (object, id_b, newString);
alloc_field (object, id_length, alloc_int (_length));
} else {
alloc_field (object, id_b, alloc_raw_string (0));
alloc_field (object, id_length, alloc_int (_length));
}
return object;
}