※現在、「wonderfl build flash online」求人コンテンツ制作に関してのアンケートを実施中です!みなさまのお力添えを頂いて、続々とアンケート結果が集まっていますが、まだまだ募集しております。ご協力のほど、どうぞよろしくお願いいたします!

wonderfl運営事務局
→アンケートページ(※ログインしてからお答えいただけるようになっています。)

 notice: Flash editor updated! Join the development! Thanks to MiniBuilder


forked from : narutohyper's noFlashSeal(偽物) [diff(181)]

FAVORITE BY
:
ファイルから読んで表示で素敵
:
:
png-encoderPNG-encoder
:
これは...w
:
png-encoderPNG ENCODER
:
これはひどい
:
これは…!!
:
わーw
:
遺影これはひどい
:
iPad
:
素晴らしいwww自分のアイコン合わせるにはあらかじめ縦長にしないと駄目だなー
:
コレは。。。
:
遺影ジェネレーターこれはひどい
FORKED
  1. // forked from matsumos's 某Pad
  2. // forked from narutohyper's noFlashSeal(偽物)
  3. // forked from clockmaker's [Alternativa3D] Basic Template
  4. package {
  5.     import alternativa.engine3d.materials.MovieClipMaterial;
  6.     import alternativa.engine3d.materials.FillMaterial;
  7.     import alternativa.engine3d.primitives.Plane;
  8.     import flash.display.Shape;
  9.     import flash.events.KeyboardEvent;
  10.     import flash.events.MouseEvent;
  11.     import alternativa.engine3d.core.Object3D;
  12.     import alternativa.engine3d.primitives.Box;
  13.     import alternativa.engine3d.primitives.Cone;
  14.     import alternativa.engine3d.events.MouseEvent3D;
  15.     import alternativa.types.Point3D;
  16.     import flash.display.Sprite;
  17.     import flash.display.MovieClip;
  18.     import flash.display.BitmapData;
  19.     import flash.display.Loader;
  20.     import flash.net.URLRequest; 
  21.     import flash.geom.Matrix; 
  22.     import flash.events.Event;
  23.     import flash.utils.ByteArray; 
  24.     import flash.net.*;
  25.     import org.libspark.thread.EnterFrameThreadExecutor;
  26.     import org.libspark.thread.Thread; 
  27.     [SWF(width = 465, height = 465, frameRate = 60)]
  28.     /*
  29.     自分用に作っただけなので、特に深い意図は無いです、
  30.     中に配置する画像(背景が透明のPNG)をロード。
  31.     */
  32.     /**
  33.      * Alternativa3D を簡単に扱うためのベーシックテンプレート
  34.      * @author Yasu (clockmaker)
  35.      */
  36.     public class SimpleDemo extends Sprite {
  37.         public var img1:Loader
  38.         public var template:BasicTemplate
  39.         public var mc1:MovieClip;
  40.         public var plane:Plane;
  41.         public function SimpleDemo():void {
  42.             // テンプレートを作成します
  43.             
  44.             var bg:Shape = new Shape();
  45.             bg.graphics.beginFill(0x0);
  46.             bg.graphics.drawRect(110200200200);
  47.             bg.graphics.endFill();
  48.             addChild(bg);
  49.             
  50.             var template:BasicTemplate = new BasicTemplate();
  51.             addChild(template);
  52.             template.camera.coords=new Point3D(-800,-800,700);
  53.             Thread.initialize(new EnterFrameThreadExecutor());
  54.             new MainThread(this).start();
  55.             
  56.             img1 = new Loader(); 
  57.             //img2 = new Loader(); 
  58.             img1.load(new URLRequest('http://assets.wonderfl.net/images/related_images/a/a2/a235/a23539b3cebd3c86ed745a2ae2afae32bfa32bfc')); 
  59.             addChild(img1);
  60.             //img1.alpha = 0.2;
  61.             img1.x = (465 - 341) >> 1;
  62.             img1.y = (465 - 450) >> 1;
  63.             
  64.             mc1=new MovieClip()
  65.             //MovieClipMaterialのスケーリングにバグがあるので、以下の対処方で処理
  66.             var clipWidth:int = 200;
  67.             var clipHeight:int = 200;
  68.             mc1.scaleY = clipWidth/clipHeight;
  69.             var matrix:Matrix = new Matrix(100, clipWidth/clipHeight); 
  70.             var mcm1:MovieClipMaterial=new MovieClipMaterial(mc1,clipWidth,clipWidth,null,matrix,true)
  71.             // プリミティブを作成します
  72.             plane = new Plane(2002002020);
  73.             plane.rotationX = 90;
  74.             plane.setMaterialToSurface(mcm1, 'front');
  75.             
  76.             template.scene.root.addChild(plane);
  77.             var pos:Point3D = new Point3D();
  78.             pos.x = plane.x;
  79.             pos.y = plane.y; 
  80.             pos.z = plane.z;
  81.             template.cameraContoller.lookAt(pos);
  82.             
  83.             with(template.camera) {
  84.                 x =  907.2257738784996
  85.                 y =  -2246.0827362651494
  86.                 z =  -1944.3373132335682
  87.                 rotationX =  -0.863937979737193
  88.                 rotationY =  -0.11999999999999998
  89.                 rotationZ =  0.2617993877991494
  90.                 fov =  0.22689280275926252
  91.             }
  92.             
  93.             stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDownHandler);
  94.             stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
  95.             stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDown);
  96.             
  97.             stage.addEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler );
  98.             stage.addEventListener( MouseEvent.MOUSE_UP, mouseUpHandler );
  99.             stage.addEventListener( Event.MOUSE_LEAVE, mouseUpHandler );
  100.         }
  101.         private function mouseUpHandler(e:Event):void 
  102.         {
  103.             stage.removeEventListener( MouseEvent.MOUSE_MOVE, moveHandler );
  104.             stage.removeEventListener( MouseEvent.MOUSE_MOVE, scaleHandler );
  105.         }
  106.         
  107.         private function mouseDownHandler(e:MouseEvent):void 
  108.         {
  109.             pastX = mouseX;
  110.             pastY = mouseY;
  111.             
  112.             if (shiftKey) {
  113.                 stage.addEventListener( MouseEvent.MOUSE_MOVE, scaleHandler );
  114.             }
  115.             else {
  116.                 stage.addEventListener( MouseEvent.MOUSE_MOVE, moveHandler );
  117.             }
  118.         }
  119.         
  120.         private function scaleHandler(e:MouseEvent):void 
  121.         {
  122.             plane.scaleX += ( pastY - mouseY ) * .01;
  123.             plane.scaleY = plane.scaleX;
  124.             pastY = mouseY;
  125.         }
  126.         
  127.         private var pastX:Number = 0;
  128.         private var pastY:Number = 0;
  129.         private var shiftKey:Boolean = false;
  130.         
  131.         private function moveHandler(e:MouseEvent):void 
  132.         {
  133.             plane.x -= pastX - mouseX;
  134.             plane.y -= pastY - mouseY;
  135.             
  136.             pastX = mouseX;
  137.             pastY = mouseY;
  138.         }
  139.         
  140.         private function KeyDownHandler(e:KeyboardEvent):void {
  141.             if (e.keyCode == 16) {
  142.                 shiftKey = true;
  143.             }
  144.         }
  145.         
  146.         private function keyUpHandler(e:KeyboardEvent):void {
  147.             if (e.keyCode == 16) {
  148.                 shiftKey = false;
  149.             }
  150.         }
  151.         
  152.         private function KeyDown(e:KeyboardEvent):void{
  153.             if (e.keyCode == 38) {
  154.                 plane.z += 2
  155.             }
  156.             if (e.keyCode == 37) {
  157.                 plane.x -= 2
  158.             }
  159.             if (e.keyCode == 40) {
  160.                 plane.z -= 2
  161.             }
  162.             if (e.keyCode == 39) {
  163.                 plane.x += 2
  164.             }
  165.         }
  166.     }
  167. }
  168. import org.libspark.thread.Thread;
  169. import flash.events.Event;
  170. import flash.events.MouseEvent;
  171. import flash.display.SimpleButton;
  172. import flash.utils.ByteArray; 
  173. import flash.net.FileReference;
  174. import flash.net.FileReferenceList;
  175. import flash.geom.Matrix;
  176. import flash.geom.Point;
  177. import flash.geom.Rectangle;
  178. import flash.display.*;
  179. import flash.text.*;
  180. import flash.system.Security;
  181. class MainThread extends Thread {
  182.     public static const CROSSDOMAIN:String = "http://assets.wonderfl.net/crossdomain.xml";
  183.     private var _base:SimpleDemo;
  184.     private var _loadButton:Button;
  185.     private var _saveButton:Button;
  186.     private var _original:Loader;
  187.     private var _guide:Guide;
  188.     private var bmd1:BitmapData
  189.     private var bm1:Bitmap
  190.     public function MainThread(base:SimpleDemo) {
  191.         this._base = base;
  192.         this._loadButton = this._base.addChild(new Button('LOAD'60)) as Button;
  193.         this._loadButton.x = this._loadButton.y = 1;
  194.         this._saveButton = this._base.addChild(new Button('SAVE'60)) as Button;
  195.         this._saveButton.x = 1;
  196.         this._saveButton.y = this._loadButton.height + 2;
  197.         this._guide = this._base.addChild(new Guide()) as Guide;
  198.         this._guide.x = (465 - 341) >> 1;
  199.         this._guide.y = (465 - 450) >> 1;
  200.         this.bmd1=new BitmapData(200,200,false,0x0)
  201.         this.bm1=new Bitmap()
  202.     }
  203.     protected override function run():void {
  204.         this._event();
  205.     }
  206.     private function _event():void {
  207.         event(this._loadButton, MouseEvent.CLICK, this._loadImage);
  208.         event(this._saveButton, MouseEvent.CLICK, this._saveImage);
  209.         Security.loadPolicyFile(CROSSDOMAIN);
  210.     }
  211.     // load image
  212.     private function _loadImage(e:MouseEvent):void {
  213.         trace(Button(e.target).label)
  214.         var file:FileReference = new FileReference();
  215.         event(file, Event.SELECT, this._loadFileSelected);
  216.         event(file, Event.CANCEL, this._loadFileCancel);
  217.         file.browse();
  218.     }
  219.     private function _loadFileSelected(e:Event):void {
  220.         var file:FileReference = FileReference(e.target);
  221.         event(file, Event.COMPLETE, this._fileLoaded);
  222.         file.load();
  223.     }
  224.     
  225.     private function _fileLoaded(e:Event):void {
  226.         if (this._original) {
  227.             //this._original.parent.removeChild(this._original);
  228.             this._original.unload();
  229.         }
  230.         this._original = new Loader();
  231.         this._original.loadBytes(FileReference(e.target).data);
  232.         event(this._original.contentLoaderInfo, Event.COMPLETE, this._imageLoaded);
  233.     }
  234.     private function _loadFileCancel(e:Event):void {
  235.         this._event();
  236.     }
  237.     
  238.     private function _imageLoaded(e:Event):void {
  239.         var a:Number = 200 / this._original.height;
  240.         var mtr:Matrix=new Matrix(a,0,0,a,(200-(this._original.width*a))/2,(200-(this._original.height*a))/2)
  241.         this.bmd1=new BitmapData(200,200,false,0x0)
  242.         this.bmd1.draw(this._original.content,mtr)
  243.         this.bm1.bitmapData=bmd1
  244.         this._base.mc1.addChild(this.bm1)
  245.         this._event();
  246.     }
  247.     
  248.     // save image 
  249.     private function _saveImage(e:Event):void { 
  250.         var raw:BitmapData = new BitmapData(341450true, 0x0); 
  251.         this._guide.visible = false;
  252.         raw.draw(this._base, new Matrix(1001, -this._guide.x, -this._guide.y), nullnullnulltrue);
  253.         this._guide.visible = true;
  254.         var png:ByteArray = PNGEnc.encode(raw); 
  255.         raw.dispose(); 
  256.         var file:FileReference = new FileReference(); 
  257.         event(file, Event.SELECT, this._saveFileSelected); 
  258.         event(file, Event.CANCEL, this._saveFileSelected);
  259.         file.save(png, 'pad.png'); 
  260.     }
  261.     private function _saveFileSelected(e:Event):void { 
  262.         this._event();
  263.     } 
  264. }
  265. class Guide extends Shape {
  266.     
  267.     public function Guide() {
  268.         var g:Graphics = this.graphics;
  269.         g.lineStyle(1, 0xFFFFFF, 0.3true);
  270.         g.drawRect(00341450);
  271.     }
  272. }
  273. class Button extends SimpleButton {
  274.     public var label:String
  275.     public function Button(_label:String, width:int = 0):void {
  276.         label=_label
  277.         var up:Sprite = _buildImage(label, 0x0, width);
  278.         var over:Sprite = _buildImage(label, 0x333333, width);
  279.         var down:Sprite = _buildImage(label, 0x333333, width);
  280.         down.y = 1;
  281.         super(up, over, down, up);
  282.     }
  283.     
  284.     private static function _buildImage(label:String, color:int, width:int = 0):Sprite {
  285.         var text:TextField = new TextField();
  286.         text.defaultTextFormat = new TextFormat('Verdana'10, 0xffffff, truenullnullnullnull, TextFormatAlign.CENTER);
  287.         text.autoSize = TextFieldAutoSize.LEFT
  288.         text.selectable = false;
  289.         text.text = label;
  290.         text.x = (width - text.width) >> 1;
  291.         text.y = 5;
  292.         var base:Shape = new Shape();
  293.         var g:Graphics = base.graphics;
  294.         g.beginFill(color);
  295.         g.drawRect(00, width, text.height + 10);
  296.         g.endFill();
  297.         var sp:Sprite = new Sprite();
  298.         sp.addChild(base);
  299.         sp.addChild(text);
  300.         return sp;
  301.     }
  302. }
  303. import alternativa.engine3d.controllers.CameraController;
  304. import alternativa.engine3d.core.Camera3D;
  305. import alternativa.engine3d.core.Object3D;
  306. import alternativa.engine3d.core.Scene3D;
  307. import alternativa.engine3d.display.View;
  308. import flash.display.Sprite;
  309. import flash.display.StageAlign;
  310. import flash.display.StageQuality;
  311. import flash.display.StageScaleMode;
  312. import flash.events.Event;
  313. /**
  314.  * BasicTemplate for Alternativa3D
  315.  * Alternativa3Dを扱いやすくするためのテンプレートです
  316.  * @author Yasu
  317.  */
  318. class BasicTemplate extends Sprite{
  319.     /**
  320.      * シーンインスタンスです。
  321.      */
  322.     public var scene:Scene3D;
  323.     /**
  324.      * ビューインスタンスです。
  325.      */
  326.     public var view:View;
  327.     /**
  328.      * カメラインスタンスです。
  329.      */
  330.     public var camera:Camera3D;
  331.     /**
  332.      * カメラコントローラーです。
  333.      */
  334.     public var cameraContoller:CameraController;
  335.     
  336.     private var _viewWidth:int;
  337.     private var _viewHeight:int;
  338.     private var _scaleToStage:Boolean;
  339.     /**
  340.      * 新しい BasicTemplate インスタンスを作成します。
  341.      * @param    viewWidth
  342.      * @param    viewHeight
  343.      * @param    scaleToStage
  344.      */
  345.     public function BasicTemplate(viewWidth:int=640, viewHeight:int=480, scaleToStage:Boolean = true) {
  346.         _viewWidth = viewWidth;
  347.         _viewHeight = viewHeight;
  348.         _scaleToStage = scaleToStage;
  349.         
  350.         // Creating scene
  351.         scene = new Scene3D();
  352.         scene.splitAnalysis = false// not analysis for performance
  353.         scene.root = new Object3D();
  354.         
  355.         // Adding camera
  356.         camera = new Camera3D();
  357.         camera.z = -1000;
  358.         scene.root.addChild(camera);
  359.         
  360.         // camera contoller
  361.         cameraContoller = new CameraController(this);
  362.         cameraContoller.camera = camera;
  363.         
  364.         // set view
  365.         view = new View();
  366.         view.camera = camera;
  367.         addChild(view);
  368.         
  369.         // stage
  370.         if (stage) init();
  371.         else addEventListener(Event.ADDED_TO_STAGE, init);
  372.     }
  373.     
  374.     /**
  375.      * 初期化されたときに実行されるイベントです。
  376.      * 初期化時に実行したい処理をオーバーライドして記述します。
  377.      */
  378.     protected function atInit():void {}
  379.     
  380.     /**
  381.      * 初期化されたときに実行されるイベントです。
  382.      * 初期化時に実行したい処理を記述します。
  383.      */
  384.     private var _onInit:Function = function():void { };
  385.     public function get onInit():Function { return _onInit; }
  386.     public function set onInit(value:Function):void {
  387.         _onInit = value;
  388.     }
  389.     
  390.     /**
  391.      * Event.ENTER_FRAME 時に実行されるレンダリングのイベントです。
  392.      * レンダリング前に実行したい処理をオーバーライドして記述します。
  393.      */
  394.     protected function atPreRender():void {}
  395.     
  396.     /**
  397.      * Event.ENTER_FRAME 時に実行されるレンダリングのイベントです。
  398.      * レンダリング前に実行したい処理を記述します。
  399.      */
  400.     private var _onPreRender:Function = function():void{};
  401.     public function get onPreRender():Function { return _onPreRender; }
  402.     public function set onPreRender(value:Function):void {
  403.         _onPreRender = value;
  404.     }
  405.     
  406.     /**
  407.      * Event.ENTER_FRAME 時に実行されるレンダリングのイベントです。
  408.      * レンダリング後に実行したい処理をオーバーライドして記述します。
  409.      */
  410.     protected function atPostRender():void {
  411.     }
  412.     
  413.     /**
  414.      * Event.ENTER_FRAME 時に実行されるレンダリングのイベントです。
  415.      * レンダリング後に実行したい処理を記述します。
  416.      */
  417.     protected var _onPostRender:Function = function():void{};
  418.     public function get onPostRender():Function { return _onPostRender; }
  419.     public function set onPostRender(value:Function):void {
  420.         _onPostRender = value;
  421.     }
  422.     
  423.     /**
  424.      * レンダリングを開始します。
  425.      */
  426.     public function startRendering():void {
  427.         addEventListener(Event.ENTER_FRAME, onRenderTick);
  428.     }
  429.     /**
  430.      * レンダリングを停止します。
  431.      */
  432.     public function stopRendering():void {
  433.         removeEventListener(Event.ENTER_FRAME, onRenderTick);
  434.     }
  435.     
  436.     /**
  437.      * シングルレンダリング(レンダリングを一回だけ)を実行します。
  438.      */
  439.     public function singleRender():void {
  440.         onRenderTick();
  441.     }
  442.     
  443.     /**
  444.      * @private
  445.      */
  446.     private function init(e:Event = null):void {
  447.         stage.scaleMode = StageScaleMode.NO_SCALE;
  448.         stage.align = StageAlign.TOP_LEFT;
  449.         stage.quality = StageQuality.HIGH;
  450.         // resize
  451.         stage.addEventListener(Event.RESIZE, onResize);
  452.         onResize(null);
  453.         
  454.         // render
  455.         startRendering();
  456.         
  457.         atInit();
  458.         _onInit();
  459.         
  460.     }
  461.     
  462.     /**
  463.      * @private
  464.      */
  465.     private function onRenderTick(e:Event = null):void {
  466.         atPostRender();
  467.         _onPostRender();
  468.         scene.calculate();
  469.         atPreRender();
  470.         _onPreRender();
  471.     }
  472.     
  473.     /**
  474.      * @private
  475.      */
  476.     private function onResize(event:Event = null):void {
  477.         if (_scaleToStage) {
  478.             view.width = stage.stageWidth;
  479.             view.height = stage.stageHeight;
  480.         }else {
  481.             view.width = _viewWidth;
  482.             view.height = _viewHeight;
  483.         }
  484.     }
  485. }
  486. // http://www.5etdemi.com/blog/archives/2006/12/as3-png-encoder-faster-better/ 
  487. class PNGEnc { 
  488.          
  489.         public static function encode(img:BitmapData, type:uint = 0):ByteArray { 
  490.                  
  491.                 // Create output byte array 
  492.                 var png:ByteArray = new ByteArray(); 
  493.                 // Write PNG signature 
  494.                 png.writeUnsignedInt(0x89504e47); 
  495.                 png.writeUnsignedInt(0x0D0A1A0A); 
  496.                 // Build IHDR chunk 
  497.                 var IHDR:ByteArray = new ByteArray(); 
  498.                 IHDR.writeInt(img.width); 
  499.                 IHDR.writeInt(img.height); 
  500.                 if(img.transparent || type == 0
  501.                 { 
  502.                         IHDR.writeUnsignedInt(0x08060000); // 32bit RGBA 
  503.                 } 
  504.                 else 
  505.                 { 
  506.                         IHDR.writeUnsignedInt(0x08020000); //24bit RGB 
  507.                 } 
  508.                 IHDR.writeByte(0); 
  509.                 writeChunk(png,0x49484452,IHDR); 
  510.                 // Build IDAT chunk 
  511.                 var IDAT:ByteArray= new ByteArray(); 
  512.                  
  513.                 switch(type) 
  514.                 { 
  515.                         case 0
  516.                                 writeRaw(img, IDAT); 
  517.                                 break
  518.                         case 1
  519.                                 writeSub(img, IDAT); 
  520.                                 break
  521.                 } 
  522.                  
  523.                 IDAT.compress(); 
  524.                 writeChunk(png,0x49444154,IDAT); 
  525.                 // Build IEND chunk 
  526.                 writeChunk(png,0x49454E44,null); 
  527.                 // return PNG 
  528.                  
  529.                  
  530.                  
  531.                 return png; 
  532.         } 
  533.          
  534.         private static function writeRaw(img:BitmapData, IDAT:ByteArray):void 
  535.         { 
  536.                 var h:int = img.height; 
  537.                 var w:int = img.width; 
  538.                 var transparent:Boolean = img.transparent; 
  539.                  
  540.                 for(var i:int=0;i < h;i++) { 
  541.                         // no filter 
  542.                         if ( !transparent ) { 
  543.                                 var subImage:ByteArray = img.getPixels( 
  544.                                         new Rectangle(0, i, w, 1)); 
  545.                                 //Here we overwrite the alpha value of the first pixel 
  546.                                 //to be the filter 0 flag 
  547.                                 subImage[0] = 0
  548.                                 IDAT.writeBytes(subImage); 
  549.                                 //And we add a byte at the end to wrap the alpha values 
  550.                                 IDAT.writeByte(0xff); 
  551.                         } else { 
  552.                                 IDAT.writeByte(0); 
  553.                                 var p:uint
  554.                                 for(var j:int=0;j < w;j++) { 
  555.                                         p = img.getPixel32(j,i); 
  556.                                         IDAT.writeUnsignedInt( 
  557.                                                 uint(((p&0xFFFFFF) << 8)| 
  558.                                                 (p>>>24))); 
  559.                                 } 
  560.                         } 
  561.                 } 
  562.         } 
  563.          
  564.         private static function writeSub(img:BitmapData, IDAT:ByteArray):void 
  565.         { 
  566.                 var r1:uint
  567.                 var g1:uint
  568.                 var b1:uint
  569.                 var a1:uint
  570.                  
  571.                 var r2:uint
  572.                 var g2:uint
  573.                 var b2:uint
  574.                 var a2:uint
  575.                  
  576.                 var r3:uint
  577.                 var g3:uint
  578.                 var b3:uint
  579.                 var a3:uint
  580.                  
  581.                 var p:uint
  582.                 var h:int = img.height; 
  583.                 var w:int = img.width; 
  584.                  
  585.                 for(var i:int=0;i < h;i++) { 
  586.                         // no filter 
  587.                         IDAT.writeByte(1); 
  588.                         if ( !img.transparent ) { 
  589.                                 r1 = 0
  590.                                 g1 = 0
  591.                                 b1 = 0
  592.                                 a1 = 0xff; 
  593.                                 for(var j:int=0;j < w;j++) { 
  594.                                         p = img.getPixel(j,i); 
  595.                                          
  596.                                         r2 = p >> 16 & 0xff; 
  597.                                         g2 = p >> 8  & 0xff; 
  598.                                         b2 = p & 0xff; 
  599.                                          
  600.                                         r3 = (r2 - r1 + 256) & 0xff; 
  601.                                         g3 = (g2 - g1 + 256) & 0xff; 
  602.                                         b3 = (b2 - b1 + 256) & 0xff; 
  603.                                          
  604.                                         IDAT.writeByte(r3); 
  605.                                         IDAT.writeByte(g3); 
  606.                                         IDAT.writeByte(b3); 
  607.                                          
  608.                                         r1 = r2; 
  609.                                         g1 = g2; 
  610.                                         b1 = b2; 
  611.                                         a1 = 0
  612.                                 } 
  613.                         } else { 
  614.                                 r1 = 0
  615.                                 g1 = 0
  616.                                 b1 = 0
  617.                                 a1 = 0
  618.                                 for(j=0;j < w;j++) { 
  619.                                         p = img.getPixel32(j,i); 
  620.                                          
  621.                                         a2 = p >> 24 & 0xff; 
  622.                                         r2 = p >> 16 & 0xff; 
  623.                                         g2 = p >> 8  & 0xff; 
  624.                                         b2 = p & 0xff; 
  625.                                          
  626.                                         r3 = (r2 - r1 + 256) & 0xff; 
  627.                                         g3 = (g2 - g1 + 256) & 0xff; 
  628.                                         b3 = (b2 - b1 + 256) & 0xff; 
  629.                                         a3 = (a2 - a1 + 256) & 0xff; 
  630.                                          
  631.                                         IDAT.writeByte(r3); 
  632.                                         IDAT.writeByte(g3); 
  633.                                         IDAT.writeByte(b3); 
  634.                                         IDAT.writeByte(a3); 
  635.                                          
  636.                                         r1 = r2; 
  637.                                         g1 = g2; 
  638.                                         b1 = b2; 
  639.                                         a1 = a2; 
  640.                                 } 
  641.                         } 
  642.                 } 
  643.         } 
  644.         private static var crcTable:Array
  645.         private static var crcTableComputed:Boolean = false
  646.         private static function writeChunk(png:ByteArray,  
  647.                         type:uint, data:ByteArray):void { 
  648.                 var c:uint
  649.                 if (!crcTableComputed) { 
  650.                         crcTableComputed = true
  651.                         crcTable = []; 
  652.                         for (var n:uint = 0; n < 256; n++) { 
  653.                                 c = n; 
  654.                                 for (var k:uint = 0; k < 8; k++) { 
  655.                                         if (c & 1) { 
  656.                                                 c = uint(uint(0xedb88320) ^  
  657.                                                         uint(c >>> 1)); 
  658.                                         } else { 
  659.                                                 c = uint(c >>> 1); 
  660.                                         } 
  661.                                 } 
  662.                                 crcTable[n] = c; 
  663.                         } 
  664.                 } 
  665.                 var len:uint = 0
  666.                 if (data != null) { 
  667.                         len = data.length; 
  668.                 } 
  669.                 png.writeUnsignedInt(len); 
  670.                 var p:uint = png.position; 
  671.                 png.writeUnsignedInt(type); 
  672.                 if ( data != null ) { 
  673.                         png.writeBytes(data); 
  674.                 } 
  675.                 var e:uint = png.position; 
  676.                 png.position = p; 
  677.                 c = 0xffffffff; 
  678.                 for (var i:int = 0; i < (e-p); i++) { 
  679.                         c = uint(crcTable[ 
  680.                                 (c ^ png.readUnsignedByte()) &    
  681.                                 0xff] ^ (c >>> 8)); 
  682.                 } 
  683.                 c = uint(c^uint(0xffffffff)); 
  684.                 png.position = e; 
  685.                 png.writeUnsignedInt(c); 
  686.         } 
noswf
  1. // forked from matsumos's 某Pad
  2. // forked from narutohyper's noFlashSeal(偽物)
  3. // forked from clockmaker's [Alternativa3D] Basic Template
  4. package {
  5.     import alternativa.engine3d.materials.MovieClipMaterial;
  6.     import alternativa.engine3d.materials.FillMaterial;
  7.     import alternativa.engine3d.primitives.Plane;
  8.     import flash.display.Shape;
  9.     import flash.events.KeyboardEvent;
  10.     import flash.events.MouseEvent;
  11.     
  12.     import alternativa.engine3d.primitives.Box;
  13.     import alternativa.engine3d.primitives.Cone;
  14.     import alternativa.engine3d.events.MouseEvent3D;
  15.     import alternativa.types.Point3D;
  16.     import flash.display.*;
  17.     import flash.display.Sprite;
  18.     import flash.display.MovieClip;
  19.     import flash.display.BitmapData;
  20.     import flash.display.Loader;
  21.     import flash.net.URLRequest; 
  22.     import flash.geom.Matrix; 
  23.     import flash.events.Event;
  24.     import flash.utils.ByteArray; 
  25.     import flash.net.*;
  26.     import org.libspark.thread.EnterFrameThreadExecutor;
  27.     import org.libspark.thread.Thread; 
  28.     [SWF(width = 465, height = 465, frameRate = 60)]
  29.     /*
  30.     自分用に作っただけなので、特に深い意図は無いです、
  31.     中に配置する画像(背景が透明のPNG)をロード。
  32.     */
  33.     /**
  34.      * Alternativa3D を簡単に扱うためのベーシックテンプレート
  35.      * @author Yasu (clockmaker)
  36.      */
  37.     public class SimpleDemo extends Sprite {
  38.         public var img1:Loader
  39.         public var template:BasicTemplate
  40.         public var mc1:MovieClip;
  41.         public var plane:Plane;
  42.         public function SimpleDemo():void {
  43.             // テンプレートを作成します
  44.             
  45.             var bg:Shape = new Shape();
  46.             bg.graphics.beginFill(0x0);
  47.             bg.graphics.drawRect(110200200200);
  48.             bg.graphics.endFill();
  49.             addChild(bg);
  50.             
  51.             var template:BasicTemplate = new BasicTemplate();
  52.             addChild(template);
  53.             template.camera.coords=new Point3D(-800,-800,700);
  54.             Thread.initialize(new EnterFrameThreadExecutor());
  55.             new MainThread(this).start();
  56.             
  57.             img1 = new Loader(); 
  58.             //img2 = new Loader(); 
  59.             img1.load(new URLRequest('http://assets.wonderfl.net/images/related_images/a/a2/a235/a23539b3cebd3c86ed745a2ae2afae32bfa32bfc')); 
  60.             addChild(img1);
  61.             //img1.alpha = 0.2;
  62.             img1.x = (465 - 341) >> 1;
  63.             img1.y = (465 - 450) >> 1;
  64.             
  65.             mc1=new MovieClip()
  66.             //MovieClipMaterialのスケーリングにバグがあるので、以下の対処方で処理
  67.             var clipWidth:int = 200;
  68.             var clipHeight:int = 200;
  69.             mc1.scaleY = clipWidth/clipHeight;
  70.             var matrix:Matrix = new Matrix(100, clipWidth/clipHeight); 
  71.             var mcm1:MovieClipMaterial=new MovieClipMaterial(mc1,clipWidth,clipWidth,null,matrix,true)
  72.             // プリミティブを作成します
  73.             plane = new Plane(2002002020);
  74.             plane.rotationX = 90;
  75.             plane.setMaterialToSurface(mcm1, 'front');
  76.             
  77.             template.scene.root.addChild(plane);
  78.             var pos:Point3D = new Point3D();
  79.             pos.x = plane.x;
  80.             pos.y = plane.y; 
  81.             pos.z = plane.z;
  82.             template.cameraContoller.lookAt(pos);
  83.             
  84.             
  85.             
  86.             with(template.camera) {
  87.                 x =  907.2257738784996
  88.                 y =  -2246.0827362651494
  89.                 z =  -1944.3373132335682
  90.                 rotationX =  -0.863937979737193
  91.                 rotationY =  -0.11999999999999998
  92.                 rotationZ =  0.2617993877991494
  93.                 fov =  0.22689280275926252
  94.             }
  95.             
  96.             stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDownHandler);
  97.             stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
  98.             stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDown);
  99.             
  100.             stage.addEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler );
  101.             stage.addEventListener( MouseEvent.MOUSE_UP, mouseUpHandler );
  102.             stage.addEventListener( Event.MOUSE_LEAVE, mouseUpHandler );
  103.         }
  104.         private function mouseUpHandler(e:Event):void 
  105.         {
  106.             stage.removeEventListener( MouseEvent.MOUSE_MOVE, moveHandler );
  107.             stage.removeEventListener( MouseEvent.MOUSE_MOVE, scaleHandler );
  108.         }
  109.         
  110.         private function mouseDownHandler(e:MouseEvent):void 
  111.         {
  112.             pastX = mouseX;
  113.             pastY = mouseY;
  114.             
  115.             if (shiftKey) {
  116.                 stage.addEventListener( MouseEvent.MOUSE_MOVE, scaleHandler );
  117.             }
  118.             else {
  119.                 stage.addEventListener( MouseEvent.MOUSE_MOVE, moveHandler );
  120.             }
  121.         }
  122.         
  123.         private function scaleHandler(e:MouseEvent):void 
  124.         {
  125.             plane.scaleX += ( pastY - mouseY ) * .01;
  126.             plane.scaleY = plane.scaleX;
  127.             pastY = mouseY;
  128.         }
  129.         
  130.         private var pastX:Number = 0;
  131.         private var pastY:Number = 0;
  132.         private var shiftKey:Boolean = false;
  133.         
  134.         private function moveHandler(e:MouseEvent):void 
  135.         {
  136.             plane.x -= pastX - mouseX;
  137.             plane.y -= pastY - mouseY;
  138.             
  139.             pastX = mouseX;
  140.             pastY = mouseY;
  141.         }
  142.         
  143.         private function KeyDownHandler(e:KeyboardEvent):void {
  144.             if (e.keyCode == 16) {
  145.                 shiftKey = true;
  146.             }
  147.         }
  148.         
  149.         private function keyUpHandler(e:KeyboardEvent):void {
  150.             if (e.keyCode == 16) {
  151.                 shiftKey = false;
  152.             }
  153.         }
  154.         
  155.         private function KeyDown(e:KeyboardEvent):void{
  156.             if (e.keyCode == 38) {
  157.                 plane.z += 2
  158.             }
  159.             if (e.keyCode == 37) {
  160.                 plane.x -= 2
  161.             }
  162.             if (e.keyCode == 40) {
  163.                 plane.z -= 2
  164.             }
  165.             if (e.keyCode == 39) {
  166.                 plane.x += 2
  167.             }
  168.         }
  169.     }
  170. }
  171. import org.libspark.thread.Thread;
  172. import flash.events.Event;
  173. import flash.events.MouseEvent;
  174. import flash.display.SimpleButton;
  175. import flash.utils.ByteArray; 
  176. import flash.net.FileReference;
  177. import flash.net.FileReferenceList;
  178. import flash.geom.Matrix;
  179. import flash.geom.Point;
  180. import flash.geom.Rectangle;
  181. import flash.display.*;
  182. import flash.text.*;
  183. import flash.system.Security;
  184. class MainThread extends Thread {
  185.     public static const CROSSDOMAIN:String = "http://assets.wonderfl.net/crossdomain.xml";
  186.     private var _base:SimpleDemo;
  187.     private var _loadButton:Button;
  188.     private var _saveButton:Button;
  189.     private var _original:Loader;
  190.     private var _guide:Guide;
  191.     private var bmd1:BitmapData
  192.     private var bm1:Bitmap
  193.     public function MainThread(base:SimpleDemo) {
  194.         this._base = base;
  195.         this._loadButton = this._base.addChild(new Button('LOAD'60)) as Button;
  196.         this._loadButton.x = this._loadButton.y = 1;
  197.         this._saveButton = this._base.addChild(new Button('SAVE'60)) as Button;
  198.         this._saveButton.x = 1;
  199.         this._saveButton.y = this._loadButton.height + 2;
  200.         this._guide = this._base.addChild(new Guide()) as Guide;
  201.         this._guide.x = (465 - 341) >> 1;
  202.         this._guide.y = (465 - 450) >> 1;
  203.         this.bmd1=new BitmapData(200,200,false,0x0)
  204.         this.bm1=new Bitmap()
  205.     }
  206.     protected override function run():void {
  207.         this._event();
  208.     }
  209.     private function _event():void {
  210.         event(this._loadButton, MouseEvent.CLICK, this._loadImage);
  211.         event(this._saveButton, MouseEvent.CLICK, this._saveImage);
  212.         Security.loadPolicyFile(CROSSDOMAIN);
  213.     }
  214.     // load image
  215.     private function _loadImage(e:MouseEvent):void {
  216.         trace(Button(e.target).label)
  217.         var file:FileReference = new FileReference();
  218.         event(file, Event.SELECT, this._loadFileSelected);
  219.         event(file, Event.CANCEL, this._loadFileCancel);
  220.         file.browse();
  221.     }
  222.     private function _loadFileSelected(e:Event):void {
  223.         var file:FileReference = FileReference(e.target);
  224.         event(file, Event.COMPLETE, this._fileLoaded);
  225.         file.load();
  226.     }
  227.     
  228.     private function _fileLoaded(e:Event):void {
  229.         if (this._original) {
  230.             //this._original.parent.removeChild(this._original);
  231.             this._original.unload();
  232.         }
  233.         this._original = new Loader();
  234.         this._original.loadBytes(FileReference(e.target).data);
  235.         event(this._original.contentLoaderInfo, Event.COMPLETE, this._imageLoaded);
  236.     }
  237.     private function _loadFileCancel(e:Event):void {
  238.         this._event();
  239.     }
  240.     
  241.     private function _imageLoaded(e:Event):void {
  242.         var a:Number = 200 / this._original.height;
  243.         var mtr:Matrix=new Matrix(a,0,0,a,(200-(this._original.width*a))/2,(200-(this._original.height*a))/2)
  244.         this.bmd1=new BitmapData(200,200,false,0x0)
  245.         this.bmd1.draw(this._original.content,mtr)
  246.         this.bm1.bitmapData=bmd1
  247.         this._base.mc1.addChild(this.bm1)
  248.         this._event();
  249.     }
  250.     
  251.     // save image 
  252.     private function _saveImage(e:Event):void { 
  253.         var raw:BitmapData = new BitmapData(341450true, 0x0); 
  254.         this._guide.visible = false;
  255.         raw.draw(this._base, new Matrix(1001, -this._guide.x, -this._guide.y), nullnullnulltrue);
  256.         this._guide.visible = true;
  257.         var png:ByteArray = PNGEnc.encode(raw); 
  258.         raw.dispose(); 
  259.         var file:FileReference = new FileReference(); 
  260.         event(file, Event.SELECT, this._saveFileSelected); 
  261.         event(file, Event.CANCEL, this._saveFileSelected);
  262.         file.save(png, 'pad.png'); 
  263.     }
  264.     private function _saveFileSelected(e:Event):void { 
  265.         this._event();
  266.     } 
  267. }
  268. class Guide extends Shape {
  269.     
  270.     public function Guide() {
  271.         var g:Graphics = this.graphics;
  272.         g.lineStyle(1, 0xFFFFFF, 0.3true);
  273.         g.drawRect(00341450);
  274.     }
  275. }
  276. class Button extends SimpleButton {
  277.     public var label:String
  278.     public function Button(_label:String, width:int = 0):void {
  279.         label=_label
  280.         var up:Sprite = _buildImage(label, 0x0, width);
  281.         var over:Sprite = _buildImage(label, 0x333333, width);
  282.         var down:Sprite = _buildImage(label, 0x333333, width);
  283.         down.y = 1;
  284.         super(up, over, down, up);
  285.     }
  286.     
  287.     private static function _buildImage(label:String, color:int, width:int = 0):Sprite {
  288.         var text:TextField = new TextField();
  289.         text.defaultTextFormat = new TextFormat('Verdana'10, 0xffffff, truenullnullnullnull, TextFormatAlign.CENTER);
  290.         text.autoSize = TextFieldAutoSize.LEFT
  291.         text.selectable = false;
  292.         text.text = label;
  293.         text.x = (width - text.width) >> 1;
  294.         text.y = 5;
  295.         var base:Shape = new Shape();
  296.         var g:Graphics = base.graphics;
  297.         g.beginFill(color);
  298.         g.drawRect(00, width, text.height + 10);
  299.         g.endFill();
  300.         var sp:Sprite = new Sprite();
  301.         sp.addChild(base);
  302.         sp.addChild(text);
  303.         return sp;
  304.     }
  305. }
  306. import alternativa.engine3d.controllers.CameraController;
  307. import alternativa.engine3d.core.Camera3D;
  308. import alternativa.engine3d.core.Object3D;
  309. import alternativa.engine3d.core.Scene3D;
  310. import alternativa.engine3d.display.View;
  311. import flash.display.Sprite;
  312. import flash.display.StageAlign;
  313. import flash.display.StageQuality;
  314. import flash.display.StageScaleMode;
  315. import flash.events.Event;
  316. /**
  317.  * BasicTemplate for Alternativa3D
  318.  * Alternativa3Dを扱いやすくするためのテンプレートです
  319.  * @author Yasu
  320.  */
  321. class BasicTemplate extends Sprite{
  322.     /**
  323.      * シーンインスタンスです。
  324.      */
  325.     public var scene:Scene3D;
  326.     /**
  327.      * ビューインスタンスです。
  328.      */
  329.     public var view:View;
  330.     /**
  331.      * カメラインスタンスです。
  332.      */
  333.     public var camera:Camera3D;
  334.     /**
  335.      * カメラコントローラーです。
  336.      */
  337.     public var cameraContoller:CameraController;
  338.     
  339.     private var _viewWidth:int;
  340.     private var _viewHeight:int;
  341.     private var _scaleToStage:Boolean;
  342.     /**
  343.      * 新しい BasicTemplate インスタンスを作成します。
  344.      * @param    viewWidth
  345.      * @param    viewHeight
  346.      * @param    scaleToStage
  347.      */
  348.     public function BasicTemplate(viewWidth:int=640, viewHeight:int=480, scaleToStage:Boolean = true) {
  349.         _viewWidth = viewWidth;
  350.         _viewHeight = viewHeight;
  351.         _scaleToStage = scaleToStage;
  352.         
  353.         // Creating scene
  354.         scene = new Scene3D();
  355.         scene.splitAnalysis = false// not analysis for performance
  356.         scene.root = new Object3D();
  357.         
  358.         // Adding camera
  359.         camera = new Camera3D();
  360.         camera.z = -1000;
  361.         scene.root.addChild(camera);
  362.         
  363.         // camera contoller
  364.         cameraContoller = new CameraController(this);
  365.         cameraContoller.camera = camera;
  366.         
  367.         // set view
  368.         view = new View();
  369.         view.camera = camera;
  370.         addChild(view);
  371.         
  372.         // stage
  373.         if (stage) init();
  374.         else addEventListener(Event.ADDED_TO_STAGE, init);
  375.     }
  376.     
  377.     /**
  378.      * 初期化されたときに実行されるイベントです。
  379.      * 初期化時に実行したい処理をオーバーライドして記述します。
  380.      */
  381.     protected function atInit():void {}
  382.     
  383.     /**
  384.      * 初期化されたときに実行されるイベントです。
  385.      * 初期化時に実行したい処理を記述します。
  386.      */
  387.     private var _onInit:Function = function():void { };
  388.     public function get onInit():Function { return _onInit; }
  389.     public function set onInit(value:Function):void {
  390.         _onInit = value;
  391.     }
  392.     
  393.     /**
  394.      * Event.ENTER_FRAME 時に実行されるレンダリングのイベントです。
  395.      * レンダリング前に実行したい処理をオーバーライドして記述します。
  396.      */
  397.     protected function atPreRender():void {}
  398.     
  399.     /**
  400.      * Event.ENTER_FRAME 時に実行されるレンダリングのイベントです。
  401.      * レンダリング前に実行したい処理を記述します。
  402.      */
  403.     private var _onPreRender:Function = function():void{};
  404.     public function get onPreRender():Function { return _onPreRender; }
  405.     public function set onPreRender(value:Function):void {
  406.         _onPreRender = value;
  407.     }
  408.     
  409.     /**
  410.      * Event.ENTER_FRAME 時に実行されるレンダリングのイベントです。
  411.      * レンダリング後に実行したい処理をオーバーライドして記述します。
  412.      */
  413.     protected function atPostRender():void {
  414.     }
  415.     
  416.     /**
  417.      * Event.ENTER_FRAME 時に実行されるレンダリングのイベントです。
  418.      * レンダリング後に実行したい処理を記述します。
  419.      */
  420.     protected var _onPostRender:Function = function():void{};
  421.     public function get onPostRender():Function { return _onPostRender; }
  422.     public function set onPostRender(value:Function):void {
  423.         _onPostRender = value;
  424.     }
  425.     
  426.     /**
  427.      * レンダリングを開始します。
  428.      */
  429.     public function startRendering():void {
  430.         addEventListener(Event.ENTER_FRAME, onRenderTick);
  431.     }
  432.     /**
  433.      * レンダリングを停止します。
  434.      */
  435.     public function stopRendering():void {
  436.         removeEventListener(Event.ENTER_FRAME, onRenderTick);
  437.     }
  438.     
  439.     /**
  440.      * シングルレンダリング(レンダリングを一回だけ)を実行します。
  441.      */
  442.     public function singleRender():void {
  443.         onRenderTick();
  444.     }
  445.     
  446.     /**
  447.      * @private
  448.      */
  449.     private function init(e:Event = null):void {
  450.         stage.scaleMode = StageScaleMode.NO_SCALE;
  451.         stage.align = StageAlign.TOP_LEFT;
  452.         stage.quality = StageQuality.HIGH;
  453.         // resize
  454.         stage.addEventListener(Event.RESIZE, onResize);
  455.         onResize(null);
  456.         
  457.         // render
  458.         startRendering();
  459.         
  460.         atInit();
  461.         _onInit();
  462.         
  463.     }
  464.     
  465.     /**
  466.      * @private
  467.      */
  468.     private function onRenderTick(e:Event = null):void {
  469.         atPostRender();
  470.         _onPostRender();
  471.         scene.calculate();
  472.         atPreRender();
  473.         _onPreRender();
  474.     }
  475.     
  476.     /**
  477.      * @private
  478.      */
  479.     private function onResize(event:Event = null):void {
  480.         if (_scaleToStage) {
  481.             view.width = stage.stageWidth;
  482.             view.height = stage.stageHeight;
  483.         }else {
  484.             view.width = _viewWidth;
  485.             view.height = _viewHeight;
  486.         }
  487.     }
  488. }
  489. // http://www.5etdemi.com/blog/archives/2006/12/as3-png-encoder-faster-better/ 
  490. class PNGEnc { 
  491.          
  492.         public static function encode(img:BitmapData, type:uint = 0):ByteArray { 
  493.                  
  494.                 // Create output byte array 
  495.                 var png:ByteArray = new ByteArray(); 
  496.                 // Write PNG signature 
  497.                 png.writeUnsignedInt(0x89504e47); 
  498.                 png.writeUnsignedInt(0x0D0A1A0A); 
  499.                 // Build IHDR chunk 
  500.                 var IHDR:ByteArray = new ByteArray(); 
  501.                 IHDR.writeInt(img.width); 
  502.                 IHDR.writeInt(img.height); 
  503.                 if(img.transparent || type == 0
  504.                 { 
  505.                         IHDR.writeUnsignedInt(0x08060000); // 32bit RGBA 
  506.                 } 
  507.                 else 
  508.                 { 
  509.                         IHDR.writeUnsignedInt(0x08020000); //24bit RGB 
  510.                 } 
  511.                 IHDR.writeByte(0); 
  512.                 writeChunk(png,0x49484452,IHDR); 
  513.                 // Build IDAT chunk 
  514.                 var IDAT:ByteArray= new ByteArray(); 
  515.                  
  516.                 switch(type) 
  517.                 { 
  518.                         case 0
  519.                                 writeRaw(img, IDAT); 
  520.                                 break
  521.                         case 1
  522.                                 writeSub(img, IDAT); 
  523.                                 break
  524.                 } 
  525.                  
  526.                 IDAT.compress(); 
  527.                 writeChunk(png,0x49444154,IDAT); 
  528.                 // Build IEND chunk 
  529.                 writeChunk(png,0x49454E44,null); 
  530.                 // return PNG 
  531.                  
  532.                  
  533.                  
  534.                 return png; 
  535.         } 
  536.          
  537.         private static function writeRaw(img:BitmapData, IDAT:ByteArray):void 
  538.         { 
  539.                 var h:int = img.height; 
  540.                 var w:int = img.width; 
  541.                 var transparent:Boolean = img.transparent; 
  542.                  
  543.                 for(var i:int=0;i < h;i++) { 
  544.                         // no filter 
  545.                         if ( !transparent ) { 
  546.                                 var subImage:ByteArray = img.getPixels( 
  547.                                         new Rectangle(0, i, w, 1)); 
  548.                                 //Here we overwrite the alpha value of the first pixel 
  549.                                 //to be the filter 0 flag 
  550.                                 subImage[0] = 0
  551.                                 IDAT.writeBytes(subImage); 
  552.                                 //And we add a byte at the end to wrap the alpha values 
  553.                                 IDAT.writeByte(0xff); 
  554.                         } else { 
  555.                                 IDAT.writeByte(0); 
  556.                                 var p:uint
  557.                                 for(var j:int=0;j < w;j++) { 
  558.                                         p = img.getPixel32(j,i); 
  559.                                         IDAT.writeUnsignedInt( 
  560.                                                 uint(((p&0xFFFFFF) << 8)| 
  561.                                                 (p>>>24))); 
  562.                                 } 
  563.                         } 
  564.                 } 
  565.         } 
  566.          
  567.         private static function writeSub(img:BitmapData, IDAT:ByteArray):void 
  568.         { 
  569.                 var r1:uint
  570.                 var g1:uint
  571.                 var b1:uint
  572.                 var a1:uint
  573.                  
  574.                 var r2:uint
  575.                 var g2:uint
  576.                 var b2:uint
  577.                 var a2:uint
  578.                  
  579.                 var r3:uint
  580.                 var g3:uint
  581.                 var b3:uint
  582.                 var a3:uint
  583.                  
  584.                 var p:uint
  585.                 var h:int = img.height; 
  586.                 var w:int = img.width; 
  587.                  
  588.                 for(var i:int=0;i < h;i++) { 
  589.                         // no filter 
  590.                         IDAT.writeByte(1); 
  591.                         if ( !img.transparent ) { 
  592.                                 r1 = 0
  593.                                 g1 = 0
  594.                                 b1 = 0
  595.                                 a1 = 0xff; 
  596.                                 for(var j:int=0;j < w;j++) { 
  597.                                         p = img.getPixel(j,i); 
  598.                                          
  599.                                         r2 = p >> 16 & 0xff; 
  600.                                         g2 = p >> 8  & 0xff; 
  601.                                         b2 = p & 0xff; 
  602.                                          
  603.                                         r3 = (r2 - r1 + 256) & 0xff; 
  604.                                         g3 = (g2 - g1 + 256) & 0xff; 
  605.                                         b3 = (b2 - b1 + 256) & 0xff; 
  606.                                          
  607.                                         IDAT.writeByte(r3); 
  608.                                         IDAT.writeByte(g3); 
  609.                                         IDAT.writeByte(b3); 
  610.                                          
  611.                                         r1 = r2; 
  612.                                         g1 = g2; 
  613.                                         b1 = b2; 
  614.                                         a1 = 0
  615.                                 } 
  616.                         } else { 
  617.                                 r1 = 0
  618.                                 g1 = 0
  619.                                 b1 = 0
  620.                                 a1 = 0
  621.                                 for(j=0;j < w;j++) { 
  622.                                         p = img.getPixel32(j,i); 
  623.                                          
  624.                                         a2 = p >> 24 & 0xff; 
  625.                                         r2 = p >> 16 & 0xff; 
  626.                                         g2 = p >> 8  & 0xff; 
  627.                                         b2 = p & 0xff; 
  628.                                          
  629.                                         r3 = (r2 - r1 + 256) & 0xff; 
  630.                                         g3 = (g2 - g1 + 256) & 0xff; 
  631.                                         b3 = (b2 - b1 + 256) & 0xff; 
  632.                                         a3 = (a2 - a1 + 256) & 0xff; 
  633.                                          
  634.                                         IDAT.writeByte(r3); 
  635.                                         IDAT.writeByte(g3); 
  636.                                         IDAT.writeByte(b3); 
  637.                                         IDAT.writeByte(a3); 
  638.                                          
  639.                                         r1 = r2; 
  640.                                         g1 = g2; 
  641.                                         b1 = b2; 
  642.                                         a1 = a2; 
  643.                                 } 
  644.                         } 
  645.                 } 
  646.         } 
  647.         private static var crcTable:Array
  648.         private static var crcTableComputed:Boolean = false
  649.         private static function writeChunk(png:ByteArray,  
  650.                         type:uint, data:ByteArray):void { 
  651.                 var c:uint
  652.                 if (!crcTableComputed) { 
  653.                         crcTableComputed = true
  654.                         crcTable = []; 
  655.                         for (var n:uint = 0; n < 256; n++) { 
  656.                                 c = n; 
  657.                                 for (var k:uint = 0; k < 8; k++) { 
  658.                                         if (c & 1) { 
  659.                                                 c = uint(uint(0xedb88320) ^  
  660.                                                         uint(c >>> 1)); 
  661.                                         } else { 
  662.                                                 c = uint(c >>> 1); 
  663.                                         } 
  664.                                 } 
  665.                                 crcTable[n] = c; 
  666.                         } 
  667.                 } 
  668.                 var len:uint = 0
  669.                 if (data != null) { 
  670.                         len = data.length; 
  671.                 } 
  672.                 png.writeUnsignedInt(len); 
  673.                 var p:uint = png.position; 
  674.                 png.writeUnsignedInt(type); 
  675.                 if ( data != null ) { 
  676.                         png.writeBytes(data); 
  677.                 } 
  678.                 var e:uint = png.position; 
  679.                 png.position = p; 
  680.                 c = 0xffffffff; 
  681.                 for (var i:int = 0; i < (e-p); i++) { 
  682.                         c = uint(crcTable[ 
  683.                                 (c ^ png.readUnsignedByte()) &    
  684.                                 0xff] ^ (c >>> 8)); 
  685.                 } 
  686.                 c = uint(c^uint(0xffffffff)); 
  687.                 png.position = e; 
  688.                 png.writeUnsignedInt(c); 
  689.         } 
noswf
  1. // forked from matsumos's 某Pad
  2. // forked from narutohyper's noFlashSeal(偽物)
  3. // forked from clockmaker's [Alternativa3D] Basic Template
  4. package {
  5.     import alternativa.engine3d.materials.MovieClipMaterial;
  6.     import alternativa.engine3d.materials.FillMaterial;
  7.     import alternativa.engine3d.primitives.Plane;
  8.     import flash.display.Shape;
  9.     import flash.events.KeyboardEvent;
  10.     import flash.events.MouseEvent;
  11.     import alternativa.engine3d.core.Object3D;
  12.     import alternativa.engine3d.primitives.Box;
  13.     import alternativa.engine3d.primitives.Cone;
  14.     import alternativa.engine3d.events.MouseEvent3D;
  15.     import alternativa.types.Point3D;
  16.     import flash.display.Sprite;
  17.     import flash.display.MovieClip;
  18.     import flash.display.BitmapData;
  19.     import flash.display.Loader;
  20.     import flash.net.URLRequest; 
  21.     import flash.geom.Matrix; 
  22.     import flash.events.Event;
  23.     import flash.utils.ByteArray; 
  24.     import flash.net.*;
  25.     import org.libspark.thread.EnterFrameThreadExecutor;
  26.     import org.libspark.thread.Thread; 
  27.     [SWF(width = 465, height = 465, frameRate = 60)]
  28.     /*
  29.     自分用に作っただけなので、特に深い意図は無いです、
  30.     中に配置する画像(背景が透明のPNG)をロード。
  31.     */
  32.     /**
  33.      * Alternativa3D を簡単に扱うためのベーシックテンプレート
  34.      * @author Yasu (clockmaker)
  35.      */
  36.     public class SimpleDemo extends Sprite {
  37.         public var img1:Loader
  38.         public var template:BasicTemplate
  39.         public var mc1:MovieClip;
  40.         public var plane:Plane;
  41.         public function SimpleDemo():void {
  42.             // テンプレートを作成します
  43.             
  44.             var bg:Shape = new Shape();
  45.             bg.graphics.beginFill(0x0);
  46.             bg.graphics.drawRect(110200200200);
  47.             bg.graphics.endFill();
  48.             addChild(bg);
  49.             
  50.             var template:BasicTemplate = new BasicTemplate();
  51.             addChild(template);
  52.             template.camera.coords=new Point3D(-800,-800,700);
  53.             Thread.initialize(new EnterFrameThreadExecutor());
  54.             new MainThread(this).start();
  55.             
  56.             img1 = new Loader(); 
  57.             //img2 = new Loader(); 
  58.             img1.load(new URLRequest('http://assets.wonderfl.net/images/related_images/a/a2/a235/a23539b3cebd3c86ed745a2ae2afae32bfa32bfc')); 
  59.             addChild(img1);
  60.             //img1.alpha = 0.2;
  61.             img1.x = (465 - 341) >> 1;
  62.             img1.y = (465 - 450) >> 1;
  63.             
  64.             mc1=new MovieClip()
  65.             //MovieClipMaterialのスケーリングにバグがあるので、以下の対処方で処理
  66.             var clipWidth:int = 200;
  67.             var clipHeight:int = 200;
  68.             mc1.scaleY = clipWidth/clipHeight;
  69.             var matrix:Matrix = new Matrix(100, clipWidth/clipHeight); 
  70.             var mcm1:MovieClipMaterial=new MovieClipMaterial(mc1,clipWidth,clipWidth,null,matrix,true)
  71.             // プリミティブを作成します
  72.             plane = new Plane(2002002020);
  73.             plane.rotationX = 90;
  74.             plane.setMaterialToSurface(mcm1, 'front');
  75.             
  76.             template.scene.root.addChild(plane);
  77.             var pos:Point3D = new Point3D();
  78.             pos.x = plane.x;
  79.             pos.y = plane.y; 
  80.             pos.z = plane.z;
  81.             template.cameraContoller.lookAt(pos);
  82.             
  83.             with(template.camera) {
  84.                 x =  907.2257738784996
  85.                 y =  -2246.0827362651494
  86.                 z =  -1944.3373132335682
  87.                 rotationX =  -0.863937979737193
  88.                 rotationY =  -0.11999999999999998
  89.                 rotationZ =  0.2617993877991494
  90.                 fov =  0.22689280275926252
  91.             }
  92.             
  93.             stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDownHandler);
  94.             stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
  95.             stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDown);
  96.             
  97.             stage.addEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler );
  98.             stage.addEventListener( MouseEvent.MOUSE_UP, mouseUpHandler );
  99.             stage.addEventListener( Event.MOUSE_LEAVE, mouseUpHandler );
  100.         }
  101.         private function mouseUpHandler(e:Event):void 
  102.         {
  103.             stage.removeEventListener( MouseEvent.MOUSE_MOVE, moveHandler );
  104.             stage.removeEventListener( MouseEvent.MOUSE_MOVE, scaleHandler );
  105.         }
  106.         
  107.         private function mouseDownHandler(e:MouseEvent):void 
  108.         {
  109.             pastX = mouseX;
  110.             pastY = mouseY;
  111.             
  112.             if (shiftKey) {
  113.                 stage.addEventListener( MouseEvent.MOUSE_MOVE, scaleHandler );
  114.             }
  115.             else {
  116.                 stage.addEventListener( MouseEvent.MOUSE_MOVE, moveHandler );
  117.             }
  118.         }
  119.         
  120.         private function scaleHandler(e:MouseEvent):void 
  121.         {
  122.             plane.scaleX += ( pastY - mouseY ) * .01;
  123.             plane.scaleY = plane.scaleX;
  124.             pastY = mouseY;
  125.         }
  126.         
  127.         private var pastX:Number = 0;
  128.         private var pastY:Number = 0;
  129.         private var shiftKey:Boolean = false;
  130.         
  131.         private function moveHandler(e:MouseEvent):void 
  132.         {
  133.             plane.x -= pastX - mouseX;
  134.             plane.y -= pastY - mouseY;
  135.             
  136.             pastX = mouseX;
  137.             pastY = mouseY;
  138.         }
  139.         
  140.         private function KeyDownHandler(e:KeyboardEvent):void {
  141.             if (e.keyCode == 16) {
  142.                 shiftKey = true;
  143.             }
  144.         }
  145.         
  146.         private function keyUpHandler(e:KeyboardEvent):void {
  147.             if (e.keyCode == 16) {
  148.                 shiftKey = false;
  149.             }
  150.         }
  151.         
  152.         private function KeyDown(e:KeyboardEvent):void{
  153.             if (e.keyCode == 38) {
  154.                 plane.z += 2
  155.             }
  156.             if (e.keyCode == 37) {
  157.                 plane.x -= 2
  158.             }
  159.             if (e.keyCode == 40) {
  160.                 plane.z -= 2
  161.             }
  162.             if (e.keyCode == 39) {
  163.                 plane.x += 2
  164.             }
  165.         }
  166.     }
  167. }
  168. import org.libspark.thread.Thread;
  169. import flash.events.Event;
  170. import flash.events.MouseEvent;
  171. import flash.display.SimpleButton;
  172. import flash.utils.ByteArray; 
  173. import flash.net.FileReference;
  174. import flash.net.FileReferenceList;
  175. import flash.geom.Matrix;
  176. import flash.geom.Point;
  177. import flash.geom.Rectangle;
  178. import flash.display.*;
  179. import flash.text.*;
  180. import flash.system.Security;
  181. class MainThread extends Thread {
  182.     public static const CROSSDOMAIN:String = "http://assets.wonderfl.net/crossdomain.xml";
  183.     private var _base:SimpleDemo;
  184.     private var _loadButton:Button;
  185.     private var _saveButton:Button;
  186.     private var _original:Loader;
  187.     private var _guide:Guide;
  188.     private var bmd1:BitmapData
  189.     private var bm1:Bitmap
  190.     public function MainThread(base:SimpleDemo) {
  191.         this._base = base;
  192.         this._loadButton = this._base.addChild(new Button('LOAD'60)) as Button;
  193.         this._loadButton.x = this._loadButton.y = 1;
  194.         this._saveButton = this._base.addChild(new Button('SAVE'60)) as Button;
  195.         this._saveButton.x = 1;
  196.         this._saveButton.y = this._loadButton.height + 2;
  197.         this._guide = this._base.addChild(new Guide()) as Guide;
  198.         this._guide.x = (465 - 341) >> 1;
  199.         this._guide.y = (465 - 450) >> 1;
  200.         this.bmd1=new BitmapData(200,200,false,0x0)
  201.         this.bm1=new Bitmap()
  202.     }
  203.     protected override function run():void {
  204.         this._event();
  205.     }
  206.     private function _event():void {
  207.         event(this._loadButton, MouseEvent.CLICK, this._loadImage);
  208.         event(this._saveButton, MouseEvent.CLICK, this._saveImage);
  209.         Security.loadPolicyFile(CROSSDOMAIN);
  210.     }
  211.     // load image
  212.     private function _loadImage(e:MouseEvent):void {
  213.         trace(Button(e.target).label)
  214.         var file:FileReference = new FileReference();
  215.         event(file, Event.SELECT, this._loadFileSelected);
  216.         event(file, Event.CANCEL, this._loadFileCancel);
  217.         file.browse();
  218.     }
  219.     private function _loadFileSelected(e:Event):void {
  220.         var file:FileReference = FileReference(e.target);
  221.         event(file, Event.COMPLETE, this._fileLoaded);
  222.         file.load();
  223.     }
  224.     
  225.     private function _fileLoaded(e:Event):void {
  226.         if (this._original) {
  227.             //this._original.parent.removeChild(this._original);
  228.             this._original.unload();
  229.         }
  230.         this._original = new Loader();
  231.         this._original.loadBytes(FileReference(e.target).data);
  232.         event(this._original.contentLoaderInfo, Event.COMPLETE, this._imageLoaded);
  233.     }
  234.     private function _loadFileCancel(e:Event):void {
  235.         this._event();
  236.     }
  237.     
  238.     private function _imageLoaded(e:Event):void {
  239.         var a:Number = 200 / this._original.height;
  240.         var mtr:Matrix=new Matrix(a,0,0,a,(200-(this._original.width*a))/2,(200-(this._original.height*a))/2)
  241.         this.bmd1=new BitmapData(200,200,false,0x0)
  242.         this.bmd1.draw(this._original.content,mtr)
  243.         this.bm1.bitmapData=bmd1
  244.         this._base.mc1.addChild(this.bm1)
  245.         this._event();
  246.     }
  247.     
  248.     // save image 
  249.     private function _saveImage(e:Event):void { 
  250.         var raw:BitmapData = new BitmapData(341450true, 0x0); 
  251.         this._guide.visible = false;
  252.         raw.draw(this._base, new Matrix(1001, -this._guide.x, -this._guide.y), nullnullnulltrue);
  253.         this._guide.visible = true;
  254.         var png:ByteArray = PNGEnc.encode(raw); 
  255.         raw.dispose(); 
  256.         var file:FileReference = new FileReference(); 
  257.         event(file, Event.SELECT, this._saveFileSelected); 
  258.         event(file, Event.CANCEL, this._saveFileSelected);
  259.         file.save(png, 'pad.png'); 
  260.     }
  261.     private function _saveFileSelected(e:Event):void { 
  262.         this._event();
  263.     } 
  264. }
  265. class Guide extends Shape {
  266.     
  267.     public function Guide() {
  268.         var g:Graphics = this.graphics;
  269.         g.lineStyle(1, 0xFFFFFF, 0.3true);
  270.         g.drawRect(00341450);
  271.     }
  272. }
  273. class Button extends SimpleButton {
  274.     public var label:String
  275.     public function Button(_label:String, width:int = 0):void {
  276.         label=_label
  277.         var up:Sprite = _buildImage(label, 0x0, width);
  278.         var over:Sprite = _buildImage(label, 0x333333, width);
  279.         var down:Sprite = _buildImage(label, 0x333333, width);
  280.         down.y = 1;
  281.         super(up, over, down, up);
  282.     }
  283.     
  284.     private static function _buildImage(label:String, color:int, width:int = 0):Sprite {
  285.         var text:TextField = new TextField();
  286.         text.defaultTextFormat = new TextFormat('Verdana'10, 0xffffff, truenullnullnullnull, TextFormatAlign.CENTER);
  287.         text.autoSize = TextFieldAutoSize.LEFT
  288.         text.selectable = false;
  289.         text.text = label;
  290.         text.x = (width - text.width) >> 1;
  291.         text.y = 5;
  292.         var base:Shape = new Shape();
  293.         var g:Graphics = base.graphics;
  294.         g.beginFill(color);
  295.         g.drawRect(00, width, text.height + 10);
  296.         g.endFill();
  297.         var sp:Sprite = new Sprite();
  298.         sp.addChild(base);
  299.         sp.addChild(text);
  300.         return sp;
  301.     }
  302. }
  303. import alternativa.engine3d.controllers.CameraController;
  304. import alternativa.engine3d.core.Camera3D;
  305. import alternativa.engine3d.core.Object3D;
  306. import alternativa.engine3d.core.Scene3D;
  307. import alternativa.engine3d.display.View;
  308. import flash.display.Sprite;
  309. import flash.display.StageAlign;
  310. import flash.display.StageQuality;
  311. import flash.display.StageScaleMode;
  312. import flash.events.Event;
  313. /**
  314.  * BasicTemplate for Alternativa3D
  315.  * Alternativa3Dを扱いやすくするためのテンプレートです
  316.  * @author Yasu
  317.  */
  318. class BasicTemplate extends Sprite{
  319.     /**
  320.      * シーンインスタンスです。
  321.      */
  322.     public var scene:Scene3D;
  323.     /**
  324.      * ビューインスタンスです。
  325.      */
  326.     public var view:View;
  327.     /**
  328.      * カメラインスタンスです。
  329.      */
  330.     public var camera:Camera3D;
  331.     /**
  332.      * カメラコントローラーです。
  333.      */
  334.     public var cameraContoller:CameraController;
  335.     
  336.     private var _viewWidth:int;
  337.     private var _viewHeight:int;
  338.     private var _scaleToStage:Boolean;
  339.     /**
  340.      * 新しい BasicTemplate インスタンスを作成します。
  341.      * @param    viewWidth
  342.      * @param    viewHeight
  343.      * @param    scaleToStage
  344.      */
  345.     public function BasicTemplate(viewWidth:int=640, viewHeight:int=480, scaleToStage:Boolean = true) {
  346.         _viewWidth = viewWidth;
  347.         _viewHeight = viewHeight;
  348.         _scaleToStage = scaleToStage;
  349.         
  350.         // Creating scene
  351.         scene = new Scene3D();
  352.         scene.splitAnalysis = false// not analysis for performance
  353.         scene.root = new Object3D();
  354.         
  355.         // Adding camera
  356.         camera = new Camera3D();
  357.         camera.z = -1000;
  358.         scene.root.addChild(camera);
  359.         
  360.         // camera contoller
  361.         cameraContoller = new CameraController(this);
  362.         cameraContoller.camera = camera;
  363.         
  364.         // set view
  365.         view = new View();
  366.         view.camera = camera;
  367.         addChild(view);
  368.         
  369.         // stage
  370.         if (stage) init();
  371.         else addEventListener(Event.ADDED_TO_STAGE, init);
  372.     }
  373.     
  374.     /**
  375.      * 初期化されたときに実行されるイベントです。
  376.      * 初期化時に実行したい処理をオーバーライドして記述します。
  377.      */
  378.     protected function atInit():void {}
  379.     
  380.     /**
  381.      * 初期化されたときに実行されるイベントです。
  382.      * 初期化時に実行したい処理を記述します。
  383.      */
  384.     private var _onInit:Function = function():void { };
  385.     public function get onInit():Function { return _onInit; }
  386.     public function set onInit(value:Function):void {
  387.         _onInit = value;
  388.     }
  389.     
  390.     /**
  391.      * Event.ENTER_FRAME 時に実行されるレンダリングのイベントです。
  392.      * レンダリング前に実行したい処理をオーバーライドして記述します。
  393.      */
  394.     protected function atPreRender():void {}
  395.     
  396.     /**
  397.      * Event.ENTER_FRAME 時に実行されるレンダリングのイベントです。
  398.      * レンダリング前に実行したい処理を記述します。
  399.      */
  400.     private var _onPreRender:Function = function():void{};
  401.     public function get onPreRender():Function { return _onPreRender; }
  402.     public function set onPreRender(value:Function):void {
  403.         _onPreRender = value;
  404.     }
  405.     
  406.     /**
  407.      * Event.ENTER_FRAME 時に実行されるレンダリングのイベントです。
  408.      * レンダリング後に実行したい処理をオーバーライドして記述します。
  409.      */
  410.     protected function atPostRender():void {
  411.     }
  412.     
  413.     /**
  414.      * Event.ENTER_FRAME 時に実行されるレンダリングのイベントです。
  415.      * レンダリング後に実行したい処理を記述します。
  416.      */
  417.     protected var _onPostRender:Function = function():void{};
  418.     public function get onPostRender():Function { return _onPostRender; }
  419.     public function set onPostRender(value:Function):void {
  420.         _onPostRender = value;
  421.     }
  422.     
  423.     /**
  424.      * レンダリングを開始します。
  425.      */
  426.     public function startRendering():void {
  427.         addEventListener(Event.ENTER_FRAME, onRenderTick);
  428.     }
  429.     /**
  430.      * レンダリングを停止します。
  431.      */
  432.     public function stopRendering():void {
  433.         removeEventListener(Event.ENTER_FRAME, onRenderTick);
  434.     }
  435.     
  436.     /**
  437.      * シングルレンダリング(レンダリングを一回だけ)を実行します。
  438.      */
  439.     public function singleRender():void {
  440.         onRenderTick();
  441.     }
  442.     
  443.     /**
  444.      * @private
  445.      */
  446.     private function init(e:Event = null):void {
  447.         stage.scaleMode = StageScaleMode.NO_SCALE;
  448.         stage.align = StageAlign.TOP_LEFT;
  449.         stage.quality = StageQuality.HIGH;
  450.         // resize
  451.         stage.addEventListener(Event.RESIZE, onResize);
  452.         onResize(null);
  453.         
  454.         // render
  455.         startRendering();
  456.         
  457.         atInit();
  458.         _onInit();
  459.         
  460.     }
  461.     
  462.     /**
  463.      * @private
  464.      */
  465.     private function onRenderTick(e:Event = null):void {
  466.         atPostRender();
  467.         _onPostRender();
  468.         scene.calculate();
  469.         atPreRender();
  470.         _onPreRender();
  471.     }
  472.     
  473.     /**
  474.      * @private
  475.      */
  476.     private function onResize(event:Event = null):void {
  477.         if (_scaleToStage) {
  478.             view.width = stage.stageWidth;
  479.             view.height = stage.stageHeight;
  480.         }else {
  481.             view.width = _viewWidth;
  482.             view.height = _viewHeight;
  483.         }
  484.     }
  485. }
  486. // http://www.5etdemi.com/blog/archives/2006/12/as3-png-encoder-faster-better/ 
  487. class PNGEnc { 
  488.          
  489.         public static function encode(img:BitmapData, type:uint = 0):ByteArray { 
  490.                  
  491.                 // Create output byte array 
  492.                 var png:ByteArray = new ByteArray(); 
  493.                 // Write PNG signature 
  494.                 png.writeUnsignedInt(0x89504e47); 
  495.                 png.writeUnsignedInt(0x0D0A1A0A); 
  496.                 // Build IHDR chunk 
  497.                 var IHDR:ByteArray = new ByteArray(); 
  498.                 IHDR.writeInt(img.width); 
  499.                 IHDR.writeInt(img.height); 
  500.                 if(img.transparent || type == 0
  501.                 { 
  502.                         IHDR.writeUnsignedInt(0x08060000); // 32bit RGBA 
  503.                 } 
  504.                 else 
  505.                 { 
  506.                         IHDR.writeUnsignedInt(0x08020000); //24bit RGB 
  507.                 } 
  508.                 IHDR.writeByte(0); 
  509.                 writeChunk(png,0x49484452,IHDR); 
  510.                 // Build IDAT chunk 
  511.                 var IDAT:ByteArray= new ByteArray(); 
  512.                  
  513.                 switch(type) 
  514.                 { 
  515.                         case 0
  516.                                 writeRaw(img, IDAT); 
  517.                                 break
  518.                         case 1
  519.                                 writeSub(img, IDAT); 
  520.                                 break
  521.                 } 
  522.                  
  523.                 IDAT.compress(); 
  524.                 writeChunk(png,0x49444154,IDAT); 
  525.                 // Build IEND chunk 
  526.                 writeChunk(png,0x49454E44,null); 
  527.                 // return PNG 
  528.                  
  529.                  
  530.                  
  531.                 return png; 
  532.         } 
  533.          
  534.         private static function writeRaw(img:BitmapData, IDAT:ByteArray):void 
  535.         { 
  536.                 var h:int = img.height; 
  537.                 var w:int = img.width; 
  538.                 var transparent:Boolean = img.transparent; 
  539.                  
  540.                 for(var i:int=0;i < h;i++) { 
  541.                         // no filter 
  542.                         if ( !transparent ) { 
  543.                                 var subImage:ByteArray = img.getPixels( 
  544.                                         new Rectangle(0, i, w, 1)); 
  545.                                 //Here we overwrite the alpha value of the first pixel 
  546.                                 //to be the filter 0 flag 
  547.                                 subImage[0] = 0
  548.                                 IDAT.writeBytes(subImage); 
  549.                                 //And we add a byte at the end to wrap the alpha values 
  550.                                 IDAT.writeByte(0xff); 
  551.                         } else { 
  552.                                 IDAT.writeByte(0); 
  553.                                 var p:uint
  554.                                 for(var j:int=0;j < w;j++) { 
  555.                                         p = img.getPixel32(j,i); 
  556.                                         IDAT.writeUnsignedInt( 
  557.                                                 uint(((p&0xFFFFFF) << 8)| 
  558.                                                 (p>>>24))); 
  559.                                 } 
  560.                         } 
  561.                 } 
  562.         } 
  563.          
  564.         private static function writeSub(img:BitmapData, IDAT:ByteArray):void 
  565.         { 
  566.                 var r1:uint
  567.                 var g1:uint
  568.                 var b1:uint
  569.                 var a1:uint
  570.                  
  571.                 var r2:uint
  572.                 var g2:uint
  573.                 var b2:uint
  574.                 var a2:uint
  575.                  
  576.                 var r3:uint
  577.                 var g3:uint
  578.                 var b3:uint
  579.                 var a3:uint
  580.                  
  581.                 var p:uint
  582.                 var h:int = img.height; 
  583.                 var w:int = img.width; 
  584.                  
  585.                 for(var i:int=0;i < h;i++) { 
  586.                         // no filter 
  587.                         IDAT.writeByte(1); 
  588.                         if ( !img.transparent ) { 
  589.                                 r1 = 0
  590.                                 g1 = 0
  591.                                 b1 = 0
  592.                                 a1 = 0xff; 
  593.                                 for(var j:int=0;j < w;j++) { 
  594.                                         p = img.getPixel(j,i); 
  595.                                          
  596.                                         r2 = p >> 16 & 0xff; 
  597.                                         g2 = p >> 8  & 0xff; 
  598.                                         b2 = p & 0xff; 
  599.                                          
  600.                                         r3 = (r2 - r1 + 256) & 0xff; 
  601.                                         g3 = (g2 - g1 + 256) & 0xff; 
  602.                                         b3 = (b2 - b1 + 256) & 0xff; 
  603.                                          
  604.                                         IDAT.writeByte(r3); 
  605.                                         IDAT.writeByte(g3); 
  606.                                         IDAT.writeByte(b3); 
  607.                                          
  608.                                         r1 = r2; 
  609.                                         g1 = g2; 
  610.                                         b1 = b2; 
  611.                                         a1 = 0
  612.                                 } 
  613.                         } else { 
  614.                                 r1 = 0
  615.                                 g1 = 0
  616.                                 b1 = 0
  617.                                 a1 = 0
  618.                                 for(j=0;j < w;j++) { 
  619.                                         p = img.getPixel32(j,i); 
  620.                                          
  621.                                         a2 = p >> 24 & 0xff; 
  622.                                         r2 = p >> 16 & 0xff; 
  623.                                         g2 = p >> 8  & 0xff; 
  624.                                         b2 = p & 0xff; 
  625.                                          
  626.                                         r3 = (r2 - r1 + 256) & 0xff; 
  627.                                         g3 = (g2 - g1 + 256) & 0xff; 
  628.                                         b3 = (b2 - b1 + 256) & 0xff; 
  629.                                         a3 = (a2 - a1 + 256) & 0xff; 
  630.                                          
  631.                                         IDAT.writeByte(r3); 
  632.                                         IDAT.writeByte(g3); 
  633.                                         IDAT.writeByte(b3); 
  634.                                         IDAT.writeByte(a3); 
  635.                                          
  636.                                         r1 = r2; 
  637.                                         g1 = g2; 
  638.                                         b1 = b2; 
  639.                                         a1 = a2; 
  640.                                 } 
  641.                         } 
  642.                 } 
  643.         } 
  644.         private static var crcTable:Array
  645.         private static var crcTableComputed:Boolean = false
  646.         private static function writeChunk(png:ByteArray,  
  647.                         type:uint, data:ByteArray):void { 
  648.                 var c:uint
  649.                 if (!crcTableComputed) { 
  650.                         crcTableComputed = true
  651.                         crcTable = []; 
  652.                         for (var n:uint = 0; n < 256; n++) { 
  653.                                 c = n; 
  654.                                 for (var k:uint = 0; k < 8; k++) { 
  655.                                         if (c & 1) { 
  656.                                                 c = uint(uint(0xedb88320) ^  
  657.                                                         uint(c >>> 1)); 
  658.                                         } else { 
  659.                                                 c = uint(c >>> 1); 
  660.                                         } 
  661.                                 } 
  662.                                 crcTable[n] = c; 
  663.                         } 
  664.                 } 
  665.                 var len:uint = 0
  666.                 if (data != null) { 
  667.                         len = data.length; 
  668.                 } 
  669.                 png.writeUnsignedInt(len); 
  670.                 var p:uint = png.position; 
  671.                 png.writeUnsignedInt(type); 
  672.                 if ( data != null ) { 
  673.                         png.writeBytes(data); 
  674.                 } 
  675.                 var e:uint = png.position; 
  676.                 png.position = p; 
  677.                 c = 0xffffffff; 
  678.                 for (var i:int = 0; i < (e-p); i++) { 
  679.                         c = uint(crcTable[ 
  680.                                 (c ^ png.readUnsignedByte()) &    
  681.                                 0xff] ^ (c >>> 8)); 
  682.                 } 
  683.                 c = uint(c^uint(0xffffffff)); 
  684.                 png.position = e; 
  685.                 png.writeUnsignedInt(c); 
  686.         } 
noswf
Get Adobe Flash Player