forked from: Manipulating pixels with BitmapData.setPixel() and BitmapData.setVector() methods

by shohei909 forked from Manipulating pixels with BitmapData.setPixel() and BitmapData.setVector() methods (diff: 10)
色の計算の仕方の違いが気になったので直してみました。
♥0 | Line 99 | Modified 2011-10-02 01:53:46 | MIT License
play

ActionScript3 source code

/**
 * Copyright shohei909 ( http://wonderfl.net/user/shohei909 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/15IH
 */

// forked from Fumio's Manipulating pixels with BitmapData.setPixel() and BitmapData.setVector() methods
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, true);
            for (var i:uint  = 0; i < nHeight; i++) {
                var pixelY:uint = i / nHeight * 256 + 0.5;
                var k:uint = i * nWidth;
                for (var j:uint = 0; j < nWidth; j++) {
                    var pixelX:uint = j / nWidth * 256 + 0.5;
                    var color:uint = pixelY << 16 | pixelX
                    pixels[j + k] = color;
                }
            }
            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");
        }
    }
}

Forked