Begin to stub in support for image data

This commit is contained in:
Joshua Granick
2014-06-13 10:58:15 -07:00
parent bd9d9902cd
commit 714357cbc0
14 changed files with 442 additions and 22 deletions

View File

@@ -2,6 +2,9 @@ package lime;
import haxe.Unserializer;
import lime.graphics.ImageData;
import lime.graphics.JPG;
import lime.graphics.PNG;
import lime.utils.ByteArray;
@@ -10,8 +13,8 @@ import lime.utils.ByteArray;
* embedded images, fonts, sounds and other resource files.</p>
*
* <p>The contents are populated automatically when an application
* is compiled using the OpenFL command-line tools, based on the
* contents of the *.nmml project file.</p>
* is compiled using the Lime command-line tools, based on the
* contents of the *.xml project file.</p>
*
* <p>For most platforms, the assets are included in the same directory
* or package as the application, and the paths are handled
@@ -37,20 +40,6 @@ class Assets {
var assetType = Assets.type.get (id);
#if pixi
if (assetType == IMAGE) {
return true;
} else {
return false;
}
#end
if (assetType != null) {
if (assetType == type || ((type == SOUND || type == MUSIC) && (assetType == MUSIC || assetType == SOUND))) {
@@ -108,7 +97,7 @@ class Assets {
//return cast (Type.createInstance (className.get (id), []), ByteArray);
#elseif (js || openfl_html5 || pixi)
#elseif js
var bytes:ByteArray = null;
/*var data = ApplicationMain.urlLoaders.get (path.get (id)).data;
@@ -152,6 +141,49 @@ class Assets {
}
public static function getImageData (id:String):ImageData {
initialize ();
#if (tools && !display)
#if (flash)
//return cast (Type.createInstance (className.get (id), []), ByteArray);
#elseif js
return null;
#else
var bytes = getBytes (id);
if (bytes != null) {
if (JPG.isFormat (bytes)) {
return JPG.decode (bytes);
} else if (PNG.isFormat (bytes)) {
return PNG.decode (bytes);
}
return null;
}
#end
#end
return null;
}
/**
* Gets the file path (if available) for an asset
* @usage var path = Assets.getPath("image.jpg");
@@ -316,11 +348,7 @@ class Assets {
#if (tools && !display)
#if pixi
handler (getBytes (id));
#elseif (flash || js)
#if (flash || js)
if (path.exists (id)) {

View File

@@ -0,0 +1,23 @@
package lime.graphics;
import lime.utils.UInt8Array;
class ImageData {
public var data:UInt8Array;
public var height:Int;
public var width:Int;
public function new () {
}
}

42
lime/graphics/JPG.hx Normal file
View File

@@ -0,0 +1,42 @@
package lime.graphics;
import lime.utils.ByteArray;
class JPG {
public static function encode (imageData:ImageData):ByteArray {
return null;
}
public static function decode (bytes:ByteArray):ImageData {
return null;
}
public static function isFormat (bytes:ByteArray):Bool {
if (bytes != null) {
var cachePosition = bytes.position;
bytes.position = 0;
var result = (bytes.readByte () == 0xFF && bytes.readByte () == 0xD8);
bytes.position = cachePosition;
return result;
}
return false;
}
}

42
lime/graphics/PNG.hx Normal file
View File

@@ -0,0 +1,42 @@
package lime.graphics;
import lime.utils.ByteArray;
class PNG {
public static function encode (imageData:ImageData):ByteArray {
return null;
}
public static function decode (bytes:ByteArray):ImageData {
return null;
}
public static function isFormat (bytes:ByteArray):Bool {
if (bytes != null) {
var cachePosition = bytes.position;
bytes.position = 0;
var result = (bytes.readByte () == 0x89 && bytes.readByte () == 0x50 && bytes.readByte () == 0x4E && bytes.readByte () == 0x47 && bytes.readByte () == 0x0D && bytes.readByte () == 0x0A && bytes.readByte () == 0x1A && bytes.readByte () == 0x0A);
bytes.position = cachePosition;
return result;
}
return false;
}
}

View File

@@ -49,6 +49,7 @@
</section>
<file name="src/app/UpdateEvent.cpp" />
<file name="src/graphics/PNG.cpp" />
<file name="src/graphics/RenderEvent.cpp" />
<file name="src/ui/KeyEvent.cpp" />
<file name="src/ui/MouseEvent.cpp" />

View File

@@ -0,0 +1,20 @@
#ifndef LIME_GRAPHICS_IMAGE_DATA_H
#define LIME_GRAPHICS_IMAGE_DATA_H
namespace lime {
struct ImageData {
int width;
int height;
unsigned char data;
};
}
#endif

View File

@@ -0,0 +1,26 @@
#ifndef LIME_GRAPHICS_PNG_H
#define LIME_GRAPHICS_PNG_H
#include <graphics/ImageData.h>
#include <utils/ByteArray.h>
namespace lime {
class PNG {
public:
static void Decode (ByteArray bytes, value imageData);
};
}
#endif

View File

@@ -10,6 +10,7 @@
#include <hx/CFFI.h>
#include <app/Application.h>
#include <app/UpdateEvent.h>
#include <graphics/PNG.h>
#include <graphics/Renderer.h>
#include <graphics/RenderEvent.h>
#include <ui/KeyEvent.h>
@@ -46,6 +47,13 @@ namespace lime {
}
value lime_jpg_decode (value bytes, value imageData) {
return alloc_null ();
}
value lime_key_event_manager_register (value callback, value eventObject) {
KeyEvent::callback = new AutoGCRoot (callback);
@@ -90,6 +98,16 @@ namespace lime {
}
value lime_png_decode (value bytes, value imageData) {
ByteArray byteArray (bytes);
PNG::Decode (bytes, imageData);
return alloc_null ();
}
value lime_render_event_manager_register (value callback, value eventObject) {
RenderEvent::callback = new AutoGCRoot (callback);
@@ -153,10 +171,12 @@ namespace lime {
DEFINE_PRIM (lime_application_create, 1);
DEFINE_PRIM (lime_application_exec, 1);
DEFINE_PRIM (lime_application_get_ticks, 0);
DEFINE_PRIM (lime_jpg_decode, 2);
DEFINE_PRIM (lime_key_event_manager_register, 2);
DEFINE_PRIM (lime_lzma_encode, 1);
DEFINE_PRIM (lime_lzma_decode, 1);
DEFINE_PRIM (lime_mouse_event_manager_register, 2);
DEFINE_PRIM (lime_png_decode, 2);
DEFINE_PRIM (lime_renderer_create, 1);
DEFINE_PRIM (lime_renderer_flip, 1);
DEFINE_PRIM (lime_render_event_manager_register, 2);

View File

@@ -29,6 +29,7 @@ namespace lime {
id_deltaTime = val_id ("deltaTime");
//id_type = val_id ("type");
init = true;
}

View File

@@ -0,0 +1,213 @@
extern "C" {
#include <png.h>
}
#include <hx/CFFI.h>
#include <graphics/PNG.h>
#include <setjmp.h>
namespace lime {
static int id_data;
static int id_height;
static int id_width;
static bool init = false;
/*static void user_error_fn (png_structp png_ptr, png_const_charp error_msg) {
longjmp (png_ptr->jmpbuf, 1);
}
static void user_warning_fn (png_structp png_ptr, png_const_charp warning_msg) { }
static void user_read_data_fn (png_structp png_ptr, png_bytep data, png_size_t length) {
png_voidp buffer = png_get_io_ptr (png_ptr);
((ReadBuf *)buffer)->Read (data, length);
}
void user_write_data (png_structp png_ptr, png_bytep data, png_size_t length) {
QuickVec<unsigned char> *buffer = (QuickVec<unsigned char> *)png_get_io_ptr (png_ptr);
buffer->append ((unsigned char *)data, (int)length);
}
void user_flush_data (png_structp png_ptr) { }*/
void PNG::Decode (ByteArray bytes, value imageData) {
/*if (!init) {
id_data = val_id ("data");
id_height = val_id ("height");
id_width = val_id ("width");
init = true;
}
png_structp png_ptr;
png_infop info_ptr;
png_uint_32 width, height;
int bit_depth, color_type, interlace_type;
png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, 0, user_error_fn, user_warning_fn);
if (png_ptr == NULL)
return;
info_ptr = png_create_info_struct (png_ptr);
if (info_ptr == NULL) {
png_destroy_read_struct (&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
return;
}
unsigned char* bytes[width * height * 4];
RenderTarget target;
if (setjmp (png_jmpbuf (png_ptr))) {
if (bytes) {
delete bytes;
}
png_destroy_read_struct (&png_ptr, &info_ptr, (png_infopp)NULL);
return;
}
ReadBuf buffer (inData, inDataLen);
//if (inFile) {
//png_init_io (png_ptr, inFile);
//} else {
png_set_read_fn (png_ptr, (void *)&buffer, user_read_data_fn);
//}
png_read_info (png_ptr, info_ptr);
png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, NULL, NULL);
bool has_alpha = (color_type == PNG_COLOR_TYPE_GRAY_ALPHA || color_type == PNG_COLOR_TYPE_RGB_ALPHA || png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS));
png_set_expand (png_ptr);
png_set_filler (png_ptr, 0xff, PNG_FILLER_AFTER);
png_set_palette_to_rgb (png_ptr);
png_set_gray_to_rgb (png_ptr);
if (bit_depth == 16)
png_set_strip_16 (png_ptr);
png_set_bgr (png_ptr);
//result = new ImageData ();
//result.width = width;
//result.height = height;
//result.data = uint8[width * height * 4];
png_read_png (png_ptr, (png_bytepp)&bytes);
png_read_end (png_ptr, info_ptr);
png_destroy_read_struct (&png_ptr, &info_ptr, (png_infopp)NULL);
value object = (KeyEvent::eventObject ? KeyEvent::eventObject->get () : alloc_empty_object ());
alloc_field (object, id_code, alloc_int (event->code));
alloc_field (object, id_type, alloc_int (event->type));
alloc_field (imageData, id_width, alloc_int (width));
alloc_field (imageData, id_height, alloc_int (height));
alloc_field (imageData, id_data, alloc_int (bytes));
return result;*/
}
static bool Encode (ImageData *imageData, 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 :
PNG_COLOR_TYPE_RGB;
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];
for(int y=0;y<h;y++)
{
uint8 *buf = &row_data[0];
const uint8 *src = (const uint8 *)inSurface->Row(y);
for(int x=0;x<w;x++)
{
buf[0] = src[2];
buf[1] = src[1];
buf[2] = src[0];
src+=3;
buf+=3;
if (do_alpha)
*buf++ = *src;
src++;
}
png_write_rows(png_ptr, &row, 1);
}
}
png_write_end(png_ptr, NULL);
*outBytes = ByteArray(out_buffer);
return true;*/
}
}

View File

@@ -29,6 +29,7 @@ namespace lime {
id_code = val_id ("code");
id_type = val_id ("type");
init = true;
}

View File

@@ -35,6 +35,7 @@ namespace lime {
id_type = val_id ("type");
id_x = val_id ("x");
id_y = val_id ("y");
init = true;
}

View File

@@ -35,6 +35,7 @@ namespace lime {
id_type = val_id ("type");
id_x = val_id ("x");
id_y = val_id ("y");
init = true;
}

View File

@@ -26,6 +26,7 @@ namespace lime {
if (!init) {
id_type = val_id ("type");
init = true;
}