/**
* Copyright actionscriptbible ( http://wonderfl.net/user/actionscriptbible )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/gQTW
*/
package {
import flash.display.*;
import flash.events.Event;
import flash.net.URLRequest;
import flash.system.LoaderContext;
import flash.utils.ByteArray;
[SWF(backgroundColor="#000000")]
public class ch36ex7 extends Sprite {
public function ch36ex7() {
var l:Loader = new Loader();
l.load(
new URLRequest("http://actionscriptbible.com/files/testpattern.png"),
new LoaderContext(true)
);
l.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoad);
}
protected function onLoad(event:Event):void {
var src:BitmapData = Bitmap(LoaderInfo(event.target).content).bitmapData;
//setting the alpha channel doesn't work if the image isn't transparent
//so we copy the bitmap data into a new transparent BitmapData
var bmp:BitmapData = new BitmapData(src.width, src.height, true);
bmp.draw(src);
var bytes:ByteArray = bmp.getPixels(bmp.rect);
for (var y:int = 0; y < bmp.height; y+=2) {
for (var x:int = 0; x < bmp.width; x++) {
//find the alpha channel of x,y
bytes[y * bmp.width * 4 + x * 4] = 0x0;
}
}
//you must rewind the ByteArray first, for some reason
bytes.position = 0;
bmp.setPixels(bmp.rect, bytes);
var bitmap:Bitmap = new Bitmap(bmp);
addChild(bitmap);
}
}
}