diff --git a/lime/Assets.hx b/lime/Assets.hx index f56250948..dca6c0388 100644 --- a/lime/Assets.hx +++ b/lime/Assets.hx @@ -5,7 +5,7 @@ package lime; import haxe.Unserializer; import lime.graphics.Font; import lime.media.AudioBuffer; -import lime.media.ImageBuffer; +import lime.media.Image; import lime.utils.ByteArray; @@ -189,7 +189,7 @@ class Assets { * @param useCache (Optional) Whether to use BitmapData from the cache(Default: true) * @return A new BitmapData object */ - public static function getImageBuffer (id:String, useCache:Bool = true):ImageBuffer { + public static function getImage (id:String, useCache:Bool = true):Image { initialize (); @@ -217,7 +217,7 @@ class Assets { if (library.isLocal (symbolName, cast AssetType.IMAGE)) { - var image = library.getImageBuffer (symbolName); + var image = library.getImage (symbolName); if (useCache && cache.enabled) { @@ -500,7 +500,7 @@ class Assets { } - private static function isValidImage (buffer:ImageBuffer):Bool { + private static function isValidImage (buffer:Image):Bool { #if (tools && !display) #if (cpp || neko) @@ -651,7 +651,7 @@ class Assets { } - public static function loadImageBuffer (id:String, handler:ImageBuffer -> Void, useCache:Bool = true):Void { + public static function loadImage (id:String, handler:Image -> Void, useCache:Bool = true):Void { initialize (); @@ -680,7 +680,7 @@ class Assets { if (useCache && cache.enabled) { - library.loadImageBuffer (symbolName, function (image:ImageBuffer):Void { + library.loadImage (symbolName, function (image:Image):Void { cache.image.set (id, image); handler (image); @@ -689,7 +689,7 @@ class Assets { } else { - library.loadImageBuffer (symbolName, handler); + library.loadImage (symbolName, handler); } @@ -965,7 +965,7 @@ class AssetLibrary { } - public function getImageBuffer (id:String):ImageBuffer { + public function getImage (id:String):Image { return null; @@ -1046,9 +1046,9 @@ class AssetLibrary { } - public function loadImageBuffer (id:String, handler:ImageBuffer -> Void):Void { + public function loadImage (id:String, handler:Image -> Void):Void { - handler (getImageBuffer (id)); + handler (getImage (id)); } @@ -1097,7 +1097,7 @@ class AssetCache { public var audio:Map; public var enabled:Bool = true; - public var image:Map; + public var image:Map; public var font:Map; @@ -1105,7 +1105,7 @@ class AssetCache { audio = new Map (); font = new Map (); - image = new Map (); + image = new Map (); } @@ -1116,7 +1116,7 @@ class AssetCache { audio = new Map (); font = new Map (); - image = new Map (); + image = new Map (); } else { diff --git a/lime/graphics/Font.hx b/lime/graphics/Font.hx index 91beef149..dada8fb4f 100644 --- a/lime/graphics/Font.hx +++ b/lime/graphics/Font.hx @@ -21,7 +21,7 @@ typedef GlyphRect = { } typedef GlyphData = { - var image:ImageBuffer; + var image:Image; var glyphs:StringMap; }; @@ -135,7 +135,7 @@ class Font { image.src = __canvas.toDataURL(); return { glyphs: glyphRects, - image: ImageBuffer.fromImage (image) + image: Image.fromImage (image) } #elseif flash @@ -207,7 +207,7 @@ class Font { return { glyphs: glyphRects, - image: ImageBuffer.fromBitmapData (bd) + image: Image.fromBitmapData (bd) } #elseif (cpp || neko) @@ -236,7 +236,7 @@ class Font { return { glyphs: glyphRects, - image: new ImageBuffer (new UInt8Array (data.image.data), data.image.width, data.image.height, data.image.bpp) + image: new Image (new ImageBuffer (new UInt8Array (data.image.data), data.image.width, data.image.height, data.image.bpp)) }; } diff --git a/lime/media/Image.hx b/lime/media/Image.hx index 7c5e5430c..a7c4fe69a 100644 --- a/lime/media/Image.hx +++ b/lime/media/Image.hx @@ -2,6 +2,7 @@ package lime.media; import lime.app.Application; +import lime.graphics.RenderContext; import lime.graphics.Renderer; import lime.utils.ByteArray; import lime.utils.UInt8Array; @@ -10,6 +11,7 @@ import lime.system.System; #if js import js.html.CanvasElement; import js.html.CanvasRenderingContext2D; +import js.html.Image in HTMLImage; import js.Browser; #elseif flash import flash.display.BitmapData; @@ -27,27 +29,25 @@ class Image { #end public var buffer:ImageBuffer; + public var data (get, set):UInt8Array; public var height:Int; public var offsetX:Int; public var offsetY:Int; public var powerOfTwo (get, set):Bool; public var premultiplied (get, set):Bool; + public var src (get, set):Dynamic; public var width:Int; - private var __renderer:Renderer; + private var __type:StoreType; - public function new (buffer:ImageBuffer, renderer:Renderer = null) { + public function new (buffer:ImageBuffer, context:RenderContext = null) { this.buffer = buffer; - if (renderer == null) { + if (context == null) { - __renderer = Application.__instance.window.currentRenderer; - - } else { - - __renderer = renderer; + context = Application.__instance.window.currentRenderer.context; } @@ -56,37 +56,91 @@ class Image { offsetX = 0; offsetY = 0; - switch (__renderer.context) { + __type = switch (context) { - case OPENGL (_): - - #if js - if (buffer.data == null && buffer.src != null && buffer.width > 0 && buffer.height > 0) { - - if (__canvas == null) { - - __canvas = cast Browser.document.createElement ("canvas"); - __context = cast __canvas.getContext ("2d"); - - } - - __canvas.width = buffer.width; - __canvas.height = buffer.height; - __context.drawImage (buffer.src, 0, 0); - - var pixels = __context.getImageData (0, 0, buffer.width, buffer.height); - buffer.data = pixels.data; - - } - #end - - default: + case DOM (_), CANVAS (_): HTML5; + case FLASH (_): FLASH; + default: DATA; } } + #if flash + public static function fromBitmapData (bitmapData:BitmapData):Image { + + var buffer = new ImageBuffer (null, bitmapData.width, bitmapData.height); + buffer.src = bitmapData; + return new Image (buffer); + + } + #end + + + public static function fromBytes (bytes:ByteArray):Image { + + #if (cpp || neko) + + var data = lime_image_load (bytes); + + if (data != null) { + + var buffer = new ImageBuffer (new UInt8Array (data.data), data.width, data.height, data.bpp); + return new Image (buffer); + + } else { + + return null; + + } + + #else + + throw "ImageBuffer.loadFromFile not supported on this target"; + + #end + + } + + + public static function fromFile (path:String):Image { + + #if (cpp || neko) + + var data = lime_image_load (path); + + if (data != null) { + + var buffer = new ImageBuffer (new UInt8Array (data.data), data.width, data.height, data.bpp); + return new Image (buffer); + + } else { + + return null; + + } + + #else + + throw "ImageBuffer.loadFromFile not supported on this target"; + + #end + + } + + + #if js + public static function fromImage (image:HTMLImage):Image { + + var buffer = new ImageBuffer (null, image.width, image.height); + buffer.src = image; + return new Image (buffer); + + } + #end + + public function getPixel (x:Int, y:Int):Int { // TODO: cache context type? @@ -94,18 +148,18 @@ class Image { x += offsetX; y += offsetY; - switch (__renderer.context) { + switch (__type) { - case OPENGL (_): + case DATA: // TODO: handle premultiplied - var data = buffer.data; + var data = this.data; var index = (y * buffer.width + x) * 4; return ((data[index + 3] << 24) | (data[index] << 16 ) | (data[index + 1] << 8) | data[index + 2]); - case FLASH (_): + case FLASH: #if flash return buffer.src.getPixel (x, y); @@ -113,22 +167,12 @@ class Image { return 0; #end - case DOM (_): + case HTML5: // TODO return 0; - case CANVAS (_): - - // TODO - - return 0; - - default: - - return 0; - } @@ -140,13 +184,13 @@ class Image { x += offsetX; y += offsetY; - switch (__renderer.context) { + switch (__type) { - case OPENGL (_): + case DATA: // TODO: handle premultiplied - var data = buffer.data; + var data = this.data; var index = (y * buffer.width + x) * 4; data[index + 3] = (value >> 24) & 0xFF; @@ -154,22 +198,16 @@ class Image { data[index + 1] = (value >> 8) & 0xFF; data[index + 2] = value & 0xFF; - case FLASH (_): + case FLASH: #if flash buffer.src.setPixel (x, y, value); #end - case DOM (_): + case HTML5: // TODO - case CANVAS (_): - - // TODO - - default: - } } @@ -182,6 +220,47 @@ class Image { + private function get_data ():UInt8Array { + + if (buffer.data == null && buffer.src != null && buffer.width > 0 && buffer.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 (src, 0, 0); + + var pixels = __context.getImageData (0, 0, buffer.width, buffer.height); + buffer.data = new UInt8Array (pixels.data); + + #elseif flash + + var pixels = buffer.src.getPixels (buffer.src.rect); + buffer.data = new UInt8Array (pixels); + + #end + + } + + return buffer.data; + + } + + + private function set_data (value:UInt8Array):UInt8Array { + + return buffer.data = value; + + } + + private function get_powerOfTwo ():Bool { return ((buffer.width != 0) && ((buffer.width & (~buffer.width + 1)) == buffer.width)) && ((buffer.height != 0) && ((buffer.height & (~buffer.height + 1)) == buffer.height)); @@ -208,11 +287,11 @@ class Image { } - switch (__renderer.context) { + switch (__type) { - case OPENGL (_): + case DATA: - var data = buffer.data; + var data = this.data; var newData = new UInt8Array (newWidth * newHeight * 4); var sourceIndex:Int, index:Int; @@ -236,7 +315,7 @@ class Image { buffer.width = newWidth; buffer.height = newHeight; - case FLASH (_): + case FLASH: #if flash var bitmapData = new BitmapData (newWidth, newHeight, true, 0x000000); @@ -247,7 +326,7 @@ class Image { buffer.height = newHeight; #end - default: + case HTML5: // TODO @@ -271,9 +350,9 @@ class Image { if (value && !buffer.premultiplied) { - switch (__renderer.context) { + switch (__type) { - case OPENGL (_): + case DATA: var data = buffer.data; var index, a; @@ -309,4 +388,39 @@ class Image { } + public function get_src ():Dynamic { + + return buffer.src; + + } + + + private function set_src (value:Dynamic):Dynamic { + + return buffer.src = value; + + } + + + + + // Native Methods + + + + + #if (cpp || neko) + private static var lime_image_load:Dynamic = System.load ("lime", "lime_image_load", 1); + #end + + +} + + +private enum StoreType { + + DATA; + HTML5; + FLASH; + } \ No newline at end of file diff --git a/lime/media/ImageBuffer.hx b/lime/media/ImageBuffer.hx index ad3775329..9d4d91fdc 100644 --- a/lime/media/ImageBuffer.hx +++ b/lime/media/ImageBuffer.hx @@ -1,16 +1,8 @@ package lime.media; -import lime.system.System; -import lime.utils.ByteArray; import lime.utils.UInt8Array; -#if js -import js.html.Image in HTMLImage; -#elseif flash -import flash.display.BitmapData; -#end - class ImageBuffer { @@ -19,15 +11,8 @@ class ImageBuffer { public var data:UInt8Array; public var height:Int; public var premultiplied:Bool; - public var width:Int; - - #if js - public var src:HTMLImage; - #elseif flash - public var src:BitmapData; - #else public var src:Dynamic; - #end + public var width:Int; public function new (data:UInt8Array = null, width:Int = 0, height:Int = 0, bitsPerPixel:Int = 4) { @@ -40,81 +25,4 @@ class ImageBuffer { } - #if flash - public static function fromBitmapData (bitmapData:BitmapData):ImageBuffer { - - var buffer = new ImageBuffer (null, bitmapData.width, bitmapData.height); - buffer.src = bitmapData; - return buffer; - - } - #end - - - public static function fromBytes (bytes:ByteArray):ImageBuffer { - - #if (cpp || neko) - - var data = lime_image_load (bytes); - - if (data != null) { - - return new ImageBuffer (new UInt8Array (data.data), data.width, data.height, data.bpp); - - } else { - - return null; - - } - - #else - - throw "ImageBuffer.loadFromFile not supported on this target"; - - #end - - } - - - public static function fromFile (path:String):ImageBuffer { - - #if (cpp || neko) - - var data = lime_image_load (path); - - if (data != null) { - - return new ImageBuffer (new UInt8Array (data.data), data.width, data.height, data.bpp); - - } else { - - return null; - - } - - #else - - throw "ImageBuffer.loadFromFile not supported on this target"; - - #end - - } - - - #if js - public static function fromImage (image:HTMLImage):ImageBuffer { - - var buffer = new ImageBuffer (null, image.width, image.height); - buffer.src = image; - return buffer; - - } - #end - - - #if (cpp || neko) - private static var lime_image_load:Dynamic = System.load ("lime", "lime_image_load", 1); - #end - - } \ No newline at end of file diff --git a/samples/SimpleImage/Source/Main.hx b/samples/SimpleImage/Source/Main.hx index 89d3afe4d..3685d8b24 100644 --- a/samples/SimpleImage/Source/Main.hx +++ b/samples/SimpleImage/Source/Main.hx @@ -4,7 +4,7 @@ package; import lime.app.Application; import lime.graphics.opengl.*; import lime.graphics.RenderContext; -import lime.media.ImageBuffer; +import lime.media.Image; import lime.utils.Float32Array; import lime.utils.GLUtils; import lime.utils.Matrix4; @@ -15,7 +15,7 @@ class Main extends Application { private var buffer:GLBuffer; - private var image:ImageBuffer; + private var image:Image; private var matrixUniform:GLUniformLocation; private var program:GLProgram; private var texture:GLTexture; @@ -32,7 +32,7 @@ class Main extends Application { public override function init (context:RenderContext):Void { - image = Assets.getImageBuffer ("assets/lime.png"); + image = Assets.getImage ("assets/lime.png"); switch (context) { @@ -44,10 +44,8 @@ class Main extends Application { case DOM (element): - #if js element.style.backgroundColor = "#" + StringTools.hex (config.background, 6); element.appendChild (image.src); - #end case FLASH (sprite): @@ -119,7 +117,7 @@ class Main extends Application { #if js gl.texImage2D (gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image.src); #else - gl.texImage2D (gl.TEXTURE_2D, 0, gl.RGBA, image.width, image.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, image.data); + gl.texImage2D (gl.TEXTURE_2D, 0, gl.RGBA, image.buffer.width, image.buffer.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, image.data); #end gl.texParameteri (gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); gl.texParameteri (gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); diff --git a/templates/haxe/DefaultAssetLibrary.hx b/templates/haxe/DefaultAssetLibrary.hx index 6f4d77474..d1669cabd 100644 --- a/templates/haxe/DefaultAssetLibrary.hx +++ b/templates/haxe/DefaultAssetLibrary.hx @@ -5,7 +5,7 @@ import haxe.Timer; import haxe.Unserializer; import lime.app.Preloader; import lime.media.AudioBuffer; -import lime.media.ImageBuffer; +import lime.media.Image; import lime.media.openal.AL; import lime.utils.ByteArray; import lime.utils.UInt8Array; @@ -224,19 +224,19 @@ class DefaultAssetLibrary extends AssetLibrary { } - public override function getImageBuffer (id:String):ImageBuffer { + public override function getImage (id:String):Image { #if flash - return ImageBuffer.fromBitmapData (cast (Type.createInstance (className.get (id), []), BitmapData)); + return Image.fromBitmapData (cast (Type.createInstance (className.get (id), []), BitmapData)); #elseif js - return ImageBuffer.fromImage (Preloader.images.get (path.get (id))); + return Image.fromImage (Preloader.images.get (path.get (id))); #else - return ImageBuffer.fromFile (path.get (id)); + return Image.fromFile (path.get (id)); #end @@ -439,7 +439,7 @@ class DefaultAssetLibrary extends AssetLibrary { } - public override function loadImageBuffer (id:String, handler:ImageBuffer -> 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 (ImageBuffer.fromBitmapData (bitmapData)); + handler (Image.fromBitmapData (bitmapData)); }); loader.load (new URLRequest (path.get (id))); } else { - handler (getImageBuffer (id)); + handler (getImage (id)); } #else - handler (getImageBuffer (id)); + handler (getImage (id)); #end