Minor refactor

This commit is contained in:
Joshua Granick
2014-07-08 11:56:25 -07:00
parent 7df1416127
commit 84fbffa923
15 changed files with 178 additions and 158 deletions

View File

@@ -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 () { 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 { public function premultiplyAlpha ():Void {
if (premultiplied) return; if (premultiplied) return;
@@ -139,8 +139,8 @@ class Image {
premultiplied = true; premultiplied = true;
} }
private function get_data ():ImageData { private function get_data ():ImageData {
if (__data == null && src != null && width > 0 && height > 0) { if (__data == null && src != null && width > 0 && height > 0) {
@@ -190,12 +190,10 @@ class Image {
#if (cpp || neko) #if (cpp || neko)
private static var lime_image_load = System.load ("lime", "lime_image_load", 1);
private static var lime_load_image = System.load("lime", "lime_load_image", 1);
#end #end
} }

View File

@@ -48,7 +48,7 @@
<section if="LIME_OPENGL"> <section if="LIME_OPENGL">
<file name="src/graphics/opengl/OpenGLBindings.cpp" /> <file name="src/graphics/OpenGLBindings.cpp" />
</section> </section>
@@ -76,7 +76,7 @@
<file name="src/system/ios/System.mm" if="ios" /> <file name="src/system/ios/System.mm" if="ios" />
<file name="src/app/UpdateEvent.cpp" /> <file name="src/app/UpdateEvent.cpp" />
<file name="src/graphics/ImageData.cpp" /> <file name="src/graphics/Image.cpp" />
<file name="src/graphics/JPEG.cpp" /> <file name="src/graphics/JPEG.cpp" />
<file name="src/graphics/PNG.cpp" /> <file name="src/graphics/PNG.cpp" />
<file name="src/graphics/RenderEvent.cpp" /> <file name="src/graphics/RenderEvent.cpp" />

View File

@@ -0,0 +1,38 @@
#ifndef LIME_GRAPHICS_IMAGE_H
#define LIME_GRAPHICS_IMAGE_H
#include <hx/CFFI.h>
#include <utils/ByteArray.h>
namespace lime {
class Image {
public:
Image ();
~Image ();
value Value ();
int width;
int height;
ByteArray *data;
private:
value mValue;
};
}
#endif

View File

@@ -1,33 +0,0 @@
#ifndef LIME_GRAPHICS_IMAGE_DATA_H
#define LIME_GRAPHICS_IMAGE_DATA_H
#include <hx/CFFI.h>
#include <utils/ByteArray.h>
namespace lime {
class ImageData {
public:
int width;
int height;
ByteArray *data;
ImageData();
~ImageData();
value Value();
private:
value mValue;
};
}
#endif

View File

@@ -3,15 +3,16 @@
namespace lime { namespace lime {
class ImageData;
class Image;
class JPEG { class JPEG {
public: public:
static bool Decode (const char *path, ImageData *imageData); static bool Decode (const char *path, Image *image);
}; };

View File

@@ -4,14 +4,15 @@
namespace lime { namespace lime {
class ImageData;
class Image;
class PNG { class PNG {
public: public:
static bool Decode (const char *path, ImageData *imageData); static bool Decode (const char *path, Image *image);
}; };

View File

@@ -10,7 +10,7 @@
#include <hx/CFFI.h> #include <hx/CFFI.h>
#include <app/Application.h> #include <app/Application.h>
#include <app/UpdateEvent.h> #include <app/UpdateEvent.h>
#include <graphics/ImageData.h> #include <graphics/Image.h>
#include <graphics/PNG.h> #include <graphics/PNG.h>
#include <graphics/JPEG.h> #include <graphics/JPEG.h>
#include <graphics/Renderer.h> #include <graphics/Renderer.h>
@@ -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) { value lime_key_event_manager_register (value callback, value eventObject) {
KeyEvent::callback = new AutoGCRoot (callback); 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) { value lime_render_event_manager_register (value callback, value eventObject) {
RenderEvent::callback = new AutoGCRoot (callback); RenderEvent::callback = new AutoGCRoot (callback);
@@ -197,7 +197,7 @@ namespace lime {
DEFINE_PRIM (lime_application_create, 1); DEFINE_PRIM (lime_application_create, 1);
DEFINE_PRIM (lime_application_exec, 1); DEFINE_PRIM (lime_application_exec, 1);
DEFINE_PRIM (lime_application_get_ticks, 0); 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_key_event_manager_register, 2);
DEFINE_PRIM (lime_lzma_encode, 1); DEFINE_PRIM (lime_lzma_encode, 1);
DEFINE_PRIM (lime_lzma_decode, 1); DEFINE_PRIM (lime_lzma_decode, 1);

View File

@@ -1,23 +1,33 @@
#include <graphics/ImageData.h> #include <graphics/Image.h>
namespace lime { namespace lime {
static int id_data; static int id_data;
static int id_height; static int id_height;
static int id_width; static int id_width;
static bool init = false; static bool init = false;
ImageData::ImageData():width(0), height(0), data(0) { }
Image::Image () {
ImageData::~ImageData() {
width = 0;
delete data; height = 0;
data = 0;
} }
value ImageData::Value() {
Image::~Image () {
delete data;
}
value Image::Value() {
if (!init) { if (!init) {
id_width = val_id ("width"); id_width = val_id ("width");
@@ -32,7 +42,8 @@ namespace lime {
alloc_field (mValue, id_height, alloc_int (height)); alloc_field (mValue, id_height, alloc_int (height));
alloc_field (mValue, id_data, data->mValue); alloc_field (mValue, id_data, data->mValue);
return mValue; return mValue;
} }
}
}

View File

@@ -6,15 +6,15 @@ extern "C" {
} }
#include <graphics/ImageData.h> #include <graphics/Image.h>
#include <graphics/JPEG.h> #include <graphics/JPEG.h>
#include <utils/ByteArray.h> #include <utils/ByteArray.h>
namespace lime { 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_decompress_struct cinfo;
struct jpeg_error_mgr jerr; struct jpeg_error_mgr jerr;
@@ -26,44 +26,47 @@ namespace lime {
jpeg_stdio_src (&cinfo, file); jpeg_stdio_src (&cinfo, file);
if (jpeg_read_header (&cinfo, TRUE) == JPEG_HEADER_OK) { if (jpeg_read_header (&cinfo, TRUE) == JPEG_HEADER_OK) {
jpeg_start_decompress (&cinfo); jpeg_start_decompress (&cinfo);
imageData->width = cinfo.output_width; image->width = cinfo.output_width;
imageData->height = cinfo.output_height; image->height = cinfo.output_height;
int components = cinfo.num_components; int components = cinfo.num_components;
imageData->data = new ByteArray (imageData->width * imageData->height * 4); image->data = new ByteArray (image->width * image->height * 4);
unsigned char *bytes = imageData->data->Bytes(); unsigned char *bytes = image->data->Bytes ();
unsigned char *scanline = new unsigned char [imageData->width * imageData->height * components]; unsigned char *scanline = new unsigned char [image->width * image->height * components];
while (cinfo.output_scanline < cinfo.output_height) { while (cinfo.output_scanline < cinfo.output_height) {
jpeg_read_scanlines (&cinfo, &scanline, 1); jpeg_read_scanlines (&cinfo, &scanline, 1);
// convert 24-bit scanline to 32-bit // convert 24-bit scanline to 32-bit
const unsigned char *line = scanline; 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) { while (line != end) {
*bytes++ = *line++; *bytes++ = *line++;
*bytes++ = *line++; *bytes++ = *line++;
*bytes++ = *line++; *bytes++ = *line++;
*bytes++ = 0xFF; *bytes++ = 0xFF;
} }
} }
delete[] scanline; delete[] scanline;
jpeg_finish_decompress (&cinfo); jpeg_finish_decompress (&cinfo);
} }
fclose (file); fclose (file);
jpeg_destroy_decompress (&cinfo); jpeg_destroy_decompress (&cinfo);
return true; return true;
} }
}
}

View File

@@ -5,7 +5,7 @@ extern "C" {
} }
#include <graphics/ImageData.h> #include <graphics/Image.h>
#include <graphics/PNG.h> #include <graphics/PNG.h>
#include <utils/ByteArray.h> #include <utils/ByteArray.h>
@@ -13,7 +13,7 @@ extern "C" {
namespace lime { 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]; unsigned char png_sig[PNG_SIG_SIZE];
png_structp png_ptr; png_structp png_ptr;
@@ -23,18 +23,18 @@ namespace lime {
FILE *file = OpenRead (path); FILE *file = OpenRead (path);
if (!file) return false; 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); fclose (file);
return false; return false;
}
}
if ((png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)) == NULL) { if ((png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)) == NULL) {
fclose (file); fclose (file);
return false; return false;
@@ -73,19 +73,19 @@ namespace lime {
png_set_strip_16 (png_ptr); png_set_strip_16 (png_ptr);
const unsigned int stride = width * 4; const unsigned int stride = width * 4;
imageData->width = width; image->width = width;
imageData->height = height; image->height = height;
imageData->data = new ByteArray (height * stride); image->data = new ByteArray (height * stride);
png_bytepp row_ptrs = new png_bytep[height]; 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++) { for (size_t i = 0; i < height; i++) {
row_ptrs[i] = bytes + i * stride; row_ptrs[i] = bytes + i * stride;
} }
png_read_image (png_ptr, row_ptrs); png_read_image (png_ptr, row_ptrs);
png_read_end (png_ptr, NULL); 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; return true;
/*png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, user_error_fn, user_warning_fn); /*png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, user_error_fn, user_warning_fn);
if (!png_ptr) if (!png_ptr)
return false; return false;
png_infop info_ptr = png_create_info_struct(png_ptr); png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) if (!info_ptr)
return false; return false;
if (setjmp(png_jmpbuf(png_ptr))) if (setjmp(png_jmpbuf(png_ptr)))
{ {
png_destroy_write_struct(&png_ptr, &info_ptr ); png_destroy_write_struct(&png_ptr, &info_ptr );
return false; return false;
} }
QuickVec<uint8> out_buffer; QuickVec<uint8> out_buffer;
png_set_write_fn(png_ptr, &out_buffer, user_write_data, user_flush_data); png_set_write_fn(png_ptr, &out_buffer, user_write_data, user_flush_data);
int w = inSurface->Width(); int w = inSurface->Width();
int h = inSurface->Height(); int h = inSurface->Height();
int bit_depth = 8; int bit_depth = 8;
int color_type = (inSurface->Format()&pfHasAlpha) ? int color_type = (inSurface->Format()&pfHasAlpha) ?
PNG_COLOR_TYPE_RGB_ALPHA : PNG_COLOR_TYPE_RGB_ALPHA :
@@ -130,11 +130,11 @@ namespace lime {
png_set_IHDR(png_ptr, info_ptr, w, h, png_set_IHDR(png_ptr, info_ptr, w, h,
bit_depth, color_type, PNG_INTERLACE_NONE, bit_depth, color_type, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(png_ptr, info_ptr); png_write_info(png_ptr, info_ptr);
bool do_alpha = color_type==PNG_COLOR_TYPE_RGBA; bool do_alpha = color_type==PNG_COLOR_TYPE_RGBA;
{ {
QuickVec<uint8> row_data(w*4); QuickVec<uint8> row_data(w*4);
png_bytep row = &row_data[0]; png_bytep row = &row_data[0];
@@ -156,12 +156,13 @@ namespace lime {
png_write_rows(png_ptr, &row, 1); png_write_rows(png_ptr, &row, 1);
} }
} }
png_write_end(png_ptr, NULL); png_write_end(png_ptr, NULL);
*outBytes = ByteArray(out_buffer); *outBytes = ByteArray(out_buffer);
return true;*/ return true;*/
} }

View File

@@ -217,7 +217,7 @@ class DefaultAssetLibrary extends AssetLibrary {
var image = Image.loadFromFile (path.get (id)); var image = Image.loadFromFile (path.get (id));
if (image == null) return null; if (image == null) return null;
return new Image(image.bytes, image.width, image.height); return new Image (image.data, image.width, image.height);
#end #end