Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: 画像の平均色

Get Adobe Flash player
by Saqoosha 07 Jul 2010
/**
 * Copyright Saqoosha ( http://wonderfl.net/user/Saqoosha )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/jKDU
 */

// forked from bkzen's 画像の平均色
package  
{
    import com.bit101.components.Label;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.filters.BlurFilter;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.utils.ByteArray;
    import flash.utils.getTimer;
    
    public class FlashTest extends Sprite
    {
        private const moja: String = "moja";
        
        public function FlashTest() 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            //
            var sp: Sprite = new Sprite();
            addChild(sp);
            var bmd: BitmapData = new BitmapData(100, 100, false);
            var baseXY: Number = 32;
            bmd.perlinNoise(baseXY, baseXY, 2, 0xFFFF * Math.random(), true, true, Math.random()*7+1);
            var bmdA: BitmapData = new BitmapData(bmd.width, bmd.height, false);
            var bmdB: BitmapData = new BitmapData(bmd.width, bmd.height, false);
            var bmdC:BitmapData = bmdA.clone();
            sp.addChild(new Bitmap(bmd, "auto", true));
            var c: uint, rgb: uint, r: uint, g: uint, b: uint, t: int;
            
            // 拡大縮小による平均
            t = getTimer();
            bmdA.draw(bmd, new Matrix(1/bmd.width, 0, 0, 1/bmd.height), null, null, null, true);
            bmdA.draw(bmdA, new Matrix(bmd.width, 0, 0, bmd.height));
            c = bmdA.getPixel(10, 10);
            new Label(sp, 0, 210, c.toString(0x10).toUpperCase() + "\n" + (getTimer() - t) + " ms");
            
            // 全ピクセル走査による平均
            t = getTimer();
            var bytes: ByteArray = bmd.getPixels(bmd.rect);
            bytes.position = 0;
            while (bytes.bytesAvailable)
            {
                rgb = bytes.readUnsignedInt() & 0xFFFFFF;
                r += (rgb >> 0x10) & 0xFF;
                g += (rgb >> 0x08) & 0xFF;
                b += (rgb >> 0x00) & 0xFF;
            }
            var cnt: uint = bmd.width * bmd.height;
            r = r / cnt;
            g = g / cnt;
            b = b / cnt;
            c = (r << 0x10) | (g << 0x08) | (b << 0x00);
            bmdB.fillRect(bmdB.rect, c);
            new Label(sp, 110, 210, c.toString(0x10).toUpperCase() + "\n" + (getTimer() - t) + " ms");
            
            // histgram
            t = getTimer();
            c = calcAverageColor(bmd);
            bmdC.fillRect(bmdC.rect, c);
            new Label(sp, 220, 210, c.toString(0x10).toUpperCase() + "\n" + (getTimer() - t) + " ms");
            
            //
            var bmpA: Bitmap = new Bitmap(bmdA, "auto", true);
            var bmpB: Bitmap = new Bitmap(bmdB, "auto", true);
            var bmpC: Bitmap = new Bitmap(bmdC, 'auto', true);
            bmpB.x = 105;
            bmpC.x = 210;
            bmpA.y = bmpB.y = bmpC.y = 105;
            sp.addChild(bmpA);
            sp.addChild(bmpB);
            sp.addChild(bmpC);
            sp.x = 100;
            sp.y = 100;
            
        }

        public function calcAverageColor(image:BitmapData, rect:Rectangle = null):uint {
            var hist:Vector.<Vector.<Number>> = image.histogram(rect);
            var r:Number = 0;
            var g:Number = 0;
            var b:Number = 0;
            var a:Number = 0;
            for (var i:int = 0; i < 256; ++i) {
                r += hist[0][i] * i;
                g += hist[1][i] * i;
                b += hist[2][i] * i;
                a += hist[3][i] * i;
            }
            var p:int = rect ? rect.width * rect.height : image.width * image.height;
            r /= p;
            g /= p;
            b /= p;
            a /= p;
            return (a << 24) | (r << 16) | (g << 8) | b;
        }
        
    }
}