BitmapData transparency code
Hello,
I implemented transparency in the BitmapData class and added some clipping. Can this be added to the official version? I don't know how to submit code or make a diff file, so I'm just pasting the code below.
Regards,
Mike
/**
* Copyright (c) 2010, Jeash contributors.
*
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package jeash.display;
import Html5Dom;
import flash.Lib;
import flash.geom.Point;
import flash.geom.
import flash.utils.
import flash.display.
import flash.display.
import flash.display.
import flash.display.
import flash.events.Event;
import flash.geom.Matrix;
import flash.geom.
import flash.filters.
import haxe.xml.Check;
typedef LoadData =
{
var image : HTMLImageElement;
var texture:
var inLoader:
var bitmapData:
}
class BitmapData implements IBitmapDrawable
{
private var mTextureBuffer:
private var mTransparent:Bool;
public var width(getWidth,
public var height(
public var graphics(
public function new(inWidth:Int, inHeight:Int,
?inTranspare
?inFillColou
{
// Load embedded images in the HTML file
var image : Dynamic = js.Lib.
if ( image != null ) {
mTextureBuffer = cast js.Lib.
var data : LoadData = {image:image, texture: mTextureBuffer, inLoader:null, bitmapData:this};
if (!image.complete)
image.
else
OnLoad(data, null);
} else {
mTextureBuffer = cast js.Lib.
mTextureBuff
mTextureBuff
mTransparent = inTransparent;
if ( inFillColour != null )
{
if (!mTransparent)
inFillColour |= 0xFF000000;
var rect = new Rectangle(
fillRect(
var imgdata = mTextureBuffer.
}
}
}
public var rect : Rectangle;
public function applyFilter(
{
throw "BitmapData.
}
public function draw( source:
matrix:Matrix = null,
colorTransfo
blendMode:String = null,
clipRect:
smoothing:Bool = false ):Void
{
source.
}
public function getColorBoundsRect( a:Int, b:Int, c:Bool ) : Rectangle {
return new Rectangle();
}
public function dispose() : Void {
}
public function compare ( inBitmapTexture : BitmapData ) : Int {
throw "Not implemented. compare";
return 0x00000000;
}
public function copyPixels(
?alphaBitmap
{
if (sourceBitmapDa
return;
var ctx : CanvasRendering
ctx.drawImage
}
private function clipRect (r: Rectangle): Rectangle
{
if (r.x < 0)
{
r.width -= -r.x;
r.x = 0;
if (r.x + r.width <= 0)
return null;
}
if (r.y < 0)
{
r.height -= -r.y;
r.y = 0;
if (r.y + r.height <= 0)
return null;
}
if (r.x + r.width >= getWidth ())
{
r.width -= r.x + r.width - getWidth ();
if (r.width <= 0)
return null;
}
if (r.y + r.height >= getHeight ())
{
r.height -= r.y + r.height - getHeight ();
if (r.height <= 0)
return null;
}
return r;
}
public function fillRect(rect: Rectangle, color: UInt) : Void
{
rect = clipRect (rect);
if (rect == null) return;
var r: Int = (color & 0xFF0000) >>> 16;
var g: Int = (color & 0x00FF00) >>> 8;
var b: Int = (color & 0x0000FF);
var a: Int = (mTransparent)? (color >>> 24) : 0xFF;
var ctx: CanvasRendering
var imagedata = ctx.getImageData (rect.x, rect.y, rect.width, rect.height);
for (i in 0...imagedata.
{
imagedata.data[i * 4] = r;
imagedata.data[i * 4 + 1] = g;
imagedata.data[i * 4 + 2] = b;
imagedata.data[i * 4 + 3] = a;
}
ctx.putImageData (imagedata, rect.x, rect.y);
}
public function getPixels(
{
var byteArray = new ByteArray();
rect = clipRect (rect);
if (rect == null) return byteArray;
var bytes = haxe.io.
var ctx : CanvasRendering
var imagedata = ctx.getImageDat
for (i in 0...imagedata.
bytes.set(i, imagedata.data[i]);
}
for ( i in 0...bytes.length )
byteArray.
return byteArray;
}
public function getPixel(x:Int, y:Int) : UInt
{
if (x < 0 || y < 0 || x >= getWidth () || y >= getHeight ()) return 0;
var ctx : CanvasRendering
var imagedata = ctx.getImageData(x, y, 1, 1);
return (imagedata.data[0] << 16) | (imagedata.data[1] << 8) | (imagedata.
}
public function getPixel32(x:Int, y:Int)
{
if (x < 0 || y < 0 || x >= getWidth () || y >= getHeight ()) return 0;
var ctx : CanvasRendering
var imagedata = ctx.getImageData(x, y, 1, 1);
return (imagedata.data[3] << 24) | (imagedata.data[0] << 16) | imagedata.data[1] << 8 | imagedata.data[2];
}
public function setPixel(x:Int, y:Int, color:UInt)
{
if (x < 0 || y < 0 || x >= getWidth () || y >= getHeight ()) return;
var ctx : CanvasRendering
var imageData = ctx.createImage
imageData.data[0] = (color & 0xFF0000) >>> 16;
imageData.data[1] = (color & 0x00FF00) >>> 8;
imageData.data[2] = (color & 0x0000FF) ;
imageData.data[3] = 0xFF;
ctx.putImageD
}
public function setPixel32(x:Int, y:Int, color:UInt)
{
if (x < 0 || y < 0 || x >= getWidth () || y >= getHeight ()) return;
var ctx : CanvasRendering
var imageData = ctx.createImage
imageData.data[0] = (color & 0xFF0000) >>> 16;
imageData.data[1] = (color & 0x00FF00) >>> 8;
imageData.data[2] = (color & 0x0000FF) ;
if (mTransparent)
imageData.
else
imageData.
ctx.putImageD
}
public function clone() : BitmapData {
return this;
}
public function getGraphics() : Graphics
{
if (graphics==null)
graphics = new Graphics(
return graphics;
}
public inline function handle()
{
return mTextureBuffer;
}
public function getWidth() : Int {
if ( mTextureBuffer != null ) {
return mTextureBuffer.
} else {
return 0;
}
}
public function getHeight() : Int {
if ( mTextureBuffer != null ) {
return mTextureBuffer.
} else {
return 0;
}
}
public function destroy()
{
mTextureBuffer = null;
}
function OnLoad( data:LoadData, e)
{
var canvas : HTMLCanvasElement = cast data.texture;
var width = (jeash.
var height = (jeash.
canvas.width = width;
canvas.height = height;
var ctx : CanvasRendering
ctx.drawImage
data.
data.
if (data.inLoader != null)
{
data.
data.
var e = new flash.events.Event( flash.events.
e.target = data.inLoader;
data.
}
}
public function LoadFromFile(
{
var image : HTMLImageElement = cast js.Lib.
if ( inLoader != null )
{
var data : LoadData = {image:image, texture: mTextureBuffer, inLoader:inLoader, bitmapData:this};
image.
}
image.src = inFilename;
}
static public function CreateFromHandl
{
var result = new BitmapData(0,0);
result.
return result;
}
public function lock() : Void
{
}
public function unlock(?changeRect : flash.geom.
{
}
// IBitmapDrawable inferface...
public function drawToSurface(
matrix:
colorTransfo
blendMode: String,
clipRect:
smothing:
{
var ctx : CanvasRendering
ctx.save();
if (matrix != null) {
ctx.
}
ctx.drawImage
ctx.restore();
}
}
Question information
- Language:
- English Edit question
- Status:
- Answered
- For:
- jeash Edit question
- Assignee:
- No assignee Edit question
- Last query:
- Last reply:
Can you help with this problem?
Provide an answer of your own, or ask Mike Wiering for more information if necessary.