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 () {
@@ -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;
@@ -190,9 +190,7 @@ 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

View File

@@ -48,7 +48,7 @@
<section if="LIME_OPENGL">
<file name="src/graphics/opengl/OpenGLBindings.cpp" />
<file name="src/graphics/OpenGLBindings.cpp" />
</section>
@@ -76,7 +76,7 @@
<file name="src/system/ios/System.mm" if="ios" />
<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/PNG.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

@@ -4,14 +4,15 @@
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);
};

View File

@@ -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);
};

View File

@@ -10,7 +10,7 @@
#include <hx/CFFI.h>
#include <app/Application.h>
#include <app/UpdateEvent.h>
#include <graphics/ImageData.h>
#include <graphics/Image.h>
#include <graphics/PNG.h>
#include <graphics/JPEG.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) {
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);

View File

@@ -1,22 +1,32 @@
#include <graphics/ImageData.h>
#include <graphics/Image.h>
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() {
Image::Image () {
width = 0;
height = 0;
data = 0;
}
Image::~Image () {
delete data;
}
value ImageData::Value() {
value Image::Value() {
if (!init) {
@@ -35,4 +45,5 @@ namespace lime {
}
}

View File

@@ -6,7 +6,7 @@ extern "C" {
}
#include <graphics/ImageData.h>
#include <graphics/Image.h>
#include <graphics/JPEG.h>
#include <utils/ByteArray.h>
@@ -14,7 +14,7 @@ extern "C" {
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;
@@ -28,13 +28,13 @@ namespace lime {
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);
image->data = new ByteArray (image->width * image->height * 4);
unsigned char *bytes = imageData->data->Bytes();
unsigned char *scanline = new unsigned char [imageData->width * imageData->height * components];
unsigned char *bytes = image->data->Bytes ();
unsigned char *scanline = new unsigned char [image->width * image->height * components];
while (cinfo.output_scanline < cinfo.output_height) {
@@ -42,7 +42,7 @@ namespace lime {
// 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) {
@@ -58,12 +58,15 @@ namespace lime {
delete[] scanline;
jpeg_finish_decompress (&cinfo);
}
fclose (file);
jpeg_destroy_decompress (&cinfo);
return true;
}
}

View File

@@ -5,7 +5,7 @@ extern "C" {
}
#include <graphics/ImageData.h>
#include <graphics/Image.h>
#include <graphics/PNG.h>
#include <utils/ByteArray.h>
@@ -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;
@@ -25,13 +25,13 @@ namespace lime {
if (!file) return false;
// verify the PNG signature
int read = fread(png_sig, PNG_SIG_SIZE, 1, file);
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) {
@@ -73,12 +73,12 @@ 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++) {
@@ -97,7 +97,7 @@ namespace lime {
}
static bool Encode (ImageData *imageData, ByteArray *bytes) {
static bool Encode (Image *image, ByteArray *bytes) {
return true;
@@ -162,6 +162,7 @@ namespace lime {
*outBytes = ByteArray(out_buffer);
return true;*/
}

View File

@@ -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