Manipulating pixels with BitmapData.setPixel() and BitmapData.setVector() methods
F-site「BitmapData.setPixel()とBitmapData.setVector()メソッドでピクセルを塗る」に簡単な解説を加えた。
http://f-site.org/articles/2011/10/02000000.html
/**
* Copyright Fumio ( http://wonderfl.net/user/Fumio )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/jo6R
*/
package {
import flash.display.Sprite;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.utils.getTimer;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
public class ManipulatingPixels extends Sprite {
private var nWidth:int = stage.stageWidth;
private var nHeight:int = stage.stageHeight;
private var myBitmapData:BitmapData;
private var myBitmap:Bitmap;
private var my_txt:TextField = new TextField();
private var label_txt:TextField = new TextField();
private var my_fmt:TextFormat = new TextFormat();
public function ManipulatingPixels() {
// Creating a TextField for display
initialize();
createTextField();
// Starting Test
test_setPixel();
test_setPixel2();
test_setVector();
}
private function initialize():void {
myBitmapData = new BitmapData(nWidth, nHeight, false);
myBitmap = new Bitmap(myBitmapData);
addChild(myBitmap);
}
private function test_setPixel():void {
resetBitmap();
var started:int = getTimer();
for (var i:uint = 0; i < nHeight; i++) {
var pixelY:uint = i / nHeight * 256 + 0.5;
for (var j:uint = 0; j < nWidth; j++) {
var pixelX:uint = j / nWidth * 256 + 0.5;
var color:uint = pixelY << 16 | pixelX
myBitmapData.setPixel(j, i, color);
}
}
xTrace("setPixel()", getTimer() - started);
}
private function test_setPixel2():void {
resetBitmap();
var started:int = getTimer();
myBitmapData.lock();
for (var i:uint = 0; i < nHeight; i++) {
var pixelY:uint = i / nHeight * 256 + 0.5;
for (var j:uint = 0; j < nWidth; j++) {
var pixelX:uint = j / nWidth * 256 + 0.5;
var color:uint = pixelY << 16 | pixelX
myBitmapData.setPixel(j, i, color);
}
}
myBitmapData.unlock();
xTrace("setPixel() with lock() and unlock()", getTimer() - started);
}
private function test_setVector():void {
resetBitmap();
var size:uint = nWidth * nHeight;
var started:int = getTimer();
var pixels:Vector.<uint> = new Vector.<uint>(size);
for (var i:uint = 0; i < size; i++) {
var pixelX:uint = (i / nWidth * 256 + 0.5) % 256;
var pixelY:uint = int(i / nWidth) / nHeight * 256 + 0.5;
pixels[i] = pixelY << 16 | pixelX;
}
myBitmapData.setVector(myBitmapData.rect, pixels);
xTrace("setVector()", getTimer() - started);
}
private function resetBitmap():void {
myBitmapData.dispose();
myBitmap.bitmapData = null;
myBitmapData = new BitmapData(nWidth, nHeight, false);
myBitmap.bitmapData = myBitmapData;
}
private function createTextField():void {
addChild(my_txt);
addChild(label_txt);
my_fmt.align = TextFormatAlign.RIGHT;
my_txt.x += 100;
my_txt.defaultTextFormat = my_fmt;
my_txt.autoSize = TextFieldAutoSize.RIGHT;
my_txt.background = true;
my_txt.backgroundColor = 0xFFFFFF;
label_txt.autoSize = TextFieldAutoSize.LEFT;
label_txt.background = true;
label_txt.backgroundColor = 0xFFFFFF;
}
private function xTrace(_str:String,n:int):void {
my_txt.appendText(String(n) + "\n");
label_txt.appendText(_str + ":" + "\n");
}
}
}