Skip to content

Instantly share code, notes, and snippets.

@stickupkid
Forked from didierbrun/ScaleBitmap.as
Created March 9, 2011 15:11
Show Gist options
  • Save stickupkid/862355 to your computer and use it in GitHub Desktop.
Save stickupkid/862355 to your computer and use it in GitHub Desktop.
/**
*
*
* ScaleBitmap
*
* @author Didier Brun
* @author Jerôme Decoster
* @author Simon Richardson
* @version 1.2
*
*/
package org.bytearray.display
{
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.geom.Matrix;
import flash.geom.Rectangle;
public class ScaleBitmap
{
private static const _matrix : Matrix = new Matrix();
private static const _widths : Vector.<int> = new Vector.<int>(3, true);
private static const _heights : Vector.<int> = new Vector.<int>(3, true);
//--------------------------------------
// PUBLIC
//--------------------------------------
/**
* @param bitmapData BitmapData source
* @param graphics Graphics to draw
* @param width Draw width
* @param height Draw height
* @param inner Inner rectangle (relative to 0,0)
* @param outer Outer rectangle (relative to 0,0)
* @param smooth If <code>false</code>, upscaled bitmap images are rendered by using a nearest-neighbor
* algorithm and look pixelated. If <code>true</code>, upscaled bitmap images are rendered by using a
* bilinear algorithm. Rendering by using the nearest neighbor algorithm is usually faster.
*/
public static function draw(bitmapData:BitmapData,
graphics:Graphics,
width:Number,
height:Number,
inner:Rectangle,
outer:Rectangle = null,
smooth:Boolean = false):void
{
// some useful local vars
var x:int, y:int;
var ox:Number = 0, oy:Number;
var dx:Number = 0, dy:Number;
var wid:Number, hei:Number;
var dwid:Number, dhei:Number;
const sw:int = bitmapData.width;
const sh:int = bitmapData.height;
_matrix.a = 1;
_matrix.b = 0;
_matrix.c = 0;
_matrix.d = 1;
_matrix.tx = 0;
_matrix.ty = 0;
_widths[0] = inner.left;
_widths[1] = inner.width;
_widths[2] = sw - inner.right;
_heights[0] = inner.top;
_heights[1] = inner.height;
_heights[2] = sh - inner.bottom;
var rx:Number = width - _widths[0] - _widths[2];
if(rx < 0)
{
rx = 0;
}
var ry:Number = height - _heights[0] - _heights[2];
if(ry < 0)
{
ry = 0;
}
var ol:Number = (outer != null) ? -outer.left : 0;
var ot:Number = (outer != null) ? -outer.top : 0;
// let's draw
for (x; x < 3 ;x++)
{
// original width
wid = _widths[x];
// draw width
dwid = x==1 ? rx : wid;
// original & draw offset
dy = oy = 0;
_matrix.a = dwid / wid;
for (y = 0; y < 3; y++)
{
// original height
hei = _heights[y];
// draw height
dhei = y==1 ? ry : hei;
if (dwid > 0 && dhei > 0)
{
// some matrix computation
_matrix.d = dhei / hei;
_matrix.tx = -ox * _matrix.a + dx;
_matrix.ty = -oy * _matrix.d + dy;
_matrix.translate(ol, ot);
// draw the cell
graphics.beginBitmapFill(bitmapData, _matrix, false, smooth);
graphics.drawRect(dx + ol, dy + ot, dwid, dhei);
}
// offset incrementation
oy += hei;
dy += dhei;
}
// offset incrementation
ox += wid;
dx += dwid;
}
graphics.endFill();
}
}
}
/**
*
*
* ScaleBitmapSprite
*
* @author Didier Brun
* @author Jerôme Decoster
* @version 1.1
*
*/
package org.bytearray.display {
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Rectangle;
public class ScaleBitmapSprite extends Sprite
{
private var _bitmapData:BitmapData;
private var _height:Number;
private var _width:Number;
private var inner:Rectangle;
private var minHeight:Number;
private var minWidth:Number;
private var outer:Rectangle;
private var outerHeight:Number;
private var outerWidth:Number;
private var smooth:Boolean;
/**
* @param bitmapData BitmapData source
* @param inner Inner rectangle (relative to 0,0)
* @param outer Outer rectangle (relative to 0,0)
* @param smooth If <code>false</code>, upscaled bitmap images are rendered by using a nearest-neighbor
* algorithm and look pixelated. If <code>true</code>, upscaled bitmap images are rendered by using a
* bilinear algorithm. Rendering by using the nearest neighbor algorithm is usually faster.
*/
function ScaleBitmapSprite(bitmapData:BitmapData,
inner:Rectangle,
outer:Rectangle = null,
smooth:Boolean = false)
{
_bitmapData = bitmapData;
this.inner = inner;
this.outer = outer;
this.smooth = smooth;
if (outer != null)
{
_width = outer.width;
_height = outer.height;
outerWidth = bitmapData.width - outer.width;
outerHeight = bitmapData.height - outer.height;
}
else
{
_width = inner.width;
_height = inner.height;
outerWidth = 0;
outerHeight = 0;
}
minWidth = bitmapData.width - inner.width - outerWidth + 2;
minHeight = bitmapData.height - inner.height - outerHeight + 2;
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
//--------------------------------------
// EVENTS
//--------------------------------------
private function onAddedToStage(event:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
addEventListener(Event.RENDER, onRender);
onRender();
}
private function onRemovedFromStage(event:Event):void
{
removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
removeEventListener(Event.RENDER, onRender);
}
private function onRender(event:Event = null):void
{
graphics.clear();
/*
* Math.floor optimisation (works only with positive values)
*
* Slower 1733ms
* var test:Number = Math.floor(1.5);
*
* Fastest 145ms
* var test:int = 1.5 >> 0;
*/
ScaleBitmap.draw(_bitmapData,
graphics,
(_width + outerWidth) >> 0,
(_height + outerHeight) >> 0,
inner,
outer,
smooth);
}
//--------------------------------------
// PUBLIC
//--------------------------------------
/**
* setter deactivated
*/
override public function set scaleX(value:Number):void
{
}
/**
* setter deactivated
*/
override public function set scaleY(value:Number):void
{
}
/**
* @inheritDoc
*/
override public function get width():Number{ return _width; }
/**
* @inheritDoc
*/
override public function set width(value:Number):void
{
_width = value > minWidth ? value : minWidth;
if (stage != null) stage.invalidate();
}
/**
* @inheritDoc
*/
override public function get height():Number{ return _height; }
/**
* @inheritDoc
*/
override public function set height(value:Number):void
{
_height = value > minHeight ? value : minHeight;
if (stage != null) stage.invalidate();
}
/**
* The BitmapData object being referenced.
*/
public function get bitmapData():BitmapData{ return _bitmapData; }
public function set bitmapData(value:BitmapData):void
{
_bitmapData = value;
if (stage != null) stage.invalidate();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment