In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

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

白黒のハイトマップから法線マップへ(簡易版)

白黒のハイトマップから、
RGBの法線マップへ
注意:真面目に計算してません。
@author jc at bk-zen.com
Get Adobe Flash player
by bkzen 26 Jun 2012
/**
 * Copyright bkzen ( http://wonderfl.net/user/bkzen )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/c9JO
 */

package  
{
    import com.adobe.images.JPGEncoder;
    import com.adobe.images.PNGEncoder;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.net.FileFilter;
    import flash.net.FileReference;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.utils.ByteArray;
    
    /**
     * 白黒のハイトマップから、
     * RGBの法線マップへ
     * 注意:真面目に計算してません。
     * @author jc at bk-zen.com
     */
    public class HightToNormalLite extends Sprite 
    {
        private var btn: Sprite;
        private var file: FileReference;
        private var loader: Loader;
        private var btnLabel: TextField;
        private var bmd: BitmapData;
        private var ext: String;
        private var fileName: String;
        
        public function HightToNormalLite() 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private function init(e: Event = null): void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            //
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
            btn = new Sprite();
            btnLabel = new TextField();
            btnLabel.autoSize = "left";
            btnLabel.defaultTextFormat = new TextFormat("_ゴシック", 15);
            btnLabel.text = "Load";
            btn.addChild(btnLabel);
            btn.mouseChildren = false;
            btn.buttonMode = true;
            addChild(btn);
            file = new FileReference();
            file.addEventListener(Event.COMPLETE, onCompLoadData);
            file.addEventListener(Event.SELECT, onSelect);
            btn.addEventListener(MouseEvent.CLICK, onClickLoad);
        }
        
        private function onSelect(e:Event):void 
        {
            if (bmd == null) 
            {
                removeChild(btn);
                var arr: Array = file.name.split(".");
                fileName = arr[0]
                ext = arr[1];
                ext = ext.toLowerCase();
                file.load();
            }
        }
        
        private function onCompLoadData(e:Event):void 
        {
            if (bmd) 
            {
                while (numChildren > 0) 
                    removeChildAt(0);
                btnLabel.text = "Load";
                addChild(btn);
                bmd = null;
            }
            else 
            {
                loader = new Loader();
                loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompLoadImage);
                loader.loadBytes(file.data);
            }
        }
        
        private function onCompLoadImage(e:Event):void 
        {
            loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onCompLoadImage);
            bmd = Bitmap(loader.content).bitmapData;
            bmd.lock();
            var vec: Vector.<uint> = bmd.getVector(bmd.rect);
            var w: int = bmd.width, h: int = bmd.height, tx: int, ty: int;
            var i: int, n: int = vec.length, c: uint, t: uint, r: int, g: int, l: Number;
            for (i = 0; i < n; i++) 
            {
                tx = i % w, ty = i / w;
                c = vec[i];
                t = vec[ty * w + (tx + 1 < w ? tx + 1 : tx)];
                r = ((c >> 0x10) & 0xFF) - ((t >> 0x10) & 0xFF);
                t = vec[(ty + 1 < h ? ty + 1 : ty) * w + tx];
                g = ((c >> 0x08) & 0xFF) - ((t >> 0x08) & 0xFF);
                r += 0x70, g += 0x70;
                if (r < 0) 
                    r = 0;
                else if (r > 0xFF) 
                    r = 0xFF;
                if (g < 0) 
                    g = 0;
                else if (g > 0xFF)
                    g = 0xFF;
                vec[i] = (r << 0x10) | (g << 0x08) | 0xFF0000FF;
            }
            bmd.setVector(bmd.rect, vec);
            bmd.unlock();
            btnLabel.text = "Save";
            var bmp: Bitmap;
            addChild(bmp = new Bitmap(bmd));
            bmp.y = btn.height + 5;
            bmp.width = stage.stageWidth;
            bmp.scaleY = bmp.scaleX;
            addChild(btn);
        }
        
        private function onClickLoad(e:MouseEvent):void 
        {
            if (bmd) 
            {
                file.save(ext == "jpg" ? PNGEncoder.encode(bmd) : new JPGEncoder(100).encode(bmd), fileName + "_bump." + ext);
            }
            else 
            {
                file.browse([new FileFilter("画像ファイル", "*.jpg;*.png")]);
            }
        }
        
    }

}