Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package {
  2.     import flash.display.DisplayObject;
  3.     import flash.display.Bitmap;
  4.     import flash.display.BitmapData;
  5.    
  6.     import flash.utils.getTimer;
  7.    
  8.     public class EdgeExtractor {
  9.         private var _inputImage:BitmapData;
  10.         private var _transformBuffer:BitmapData;
  11.         private var _countBuffer:BitmapData;
  12.         private var _result:Bitmap;
  13.        
  14.         private var _frameMS:int;
  15.         private var _totalScans:int;
  16.         private var _tolerance:int;
  17.        
  18.         private var _currentScan:int;
  19.         private var _imgWidth:int;
  20.         private var _imgHeight:int;
  21.        
  22.         private var _notify:Function;
  23.         private var _running:Boolean;
  24.        
  25.         public function EdgeExtractor(){
  26.            
  27.         }
  28.        
  29.         public function Init(framerate:int, quality:Number, threshold:Number){
  30.             _frameMS = 1000 / framerate;
  31.             _totalScans = Math.round(quality * 255.0);
  32.             _tolerance = Math.round(threshold * _totalScans);
  33.            
  34.             _result = null;
  35.         }
  36.        
  37.         public function RunExtract(dobj:DisplayObject, notify:Function){
  38.             _currentScan = 0;
  39.             _notify = notify;
  40.            
  41.             _imgWidth = Math.round(dobj.width);
  42.             _imgHeight = Math.round(dobj.height);
  43.            
  44.             _inputImage = new BitmapData(_imgWidth, _imgHeight, false, 0);
  45.             _transformBuffer = new BitmapData(_imgWidth, _imgHeight, false, 0);
  46.             _countBuffer = new BitmapData(_imgWidth, _imgHeight, false, 0);
  47.            
  48.             _inputImage.draw(dobj);
  49.            
  50.             _running = true;
  51.         }
  52.        
  53.         public function GetResult():DisplayObject{
  54.             return _result;
  55.         }
  56.        
  57.         public function Update(){
  58.             var endTime:int;
  59.            
  60.             if(_running){
  61.                 endTime = getTimer() + _frameMS;
  62.                
  63.                 while(getTimer() < endTime){
  64.                     Scan();
  65.                    
  66.                     if(_currentScan >= _totalScans){
  67.                         Finish();
  68.                        
  69.                         _running = false;
  70.                         _notify(true, 1.0);
  71.                        
  72.                         break;
  73.                     } else {
  74.                         _notify(false, _currentScan / _totalScans);
  75.                     }
  76.                 }
  77.             }
  78.         }
  79.        
  80.         private function Scan(){
  81.             _transformBuffer.draw(_inputImage);
  82.            
  83.             var px:int;
  84.             var py:int;
  85.            
  86.             var w:int = _inputImage.width;
  87.             var h:int = _inputImage.height;
  88.            
  89.             var scanWidth:int = Math.floor(255.0 * (_currentScan / _totalScans));
  90.            
  91.             var r:int;
  92.             var g:int;
  93.             var b:int;
  94.             var color:uint;
  95.            
  96.             var diff:Boolean;
  97.            
  98.             for(py = 0; py < h; py++){
  99.                 for(px = 0; px < w; px++){
  100.                     color = _transformBuffer.getPixel(px, py);
  101.                     r = (color >> 16) & 0xFF;
  102.                     g = (color >> 8) & 0xFF;
  103.                     b = color & 0xFF;
  104.                    
  105.                     r = Math.floor(r / scanWidth) * scanWidth;
  106.                     g = Math.floor(g / scanWidth) * scanWidth;
  107.                     b = Math.floor(b / scanWidth) * scanWidth;
  108.                    
  109.                     color = (r << 16) + (g << 8) + b;
  110.                     _transformBuffer.setPixel(px, py, color);
  111.                 }
  112.             }
  113.            
  114.             for(py = 1; py < h; py++){
  115.                 for(px = 1; px < w; px++){
  116.                     color = _transformBuffer.getPixel(px, py);
  117.                    
  118.                     if((color != _transformBuffer.getPixel(px-1, py)) || (color != _transformBuffer.getPixel(px, py-1))){
  119.                         _countBuffer.setPixel(px, py, _countBuffer.getPixel(px, py) + 1);
  120.                     }
  121.                 }
  122.             }
  123.            
  124.             _currentScan++;
  125.         }
  126.        
  127.         private function Finish(){
  128.             var outBuffer:BitmapData = new BitmapData(_inputImage.width, _inputImage.height, false, 0xFFFFFF);
  129.            
  130.             var px:int;
  131.             var py:int;
  132.            
  133.             var w:int = _inputImage.width;
  134.             var h:int = _inputImage.height;
  135.            
  136.             for(py = 0; py < h; py++){
  137.                 for(px = 0; px < w; px++){
  138.                     if(_countBuffer.getPixel(px, py) >= _tolerance){
  139.                         outBuffer.setPixel(px, py, 0);
  140.                     }
  141.                 }
  142.             }
  143.            
  144.             _result = new Bitmap(outBuffer);
  145.         }
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement