Minor refactor
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -1,23 +1,33 @@
|
||||
#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() {
|
||||
|
||||
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;
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -6,15 +6,15 @@ extern "C" {
|
||||
|
||||
}
|
||||
|
||||
#include <graphics/ImageData.h>
|
||||
#include <graphics/Image.h>
|
||||
#include <graphics/JPEG.h>
|
||||
#include <utils/ByteArray.h>
|
||||
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
@@ -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<uint8> 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<uint8> 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;*/
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user