From 84fbffa923489b8dac8a46e07cbddc7f2c3d7749 Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Tue, 8 Jul 2014 11:56:25 -0700 Subject: [PATCH] Minor refactor --- lime/graphics/Image.hx | 44 +++++++------- project/Build.xml | 4 +- project/include/graphics/Image.h | 38 ++++++++++++ project/include/graphics/ImageData.h | 33 ----------- project/include/graphics/JPEG.h | 9 +-- project/include/graphics/PNG.h | 5 +- project/src/ExternalInterface.cpp | 48 +++++++-------- .../src/graphics/{ImageData.cpp => Image.cpp} | 41 ++++++++----- project/src/graphics/JPEG.cpp | 53 +++++++++-------- project/src/graphics/{opengl => }/OpenGL.h | 0 .../graphics/{opengl => }/OpenGLBindings.cpp | 0 .../graphics/{opengl => }/OpenGLBindings.h | 0 .../graphics/{opengl => }/OpenGLExtensions.h | 0 project/src/graphics/PNG.cpp | 59 ++++++++++--------- templates/haxe/DefaultAssetLibrary.hx | 2 +- 15 files changed, 178 insertions(+), 158 deletions(-) create mode 100644 project/include/graphics/Image.h delete mode 100644 project/include/graphics/ImageData.h rename project/src/graphics/{ImageData.cpp => Image.cpp} (73%) rename project/src/graphics/{opengl => }/OpenGL.h (100%) rename project/src/graphics/{opengl => }/OpenGLBindings.cpp (100%) rename project/src/graphics/{opengl => }/OpenGLBindings.h (100%) rename project/src/graphics/{opengl => }/OpenGLExtensions.h (100%) diff --git a/lime/graphics/Image.hx b/lime/graphics/Image.hx index de0647cd5..2bd028794 100644 --- a/lime/graphics/Image.hx +++ b/lime/graphics/Image.hx @@ -47,22 +47,6 @@ class Image { } - public static function loadFromFile (path:String) { - - #if flash - - throw "Can not load image from file in Flash"; - - #elseif (cpp || neko) - - var imageData = lime_load_image (path); - - return (imageData == null ? null : new Image (new UInt8Array (imageData.data), imageData.width, imageData.height)); - - #end - - } - public function forcePowerOfTwo () { @@ -129,6 +113,22 @@ class Image { } + public static function loadFromFile (path:String) { + + #if flash + + throw "Can not load image from file in Flash"; + + #elseif (cpp || neko) + + var imageData = lime_image_load (path); + return (imageData == null ? null : new Image (new UInt8Array (imageData.data), imageData.width, imageData.height)); + + #end + + } + + public function premultiplyAlpha ():Void { if (premultiplied) return; @@ -139,8 +139,8 @@ class Image { premultiplied = true; } - - + + private function get_data ():ImageData { if (__data == null && src != null && width > 0 && height > 0) { @@ -190,12 +190,10 @@ class Image { #if (cpp || neko) - - private static var lime_load_image = System.load("lime", "lime_load_image", 1); - + private static var lime_image_load = System.load ("lime", "lime_image_load", 1); #end - - + + } diff --git a/project/Build.xml b/project/Build.xml index 388cf5bab..6391f1339 100644 --- a/project/Build.xml +++ b/project/Build.xml @@ -48,7 +48,7 @@
- +
@@ -76,7 +76,7 @@ - + diff --git a/project/include/graphics/Image.h b/project/include/graphics/Image.h new file mode 100644 index 000000000..1b46c0e28 --- /dev/null +++ b/project/include/graphics/Image.h @@ -0,0 +1,38 @@ +#ifndef LIME_GRAPHICS_IMAGE_H +#define LIME_GRAPHICS_IMAGE_H + + +#include +#include + + +namespace lime { + + + class Image { + + + public: + + Image (); + ~Image (); + + value Value (); + + int width; + int height; + ByteArray *data; + + + private: + + value mValue; + + + }; + + +} + + +#endif \ No newline at end of file diff --git a/project/include/graphics/ImageData.h b/project/include/graphics/ImageData.h deleted file mode 100644 index 638d3d38d..000000000 --- a/project/include/graphics/ImageData.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef LIME_GRAPHICS_IMAGE_DATA_H -#define LIME_GRAPHICS_IMAGE_DATA_H - -#include -#include - - -namespace lime { - - - class ImageData { - - public: - - int width; - int height; - ByteArray *data; - - ImageData(); - ~ImageData(); - value Value(); - - private: - - value mValue; - - }; - - -} - - -#endif \ No newline at end of file diff --git a/project/include/graphics/JPEG.h b/project/include/graphics/JPEG.h index a5423ebfe..6fcf4fde9 100644 --- a/project/include/graphics/JPEG.h +++ b/project/include/graphics/JPEG.h @@ -3,15 +3,16 @@ namespace lime { - - class ImageData; - + + + class Image; + class JPEG { public: - static bool Decode (const char *path, ImageData *imageData); + static bool Decode (const char *path, Image *image); }; diff --git a/project/include/graphics/PNG.h b/project/include/graphics/PNG.h index 63db9da1b..263e3b779 100644 --- a/project/include/graphics/PNG.h +++ b/project/include/graphics/PNG.h @@ -4,14 +4,15 @@ namespace lime { - class ImageData; + + class Image; class PNG { public: - static bool Decode (const char *path, ImageData *imageData); + static bool Decode (const char *path, Image *image); }; diff --git a/project/src/ExternalInterface.cpp b/project/src/ExternalInterface.cpp index bd0844878..6d98442af 100644 --- a/project/src/ExternalInterface.cpp +++ b/project/src/ExternalInterface.cpp @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include #include @@ -51,6 +51,28 @@ namespace lime { } + value lime_image_load (value path) { + + Image image; + const char *filePath = val_string (path); + + if (PNG::Decode (filePath, &image)) { + + return image.Value (); + + } + + if (JPEG::Decode (filePath, &image)) { + + return image.Value (); + + } + + return alloc_null (); + + } + + value lime_key_event_manager_register (value callback, value eventObject) { KeyEvent::callback = new AutoGCRoot (callback); @@ -105,28 +127,6 @@ namespace lime { } - value lime_load_image (value path) { - - ImageData imageData; - const char *filePath = val_string (path); - - if (PNG::Decode (filePath, &imageData)) { - - return imageData.Value (); - - } - - if (JPEG::Decode(filePath, &imageData)) { - - return imageData.Value (); - - } - - return alloc_null (); - - } - - value lime_render_event_manager_register (value callback, value eventObject) { RenderEvent::callback = new AutoGCRoot (callback); @@ -197,7 +197,7 @@ namespace lime { DEFINE_PRIM (lime_application_create, 1); DEFINE_PRIM (lime_application_exec, 1); DEFINE_PRIM (lime_application_get_ticks, 0); - DEFINE_PRIM (lime_load_image, 1); + DEFINE_PRIM (lime_image_load, 1); DEFINE_PRIM (lime_key_event_manager_register, 2); DEFINE_PRIM (lime_lzma_encode, 1); DEFINE_PRIM (lime_lzma_decode, 1); diff --git a/project/src/graphics/ImageData.cpp b/project/src/graphics/Image.cpp similarity index 73% rename from project/src/graphics/ImageData.cpp rename to project/src/graphics/Image.cpp index 95ea16661..f42b1ea34 100644 --- a/project/src/graphics/ImageData.cpp +++ b/project/src/graphics/Image.cpp @@ -1,23 +1,33 @@ -#include +#include namespace lime { - + + static int id_data; static int id_height; static int id_width; static bool init = false; - - ImageData::ImageData():width(0), height(0), data(0) { } - - ImageData::~ImageData() { - - delete data; - + + + Image::Image () { + + width = 0; + height = 0; + data = 0; + } - - value ImageData::Value() { - + + + Image::~Image () { + + delete data; + + } + + + value Image::Value() { + if (!init) { id_width = val_id ("width"); @@ -32,7 +42,8 @@ namespace lime { alloc_field (mValue, id_height, alloc_int (height)); alloc_field (mValue, id_data, data->mValue); return mValue; - + } - -} + + +} \ No newline at end of file diff --git a/project/src/graphics/JPEG.cpp b/project/src/graphics/JPEG.cpp index 46b00ff71..220f0b6e6 100644 --- a/project/src/graphics/JPEG.cpp +++ b/project/src/graphics/JPEG.cpp @@ -6,15 +6,15 @@ extern "C" { } -#include +#include #include #include namespace lime { - - - bool JPEG::Decode (const char *path, ImageData *imageData) { + + + bool JPEG::Decode (const char *path, Image *image) { struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; @@ -26,44 +26,47 @@ namespace lime { jpeg_stdio_src (&cinfo, file); if (jpeg_read_header (&cinfo, TRUE) == JPEG_HEADER_OK) { - + jpeg_start_decompress (&cinfo); - imageData->width = cinfo.output_width; - imageData->height = cinfo.output_height; + image->width = cinfo.output_width; + image->height = cinfo.output_height; int components = cinfo.num_components; - imageData->data = new ByteArray (imageData->width * imageData->height * 4); - - unsigned char *bytes = imageData->data->Bytes(); - unsigned char *scanline = new unsigned char [imageData->width * imageData->height * components]; - + image->data = new ByteArray (image->width * image->height * 4); + + unsigned char *bytes = image->data->Bytes (); + unsigned char *scanline = new unsigned char [image->width * image->height * components]; + while (cinfo.output_scanline < cinfo.output_height) { - + jpeg_read_scanlines (&cinfo, &scanline, 1); - + // convert 24-bit scanline to 32-bit const unsigned char *line = scanline; - const unsigned char *const end = line + imageData->width * components; - + const unsigned char *const end = line + image->width * components; + while (line != end) { - + *bytes++ = *line++; *bytes++ = *line++; *bytes++ = *line++; *bytes++ = 0xFF; - + } - + } - + delete[] scanline; - + jpeg_finish_decompress (&cinfo); + } - + fclose (file); jpeg_destroy_decompress (&cinfo); - + return true; + } - -} + + +} \ No newline at end of file diff --git a/project/src/graphics/opengl/OpenGL.h b/project/src/graphics/OpenGL.h similarity index 100% rename from project/src/graphics/opengl/OpenGL.h rename to project/src/graphics/OpenGL.h diff --git a/project/src/graphics/opengl/OpenGLBindings.cpp b/project/src/graphics/OpenGLBindings.cpp similarity index 100% rename from project/src/graphics/opengl/OpenGLBindings.cpp rename to project/src/graphics/OpenGLBindings.cpp diff --git a/project/src/graphics/opengl/OpenGLBindings.h b/project/src/graphics/OpenGLBindings.h similarity index 100% rename from project/src/graphics/opengl/OpenGLBindings.h rename to project/src/graphics/OpenGLBindings.h diff --git a/project/src/graphics/opengl/OpenGLExtensions.h b/project/src/graphics/OpenGLExtensions.h similarity index 100% rename from project/src/graphics/opengl/OpenGLExtensions.h rename to project/src/graphics/OpenGLExtensions.h diff --git a/project/src/graphics/PNG.cpp b/project/src/graphics/PNG.cpp index 8e861a689..37725b3f5 100644 --- a/project/src/graphics/PNG.cpp +++ b/project/src/graphics/PNG.cpp @@ -5,7 +5,7 @@ extern "C" { } -#include +#include #include #include @@ -13,7 +13,7 @@ extern "C" { namespace lime { - bool PNG::Decode (const char *path, ImageData *imageData) { + bool PNG::Decode (const char *path, Image *image) { unsigned char png_sig[PNG_SIG_SIZE]; png_structp png_ptr; @@ -23,18 +23,18 @@ namespace lime { FILE *file = OpenRead (path); if (!file) return false; - - // verify the PNG signature - int read = fread(png_sig, PNG_SIG_SIZE, 1, file); - if (png_sig_cmp (png_sig, 0, PNG_SIG_SIZE)) { + // verify the PNG signature + int read = fread (png_sig, PNG_SIG_SIZE, 1, file); + if (png_sig_cmp (png_sig, 0, PNG_SIG_SIZE)) { + fclose (file); return false; + + } - } - if ((png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)) == NULL) { - + fclose (file); return false; @@ -73,19 +73,19 @@ namespace lime { png_set_strip_16 (png_ptr); const unsigned int stride = width * 4; - imageData->width = width; - imageData->height = height; - imageData->data = new ByteArray (height * stride); + image->width = width; + image->height = height; + image->data = new ByteArray (height * stride); png_bytepp row_ptrs = new png_bytep[height]; - unsigned char *bytes = imageData->data->Bytes(); + unsigned char *bytes = image->data->Bytes (); for (size_t i = 0; i < height; i++) { - + row_ptrs[i] = bytes + i * stride; } - + png_read_image (png_ptr, row_ptrs); png_read_end (png_ptr, NULL); @@ -97,32 +97,32 @@ namespace lime { } - static bool Encode (ImageData *imageData, ByteArray *bytes) { + static bool Encode (Image *image, ByteArray *bytes) { return true; /*png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, user_error_fn, user_warning_fn); - + if (!png_ptr) return false; - + png_infop info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) return false; - + if (setjmp(png_jmpbuf(png_ptr))) { png_destroy_write_struct(&png_ptr, &info_ptr ); return false; } - + QuickVec out_buffer; - + png_set_write_fn(png_ptr, &out_buffer, user_write_data, user_flush_data); - + int w = inSurface->Width(); int h = inSurface->Height(); - + int bit_depth = 8; int color_type = (inSurface->Format()&pfHasAlpha) ? PNG_COLOR_TYPE_RGB_ALPHA : @@ -130,11 +130,11 @@ namespace lime { png_set_IHDR(png_ptr, info_ptr, w, h, bit_depth, color_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); - + png_write_info(png_ptr, info_ptr); - + bool do_alpha = color_type==PNG_COLOR_TYPE_RGBA; - + { QuickVec row_data(w*4); png_bytep row = &row_data[0]; @@ -156,12 +156,13 @@ namespace lime { png_write_rows(png_ptr, &row, 1); } } - + png_write_end(png_ptr, NULL); - + *outBytes = ByteArray(out_buffer); - + return true;*/ + } diff --git a/templates/haxe/DefaultAssetLibrary.hx b/templates/haxe/DefaultAssetLibrary.hx index 3a7a90c7b..a5050aa26 100644 --- a/templates/haxe/DefaultAssetLibrary.hx +++ b/templates/haxe/DefaultAssetLibrary.hx @@ -217,7 +217,7 @@ class DefaultAssetLibrary extends AssetLibrary { var image = Image.loadFromFile (path.get (id)); if (image == null) return null; - return new Image(image.bytes, image.width, image.height); + return new Image (image.data, image.width, image.height); #end