1  
     8  
     9   import com.sekati.core.App;
    10   import com.sekati.display.IBitmapImageClip;
    11   import com.sekati.display.BaseClip;
    12   import com.sekati.utils.Delegate;
    13   import com.sekati.utils.MovieClipUtils;
    14   import com.sekati.except.FatalException;
    15   import flash.display.BitmapData;
    16   import flash.geom.ColorTransform;
    17   import flash.geom.Matrix;
    18   import flash.geom.Rectangle;
    19  
    20  
    40  class com.sekati.display.BitmapImageDistributedSliceClip extends BaseClip implements IBitmapImageClip {
    41  	
    42  	public var img:MovieClip;
    43  	public var bmp:Array;
    44  	private var _uri:String;
    45  	private var _smooth:Boolean;
    46  	private var _progressCb:Function;
    47  	private var _initCb:Function;
    48  	private var _errorCb:Function;
    49  	private var _loader:MovieClipLoader;
    50  	private var _tmp:MovieClip;
    51  	
    52  	
    55  	public function BitmapImageDistributedSliceClip() {
    56  		super();
    57  		_loader = new MovieClipLoader();
    58  		_this.onLoadInit = Delegate.create(_this, _onInit);
    59  		_this.onLoadError = Delegate.create(_this, _onError);
    60  		_this.onLoadProgress = Delegate.create(_this, _onProgress);
    61  		_loader.addListener(_this);
    62  	}
    63  	
    64  	
    73  	public function load(uri:String, isSmoothed:Boolean, onInit:Function, onProgress:Function, onError:Function):Void {
    74  		_uri = uri;
    75  		_smooth = isSmoothed;
    76  		_progressCb = onProgress;
    77  		_initCb = onInit;
    78  		_errorCb = onError;
    79  		createContainers();
    80  		_loader.loadClip(_uri, _tmp);	
    81  	}
    82  	
    83  	
    87  	public function unload():Void {
    88  		_loader.unloadClip(img);
    89  		bmp.dispose();
    90  		MovieClipUtils.rmClip(_tmp); 
    91  		MovieClipUtils.rmClip(img);
    92  	}
    93  
    94  	
    98  	private function createContainers():Void {
    99  		if (bmp.length > 0) {
   100  			for (var i:Number = 0; i < bmp.length; i++) bmp[i].dispose();
   101  		}
   102  		if (_tmp) MovieClipUtils.rmClip(_tmp); 
   103  		if (img) MovieClipUtils.rmClip(img);
   104  		img = _this.createEmptyMovieClip("img", _this.getNextHighestDepth());
   105  		_tmp = _this.createEmptyMovieClip("_tmp", _this.getNextHighestDepth());
   106  		bmp = new Array();
   107  		
   108  		_tmp._visible = false;
   109  		_this._alpha = 0;
   110  	}
   111  	
   112  	
   116  	private function clearCallbacks():Void {
   117  		_initCb = undefined;
   118  		_progressCb = undefined;
   119  		_errorCb = undefined;		
   120  	}
   121  	
   122  	private function _onInit():Void {
   123  		var sw:Number = _tmp._width;
   124  		var sh:Number = _tmp._height;
   125  		var sliceWidth:Number = 1000;
   126  		var sliceHeight:Number = sh;
   127  		var sliceNum:Number = Math.ceil (sw / sliceWidth);
   128  		var rect:Rectangle = new Rectangle (0, 0, sliceWidth, sliceHeight);
   129  		var ct:ColorTransform = new ColorTransform ();		
   130  		
   131  		App.log.info(_this, "### Source Image (" + sw + "x" + sh + ") will result in: " + sliceNum + " slices @ " + sliceWidth + " px/slice (*including overflow)");
   132  		
   133  		var i:Number = 0;
   134  		var createSlice:Function = function():Void {
   135  			if (i >= sliceNum) {
   136  				clearInterval (sliceInterval);
   137  				App.log.info (_this, "Bitmap:Image slice assembly completed successfully: " + img._width + "x" + img._height);
   138  				MovieClipUtils.rmClip(_tmp);
   139  				_initCb();
   140  				clearCallbacks();				
   141  			}
   142  			var xslice:Number = sliceWidth * i;
   143  			var diff:Number = 0;
   144  			if (i == (sliceNum - 1) && (xslice + sliceWidth) > _tmp._width) {
   145  				App.log.warn(_this, "WARNING: Bitmap:Image -> Buffer Overflow !!!");
   146  				diff = sliceWidth - (_tmp._width - xslice);
   147  				rect = new Rectangle (0, 0, sliceWidth - diff, sliceHeight);
   148  				App.log.status(_this, "Adjusting for overflow: New sliceWidth: " + (sliceWidth - diff));
   149  			}
   150  			var b:BitmapData = new BitmapData ((sliceWidth - diff), sliceHeight, true, 0x00FFFFFF);
   151  			var m:Matrix = new Matrix ();
   152  			m.translate (-i * sliceWidth, 0);
   153  			b.draw (_tmp, m, ct, "normal", rect, true);
   154  			var container:MovieClip = img.createEmptyMovieClip ("bmp" + i, img.getNextHighestDepth ());
   155  			container.attachBitmap (b, container.getNextHighestDepth(), "auto", _smooth);
   156  			container._x = xslice;
   157  			bmp.push(b);
   158  			App.log.info(_this, "Attaching bitmapData slice: " + i + " (" + container + "._x:" + container._x + ") slice section: " + xslice + " - " + (xslice + sliceWidth - diff));
   159  			i++;
   160  		};
   161  		var sliceInterval:Number = setInterval (Delegate.create(_this,createSlice), 5);
   162  	}	
   163  	
   164  	
   165  	
   170  	private function _onInitOLD():Void {
   171  		var sw:Number = _tmp._width;
   172  		var sh:Number = _tmp._height;
   173  		var sliceWidth:Number = 2000;
   174  		var slices:Number = Math.ceil( sw / sliceWidth );
   175  		var rect:Rectangle = new Rectangle( 0, 0, sliceWidth, sh );
   176  		var ct:ColorTransform = new ColorTransform();
   177  		
   178  		App.log.info("foo", "### Source Image ("+sw+"x"+sh+") results in: "+slices+" slices @ "+sliceWidth+" px/slice (including overflow)");
   179  		
   180  		
   181  		for (var i:Number = 0; i < slices; i++) {
   182  			var xslice:Number = sliceWidth * i;
   183  			var diff:Number = 0;
   184  			if (i == (slices - 1) && (xslice + sliceWidth) > _tmp._width) {
   185  				App.log.warn(_this, "!!! BITMAP:IMAGE -> BUFFER OVERFLOW !!!");
   186  				diff = sliceWidth-(_tmp._width - xslice);
   187  				rect = new Rectangle(0, 0, sliceWidth-diff, sh);
   188  				App.log.info(_this, "Adjusting for overflow: New sliceWidth: "+(sliceWidth-diff));
   189  			}
   190  			var b:BitmapData = new BitmapData( (sliceWidth-diff), sh, true, 0x00FFFFFF );	
   191  			var m:Matrix = new Matrix();
   192  			m.translate( -i*sliceWidth, 0 );
   193  			b.draw( _tmp, m, ct, "normal", rect, true );	
   194  			var container:MovieClip = img.createEmptyMovieClip( "bmp"+i, img.getNextHighestDepth() );
   195  			container.attachBitmap( b, container.getNextHighestDepth(), "auto", _smooth );
   196  			container._x = xslice;
   197  			bmp.push(b);
   198  			App.log.info(_this, "Attaching bitmapData slice: "+i+" ("+container+"._x:"+container._x+") slice section: "+xslice+" - " +(xslice + sliceWidth - diff));	
   199  		}
   200  		
   201  		App.log.status(_this, ":Bitmap:Image slice assembly completed successfully: "+img._width+"x"+img._height);
   202  		
   203  		MovieClipUtils.rmClip(_tmp);
   204  		_initCb();
   205  		clearCallbacks();
   206  	}
   207  
   208  	
   215  	private function _onProgress(target:MovieClip, bytesLoaded:Number, bytesTotal:Number):Void {
   216  		
   217  		_progressCb(bytesLoaded, bytesTotal);
   218  	}
   219  
   220  	
   225  	private function _onError(target_mc:MovieClip, errorCode:String, httpStatus:Number):Void {
   226  		_errorCb(errorCode, httpStatus);
   227  		clearCallbacks();
   228  		throw new FatalException(_this, "Could not load image from url:"+_uri, arguments);
   229  	}
   230  }
   231