Use 'Image' instead of 'ImageData', expose both 'data' and 'bytes' from the Image object
This commit is contained in:
@@ -3,7 +3,7 @@ package lime;
|
||||
|
||||
|
||||
import haxe.Unserializer;
|
||||
import lime.graphics.ImageData;
|
||||
import lime.graphics.Image;
|
||||
import lime.utils.ByteArray;
|
||||
|
||||
|
||||
@@ -118,19 +118,19 @@ class Assets {
|
||||
* @param useCache (Optional) Whether to use BitmapData from the cache(Default: true)
|
||||
* @return A new BitmapData object
|
||||
*/
|
||||
public static function getImageData (id:String, useCache:Bool = true):ImageData {
|
||||
public static function getImage (id:String, useCache:Bool = true):Image {
|
||||
|
||||
initialize ();
|
||||
|
||||
#if (tools && !display)
|
||||
|
||||
if (useCache && cache.enabled && cache.imageData.exists (id)) {
|
||||
if (useCache && cache.enabled && cache.image.exists (id)) {
|
||||
|
||||
var imageData = cache.imageData.get (id);
|
||||
var image = cache.image.get (id);
|
||||
|
||||
if (isValidImageData (imageData)) {
|
||||
if (isValidImage (image)) {
|
||||
|
||||
return imageData;
|
||||
return image;
|
||||
|
||||
}
|
||||
|
||||
@@ -146,25 +146,25 @@ class Assets {
|
||||
|
||||
if (library.isLocal (symbolName, cast AssetType.IMAGE)) {
|
||||
|
||||
var imageData = library.getImageData (symbolName);
|
||||
var image = library.getImage (symbolName);
|
||||
|
||||
if (useCache && cache.enabled) {
|
||||
|
||||
cache.imageData.set (id, imageData);
|
||||
cache.image.set (id, image);
|
||||
|
||||
}
|
||||
|
||||
return imageData;
|
||||
return image;
|
||||
|
||||
} else {
|
||||
|
||||
trace ("[Assets] ImageData asset \"" + id + "\" exists, but only asynchronously");
|
||||
trace ("[Assets] Image asset \"" + id + "\" exists, but only asynchronously");
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
trace ("[Assets] There is no ImageData asset with an ID of \"" + id + "\"");
|
||||
trace ("[Assets] There is no Image asset with an ID of \"" + id + "\"");
|
||||
|
||||
}
|
||||
|
||||
@@ -449,7 +449,7 @@ class Assets {
|
||||
|
||||
if (type == AssetType.IMAGE || type == null) {
|
||||
|
||||
if (cache.imageData.exists (id)) return true;
|
||||
if (cache.image.exists (id)) return true;
|
||||
|
||||
}
|
||||
|
||||
@@ -484,7 +484,7 @@ class Assets {
|
||||
}
|
||||
|
||||
|
||||
private static function isValidImageData (imageData:ImageData):Bool {
|
||||
private static function isValidImage (image:Image):Bool {
|
||||
|
||||
#if (tools && !display)
|
||||
#if (cpp || neko)
|
||||
@@ -494,15 +494,16 @@ class Assets {
|
||||
|
||||
#elseif flash
|
||||
|
||||
try {
|
||||
/*try {
|
||||
|
||||
imageData.data.width;
|
||||
image.bytes.width;
|
||||
|
||||
} catch (e:Dynamic) {
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
}*/
|
||||
return true;
|
||||
|
||||
#end
|
||||
#end
|
||||
@@ -587,19 +588,19 @@ class Assets {
|
||||
}
|
||||
|
||||
|
||||
public static function loadImageData (id:String, handler:ImageData -> Void, useCache:Bool = true):Void {
|
||||
public static function loadImage (id:String, handler:Image -> Void, useCache:Bool = true):Void {
|
||||
|
||||
initialize ();
|
||||
|
||||
#if (tools && !display)
|
||||
|
||||
if (useCache && cache.enabled && cache.imageData.exists (id)) {
|
||||
if (useCache && cache.enabled && cache.image.exists (id)) {
|
||||
|
||||
var imageData = cache.imageData.get (id);
|
||||
var image = cache.image.get (id);
|
||||
|
||||
if (isValidImageData (imageData)) {
|
||||
if (isValidImage (image)) {
|
||||
|
||||
handler (imageData);
|
||||
handler (image);
|
||||
return;
|
||||
|
||||
}
|
||||
@@ -616,16 +617,16 @@ class Assets {
|
||||
|
||||
if (useCache && cache.enabled) {
|
||||
|
||||
library.loadImageData (symbolName, function (imageData:ImageData):Void {
|
||||
library.loadImage (symbolName, function (image:Image):Void {
|
||||
|
||||
cache.imageData.set (id, imageData);
|
||||
handler (imageData);
|
||||
cache.image.set (id, image);
|
||||
handler (image);
|
||||
|
||||
});
|
||||
|
||||
} else {
|
||||
|
||||
library.loadImageData (symbolName, handler);
|
||||
library.loadImage (symbolName, handler);
|
||||
|
||||
}
|
||||
|
||||
@@ -633,7 +634,7 @@ class Assets {
|
||||
|
||||
} else {
|
||||
|
||||
trace ("[Assets] There is no ImageData asset with an ID of \"" + id + "\"");
|
||||
trace ("[Assets] There is no Image asset with an ID of \"" + id + "\"");
|
||||
|
||||
}
|
||||
|
||||
@@ -957,7 +958,7 @@ class AssetLibrary {
|
||||
}
|
||||
|
||||
|
||||
public function getImageData (id:String):ImageData {
|
||||
public function getImage (id:String):Image {
|
||||
|
||||
return null;
|
||||
|
||||
@@ -1038,9 +1039,9 @@ class AssetLibrary {
|
||||
}
|
||||
|
||||
|
||||
public function loadImageData (id:String, handler:ImageData -> Void):Void {
|
||||
public function loadImage (id:String, handler:Image -> Void):Void {
|
||||
|
||||
handler (getImageData (id));
|
||||
handler (getImage (id));
|
||||
|
||||
}
|
||||
|
||||
@@ -1095,7 +1096,7 @@ class AssetCache {
|
||||
|
||||
|
||||
public var enabled:Bool = true;
|
||||
public var imageData:Map<String, ImageData>;
|
||||
public var image:Map<String, Image>;
|
||||
//public var font:Map<String, Font>;
|
||||
public var sound:Map<String, Dynamic /*Sound*/>;
|
||||
|
||||
@@ -1103,7 +1104,7 @@ class AssetCache {
|
||||
public function new () {
|
||||
|
||||
//font = new Map<String, Font> ();
|
||||
imageData = new Map<String, ImageData> ();
|
||||
image = new Map<String, Image> ();
|
||||
sound = new Map<String, Dynamic /*Sound*/> ();
|
||||
|
||||
}
|
||||
@@ -1114,18 +1115,18 @@ class AssetCache {
|
||||
if (prefix == null) {
|
||||
|
||||
//font = new Map<String, Font> ();
|
||||
imageData = new Map<String, ImageData> ();
|
||||
image = new Map<String, Image> ();
|
||||
sound = new Map<String, Dynamic /*Sound*/> ();
|
||||
|
||||
} else {
|
||||
|
||||
var keys = imageData.keys ();
|
||||
var keys = image.keys ();
|
||||
|
||||
for (key in keys) {
|
||||
|
||||
if (StringTools.startsWith (key, prefix)) {
|
||||
|
||||
imageData.remove (key);
|
||||
image.remove (key);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -44,9 +44,9 @@ class CanvasRenderContext {
|
||||
public function clearShadow ():Void {};
|
||||
public function clip ():Void {};
|
||||
public function closePath ():Void {};
|
||||
@:overload(function(Dynamic /*ImageData*/:Dynamic /*ImageData*/):Dynamic /*ImageData*/ {})
|
||||
public function createDynamic /*ImageData*/(sw:Float, sh:Float):Dynamic /*ImageData*/ { return null; };
|
||||
public function createLinearGradient(x0:Float, y0:Float, x1:Float, y1:Float):Dynamic /*CanvasGradient*/ { return null; };
|
||||
@:overload(function(dynamicImageData:Dynamic /*ImageData*/):Dynamic /*ImageData*/ {})
|
||||
public function createDynamicImageData (sw:Float, sh:Float):Dynamic /*ImageData*/ { return null; };
|
||||
public function createLinearGradient (x0:Float, y0:Float, x1:Float, y1:Float):Dynamic /*CanvasGradient*/ { return null; };
|
||||
@:overload(function(canvas:Dynamic /*CanvasElement*/, repetitionType:String):Dynamic /*CanvasPattern*/ {})
|
||||
public function createPattern (image:Dynamic /*ImageElement*/, repetitionType:String):Dynamic /*CanvasPattern*/ { return null; };
|
||||
public function createRadialGradient (x0:Float, y0:Float, r0:Float, x1:Float, y1:Float, r1:Float):Dynamic /*CanvasGradient*/ { return null; };
|
||||
|
||||
104
lime/graphics/Image.hx
Normal file
104
lime/graphics/Image.hx
Normal file
@@ -0,0 +1,104 @@
|
||||
package lime.graphics;
|
||||
|
||||
|
||||
import lime.utils.UInt8Array;
|
||||
|
||||
#if js
|
||||
import js.html.CanvasElement;
|
||||
import js.html.CanvasRenderingContext2D;
|
||||
import js.Browser;
|
||||
#end
|
||||
|
||||
|
||||
class Image {
|
||||
|
||||
|
||||
#if js
|
||||
private static var __canvas:CanvasElement;
|
||||
private static var __context:CanvasRenderingContext2D;
|
||||
#end
|
||||
|
||||
#if (js || flash)
|
||||
public var bytes (get, set):UInt8Array;
|
||||
#else
|
||||
public var bytes:UInt8Array;
|
||||
#end
|
||||
|
||||
public var data:ImageData;
|
||||
public var height:Int;
|
||||
public var offsetX:Int;
|
||||
public var offsetY:Int;
|
||||
public var width:Int;
|
||||
|
||||
#if (js || flash)
|
||||
private var __bytes:UInt8Array;
|
||||
#end
|
||||
|
||||
|
||||
public function new (data:ImageData = null, width:Int = 0, height:Int = 0) {
|
||||
|
||||
this.data = data;
|
||||
#if (!js && !flash)
|
||||
this.bytes = data;
|
||||
#end
|
||||
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
|
||||
}
|
||||
|
||||
|
||||
#if (js || flash)
|
||||
private function get_bytes ():UInt8Array {
|
||||
|
||||
if (data != null && width > 0 && height > 0) {
|
||||
|
||||
#if js
|
||||
|
||||
if (__canvas == null) {
|
||||
|
||||
__canvas = cast Browser.document.createElement ("canvas");
|
||||
__context = cast __canvas.getContext ("2d");
|
||||
|
||||
}
|
||||
|
||||
__canvas.width = width;
|
||||
__canvas.height = height;
|
||||
__context.drawImage (data, 0, 0);
|
||||
js.Browser.document.body.appendChild (__canvas);
|
||||
|
||||
var pixels = __context.getImageData (0, 0, width, height);
|
||||
__bytes = new UInt8Array (pixels.data);
|
||||
|
||||
#elseif flash
|
||||
|
||||
var pixels = data.getPixels (data.rect);
|
||||
__bytes = new UInt8Array (pixels);
|
||||
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
return __bytes;
|
||||
|
||||
}
|
||||
|
||||
|
||||
private function set_bytes (value:UInt8Array):UInt8Array {
|
||||
|
||||
return __bytes = value;
|
||||
|
||||
}
|
||||
#end
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#if js
|
||||
typedef ImageData = js.html.Image;
|
||||
#elseif flash
|
||||
typedef ImageData = flash.display.BitmapData;
|
||||
#else
|
||||
typedef ImageData = UInt8Array;
|
||||
#end
|
||||
@@ -1,31 +0,0 @@
|
||||
package lime.graphics;
|
||||
|
||||
|
||||
class ImageData {
|
||||
|
||||
|
||||
public var data:ImageDataType;
|
||||
public var height:Int;
|
||||
public var width:Int;
|
||||
|
||||
|
||||
public function new (data:ImageDataType = null, width:Int = 0, height:Int = 0) {
|
||||
|
||||
this.data = data;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#if js
|
||||
typedef ImageDataType = js.html.Image;
|
||||
#elseif flash
|
||||
typedef ImageDataType = flash.display.BitmapData;
|
||||
#else
|
||||
typedef ImageDataType = lime.utils.UInt8Array;
|
||||
#end
|
||||
@@ -7,14 +7,14 @@ import lime.utils.ByteArray;
|
||||
class JPG {
|
||||
|
||||
|
||||
public static function encode (imageData:ImageData):ByteArray {
|
||||
public static function encode (image:Image):ByteArray {
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static function decode (bytes:ByteArray):ImageData {
|
||||
public static function decode (bytes:ByteArray):Image {
|
||||
|
||||
return null;
|
||||
|
||||
|
||||
@@ -7,14 +7,14 @@ import lime.utils.ByteArray;
|
||||
class PNG {
|
||||
|
||||
|
||||
public static function encode (imageData:ImageData):ByteArray {
|
||||
public static function encode (image:Image):ByteArray {
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static function decode (bytes:ByteArray):ImageData {
|
||||
public static function decode (bytes:ByteArray):Image {
|
||||
|
||||
return null;
|
||||
|
||||
|
||||
@@ -143,7 +143,7 @@ class URLLoader {
|
||||
|
||||
switch (dataFormat) {
|
||||
|
||||
//case BINARY: uri = data.__getBuffer ();
|
||||
case BINARY: uri = data.__getBuffer ();
|
||||
default: uri = data.readUTFBytes (data.length);
|
||||
|
||||
}
|
||||
@@ -244,7 +244,7 @@ class URLLoader {
|
||||
|
||||
switch (dataFormat) {
|
||||
|
||||
//case BINARY: this.data = ByteArray.__ofBuffer (content);
|
||||
case BINARY: this.data = ByteArray.__ofBuffer (content);
|
||||
default: this.data = Std.string (content);
|
||||
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import lime.app.Application;
|
||||
import lime.graphics.opengl.GLBuffer;
|
||||
import lime.graphics.opengl.GLProgram;
|
||||
import lime.graphics.opengl.GLTexture;
|
||||
import lime.graphics.ImageData;
|
||||
import lime.graphics.Image;
|
||||
import lime.graphics.GLRenderContext;
|
||||
import lime.graphics.RenderContext;
|
||||
import lime.utils.Float32Array;
|
||||
@@ -18,7 +18,7 @@ import lime.Assets;
|
||||
class Main extends Application {
|
||||
|
||||
|
||||
private var image:ImageData;
|
||||
private var image:Image;
|
||||
private var initialized:Bool;
|
||||
|
||||
private var shaderProgram:GLProgram;
|
||||
|
||||
@@ -4,7 +4,7 @@ package;
|
||||
import haxe.Timer;
|
||||
import haxe.Unserializer;
|
||||
import lime.app.Preloader;
|
||||
import lime.graphics.ImageData;
|
||||
import lime.graphics.Image;
|
||||
import lime.utils.ByteArray;
|
||||
import lime.utils.UInt8Array;
|
||||
import lime.Assets;
|
||||
@@ -197,17 +197,17 @@ class DefaultAssetLibrary extends AssetLibrary {
|
||||
}
|
||||
|
||||
|
||||
public override function getImageData (id:String):ImageData {
|
||||
public override function getImage (id:String):Image {
|
||||
|
||||
#if flash
|
||||
|
||||
var bitmapData = cast (Type.createInstance (className.get (id), []), BitmapData);
|
||||
return new ImageData (bitmapData, bitmapData.width, bitmapData.height);
|
||||
return new Image (bitmapData, bitmapData.width, bitmapData.height);
|
||||
|
||||
#elseif js
|
||||
|
||||
var image = Preloader.images.get (path.get (id));
|
||||
return new ImageData (image, image.width, image.height);
|
||||
return new Image (image, image.width, image.height);
|
||||
|
||||
#else
|
||||
|
||||
@@ -240,7 +240,7 @@ class DefaultAssetLibrary extends AssetLibrary {
|
||||
|
||||
}
|
||||
|
||||
return new ImageData (imageData, imageWidth, imageHeight);
|
||||
return new Image (imageData, imageWidth, imageHeight);
|
||||
|
||||
//if (className.exists(id)) return cast (Type.createInstance (className.get (id), []), BitmapData);
|
||||
//else return BitmapData.load (path.get (id));
|
||||
@@ -439,7 +439,7 @@ class DefaultAssetLibrary extends AssetLibrary {
|
||||
}
|
||||
|
||||
|
||||
public override function loadImageData (id:String, handler:ImageData -> Void):Void {
|
||||
public override function loadImage (id:String, handler:Image -> Void):Void {
|
||||
|
||||
#if flash
|
||||
|
||||
@@ -449,20 +449,20 @@ class DefaultAssetLibrary extends AssetLibrary {
|
||||
loader.contentLoaderInfo.addEventListener (Event.COMPLETE, function (event:Event) {
|
||||
|
||||
var bitmapData = cast (event.currentTarget.content, Bitmap).bitmapData;
|
||||
handler (new ImageData (bitmapData, bitmapData.width, bitmapData.height));
|
||||
handler (new Image (bitmapData.width, bitmapData.height, new UInt8Array (bitmapData.getPixels (bitmapData.rect))));
|
||||
|
||||
});
|
||||
loader.load (new URLRequest (path.get (id)));
|
||||
|
||||
} else {
|
||||
|
||||
handler (getImageData (id));
|
||||
handler (getImage (id));
|
||||
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
handler (getImageData (id));
|
||||
handler (getImage (id));
|
||||
|
||||
#end
|
||||
|
||||
@@ -501,7 +501,7 @@ class DefaultAssetLibrary extends AssetLibrary {
|
||||
if (!className.exists (asset.id)) {
|
||||
|
||||
path.set (asset.id, asset.path);
|
||||
type.set (asset.id, Type.createEnum (AssetType, asset.type));
|
||||
type.set (asset.id, cast (asset.type, AssetType));
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user