diff --git a/lime/graphics/format/JPEG.hx b/lime/graphics/format/JPEG.hx index 60a762601..35381fca6 100644 --- a/lime/graphics/format/JPEG.hx +++ b/lime/graphics/format/JPEG.hx @@ -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 diff --git a/lime/graphics/format/PNG.hx b/lime/graphics/format/PNG.hx index a34aa86b9..c56b5a903 100644 --- a/lime/graphics/format/PNG.hx +++ b/lime/graphics/format/PNG.hx @@ -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; } diff --git a/project/include/utils/Bytes.h b/project/include/utils/Bytes.h index 71e15b31a..d680b83c6 100644 --- a/project/include/utils/Bytes.h +++ b/project/include/utils/Bytes.h @@ -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 data); value Value (); unsigned char *_data; diff --git a/project/src/graphics/format/JPEG.cpp b/project/src/graphics/format/JPEG.cpp index 626d5caa0..7017112ad 100644 --- a/project/src/graphics/format/JPEG.cpp +++ b/project/src/graphics/format/JPEG.cpp @@ -361,7 +361,7 @@ namespace lime { jpeg_finish_compress (&cinfo); - *bytes = Bytes (dest.mOutput); + bytes->Set (dest.mOutput); return true; diff --git a/project/src/graphics/format/PNG.cpp b/project/src/graphics/format/PNG.cpp index 0bab1d617..661ac9996 100644 --- a/project/src/graphics/format/PNG.cpp +++ b/project/src/graphics/format/PNG.cpp @@ -295,7 +295,7 @@ namespace lime { png_write_end (png_ptr, NULL); - *bytes = Bytes (out_buffer); + bytes->Set (out_buffer); return true; diff --git a/project/src/utils/Bytes.cpp b/project/src/utils/Bytes.cpp index 4f126b797..36e7bd354 100644 --- a/project/src/utils/Bytes.cpp +++ b/project/src/utils/Bytes.cpp @@ -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 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 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; }