mirror of
https://github.com/burrow-dev/burrow-server.git
synced 2026-11-21 09:37:50 -05:00
Brought up to date with the private repository (feea4c7)
This commit is contained in:
1 parent
1aa738d692
commit
1b1df1ae48
7925 files changed
+2617237
-20
No files matched your search
@@ -0,0 +1,433 @@
|
||||
package com.sbi.graphics
|
||||
{
|
||||
import flash.display.BitmapData;
|
||||
import flash.geom.Rectangle;
|
||||
import flash.utils.ByteArray;
|
||||
|
||||
public class BitmapByteArray
|
||||
{
|
||||
|
||||
private static const PALETTE_ALPHA_OFFSET:int = 25;
|
||||
|
||||
private static const SHADOW_GRADIENT_SCALE:int = 3;
|
||||
|
||||
private const TRANS:int = 0;
|
||||
|
||||
private var _palette:Array;
|
||||
|
||||
private var _h:int;
|
||||
|
||||
private var _w:int;
|
||||
|
||||
private var _offX:int;
|
||||
|
||||
private var _offY:int;
|
||||
|
||||
private var _ba:Vector.<int>;
|
||||
|
||||
public function BitmapByteArray()
|
||||
{
|
||||
super();
|
||||
this._palette = PaletteHelper.getAnimalPalette();
|
||||
}
|
||||
|
||||
public static function scaleAndPackImage(bm:BitmapData) : Object
|
||||
{
|
||||
var bmba:BitmapByteArray = new BitmapByteArray();
|
||||
bmba.loadBitmap(bm);
|
||||
bmba.halfImage();
|
||||
bmba.crop();
|
||||
return bmba.processPackImage();
|
||||
}
|
||||
|
||||
public static function scaleAndPackShadow(bm:BitmapData, xOff:int, yOff:int, cropW:int, cropH:int) : Object
|
||||
{
|
||||
var bmba:BitmapByteArray = new BitmapByteArray();
|
||||
return bmba.scaleShadow(bm,xOff,yOff,cropW,cropH);
|
||||
}
|
||||
|
||||
private function loadBitmap(bmd:BitmapData) : void
|
||||
{
|
||||
var x:uint = 0;
|
||||
var y:uint = 0;
|
||||
var color:uint = 0;
|
||||
this._w = bmd.width;
|
||||
this._h = bmd.height;
|
||||
this._ba = new Vector.<int>(this._w * this._h,true);
|
||||
var lastColor:uint = 0;
|
||||
var lastIndex:int = -1;
|
||||
var i:int = 0;
|
||||
for(y = 0; y < this._h; y++)
|
||||
{
|
||||
for(x = 0; x < this._w; x++)
|
||||
{
|
||||
color = bmd.getPixel32(x,y);
|
||||
if(lastColor != color || lastIndex < 0)
|
||||
{
|
||||
lastColor = color;
|
||||
lastIndex = int(this.findPaletteColor(color));
|
||||
}
|
||||
this._ba[i++] = lastIndex;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function scaleShadow(bm:BitmapData, xOff:int, yOff:int, cropW:int, cropH:int) : Object
|
||||
{
|
||||
var c1:uint = 0;
|
||||
var c2:uint = 0;
|
||||
var c3:uint = 0;
|
||||
var c4:uint = 0;
|
||||
var x:int = 0;
|
||||
var pos:int = 0;
|
||||
this._w = bm.width;
|
||||
this._h = bm.height;
|
||||
var pixels:Vector.<uint> = bm.getVector(new Rectangle(0,0,this._w,this._h));
|
||||
var newBa:Vector.<int> = new Vector.<int>(this._w * this._h * 0.25,true);
|
||||
var p:int = 0;
|
||||
var i:int = 0;
|
||||
for(var y:int = 0; y < this._h; y += 2)
|
||||
{
|
||||
for(x = 0; x < this._w; x += 2)
|
||||
{
|
||||
p = x + y * this._w;
|
||||
c1 = pixels[p];
|
||||
c2 = pixels[p + 1];
|
||||
p += this._w;
|
||||
c3 = pixels[p];
|
||||
c4 = pixels[p + 1];
|
||||
newBa[i++] = this.blendShadowColors(c1,c2,c3,c4);
|
||||
}
|
||||
}
|
||||
this._ba = newBa;
|
||||
this._h /= 2;
|
||||
this._w /= 2;
|
||||
if(cropW != this._w || cropH != this._h)
|
||||
{
|
||||
i = 0;
|
||||
newBa = new Vector.<int>(cropW * cropH,true);
|
||||
for(y = yOff; y < yOff + cropH; y++)
|
||||
{
|
||||
pos = y * this._w + xOff;
|
||||
for(x = 0; x < cropW; x++)
|
||||
{
|
||||
p = this._ba[pos++];
|
||||
newBa[i++] = p;
|
||||
}
|
||||
}
|
||||
this._w = cropW;
|
||||
this._h = cropH;
|
||||
this._offX = xOff;
|
||||
this._offY = yOff;
|
||||
this._ba = newBa;
|
||||
}
|
||||
var ret:Object = this.processPackImage();
|
||||
ret.sm = SHADOW_GRADIENT_SCALE;
|
||||
return ret;
|
||||
}
|
||||
|
||||
private function halfImage() : void
|
||||
{
|
||||
var c1:int = 0;
|
||||
var c2:int = 0;
|
||||
var c3:int = 0;
|
||||
var c4:int = 0;
|
||||
var x:int = 0;
|
||||
var newBa:Vector.<int> = new Vector.<int>(this._w * this._h * 0.25,true);
|
||||
var p:int = 0;
|
||||
var i:int = 0;
|
||||
for(var y:int = 0; y < this._h; y += 2)
|
||||
{
|
||||
for(x = 0; x < this._w; x += 2)
|
||||
{
|
||||
p = x + y * this._w;
|
||||
c1 = this._ba[p];
|
||||
c2 = this._ba[p + 1];
|
||||
p += this._w;
|
||||
c3 = this._ba[p];
|
||||
c4 = this._ba[p + 1];
|
||||
newBa[i++] = this.blendColors(c1,c2,c3,c4);
|
||||
}
|
||||
}
|
||||
this._ba = newBa;
|
||||
this._h /= 2;
|
||||
this._w /= 2;
|
||||
}
|
||||
|
||||
private function crop() : void
|
||||
{
|
||||
var p:uint = 0;
|
||||
var x:int = 0;
|
||||
var y:int = 0;
|
||||
var newBA:Vector.<int> = null;
|
||||
var pos:int = 0;
|
||||
var xmin:int = this._w;
|
||||
var xmax:int = 0;
|
||||
var ymin:int = this._h;
|
||||
var ymax:int = 0;
|
||||
var i:int = 0;
|
||||
for(y = 0; y < this._h; y++)
|
||||
{
|
||||
for(x = 0; x < this._w; x++)
|
||||
{
|
||||
p = uint(this._ba[i++]);
|
||||
if(p > 0)
|
||||
{
|
||||
if(x < xmin)
|
||||
{
|
||||
xmin = x;
|
||||
}
|
||||
if(x > xmax)
|
||||
{
|
||||
xmax = x;
|
||||
}
|
||||
if(y < ymin)
|
||||
{
|
||||
ymin = y;
|
||||
}
|
||||
if(y > ymax)
|
||||
{
|
||||
ymax = y;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
this._offX = xmin;
|
||||
this._offY = ymin;
|
||||
var w:uint = xmax - xmin + 1;
|
||||
var h:uint = ymax - ymin + 1;
|
||||
if(xmin == this._w && ymin == this._h)
|
||||
{
|
||||
this._w = 0;
|
||||
this._h = 0;
|
||||
this._ba = null;
|
||||
return;
|
||||
}
|
||||
i = 0;
|
||||
if(w != this._w || h != this._h)
|
||||
{
|
||||
newBA = new Vector.<int>(w * h,true);
|
||||
for(y = ymin; y < ymin + h; y++)
|
||||
{
|
||||
pos = y * this._w + this._offX;
|
||||
for(x = 0; x < w; x++)
|
||||
{
|
||||
p = uint(this._ba[pos++]);
|
||||
newBA[i++] = p;
|
||||
}
|
||||
}
|
||||
this._w = w;
|
||||
this._h = h;
|
||||
this._ba = newBA;
|
||||
}
|
||||
}
|
||||
|
||||
private function processPackImage() : Object
|
||||
{
|
||||
var x:int = 0;
|
||||
var y:int = 0;
|
||||
var line:String = null;
|
||||
var c:uint = 0;
|
||||
if(this._ba == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var o:Object = {
|
||||
"x":this._offX,
|
||||
"y":this._offY,
|
||||
"w":this._w,
|
||||
"h":this._h
|
||||
};
|
||||
var pack:ByteArray = new ByteArray();
|
||||
o.b = pack;
|
||||
var len:int = int(this._ba.length);
|
||||
var lastColor:uint = 256;
|
||||
var sameCount:uint = 0;
|
||||
var p:int = 0;
|
||||
while(p < len)
|
||||
{
|
||||
c = uint(this._ba[p++]);
|
||||
if(lastColor == 256)
|
||||
{
|
||||
lastColor = c;
|
||||
}
|
||||
else if(c != lastColor)
|
||||
{
|
||||
if(Boolean(sameCount))
|
||||
{
|
||||
lastColor |= 128;
|
||||
while(sameCount > 255)
|
||||
{
|
||||
pack.writeByte(lastColor);
|
||||
pack.writeByte(uint(255));
|
||||
sameCount -= 256;
|
||||
}
|
||||
if(sameCount > 0)
|
||||
{
|
||||
pack.writeByte(lastColor);
|
||||
pack.writeByte(sameCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
pack.writeByte(uint(lastColor & 0x7F));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
pack.writeByte(lastColor);
|
||||
}
|
||||
lastColor = c;
|
||||
sameCount = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
sameCount++;
|
||||
}
|
||||
}
|
||||
if(Boolean(sameCount) || len == 1)
|
||||
{
|
||||
lastColor |= 128;
|
||||
while(sameCount > 255)
|
||||
{
|
||||
pack.writeByte(lastColor);
|
||||
pack.writeByte(uint(255));
|
||||
sameCount -= 256;
|
||||
}
|
||||
if(sameCount > 0)
|
||||
{
|
||||
pack.writeByte(lastColor);
|
||||
pack.writeByte(sameCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
pack.writeByte(uint(lastColor & 0x7F));
|
||||
}
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
private function findPaletteColor(color:uint) : uint
|
||||
{
|
||||
var alpha:uint = uint((color & 0xFF000000) >> 24);
|
||||
if(alpha < 160)
|
||||
{
|
||||
return this.TRANS;
|
||||
}
|
||||
color |= 4278190080;
|
||||
for(var i:uint = 0; i < PALETTE_ALPHA_OFFSET; i++)
|
||||
{
|
||||
if(this._palette[i] == color)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
var cr:int = color >> 16 & 0xFF;
|
||||
var cg:int = color >> 8 & 0xFF;
|
||||
var cb:int = color & 0xFF;
|
||||
return this.findClosetPaletteIndex(cr,cg,cb);
|
||||
}
|
||||
|
||||
private function findClosetPaletteIndex(cr:int, cg:int, cb:int) : int
|
||||
{
|
||||
var a:int = 0;
|
||||
var r:int = 0;
|
||||
var g:int = 0;
|
||||
var b:int = 0;
|
||||
var distance:int = 0;
|
||||
var pc:uint = 0;
|
||||
var smallest:int = 10000000;
|
||||
var smallestIndex:int = 0;
|
||||
for(var j:uint = 0; j < PALETTE_ALPHA_OFFSET; j++)
|
||||
{
|
||||
pc = uint(this._palette[j]);
|
||||
r = (pc >> 16 & 0xFF) - cr;
|
||||
g = (pc >> 8 & 0xFF) - cg;
|
||||
b = (pc & 0xFF) - cb;
|
||||
distance = r * r + g * g + b * b;
|
||||
if(distance < smallest)
|
||||
{
|
||||
smallest = distance;
|
||||
smallestIndex = int(j);
|
||||
}
|
||||
}
|
||||
return smallestIndex;
|
||||
}
|
||||
|
||||
private function blendColors(c:uint, c1:uint, c2:uint, c3:uint) : uint
|
||||
{
|
||||
var newColor:uint = 0;
|
||||
var transCount:int = 0;
|
||||
var a:Array = null;
|
||||
var total:int = 0;
|
||||
var cr:int = 0;
|
||||
var cg:int = 0;
|
||||
var cb:int = 0;
|
||||
var i:int = 0;
|
||||
if(c == this.TRANS)
|
||||
{
|
||||
transCount++;
|
||||
}
|
||||
if(c1 == this.TRANS)
|
||||
{
|
||||
transCount++;
|
||||
}
|
||||
if(c2 == this.TRANS)
|
||||
{
|
||||
transCount++;
|
||||
}
|
||||
if(c3 == this.TRANS)
|
||||
{
|
||||
transCount++;
|
||||
}
|
||||
if(transCount == 4)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if(c == c1 && c == c2 && c == c3)
|
||||
{
|
||||
newColor = c;
|
||||
}
|
||||
else
|
||||
{
|
||||
for(a = [c,c1,c2,c3]; i < 4; )
|
||||
{
|
||||
if(a[i] != this.TRANS)
|
||||
{
|
||||
c = uint(this._palette[a[i]]);
|
||||
cr += c >> 16 & 0xFF;
|
||||
cg += c >> 8 & 0xFF;
|
||||
cb += c & 0xFF;
|
||||
total++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
cr /= total;
|
||||
cg /= total;
|
||||
cb /= total;
|
||||
newColor = uint(this.findClosetPaletteIndex(cr,cg,cb));
|
||||
}
|
||||
return uint(newColor + transCount * PALETTE_ALPHA_OFFSET);
|
||||
}
|
||||
|
||||
private function blendShadowColors(c1:uint, c2:uint, c3:uint, c4:uint) : int
|
||||
{
|
||||
var a:uint = ((c1 >> 24 & 0xFF) + (c2 >> 24 & 0xFF) + (c3 >> 24 & 0xFF) + (c4 >> 24 & 0xFF)) / 4;
|
||||
var c:uint = ((c1 & 0xFF & 0xFF) + (c2 & 0xFF & 0xFF) + (c3 & 0xFF & 0xFF) + (c4 & 0xFF & 0xFF)) / 4;
|
||||
if(Boolean(a))
|
||||
{
|
||||
c += (255 - c) * ((255 - a) / 255);
|
||||
c >>= BitmapByteArray.SHADOW_GRADIENT_SCALE;
|
||||
if(c == 0)
|
||||
{
|
||||
c = 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
c = 0;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
package com.sbi.graphics
|
||||
{
|
||||
import flash.display.BitmapData;
|
||||
import flash.utils.ByteArray;
|
||||
|
||||
public class ImageArray
|
||||
{
|
||||
|
||||
public static const VERSION:uint = 117967104;
|
||||
|
||||
private var _frames:Array;
|
||||
|
||||
public function ImageArray()
|
||||
{
|
||||
super();
|
||||
this._frames = new Array();
|
||||
}
|
||||
|
||||
public function get length() : int
|
||||
{
|
||||
return this._frames.length;
|
||||
}
|
||||
|
||||
public function release() : void
|
||||
{
|
||||
this._frames.splice(0,this._frames.length);
|
||||
}
|
||||
|
||||
public function addBitmap(bm:BitmapData) : void
|
||||
{
|
||||
var o:Object = BitmapByteArray.scaleAndPackImage(bm);
|
||||
this._frames.push(o);
|
||||
}
|
||||
|
||||
public function attachShadow(bm:BitmapData) : void
|
||||
{
|
||||
var current:Object = null;
|
||||
var o:Object = null;
|
||||
if(Boolean(this._frames.length))
|
||||
{
|
||||
current = this._frames[this._frames.length - 1];
|
||||
o = BitmapByteArray.scaleAndPackShadow(bm,current.x,current.y,current.w,current.h);
|
||||
current.s = o.b;
|
||||
current.sm = o.sm;
|
||||
}
|
||||
}
|
||||
|
||||
public function addFrame(o:Object) : void
|
||||
{
|
||||
this._frames.push(o);
|
||||
}
|
||||
|
||||
public function loadPackage(pack:ByteArray) : void
|
||||
{
|
||||
this.release();
|
||||
pack.position = 0;
|
||||
try
|
||||
{
|
||||
pack.uncompress();
|
||||
}
|
||||
catch(err:Error)
|
||||
{
|
||||
}
|
||||
var o:Object = pack.readObject();
|
||||
if(o.v == ImageArray.VERSION)
|
||||
{
|
||||
this._frames = o.f;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public function getFrame(i:int) : Object
|
||||
{
|
||||
return this._frames[i];
|
||||
}
|
||||
|
||||
public function getPackage(bCompress:Boolean = true) : ByteArray
|
||||
{
|
||||
var i:int = 0;
|
||||
var ba:ByteArray = null;
|
||||
var top:Object = null;
|
||||
if(Boolean(this._frames.length))
|
||||
{
|
||||
top = {
|
||||
"v":ImageArray.VERSION,
|
||||
"f":this._frames
|
||||
};
|
||||
ba = new ByteArray();
|
||||
ba.writeObject(top);
|
||||
ba.position = 0;
|
||||
if(bCompress)
|
||||
{
|
||||
ba.compress();
|
||||
}
|
||||
}
|
||||
return ba;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.sbi.graphics
|
||||
{
|
||||
public class ImageArrayHelper
|
||||
{
|
||||
|
||||
public function ImageArrayHelper()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public static function packId(aId:int, cId:int, lId:int) : uint
|
||||
{
|
||||
return aId << 24 | cId << 16 | lId;
|
||||
}
|
||||
|
||||
public static function avatarId(id:uint) : int
|
||||
{
|
||||
return id >> 24 & 0xFF;
|
||||
}
|
||||
|
||||
public static function categoryId(id:uint) : int
|
||||
{
|
||||
return id >> 16 & 0xFF;
|
||||
}
|
||||
|
||||
public static function layerId(id:uint) : int
|
||||
{
|
||||
return id & 0xFFFF;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.sbi.graphics
|
||||
{
|
||||
import flash.events.Event;
|
||||
import flash.utils.ByteArray;
|
||||
|
||||
public class JPEGAsyncCompleteEvent extends Event
|
||||
{
|
||||
|
||||
public static const JPEGASYNC_COMPLETE:String = "JPEGAsyncComplete";
|
||||
|
||||
public var ImageData:ByteArray;
|
||||
|
||||
public function JPEGAsyncCompleteEvent(data:ByteArray)
|
||||
{
|
||||
this.ImageData = data;
|
||||
super(JPEGASYNC_COMPLETE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,702 @@
|
||||
package com.sbi.graphics
|
||||
{
|
||||
import flash.display.BitmapData;
|
||||
import flash.events.EventDispatcher;
|
||||
import flash.events.ProgressEvent;
|
||||
import flash.utils.ByteArray;
|
||||
import flash.utils.setTimeout;
|
||||
|
||||
public class JpegAsynchEncoder extends EventDispatcher
|
||||
{
|
||||
|
||||
public static const CONTENT_TYPE:String = "image/jpeg";
|
||||
|
||||
private var ZigZag:Array = [0,1,5,6,14,15,27,28,2,4,7,13,16,26,29,42,3,8,12,17,25,30,41,43,9,11,18,24,31,40,44,53,10,19,23,32,39,45,52,54,20,22,33,38,46,51,55,60,21,34,37,47,50,56,59,61,35,36,48,49,57,58,62,63];
|
||||
|
||||
private var std_dc_luminance_nrcodes:Array = [0,0,1,5,1,1,1,1,1,1,0,0,0,0,0,0,0];
|
||||
|
||||
private var std_dc_luminance_values:Array = [0,1,2,3,4,5,6,7,8,9,10,11];
|
||||
|
||||
private var std_ac_luminance_nrcodes:Array = [0,0,2,1,3,3,2,4,3,5,5,4,4,0,0,1,125];
|
||||
|
||||
private var std_ac_luminance_values:Array = [1,2,3,0,4,17,5,18,33,49,65,6,19,81,97,7,34,113,20,50,129,145,161,8,35,66,177,193,21,82,209,240,36,51,98,114,130,9,10,22,23,24,25,26,37,38,39,40,41,42,52,53,54,55,56,57,58,67,68,69,70,71,72,73,74,83,84,85,86,87,88,89,90,99,100,101,102,103,104,105,106,115,116,117,118,119,120,121,122,131,132,133,134,135,136,137,138,146,147,148,149,150,151,152,153,154,162,163,164,165,166,167,168,169,170,178,179,180,181,182,183,184,185,186,194,195,196,197,198,199,200,201,202,210,211,212,213,214,215,216,217,218,225,226,227,228,229,230,231,232,233,234,241,242,243,244,245,246,247,248,249,250];
|
||||
|
||||
private var std_dc_chrominance_nrcodes:Array = [0,0,3,1,1,1,1,1,1,1,1,1,0,0,0,0,0];
|
||||
|
||||
private var std_dc_chrominance_values:Array = [0,1,2,3,4,5,6,7,8,9,10,11];
|
||||
|
||||
private var std_ac_chrominance_nrcodes:Array = [0,0,2,1,2,4,4,3,4,7,5,4,4,0,1,2,119];
|
||||
|
||||
private var std_ac_chrominance_values:Array = [0,1,2,3,17,4,5,33,49,6,18,65,81,7,97,113,19,34,50,129,8,20,66,145,161,177,193,9,35,51,82,240,21,98,114,209,10,22,36,52,225,37,241,23,24,25,26,38,39,40,41,42,53,54,55,56,57,58,67,68,69,70,71,72,73,74,83,84,85,86,87,88,89,90,99,100,101,102,103,104,105,106,115,116,117,118,119,120,121,122,130,131,132,133,134,135,136,137,138,146,147,148,149,150,151,152,153,154,162,163,164,165,166,167,168,169,170,178,179,180,181,182,183,184,185,186,194,195,196,197,198,199,200,201,202,210,211,212,213,214,215,216,217,218,226,227,228,229,230,231,232,233,234,242,243,244,245,246,247,248,249,250];
|
||||
|
||||
private var YTable:Array = new Array(64);
|
||||
|
||||
private var UVTable:Array = new Array(64);
|
||||
|
||||
private var fdtbl_Y:Array = new Array(64);
|
||||
|
||||
private var fdtbl_UV:Array = new Array(64);
|
||||
|
||||
private var YDC_HT:Array;
|
||||
|
||||
private var UVDC_HT:Array;
|
||||
|
||||
private var YAC_HT:Array;
|
||||
|
||||
private var UVAC_HT:Array;
|
||||
|
||||
private var YDU:Array = new Array(64);
|
||||
|
||||
private var UDU:Array = new Array(64);
|
||||
|
||||
private var VDU:Array = new Array(64);
|
||||
|
||||
private var DU:Array = new Array(64);
|
||||
|
||||
private var bitcode:Array = new Array(65535);
|
||||
|
||||
private var category:Array = new Array(65535);
|
||||
|
||||
private var byteout:ByteArray;
|
||||
|
||||
private var bytenew:int = 0;
|
||||
|
||||
private var bytepos:int = 7;
|
||||
|
||||
private var DCY:Number = 0;
|
||||
|
||||
private var DCU:Number = 0;
|
||||
|
||||
private var DCV:Number = 0;
|
||||
|
||||
private var SrcWidth:int = 0;
|
||||
|
||||
private var SrcHeight:int = 0;
|
||||
|
||||
private var Source:Object = null;
|
||||
|
||||
private var TotalSize:int = 0;
|
||||
|
||||
private var PixelsPerIter:int = 128;
|
||||
|
||||
private var PercentageInc:int = 0;
|
||||
|
||||
private var NextProgressAt:int = 0;
|
||||
|
||||
private var CurrentTotalPos:int = 0;
|
||||
|
||||
private var Working:Boolean = false;
|
||||
|
||||
public function JpegAsynchEncoder()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public function JPEGAsyncEncoder(quality:Number = 50) : void
|
||||
{
|
||||
if(quality <= 0)
|
||||
{
|
||||
quality = 1;
|
||||
}
|
||||
if(quality > 100)
|
||||
{
|
||||
quality = 100;
|
||||
}
|
||||
var sf:int = 0;
|
||||
if(quality < 50)
|
||||
{
|
||||
sf = int(5000 / quality);
|
||||
}
|
||||
else
|
||||
{
|
||||
sf = int(200 - quality * 2);
|
||||
}
|
||||
this.initHuffmanTbl();
|
||||
this.initCategoryNumber();
|
||||
this.initQuantTables(sf);
|
||||
}
|
||||
|
||||
public function get contentType() : String
|
||||
{
|
||||
return CONTENT_TYPE;
|
||||
}
|
||||
|
||||
public function get PixelsPerIteration() : int
|
||||
{
|
||||
return this.PixelsPerIter;
|
||||
}
|
||||
|
||||
public function set PixelsPerIteration(val:int) : void
|
||||
{
|
||||
this.PixelsPerIter = val;
|
||||
}
|
||||
|
||||
public function get ImageData() : ByteArray
|
||||
{
|
||||
return this.byteout;
|
||||
}
|
||||
|
||||
public function encodeByteArray(raw:ByteArray, width:int, height:int) : Boolean
|
||||
{
|
||||
return this.internalEncode(raw,width,height);
|
||||
}
|
||||
|
||||
public function encode(image:BitmapData) : Boolean
|
||||
{
|
||||
return this.internalEncode(image,image.width,image.height);
|
||||
}
|
||||
|
||||
private function internalEncode(newSource:Object, width:int, height:int) : Boolean
|
||||
{
|
||||
if(this.Working)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
this.Working = true;
|
||||
this.Source = newSource;
|
||||
this.SrcWidth = width;
|
||||
this.SrcHeight = height;
|
||||
this.TotalSize = width * height;
|
||||
this.PercentageInc = this.TotalSize / 100;
|
||||
this.NextProgressAt = this.PercentageInc;
|
||||
this.CurrentTotalPos = 0;
|
||||
setTimeout(this.StartEncode,10);
|
||||
return true;
|
||||
}
|
||||
|
||||
private function StartEncode() : void
|
||||
{
|
||||
this.byteout = new ByteArray();
|
||||
this.bytenew = 0;
|
||||
this.bytepos = 7;
|
||||
this.writeWord(65496);
|
||||
this.writeAPP0();
|
||||
this.writeDQT();
|
||||
this.writeSOF0(this.SrcWidth,this.SrcHeight);
|
||||
this.writeDHT();
|
||||
this.writeSOS();
|
||||
this.DCY = 0;
|
||||
this.DCV = 0;
|
||||
this.DCU = 0;
|
||||
this.bytenew = 0;
|
||||
this.bytepos = 7;
|
||||
this.dispatchEvent(new ProgressEvent(ProgressEvent.PROGRESS,false,false,0,this.TotalSize));
|
||||
setTimeout(this.AsyncLoop,10,0,0);
|
||||
}
|
||||
|
||||
private function AsyncLoop(xpos:int, ypos:int) : void
|
||||
{
|
||||
for(var i:int = 0; i < this.PixelsPerIter; i++)
|
||||
{
|
||||
this.RGB2YUV(this.Source,xpos,ypos,this.SrcWidth,this.SrcHeight);
|
||||
this.DCY = this.processDU(this.YDU,this.fdtbl_Y,this.DCY,this.YDC_HT,this.YAC_HT);
|
||||
this.DCU = this.processDU(this.UDU,this.fdtbl_UV,this.DCU,this.UVDC_HT,this.UVAC_HT);
|
||||
this.DCV = this.processDU(this.VDU,this.fdtbl_UV,this.DCV,this.UVDC_HT,this.UVAC_HT);
|
||||
xpos += 8;
|
||||
if(xpos >= this.SrcWidth)
|
||||
{
|
||||
xpos = 0;
|
||||
ypos += 8;
|
||||
}
|
||||
if(ypos >= this.SrcHeight)
|
||||
{
|
||||
setTimeout(this.FinishEncode,10);
|
||||
return;
|
||||
}
|
||||
this.CurrentTotalPos += 64;
|
||||
if(this.CurrentTotalPos >= this.NextProgressAt)
|
||||
{
|
||||
this.dispatchEvent(new ProgressEvent(ProgressEvent.PROGRESS,false,false,this.CurrentTotalPos,this.TotalSize));
|
||||
this.NextProgressAt += this.PercentageInc;
|
||||
}
|
||||
}
|
||||
setTimeout(this.AsyncLoop,10,xpos,ypos);
|
||||
}
|
||||
|
||||
private function FinishEncode() : void
|
||||
{
|
||||
var fillbits:BitString = null;
|
||||
if(this.bytepos >= 0)
|
||||
{
|
||||
fillbits = new BitString();
|
||||
fillbits.len = this.bytepos + 1;
|
||||
fillbits.val = (1 << this.bytepos + 1) - 1;
|
||||
this.writeBits(fillbits);
|
||||
}
|
||||
this.writeWord(65497);
|
||||
this.dispatchEvent(new ProgressEvent(ProgressEvent.PROGRESS,false,false,this.TotalSize,this.TotalSize));
|
||||
this.dispatchEvent(new JPEGAsyncCompleteEvent(this.byteout));
|
||||
this.Working = false;
|
||||
}
|
||||
|
||||
private function initQuantTables(sf:int) : void
|
||||
{
|
||||
var t:Number = NaN;
|
||||
var col:int = 0;
|
||||
var i:int = 0;
|
||||
var YQT:Array = [16,11,10,16,24,40,51,61,12,12,14,19,26,58,60,55,14,13,16,24,40,57,69,56,14,17,22,29,51,87,80,62,18,22,37,56,68,109,103,77,24,35,55,64,81,104,113,92,49,64,78,87,103,121,120,101,72,92,95,98,112,100,103,99];
|
||||
for(i = 0; i < 64; i++)
|
||||
{
|
||||
t = Math.floor((YQT[i] * sf + 50) / 100);
|
||||
if(t < 1)
|
||||
{
|
||||
t = 1;
|
||||
}
|
||||
else if(t > 255)
|
||||
{
|
||||
t = 255;
|
||||
}
|
||||
this.YTable[this.ZigZag[i]] = t;
|
||||
}
|
||||
var UVQT:Array = [17,18,24,47,99,99,99,99,18,21,26,66,99,99,99,99,24,26,56,99,99,99,99,99,47,66,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99];
|
||||
for(i = 0; i < 64; i++)
|
||||
{
|
||||
t = Math.floor((UVQT[i] * sf + 50) / 100);
|
||||
if(t < 1)
|
||||
{
|
||||
t = 1;
|
||||
}
|
||||
else if(t > 255)
|
||||
{
|
||||
t = 255;
|
||||
}
|
||||
this.UVTable[this.ZigZag[i]] = t;
|
||||
}
|
||||
var aasf:Array = [1,1.387039845,1.306562965,1.175875602,1,0.785694958,0.5411961,0.275899379];
|
||||
i = 0;
|
||||
for(var row:int = 0; row < 8; row++)
|
||||
{
|
||||
for(col = 0; col < 8; col++)
|
||||
{
|
||||
this.fdtbl_Y[i] = 1 / (this.YTable[this.ZigZag[i]] * aasf[row] * aasf[col] * 8);
|
||||
this.fdtbl_UV[i] = 1 / (this.UVTable[this.ZigZag[i]] * aasf[row] * aasf[col] * 8);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function computeHuffmanTbl(nrcodes:Array, std_table:Array) : Array
|
||||
{
|
||||
var j:int = 0;
|
||||
var codevalue:int = 0;
|
||||
var pos_in_table:int = 0;
|
||||
var HT:Array = new Array();
|
||||
for(var k:int = 1; k <= 16; k++)
|
||||
{
|
||||
for(j = 1; j <= nrcodes[k]; j++)
|
||||
{
|
||||
HT[std_table[pos_in_table]] = new BitString();
|
||||
HT[std_table[pos_in_table]].val = codevalue;
|
||||
HT[std_table[pos_in_table]].len = k;
|
||||
pos_in_table++;
|
||||
codevalue++;
|
||||
}
|
||||
codevalue *= 2;
|
||||
}
|
||||
return HT;
|
||||
}
|
||||
|
||||
private function initHuffmanTbl() : void
|
||||
{
|
||||
this.YDC_HT = this.computeHuffmanTbl(this.std_dc_luminance_nrcodes,this.std_dc_luminance_values);
|
||||
this.UVDC_HT = this.computeHuffmanTbl(this.std_dc_chrominance_nrcodes,this.std_dc_chrominance_values);
|
||||
this.YAC_HT = this.computeHuffmanTbl(this.std_ac_luminance_nrcodes,this.std_ac_luminance_values);
|
||||
this.UVAC_HT = this.computeHuffmanTbl(this.std_ac_chrominance_nrcodes,this.std_ac_chrominance_values);
|
||||
}
|
||||
|
||||
private function initCategoryNumber() : void
|
||||
{
|
||||
var nr:int = 0;
|
||||
var nrlower:int = 1;
|
||||
var nrupper:int = 2;
|
||||
for(var cat:int = 1; cat <= 15; cat++)
|
||||
{
|
||||
for(nr = nrlower; nr < nrupper; nr++)
|
||||
{
|
||||
this.category[32767 + nr] = cat;
|
||||
this.bitcode[32767 + nr] = new BitString();
|
||||
this.bitcode[32767 + nr].len = cat;
|
||||
this.bitcode[32767 + nr].val = nr;
|
||||
}
|
||||
for(nr = -(nrupper - 1); nr <= -nrlower; nr++)
|
||||
{
|
||||
this.category[32767 + nr] = cat;
|
||||
this.bitcode[32767 + nr] = new BitString();
|
||||
this.bitcode[32767 + nr].len = cat;
|
||||
this.bitcode[32767 + nr].val = nrupper - 1 + nr;
|
||||
}
|
||||
nrlower <<= 1;
|
||||
nrupper <<= 1;
|
||||
}
|
||||
}
|
||||
|
||||
private function writeBits(bs:BitString) : void
|
||||
{
|
||||
var value:int = bs.val;
|
||||
var posval:int = bs.len - 1;
|
||||
while(posval >= 0)
|
||||
{
|
||||
if(Boolean(value & uint(1 << posval)))
|
||||
{
|
||||
this.bytenew |= uint(1 << this.bytepos);
|
||||
}
|
||||
posval--;
|
||||
--this.bytepos;
|
||||
if(this.bytepos < 0)
|
||||
{
|
||||
if(this.bytenew == 255)
|
||||
{
|
||||
this.writeByte(255);
|
||||
this.writeByte(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.writeByte(this.bytenew);
|
||||
}
|
||||
this.bytepos = 7;
|
||||
this.bytenew = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function writeByte(value:int) : void
|
||||
{
|
||||
this.byteout.writeByte(value);
|
||||
}
|
||||
|
||||
private function writeWord(value:int) : void
|
||||
{
|
||||
this.writeByte(value >> 8 & 0xFF);
|
||||
this.writeByte(value & 0xFF);
|
||||
}
|
||||
|
||||
private function fDCTQuant(data:Array, fdtbl:Array) : Array
|
||||
{
|
||||
var i:int = 0;
|
||||
var tmp0:Number = NaN;
|
||||
var tmp7:Number = NaN;
|
||||
var tmp1:Number = NaN;
|
||||
var tmp6:Number = NaN;
|
||||
var tmp2:Number = NaN;
|
||||
var tmp5:Number = NaN;
|
||||
var tmp3:Number = NaN;
|
||||
var tmp4:Number = NaN;
|
||||
var tmp10:Number = NaN;
|
||||
var tmp13:Number = NaN;
|
||||
var tmp11:Number = NaN;
|
||||
var tmp12:Number = NaN;
|
||||
var z1:Number = NaN;
|
||||
var z5:Number = NaN;
|
||||
var z2:Number = NaN;
|
||||
var z4:Number = NaN;
|
||||
var z3:Number = NaN;
|
||||
var z11:Number = NaN;
|
||||
var z13:Number = NaN;
|
||||
var dataOff:int = 0;
|
||||
for(i = 0; i < 8; i++)
|
||||
{
|
||||
tmp0 = data[dataOff + 0] + data[dataOff + 7];
|
||||
tmp7 = data[dataOff + 0] - data[dataOff + 7];
|
||||
tmp1 = data[dataOff + 1] + data[dataOff + 6];
|
||||
tmp6 = data[dataOff + 1] - data[dataOff + 6];
|
||||
tmp2 = data[dataOff + 2] + data[dataOff + 5];
|
||||
tmp5 = data[dataOff + 2] - data[dataOff + 5];
|
||||
tmp3 = data[dataOff + 3] + data[dataOff + 4];
|
||||
tmp4 = data[dataOff + 3] - data[dataOff + 4];
|
||||
tmp10 = tmp0 + tmp3;
|
||||
tmp13 = tmp0 - tmp3;
|
||||
tmp11 = tmp1 + tmp2;
|
||||
tmp12 = tmp1 - tmp2;
|
||||
data[dataOff + 0] = tmp10 + tmp11;
|
||||
data[dataOff + 4] = tmp10 - tmp11;
|
||||
z1 = (tmp12 + tmp13) * 0.707106781;
|
||||
data[dataOff + 2] = tmp13 + z1;
|
||||
data[dataOff + 6] = tmp13 - z1;
|
||||
tmp10 = tmp4 + tmp5;
|
||||
tmp11 = tmp5 + tmp6;
|
||||
tmp12 = tmp6 + tmp7;
|
||||
z5 = (tmp10 - tmp12) * 0.382683433;
|
||||
z2 = 0.5411961 * tmp10 + z5;
|
||||
z4 = 1.306562965 * tmp12 + z5;
|
||||
z3 = tmp11 * 0.707106781;
|
||||
z11 = tmp7 + z3;
|
||||
z13 = tmp7 - z3;
|
||||
data[dataOff + 5] = z13 + z2;
|
||||
data[dataOff + 3] = z13 - z2;
|
||||
data[dataOff + 1] = z11 + z4;
|
||||
data[dataOff + 7] = z11 - z4;
|
||||
dataOff += 8;
|
||||
}
|
||||
dataOff = 0;
|
||||
for(i = 0; i < 8; i++)
|
||||
{
|
||||
tmp0 = data[dataOff + 0] + data[dataOff + 56];
|
||||
tmp7 = data[dataOff + 0] - data[dataOff + 56];
|
||||
tmp1 = data[dataOff + 8] + data[dataOff + 48];
|
||||
tmp6 = data[dataOff + 8] - data[dataOff + 48];
|
||||
tmp2 = data[dataOff + 16] + data[dataOff + 40];
|
||||
tmp5 = data[dataOff + 16] - data[dataOff + 40];
|
||||
tmp3 = data[dataOff + 24] + data[dataOff + 32];
|
||||
tmp4 = data[dataOff + 24] - data[dataOff + 32];
|
||||
tmp10 = tmp0 + tmp3;
|
||||
tmp13 = tmp0 - tmp3;
|
||||
tmp11 = tmp1 + tmp2;
|
||||
tmp12 = tmp1 - tmp2;
|
||||
data[dataOff + 0] = tmp10 + tmp11;
|
||||
data[dataOff + 32] = tmp10 - tmp11;
|
||||
z1 = (tmp12 + tmp13) * 0.707106781;
|
||||
data[dataOff + 16] = tmp13 + z1;
|
||||
data[dataOff + 48] = tmp13 - z1;
|
||||
tmp10 = tmp4 + tmp5;
|
||||
tmp11 = tmp5 + tmp6;
|
||||
tmp12 = tmp6 + tmp7;
|
||||
z5 = (tmp10 - tmp12) * 0.382683433;
|
||||
z2 = 0.5411961 * tmp10 + z5;
|
||||
z4 = 1.306562965 * tmp12 + z5;
|
||||
z3 = tmp11 * 0.707106781;
|
||||
z11 = tmp7 + z3;
|
||||
z13 = tmp7 - z3;
|
||||
data[dataOff + 40] = z13 + z2;
|
||||
data[dataOff + 24] = z13 - z2;
|
||||
data[dataOff + 8] = z11 + z4;
|
||||
data[dataOff + 56] = z11 - z4;
|
||||
dataOff++;
|
||||
}
|
||||
for(i = 0; i < 64; i++)
|
||||
{
|
||||
data[i] = Math.round(data[i] * fdtbl[i]);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
private function writeAPP0() : void
|
||||
{
|
||||
this.writeWord(65504);
|
||||
this.writeWord(16);
|
||||
this.writeByte(74);
|
||||
this.writeByte(70);
|
||||
this.writeByte(73);
|
||||
this.writeByte(70);
|
||||
this.writeByte(0);
|
||||
this.writeByte(1);
|
||||
this.writeByte(1);
|
||||
this.writeByte(0);
|
||||
this.writeWord(1);
|
||||
this.writeWord(1);
|
||||
this.writeByte(0);
|
||||
this.writeByte(0);
|
||||
}
|
||||
|
||||
private function writeSOF0(width:int, height:int) : void
|
||||
{
|
||||
this.writeWord(65472);
|
||||
this.writeWord(17);
|
||||
this.writeByte(8);
|
||||
this.writeWord(height);
|
||||
this.writeWord(width);
|
||||
this.writeByte(3);
|
||||
this.writeByte(1);
|
||||
this.writeByte(17);
|
||||
this.writeByte(0);
|
||||
this.writeByte(2);
|
||||
this.writeByte(17);
|
||||
this.writeByte(1);
|
||||
this.writeByte(3);
|
||||
this.writeByte(17);
|
||||
this.writeByte(1);
|
||||
}
|
||||
|
||||
private function writeDQT() : void
|
||||
{
|
||||
var i:int = 0;
|
||||
this.writeWord(65499);
|
||||
this.writeWord(132);
|
||||
this.writeByte(0);
|
||||
for(i = 0; i < 64; i++)
|
||||
{
|
||||
this.writeByte(this.YTable[i]);
|
||||
}
|
||||
this.writeByte(1);
|
||||
for(i = 0; i < 64; i++)
|
||||
{
|
||||
this.writeByte(this.UVTable[i]);
|
||||
}
|
||||
}
|
||||
|
||||
private function writeDHT() : void
|
||||
{
|
||||
var i:int = 0;
|
||||
this.writeWord(65476);
|
||||
this.writeWord(418);
|
||||
this.writeByte(0);
|
||||
for(i = 0; i < 16; i++)
|
||||
{
|
||||
this.writeByte(this.std_dc_luminance_nrcodes[i + 1]);
|
||||
}
|
||||
for(i = 0; i <= 11; i++)
|
||||
{
|
||||
this.writeByte(this.std_dc_luminance_values[i]);
|
||||
}
|
||||
this.writeByte(16);
|
||||
for(i = 0; i < 16; i++)
|
||||
{
|
||||
this.writeByte(this.std_ac_luminance_nrcodes[i + 1]);
|
||||
}
|
||||
for(i = 0; i <= 161; i++)
|
||||
{
|
||||
this.writeByte(this.std_ac_luminance_values[i]);
|
||||
}
|
||||
this.writeByte(1);
|
||||
for(i = 0; i < 16; i++)
|
||||
{
|
||||
this.writeByte(this.std_dc_chrominance_nrcodes[i + 1]);
|
||||
}
|
||||
for(i = 0; i <= 11; i++)
|
||||
{
|
||||
this.writeByte(this.std_dc_chrominance_values[i]);
|
||||
}
|
||||
this.writeByte(17);
|
||||
for(i = 0; i < 16; i++)
|
||||
{
|
||||
this.writeByte(this.std_ac_chrominance_nrcodes[i + 1]);
|
||||
}
|
||||
for(i = 0; i <= 161; i++)
|
||||
{
|
||||
this.writeByte(this.std_ac_chrominance_values[i]);
|
||||
}
|
||||
}
|
||||
|
||||
private function writeSOS() : void
|
||||
{
|
||||
this.writeWord(65498);
|
||||
this.writeWord(12);
|
||||
this.writeByte(3);
|
||||
this.writeByte(1);
|
||||
this.writeByte(0);
|
||||
this.writeByte(2);
|
||||
this.writeByte(17);
|
||||
this.writeByte(3);
|
||||
this.writeByte(17);
|
||||
this.writeByte(0);
|
||||
this.writeByte(63);
|
||||
this.writeByte(0);
|
||||
}
|
||||
|
||||
private function processDU(CDU:Array, fdtbl:Array, DC:Number, HTDC:Array, HTAC:Array) : Number
|
||||
{
|
||||
var i:int = 0;
|
||||
var startpos:int = 0;
|
||||
var nrzeroes:int = 0;
|
||||
var nrmarker:int = 0;
|
||||
var EOB:BitString = HTAC[0];
|
||||
var M16zeroes:BitString = HTAC[240];
|
||||
var DU_DCT:Array = this.fDCTQuant(CDU,fdtbl);
|
||||
for(i = 0; i < 64; i++)
|
||||
{
|
||||
this.DU[this.ZigZag[i]] = DU_DCT[i];
|
||||
}
|
||||
var Diff:int = this.DU[0] - DC;
|
||||
DC = Number(this.DU[0]);
|
||||
if(Diff == 0)
|
||||
{
|
||||
this.writeBits(HTDC[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.writeBits(HTDC[this.category[32767 + Diff]]);
|
||||
this.writeBits(this.bitcode[32767 + Diff]);
|
||||
}
|
||||
var end0pos:int = 63;
|
||||
while(end0pos > 0 && this.DU[end0pos] == 0)
|
||||
{
|
||||
end0pos--;
|
||||
}
|
||||
if(end0pos == 0)
|
||||
{
|
||||
this.writeBits(EOB);
|
||||
return DC;
|
||||
}
|
||||
i = 1;
|
||||
while(i <= end0pos)
|
||||
{
|
||||
startpos = i;
|
||||
while(this.DU[i] == 0 && i <= end0pos)
|
||||
{
|
||||
i++;
|
||||
}
|
||||
nrzeroes = i - startpos;
|
||||
if(nrzeroes >= 16)
|
||||
{
|
||||
for(nrmarker = 1; nrmarker <= nrzeroes / 16; nrmarker++)
|
||||
{
|
||||
this.writeBits(M16zeroes);
|
||||
}
|
||||
nrzeroes = int(nrzeroes & 0x0F);
|
||||
}
|
||||
this.writeBits(HTAC[nrzeroes * 16 + this.category[32767 + this.DU[i]]]);
|
||||
this.writeBits(this.bitcode[32767 + this.DU[i]]);
|
||||
i++;
|
||||
}
|
||||
if(end0pos != 63)
|
||||
{
|
||||
this.writeBits(EOB);
|
||||
}
|
||||
return DC;
|
||||
}
|
||||
|
||||
private function RGB2YUV(source:Object, xpos:int, ypos:int, width:int = 0, height:int = 0) : void
|
||||
{
|
||||
var x:int = 0;
|
||||
var P:uint = 0;
|
||||
var R:Number = NaN;
|
||||
var G:Number = NaN;
|
||||
var B:Number = NaN;
|
||||
var pos:int = 0;
|
||||
for(var y:int = 0; y < 8; y++)
|
||||
{
|
||||
for(x = 0; x < 8; x++)
|
||||
{
|
||||
P = this.getPixel32(source,xpos + x,ypos + y,width,height);
|
||||
R = Number(P >> 16 & 0xFF);
|
||||
G = Number(P >> 8 & 0xFF);
|
||||
B = Number(P & 0xFF);
|
||||
this.YDU[pos] = 0.299 * R + 0.587 * G + 0.114 * B - 128;
|
||||
this.UDU[pos] = -0.16874 * R + -0.33126 * G + 0.5 * B;
|
||||
this.VDU[pos] = 0.5 * R + -0.41869 * G + -0.08131 * B;
|
||||
pos++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function getPixel32(source:Object, x:int, y:int, width:int = 0, height:int = 0) : uint
|
||||
{
|
||||
var bitmap:BitmapData = null;
|
||||
var byteArray:ByteArray = null;
|
||||
if(source is BitmapData)
|
||||
{
|
||||
bitmap = source as BitmapData;
|
||||
return bitmap.getPixel32(x,y);
|
||||
}
|
||||
if(source is ByteArray)
|
||||
{
|
||||
byteArray = source as ByteArray;
|
||||
byteArray.position = y * width * 4 + x * 4;
|
||||
return byteArray.readUnsignedInt();
|
||||
}
|
||||
throw new ArgumentError("The source argument must be an instance of flash.display.BitmapData or flash.utils.ByteArray.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class BitString
|
||||
{
|
||||
|
||||
public var len:int = 0;
|
||||
|
||||
public var val:int = 0;
|
||||
|
||||
public function BitString()
|
||||
{
|
||||
super();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,736 @@
|
||||
package com.sbi.graphics
|
||||
{
|
||||
import com.sbi.loader.ImageServerEvent;
|
||||
import com.sbi.loader.ImageServerURL;
|
||||
import flash.display.DisplayObjectContainer;
|
||||
import flash.geom.Matrix;
|
||||
import flash.geom.Point;
|
||||
import flash.geom.Rectangle;
|
||||
|
||||
public class LayerAnim
|
||||
{
|
||||
|
||||
private static var _activePool:Array;
|
||||
|
||||
private static var _freePool:Array;
|
||||
|
||||
private static var _avOffsets:Array;
|
||||
|
||||
private static var _lastEntry:int;
|
||||
|
||||
private static var _useMaxThrottle:Function;
|
||||
|
||||
public static const PLAY_LOOP:int = 0;
|
||||
|
||||
public static const PLAY_ONE_SHOT:int = 1;
|
||||
|
||||
public static const PLAY_LOOP__KEEP_CURRENT_FRAME:int = 2;
|
||||
|
||||
public static const AN_SIT_NNE:int = 1;
|
||||
|
||||
public static const AN_SIT_NE:int = 2;
|
||||
|
||||
public static const AN_SIT_E:int = 3;
|
||||
|
||||
public static const AN_SIT_SE:int = 4;
|
||||
|
||||
public static const AN_SIT_SSE:int = 5;
|
||||
|
||||
public static const AN_MOVE_NNE:int = 7;
|
||||
|
||||
public static const AN_MOVE_NE:int = 8;
|
||||
|
||||
public static const AN_MOVE_E:int = 9;
|
||||
|
||||
public static const AN_MOVE_SE:int = 10;
|
||||
|
||||
public static const AN_MOVE_SSE:int = 11;
|
||||
|
||||
public static const AN_BBCARD:int = 13;
|
||||
|
||||
public static const AN_IDLE_SE:int = 14;
|
||||
|
||||
public static const AN_HUD_AVT:int = 15;
|
||||
|
||||
public static const AN_IDLE_NE:int = 16;
|
||||
|
||||
public static const AN_HOP:int = 17;
|
||||
|
||||
public static const AN_SLEEP:int = 22;
|
||||
|
||||
public static const AN_DANCE:int = 23;
|
||||
|
||||
public static const AN_PLAY:int = 6;
|
||||
|
||||
public static const AN_SWIM_N:int = 36;
|
||||
|
||||
public static const AN_SWIM_NE:int = 34;
|
||||
|
||||
public static const AN_SWIM_E:int = 29;
|
||||
|
||||
public static const AN_SWIM_SE:int = 35;
|
||||
|
||||
public static const AN_SWIM_S:int = 37;
|
||||
|
||||
public static const AN_FLOAT:int = 32;
|
||||
|
||||
public static const AN_WATER_DIVE:int = 41;
|
||||
|
||||
public static const AN_WATER_PLAY:int = 39;
|
||||
|
||||
public static const AN_WATER_SPIN:int = 33;
|
||||
|
||||
public static const AN_WATER_JIG:int = 38;
|
||||
|
||||
public static const AN_WATER_POSE:int = 40;
|
||||
|
||||
public static const AN_PHOTO:int = 42;
|
||||
|
||||
public static const AN_PARACHUTE_IDLE:int = 19;
|
||||
|
||||
public static const AN_PARACHUTE_JUMP:int = 24;
|
||||
|
||||
public static const AN_PARACHUTE_DISTRESS:int = 20;
|
||||
|
||||
public static const AN_PARACHUTE_LAND:int = 21;
|
||||
|
||||
public static const AN_WINDRIDER_JUMP:int = 28;
|
||||
|
||||
public static const AN_WINDRIDER_IDLE:int = 29;
|
||||
|
||||
public static const AN_WINDRIDER_FALL:int = 30;
|
||||
|
||||
public static const AN_WINDRIDER_START:int = 31;
|
||||
|
||||
public static const AN_WINDRIDER_DISTRESS:int = 20;
|
||||
|
||||
public static const AVATAR_BITMAP_OFFSET_X:int = 375;
|
||||
|
||||
public static const AVATAR_BITMAP_OFFSET_Y:int = 365;
|
||||
|
||||
private static var _throttle:int = 8;
|
||||
|
||||
private var _bitmap:LayerBitmap;
|
||||
|
||||
private var _avDefId:int;
|
||||
|
||||
private var _animId:int;
|
||||
|
||||
private var _frame:int;
|
||||
|
||||
private var _hFlip:Boolean;
|
||||
|
||||
private var _totalFrames:int;
|
||||
|
||||
private var _x:int;
|
||||
|
||||
private var _y:int;
|
||||
|
||||
private var _rotation:int;
|
||||
|
||||
private var _xOff:int;
|
||||
|
||||
private var _yOff:int;
|
||||
|
||||
private var _bPause:Boolean;
|
||||
|
||||
private var _layers:Array;
|
||||
|
||||
private var _colors:Array;
|
||||
|
||||
private var _callback:Function;
|
||||
|
||||
private var _mode:int;
|
||||
|
||||
private var _tStep:int;
|
||||
|
||||
private var _bRedraw:Boolean;
|
||||
|
||||
private var _bReload:Boolean;
|
||||
|
||||
private var _bBaseLayer:Boolean;
|
||||
|
||||
private var _avatarEnabled:Boolean = true;
|
||||
|
||||
private var _bNewAnim:Boolean;
|
||||
|
||||
private var _requestAnimId:int;
|
||||
|
||||
private var _receivedData:Array;
|
||||
|
||||
private var _requestedIds:Array;
|
||||
|
||||
private var _requestCallback:Function;
|
||||
|
||||
private var _requestMode:int;
|
||||
|
||||
private var _preloadRequestedIds:Array;
|
||||
|
||||
private var _preloadCallback:Function;
|
||||
|
||||
private var _bounceEnabled:Boolean;
|
||||
|
||||
public function LayerAnim(lock:Class)
|
||||
{
|
||||
super();
|
||||
if(lock != SingletonLock)
|
||||
{
|
||||
throw new Error("Invalid access. Use LayerAnim.getNew");
|
||||
}
|
||||
this._requestedIds = new Array();
|
||||
this._colors = new Array();
|
||||
}
|
||||
|
||||
public static function set avOffsets(a:Array) : void
|
||||
{
|
||||
_avOffsets = a;
|
||||
}
|
||||
|
||||
public static function getAvOffset(avDefId:int) : Point
|
||||
{
|
||||
if(Boolean(_avOffsets[avDefId]))
|
||||
{
|
||||
return _avOffsets[avDefId];
|
||||
}
|
||||
return new Point(-100,-100);
|
||||
}
|
||||
|
||||
public static function set throttle(t:int) : void
|
||||
{
|
||||
_throttle = t;
|
||||
}
|
||||
|
||||
public static function getNew() : LayerAnim
|
||||
{
|
||||
var an:LayerAnim = null;
|
||||
if(!_activePool)
|
||||
{
|
||||
_activePool = new Array();
|
||||
_freePool = new Array();
|
||||
}
|
||||
if(Boolean(_freePool.length))
|
||||
{
|
||||
an = _freePool.pop();
|
||||
}
|
||||
else
|
||||
{
|
||||
an = new LayerAnim(SingletonLock);
|
||||
}
|
||||
_activePool.push(an);
|
||||
an.init();
|
||||
return an;
|
||||
}
|
||||
|
||||
public static function destroy(an:LayerAnim) : void
|
||||
{
|
||||
var i:int = _activePool.indexOf(an);
|
||||
if(i >= 0)
|
||||
{
|
||||
_activePool.splice(i,1);
|
||||
an.destroy();
|
||||
_freePool.push(an);
|
||||
}
|
||||
}
|
||||
|
||||
public static function heartbeat() : void
|
||||
{
|
||||
var total:int = 0;
|
||||
var countdown:int = 0;
|
||||
var throttle:int = 0;
|
||||
var i:int = 0;
|
||||
var la:LayerAnim = null;
|
||||
var tStep:int = 0;
|
||||
if(Boolean(_activePool) && Boolean(_activePool.length))
|
||||
{
|
||||
total = int(_activePool.length);
|
||||
countdown = total;
|
||||
throttle = total * 0.5;
|
||||
if(throttle < 1 || Boolean(_useMaxThrottle) && Boolean(_useMaxThrottle()))
|
||||
{
|
||||
throttle = 1;
|
||||
}
|
||||
if(throttle > _throttle)
|
||||
{
|
||||
throttle = _throttle;
|
||||
}
|
||||
i = _lastEntry;
|
||||
for each(la in _activePool)
|
||||
{
|
||||
++la._tStep;
|
||||
}
|
||||
while(throttle > 0 && countdown > 0)
|
||||
{
|
||||
if(i >= total)
|
||||
{
|
||||
i = 0;
|
||||
}
|
||||
la = _activePool[i++];
|
||||
tStep = la._tStep;
|
||||
if(tStep >= 2)
|
||||
{
|
||||
if((la._requestMode == PLAY_ONE_SHOT || isOnscreen(la)) && la.heartbeat())
|
||||
{
|
||||
la._tStep = 0;
|
||||
throttle--;
|
||||
}
|
||||
}
|
||||
countdown--;
|
||||
}
|
||||
_lastEntry = i;
|
||||
}
|
||||
}
|
||||
|
||||
private static function isOnscreen(la:LayerAnim) : Boolean
|
||||
{
|
||||
var left1:int = 0;
|
||||
var right1:int = 0;
|
||||
var top1:int = 0;
|
||||
var bottom1:int = 0;
|
||||
var rect:Rectangle = null;
|
||||
var theParent:DisplayObjectContainer = la.bitmap.parent;
|
||||
if(Boolean(theParent))
|
||||
{
|
||||
left1 = 0;
|
||||
right1 = 900;
|
||||
top1 = 0;
|
||||
bottom1 = 550;
|
||||
rect = theParent.getBounds(gMainFrame.stage);
|
||||
return rect.height == 0 || !(bottom1 < rect.top || top1 > rect.bottom || right1 < rect.left || left1 > rect.right);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function set useMaxThrottle(func:Function) : void
|
||||
{
|
||||
_useMaxThrottle = func;
|
||||
}
|
||||
|
||||
public function get bounceEnabled() : Boolean
|
||||
{
|
||||
return this._bounceEnabled;
|
||||
}
|
||||
|
||||
public function set bounceEnabled(b:Boolean) : void
|
||||
{
|
||||
this._bounceEnabled = b;
|
||||
}
|
||||
|
||||
private function init() : void
|
||||
{
|
||||
this._bitmap = new LayerBitmap();
|
||||
this._layers = null;
|
||||
this._requestAnimId = -1;
|
||||
this._avDefId = -1;
|
||||
this._animId = -1;
|
||||
this._bNewAnim = false;
|
||||
this._totalFrames = 0;
|
||||
this._bReload = true;
|
||||
this._tStep = 1;
|
||||
this._bounceEnabled = false;
|
||||
}
|
||||
|
||||
private function destroy() : void
|
||||
{
|
||||
this._bitmap = null;
|
||||
this._layers = null;
|
||||
if(this._requestedIds.length != 0)
|
||||
{
|
||||
ImageServerURL.instance.removeEventListener(ImageServerEvent.ON_NEW_DATA,this.onReceiveData);
|
||||
this._requestedIds.splice(0,this._requestedIds.length);
|
||||
}
|
||||
}
|
||||
|
||||
public function set avDefId(i:int) : void
|
||||
{
|
||||
if(i != this._avDefId)
|
||||
{
|
||||
this._avDefId = i;
|
||||
this._xOff = -getAvOffset(this._avDefId).x - AVATAR_BITMAP_OFFSET_X;
|
||||
this._yOff = -getAvOffset(this._avDefId).y - AVATAR_BITMAP_OFFSET_Y;
|
||||
this._bReload = true;
|
||||
}
|
||||
}
|
||||
|
||||
public function get layers() : Array
|
||||
{
|
||||
return this._layers;
|
||||
}
|
||||
|
||||
public function get avDefId() : int
|
||||
{
|
||||
return this._avDefId;
|
||||
}
|
||||
|
||||
public function get animId() : int
|
||||
{
|
||||
return this._animId;
|
||||
}
|
||||
|
||||
public function get hFlip() : Boolean
|
||||
{
|
||||
return this._hFlip;
|
||||
}
|
||||
|
||||
public function get frame() : int
|
||||
{
|
||||
return this._frame;
|
||||
}
|
||||
|
||||
public function set frame(f:int) : void
|
||||
{
|
||||
this._frame = f;
|
||||
}
|
||||
|
||||
public function set avatarEnabled(b:Boolean) : void
|
||||
{
|
||||
this._avatarEnabled = b;
|
||||
}
|
||||
|
||||
public function set layers(l:Array) : void
|
||||
{
|
||||
var i:int = 0;
|
||||
var o:Object = null;
|
||||
var layerIndex:int = 0;
|
||||
var bDiffLayer:Boolean = true;
|
||||
var bDiffColor:Boolean = false;
|
||||
if(Boolean(this._layers) && this._layers.length == l.length)
|
||||
{
|
||||
bDiffLayer = false;
|
||||
for(i = 0; i < l.length; i++)
|
||||
{
|
||||
if(this._layers[i].l != l[i].l)
|
||||
{
|
||||
bDiffLayer = true;
|
||||
break;
|
||||
}
|
||||
if(this._layers[i].c != l[i].c)
|
||||
{
|
||||
bDiffColor = true;
|
||||
}
|
||||
}
|
||||
if(!bDiffLayer && !bDiffColor)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
if(l.length > 0)
|
||||
{
|
||||
this._layers = l;
|
||||
if(bDiffLayer)
|
||||
{
|
||||
this._bReload = true;
|
||||
}
|
||||
else if(bDiffColor)
|
||||
{
|
||||
for each(o in l)
|
||||
{
|
||||
layerIndex = ImageServerURL.instance.getLayerIndex(o.l);
|
||||
this._bitmap.setLayerColor(layerIndex,o.c);
|
||||
}
|
||||
this._bRedraw = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function get x() : int
|
||||
{
|
||||
return this._x;
|
||||
}
|
||||
|
||||
public function get y() : int
|
||||
{
|
||||
return this._y;
|
||||
}
|
||||
|
||||
public function set x(x:int) : void
|
||||
{
|
||||
this._x = x;
|
||||
}
|
||||
|
||||
public function set y(y:int) : void
|
||||
{
|
||||
this._y = y;
|
||||
}
|
||||
|
||||
public function set alpha(a:Number) : void
|
||||
{
|
||||
this._bitmap.alpha = a;
|
||||
}
|
||||
|
||||
public function set rotation(deg:Number) : void
|
||||
{
|
||||
this._rotation = deg;
|
||||
}
|
||||
|
||||
public function set hFlip(f:Boolean) : void
|
||||
{
|
||||
if(f != this._hFlip)
|
||||
{
|
||||
this._hFlip = f;
|
||||
this._bRedraw = true;
|
||||
}
|
||||
}
|
||||
|
||||
public function set visible(f:Boolean) : void
|
||||
{
|
||||
this._bitmap.visible = f;
|
||||
}
|
||||
|
||||
public function get visible() : Boolean
|
||||
{
|
||||
return this._bitmap.visible;
|
||||
}
|
||||
|
||||
public function set pause(p:Boolean) : void
|
||||
{
|
||||
this._bPause = p;
|
||||
}
|
||||
|
||||
public function get bitmap() : LayerBitmap
|
||||
{
|
||||
return this._bitmap;
|
||||
}
|
||||
|
||||
public function playAnim(animId:int, mode:int = 0, callback:Function = null) : void
|
||||
{
|
||||
if(this._requestAnimId == -1 && animId != this._animId || this._requestAnimId != animId)
|
||||
{
|
||||
this._requestCallback = callback;
|
||||
this._requestMode = mode;
|
||||
this._requestAnimId = animId;
|
||||
this._bReload = true;
|
||||
}
|
||||
}
|
||||
|
||||
public function preload(animList:Array, callback:Function) : void
|
||||
{
|
||||
var i:int = 0;
|
||||
this._preloadCallback = callback;
|
||||
for(this._preloadRequestedIds = []; i < animList.length; )
|
||||
{
|
||||
this.buildRequestArray(animList[i],this._preloadRequestedIds,null);
|
||||
i++;
|
||||
}
|
||||
ImageServerURL.instance.addEventListener(ImageServerEvent.ON_NEW_DATA,this.onReceiveData);
|
||||
ImageServerURL.instance.requestImages(this._preloadRequestedIds.slice());
|
||||
}
|
||||
|
||||
private function heartbeat() : Boolean
|
||||
{
|
||||
var bounceHeight:int = 0;
|
||||
var bPainted:Boolean = false;
|
||||
if(this._bReload)
|
||||
{
|
||||
this.loadAnim();
|
||||
}
|
||||
if(this._totalFrames == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if(this._frame >= this._totalFrames)
|
||||
{
|
||||
if(this._mode == PLAY_ONE_SHOT)
|
||||
{
|
||||
this._frame = this._totalFrames > 0 ? int(this._totalFrames - 1) : 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
this._frame = 0;
|
||||
}
|
||||
if(this._callback != null)
|
||||
{
|
||||
this._callback(this,this._animId);
|
||||
if(this._mode == PLAY_ONE_SHOT)
|
||||
{
|
||||
this._callback = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(this._bRedraw)
|
||||
{
|
||||
this._bitmap.invalidate();
|
||||
this._bRedraw = false;
|
||||
}
|
||||
if(this._avatarEnabled)
|
||||
{
|
||||
bPainted = this._bitmap.paint(this._frame,this._hFlip);
|
||||
}
|
||||
var offSet:int = 0;
|
||||
if(this._bounceEnabled)
|
||||
{
|
||||
bounceHeight = 150;
|
||||
offSet = bounceHeight * (this._frame + 1) / this._totalFrames;
|
||||
if(offSet > bounceHeight / 2)
|
||||
{
|
||||
offSet = bounceHeight - offSet;
|
||||
}
|
||||
}
|
||||
this.updateBitmapPos(offSet);
|
||||
if(!this._bPause)
|
||||
{
|
||||
++this._frame;
|
||||
}
|
||||
return bPainted;
|
||||
}
|
||||
|
||||
private function loadAnim() : void
|
||||
{
|
||||
var anId:int = 0;
|
||||
if(this._layers != null && this._layers.length > 0)
|
||||
{
|
||||
anId = this._requestAnimId != -1 ? this._requestAnimId : this._animId;
|
||||
if(anId >= 0)
|
||||
{
|
||||
this._requestAnimId = anId;
|
||||
this.requestAnim(anId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function updateBitmapPos(offSet:int) : void
|
||||
{
|
||||
var m:Matrix = null;
|
||||
var ap:Point = this._bitmap.anchorPoint;
|
||||
var x:int = this._x + ap.x + this._xOff;
|
||||
var y:int = this._y + ap.y + this._yOff - offSet;
|
||||
if(Boolean(this._rotation))
|
||||
{
|
||||
m = this._bitmap.transform.matrix;
|
||||
m.identity();
|
||||
m.translate(ap.x + this._xOff,ap.y + this._yOff);
|
||||
m.rotate(this._rotation * (Math.PI / 180));
|
||||
m.translate(x,y);
|
||||
this._bitmap.transform.matrix = m;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(this._bitmap.rotation != this._rotation)
|
||||
{
|
||||
this._bitmap.rotation = this._rotation;
|
||||
}
|
||||
this._bitmap.x = x;
|
||||
this._bitmap.y = y;
|
||||
}
|
||||
}
|
||||
|
||||
private function requestAnim(id:int) : void
|
||||
{
|
||||
this._bNewAnim = false;
|
||||
this._requestedIds.splice(0,this._requestedIds.length);
|
||||
this._colors = [];
|
||||
this._receivedData = [];
|
||||
this.buildRequestArray(id,this._requestedIds,this._colors);
|
||||
ImageServerURL.instance.addEventListener(ImageServerEvent.ON_NEW_DATA,this.onReceiveData);
|
||||
ImageServerURL.instance.requestImages(this._requestedIds.slice());
|
||||
this._bReload = false;
|
||||
this._bBaseLayer = false;
|
||||
}
|
||||
|
||||
private function buildRequestArray(id:int, a:Array, colors:Array) : void
|
||||
{
|
||||
var o:Object = null;
|
||||
var packId:uint = 0;
|
||||
a.push(ImageArrayHelper.packId(this._avDefId,id,0));
|
||||
for each(o in this._layers)
|
||||
{
|
||||
packId = ImageArrayHelper.packId(this._avDefId,id,o.l);
|
||||
a.push(packId);
|
||||
if(Boolean(colors))
|
||||
{
|
||||
colors[packId] = o.c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function onReceiveData(e:ImageServerEvent) : void
|
||||
{
|
||||
var i:int = 0;
|
||||
var color:uint = 0;
|
||||
var o:Object = null;
|
||||
var layer:int = e.layer;
|
||||
if(Boolean(this._preloadRequestedIds) && Boolean(this._preloadRequestedIds.length))
|
||||
{
|
||||
i = this._preloadRequestedIds.indexOf(e.id);
|
||||
if(i >= 0)
|
||||
{
|
||||
this._preloadRequestedIds.splice(i,1);
|
||||
if(this._preloadRequestedIds.length == 0)
|
||||
{
|
||||
this._preloadRequestedIds = null;
|
||||
if(this._requestedIds.length == 0)
|
||||
{
|
||||
ImageServerURL.instance.removeEventListener(ImageServerEvent.ON_NEW_DATA,this.onReceiveData);
|
||||
}
|
||||
if(this._preloadCallback != null)
|
||||
{
|
||||
this._preloadCallback(this);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
i = this._requestedIds.indexOf(e.id);
|
||||
if(i >= 0)
|
||||
{
|
||||
this._requestedIds.splice(i,1);
|
||||
if(layer > 0)
|
||||
{
|
||||
color = uint(this._colors[e.id]);
|
||||
}
|
||||
if(e.success)
|
||||
{
|
||||
this._receivedData.push({
|
||||
"l":layer,
|
||||
"d":e.imageData,
|
||||
"c":color
|
||||
});
|
||||
}
|
||||
if(this._requestedIds.length == 0)
|
||||
{
|
||||
this._bNewAnim = true;
|
||||
if(this._preloadRequestedIds == null)
|
||||
{
|
||||
ImageServerURL.instance.removeEventListener(ImageServerEvent.ON_NEW_DATA,this.onReceiveData);
|
||||
}
|
||||
}
|
||||
if(layer == 1 || this._bNewAnim)
|
||||
{
|
||||
this._bBaseLayer = true;
|
||||
if(this._mode == LayerAnim.PLAY_LOOP__KEEP_CURRENT_FRAME)
|
||||
{
|
||||
this._mode == LayerAnim.PLAY_LOOP;
|
||||
}
|
||||
else
|
||||
{
|
||||
this._frame = 0;
|
||||
}
|
||||
if(this._requestAnimId >= 0)
|
||||
{
|
||||
this._animId = this._requestAnimId;
|
||||
}
|
||||
this._callback = this._requestCallback;
|
||||
this._mode = this._requestMode;
|
||||
this._requestAnimId = -1;
|
||||
this._bitmap.release();
|
||||
}
|
||||
if(this._bBaseLayer)
|
||||
{
|
||||
for each(o in this._receivedData)
|
||||
{
|
||||
this._bitmap.setLayer(o.l,o.d,o.c);
|
||||
}
|
||||
this._totalFrames = this._bitmap.length;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class SingletonLock
|
||||
{
|
||||
|
||||
public function SingletonLock()
|
||||
{
|
||||
super();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,646 @@
|
||||
package com.sbi.graphics
|
||||
{
|
||||
import flash.display.Bitmap;
|
||||
import flash.display.BitmapData;
|
||||
import flash.geom.Point;
|
||||
import flash.geom.Rectangle;
|
||||
import flash.utils.ByteArray;
|
||||
|
||||
public class LayerBitmap extends Bitmap
|
||||
{
|
||||
|
||||
private const _backgroundColor:uint = 0;
|
||||
|
||||
private var _anchorPoint:Point;
|
||||
|
||||
private var _layers:Array;
|
||||
|
||||
private var _bmin:Point;
|
||||
|
||||
private var _bmax:Point;
|
||||
|
||||
private var _lastFrame:int;
|
||||
|
||||
private var _lastHFlip:Boolean;
|
||||
|
||||
private var _blendColor:uint;
|
||||
|
||||
private var _bColorChanged:Boolean;
|
||||
|
||||
public function LayerBitmap()
|
||||
{
|
||||
super();
|
||||
this._layers = new Array();
|
||||
this._anchorPoint = new Point();
|
||||
this._bmin = new Point();
|
||||
this._bmax = new Point();
|
||||
}
|
||||
|
||||
public function get length() : int
|
||||
{
|
||||
var o:Object = this._layers[0];
|
||||
if(!o)
|
||||
{
|
||||
o = this._layers[1];
|
||||
}
|
||||
if(!o)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return o.o.f.length;
|
||||
}
|
||||
|
||||
public function release() : void
|
||||
{
|
||||
this._layers.splice(0,this._layers.length);
|
||||
this._lastFrame = -1;
|
||||
this._bColorChanged = false;
|
||||
}
|
||||
|
||||
public function invalidate() : void
|
||||
{
|
||||
this._lastFrame = -1;
|
||||
}
|
||||
|
||||
public function setLayer(layer:int, o:Object, color:uint) : void
|
||||
{
|
||||
if(layer < 0 || layer > 10)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if(Boolean(o))
|
||||
{
|
||||
if(this._layers[layer] == null)
|
||||
{
|
||||
this._layers[layer] = {
|
||||
"c":-1,
|
||||
"lastColor":-1,
|
||||
"blendColor":0
|
||||
};
|
||||
}
|
||||
this._layers[layer].o = o;
|
||||
}
|
||||
else
|
||||
{
|
||||
this._layers[layer] = null;
|
||||
}
|
||||
this.setLayerColor(layer,color);
|
||||
this._lastFrame = -1;
|
||||
}
|
||||
|
||||
public function setLayerColor(layer:int, c:uint) : void
|
||||
{
|
||||
var o:Object = this._layers[layer];
|
||||
if(Boolean(o) && c != o.c)
|
||||
{
|
||||
o.c = c;
|
||||
this._bColorChanged = true;
|
||||
}
|
||||
}
|
||||
|
||||
public function setBlendColor(argb:uint) : void
|
||||
{
|
||||
if(this._blendColor != argb)
|
||||
{
|
||||
this._blendColor = argb;
|
||||
this._bColorChanged = true;
|
||||
}
|
||||
}
|
||||
|
||||
public function get anchorPoint() : Point
|
||||
{
|
||||
return this._anchorPoint;
|
||||
}
|
||||
|
||||
private function refreshLayerPalette(i:int) : void
|
||||
{
|
||||
var c:uint = 0;
|
||||
var gp:Array = null;
|
||||
var c1:int = 0;
|
||||
var c2:int = 0;
|
||||
var c3:int = 0;
|
||||
var c4:int = 0;
|
||||
var a:int = 0;
|
||||
var o:Object = this._layers[i];
|
||||
if(Boolean(o) && (o.lastColor != o.c || o.blendColor != this._blendColor))
|
||||
{
|
||||
c = uint(o.c);
|
||||
gp = PaletteHelper.gamePalette;
|
||||
c1 = c >> 24 & 0xFF;
|
||||
c2 = c >> 16 & 0xFF;
|
||||
c3 = c >> 8 & 0xFF;
|
||||
c4 = c & 0xFF;
|
||||
if(!o.pal)
|
||||
{
|
||||
o.pal = [];
|
||||
}
|
||||
PaletteHelper.BuildAnimalPalette(o.pal,gp[c1],gp[c2],gp[c3],gp[c4]);
|
||||
a = this._blendColor >> 24 & 0xFF;
|
||||
if(i != 3)
|
||||
{
|
||||
if(a > 0)
|
||||
{
|
||||
PaletteHelper.adjustPaletteColor(o.pal,this._blendColor);
|
||||
}
|
||||
o.blendColor = this._blendColor;
|
||||
}
|
||||
o.lastColor = c;
|
||||
this._lastFrame = -1;
|
||||
}
|
||||
}
|
||||
|
||||
private function refreshAllPalettes() : void
|
||||
{
|
||||
for(var i:int = 1; i <= 10; i++)
|
||||
{
|
||||
this.refreshLayerPalette(i);
|
||||
}
|
||||
this._bColorChanged = false;
|
||||
}
|
||||
|
||||
private function calcBounds(index:int) : void
|
||||
{
|
||||
var obj:Object = null;
|
||||
var o:Object = null;
|
||||
var total:int = 0;
|
||||
var fo:Object = null;
|
||||
var xOff:int = 0;
|
||||
var yOff:int = 0;
|
||||
var w:int = 0;
|
||||
var h:int = 0;
|
||||
this._bmin.x = 1000;
|
||||
this._bmin.y = 1000;
|
||||
this._bmax.x = 0;
|
||||
this._bmax.y = 0;
|
||||
for(var i:int = 1; i < this._layers.length; i++)
|
||||
{
|
||||
obj = this._layers[i];
|
||||
if(obj != null)
|
||||
{
|
||||
o = obj.o;
|
||||
if(o.v != ImageArray.VERSION)
|
||||
{
|
||||
return;
|
||||
}
|
||||
total = int(o.f.length);
|
||||
if(index < total)
|
||||
{
|
||||
fo = o.f[index];
|
||||
if(fo)
|
||||
{
|
||||
xOff = int(fo.x);
|
||||
yOff = int(fo.y);
|
||||
w = int(fo.w);
|
||||
h = int(fo.h);
|
||||
if(xOff < this._bmin.x)
|
||||
{
|
||||
this._bmin.x = xOff;
|
||||
}
|
||||
if(yOff < this._bmin.y)
|
||||
{
|
||||
this._bmin.y = yOff;
|
||||
}
|
||||
if(xOff + w > this._bmax.x)
|
||||
{
|
||||
this._bmax.x = xOff + w;
|
||||
}
|
||||
if(yOff + h > this._bmax.y)
|
||||
{
|
||||
this._bmax.y = yOff + h;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(this._bmin.x == 1000 || this._bmin.y == 1000)
|
||||
{
|
||||
if(Boolean(this.bitmapData))
|
||||
{
|
||||
trace("WARNING: making new empty 1x1 bitmap data (parent.name:" + parent.name + ") - is index:" + index + " valid?");
|
||||
this.bitmapData = new BitmapData(1,1,true,this._backgroundColor);
|
||||
}
|
||||
return;
|
||||
}
|
||||
this.resizeBitmap(1 + this._bmax.x - this._bmin.x,1 + this._bmax.y - this._bmin.y,true);
|
||||
this._anchorPoint.x = this._bmin.x;
|
||||
this._anchorPoint.y = this._bmin.y;
|
||||
}
|
||||
|
||||
public function paint(index:int, bHFlip:Boolean = false, bUseSeq:Boolean = true) : Boolean
|
||||
{
|
||||
var o:Object = null;
|
||||
var bRet:Boolean = false;
|
||||
var i:int = 0;
|
||||
if(bUseSeq && this._layers[0] != null)
|
||||
{
|
||||
o = this._layers[0];
|
||||
index = int(o.o.f[index]);
|
||||
}
|
||||
if(index != this._lastFrame || bHFlip != this._lastHFlip || this._bColorChanged)
|
||||
{
|
||||
if(this._bColorChanged)
|
||||
{
|
||||
this.refreshAllPalettes();
|
||||
}
|
||||
this._lastFrame = index;
|
||||
this._lastHFlip = bHFlip;
|
||||
this.calcBounds(index);
|
||||
for(i = 1; i < this._layers.length; i++)
|
||||
{
|
||||
o = this._layers[i];
|
||||
if(o != null)
|
||||
{
|
||||
this.paintFrame(o.o,index,o.pal,bHFlip);
|
||||
}
|
||||
}
|
||||
bRet = true;
|
||||
if(bHFlip)
|
||||
{
|
||||
this._anchorPoint.x = 500 - this._bmax.x;
|
||||
}
|
||||
}
|
||||
return bRet;
|
||||
}
|
||||
|
||||
private function paintFrame(top:Object, index:int, palette:Array, bHFlip:Boolean) : void
|
||||
{
|
||||
var p:int = 0;
|
||||
var count:int = 0;
|
||||
var c:uint = 0;
|
||||
var d:uint = 0;
|
||||
var a1:int = 0;
|
||||
var r1:int = 0;
|
||||
var g1:int = 0;
|
||||
var b1:int = 0;
|
||||
var a2:int = 0;
|
||||
var r2:int = 0;
|
||||
var g2:int = 0;
|
||||
var b2:int = 0;
|
||||
var wOff:int = 0;
|
||||
var scr:Number = NaN;
|
||||
var c1:uint = 0;
|
||||
var shadowMultiplyer:int = 0;
|
||||
if(index >= top.f.length)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var o:Object = top.f[index];
|
||||
if(!o)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var bm:BitmapData = this.bitmapData;
|
||||
var pack:ByteArray = o.b;
|
||||
var xOff:int = int(o.x);
|
||||
var yOff:int = int(o.y);
|
||||
var w:int = int(o.w);
|
||||
var h:int = int(o.h);
|
||||
xOff -= this._bmin.x;
|
||||
yOff -= this._bmin.y;
|
||||
w += xOff;
|
||||
pack.position = 0;
|
||||
var max:int = int(pack.length);
|
||||
var x:int = xOff;
|
||||
var y:int = yOff;
|
||||
this.bitmapData.lock();
|
||||
var ow:int = w - xOff;
|
||||
if(!bHFlip)
|
||||
{
|
||||
while(pack.position < max)
|
||||
{
|
||||
p = int(pack.readUnsignedByte());
|
||||
count = 1;
|
||||
if((p & 0x80) == 128)
|
||||
{
|
||||
p &= 127;
|
||||
count = pack.readUnsignedByte() + 1;
|
||||
}
|
||||
if(p == 0)
|
||||
{
|
||||
x += count;
|
||||
if(x >= w)
|
||||
{
|
||||
count = x - w;
|
||||
x = xOff + count % ow;
|
||||
y += count / ow + 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
c = uint(palette[p]);
|
||||
if(c >= 4278190080)
|
||||
{
|
||||
if(x + count < w)
|
||||
{
|
||||
while(--count > -1)
|
||||
{
|
||||
bm.setPixel32(x++,y,c);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while(--count > -1)
|
||||
{
|
||||
bm.setPixel32(x++,y,c);
|
||||
if(x == w)
|
||||
{
|
||||
x = xOff;
|
||||
y++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while(--count > -1)
|
||||
{
|
||||
d = bm.getPixel32(x,y);
|
||||
if(d != this._backgroundColor)
|
||||
{
|
||||
a1 = c >> 24 & 0xFF;
|
||||
r1 = c >> 16 & 0xFF;
|
||||
g1 = c >> 8 & 0xFF;
|
||||
b1 = c & 0xFF;
|
||||
a2 = d >> 24 & 0xFF;
|
||||
r2 = d >> 16 & 0xFF;
|
||||
g2 = d >> 8 & 0xFF;
|
||||
b2 = d & 0xFF;
|
||||
if(a1 == 192)
|
||||
{
|
||||
r1 = (r1 + r1 + r1 + r2) * 0.25;
|
||||
g1 = (g1 + g1 + g1 + g2) * 0.25;
|
||||
b1 = (b1 + b1 + b1 + b2) * 0.25;
|
||||
}
|
||||
else if(a1 == 128)
|
||||
{
|
||||
r1 = (r1 + r2) * 0.5;
|
||||
g1 = (g1 + g2) * 0.5;
|
||||
b1 = (b1 + b2) * 0.5;
|
||||
}
|
||||
else
|
||||
{
|
||||
r1 = (r1 + r2 + r2 + r2) * 0.25;
|
||||
g1 = (g1 + g2 + g2 + g2) * 0.25;
|
||||
b1 = (b1 + b2 + b2 + b2) * 0.25;
|
||||
}
|
||||
if(a2 > a1)
|
||||
{
|
||||
a1 = a2;
|
||||
}
|
||||
c = uint(a1 << 24 | r1 << 16 | g1 << 8 | b1);
|
||||
}
|
||||
bm.setPixel32(x++,y,c);
|
||||
if(x == w)
|
||||
{
|
||||
x = xOff;
|
||||
y++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
wOff = this._bmax.x - this._bmin.x;
|
||||
while(pack.position < max)
|
||||
{
|
||||
p = int(pack.readUnsignedByte());
|
||||
count = 1;
|
||||
if((p & 0x80) == 128)
|
||||
{
|
||||
p &= 127;
|
||||
count = pack.readUnsignedByte() + 1;
|
||||
}
|
||||
if(p == 0)
|
||||
{
|
||||
x += count;
|
||||
if(x >= w)
|
||||
{
|
||||
count = x - w;
|
||||
x = xOff + count % ow;
|
||||
y += count / ow + 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
c = uint(palette[p]);
|
||||
if(c >= 4278190080)
|
||||
{
|
||||
if(x + count < w)
|
||||
{
|
||||
while(--count > -1)
|
||||
{
|
||||
bm.setPixel32(wOff - x++,y,c);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while(--count > -1)
|
||||
{
|
||||
bm.setPixel32(wOff - x++,y,c);
|
||||
if(x == w)
|
||||
{
|
||||
x = xOff;
|
||||
y++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while(--count > -1)
|
||||
{
|
||||
d = bm.getPixel32(wOff - x,y);
|
||||
if(d != this._backgroundColor)
|
||||
{
|
||||
a1 = c >> 24 & 0xFF;
|
||||
r1 = c >> 16 & 0xFF;
|
||||
g1 = c >> 8 & 0xFF;
|
||||
b1 = c & 0xFF;
|
||||
a2 = d >> 24 & 0xFF;
|
||||
r2 = d >> 16 & 0xFF;
|
||||
g2 = d >> 8 & 0xFF;
|
||||
b2 = d & 0xFF;
|
||||
switch(a1)
|
||||
{
|
||||
case 192:
|
||||
r1 = (r1 + r1 + r1 + r2) * 0.25;
|
||||
g1 = (g1 + g1 + g1 + g2) * 0.25;
|
||||
b1 = (b1 + b1 + b1 + b2) * 0.25;
|
||||
break;
|
||||
case 128:
|
||||
r1 = (r1 + r2) * 0.5;
|
||||
g1 = (g1 + g2) * 0.5;
|
||||
b1 = (b1 + b2) * 0.5;
|
||||
break;
|
||||
case 64:
|
||||
r1 = (r1 + r2 + r2 + r2) * 0.25;
|
||||
g1 = (g1 + g2 + g2 + g2) * 0.25;
|
||||
b1 = (b1 + b2 + b2 + b2) * 0.25;
|
||||
}
|
||||
if(a2 > a1)
|
||||
{
|
||||
a1 = a2;
|
||||
}
|
||||
c = uint(a1 << 24 | r1 << 16 | g1 << 8 | b1);
|
||||
}
|
||||
bm.setPixel32(wOff - x++,y,c);
|
||||
if(x == w)
|
||||
{
|
||||
x = xOff;
|
||||
y++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(o.hasOwnProperty("s"))
|
||||
{
|
||||
shadowMultiplyer = int(o.sm);
|
||||
pack = o.s;
|
||||
max = int(pack.length);
|
||||
pack.position = 0;
|
||||
x = xOff;
|
||||
y = yOff;
|
||||
if(!bHFlip)
|
||||
{
|
||||
while(pack.position < max)
|
||||
{
|
||||
p = int(pack.readUnsignedByte());
|
||||
count = 1;
|
||||
if((p & 0x80) == 128)
|
||||
{
|
||||
p &= 127;
|
||||
count = pack.readUnsignedByte() + 1;
|
||||
}
|
||||
if(p == 0)
|
||||
{
|
||||
x += count;
|
||||
if(x >= w)
|
||||
{
|
||||
count = x - w;
|
||||
x = xOff + count % ow;
|
||||
y += count / ow + 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
c = uint(p << shadowMultiplyer);
|
||||
c += 24;
|
||||
if(c > 255)
|
||||
{
|
||||
scr = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
scr = c / 255;
|
||||
}
|
||||
while(--count > -1)
|
||||
{
|
||||
d = bm.getPixel32(x,y);
|
||||
if(d != this._backgroundColor)
|
||||
{
|
||||
a1 = d >> 24 & 0xFF;
|
||||
r1 = (d >> 16 & 0xFF) * scr;
|
||||
g1 = (d >> 8 & 0xFF) * scr;
|
||||
b1 = (d & 0xFF) * scr;
|
||||
c1 = uint(a1 << 24 | r1 << 16 | g1 << 8 | b1);
|
||||
bm.setPixel32(x,y,c1);
|
||||
}
|
||||
if(++x == w)
|
||||
{
|
||||
x = xOff;
|
||||
y++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
wOff = this._bmax.x - this._bmin.x;
|
||||
while(pack.position < max)
|
||||
{
|
||||
p = int(pack.readUnsignedByte());
|
||||
count = 1;
|
||||
if((p & 0x80) == 128)
|
||||
{
|
||||
p &= 127;
|
||||
count = pack.readUnsignedByte() + 1;
|
||||
}
|
||||
if(p == 0)
|
||||
{
|
||||
x += count;
|
||||
if(x >= w)
|
||||
{
|
||||
count = x - w;
|
||||
x = xOff + count % ow;
|
||||
y += count / ow + 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
c = uint(p << shadowMultiplyer);
|
||||
c += 24;
|
||||
if(c > 255)
|
||||
{
|
||||
scr = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
scr = c / 255;
|
||||
}
|
||||
while(--count > -1)
|
||||
{
|
||||
d = bm.getPixel32(wOff - x,y);
|
||||
if(d != this._backgroundColor)
|
||||
{
|
||||
a1 = d >> 24 & 0xFF;
|
||||
r1 = (d >> 16 & 0xFF) * scr;
|
||||
g1 = (d >> 8 & 0xFF) * scr;
|
||||
b1 = (d & 0xFF) * scr;
|
||||
c1 = uint(a1 << 24 | r1 << 16 | g1 << 8 | b1);
|
||||
bm.setPixel32(wOff - x,y,c1);
|
||||
}
|
||||
if(++x == w)
|
||||
{
|
||||
x = xOff;
|
||||
y++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
bm.unlock();
|
||||
}
|
||||
|
||||
private function resizeBitmap(w:int, h:int, bClearBitmap:Boolean) : void
|
||||
{
|
||||
var iw:int = w;
|
||||
var ih:int = h;
|
||||
if(!this.bitmapData)
|
||||
{
|
||||
bClearBitmap = true;
|
||||
}
|
||||
var rect:Rectangle = new Rectangle(0,0,iw,ih);
|
||||
var ba:ByteArray = null;
|
||||
if(!bClearBitmap)
|
||||
{
|
||||
ba = this.bitmapData.getPixels(rect);
|
||||
}
|
||||
if(Boolean(this.bitmapData))
|
||||
{
|
||||
this.bitmapData.dispose();
|
||||
}
|
||||
this.bitmapData = new BitmapData(w,h,true,this._backgroundColor);
|
||||
if(!bClearBitmap)
|
||||
{
|
||||
this.bitmapData.setPixels(rect,ba);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,238 @@
|
||||
package com.sbi.graphics
|
||||
{
|
||||
import flash.utils.ByteArray;
|
||||
|
||||
public class PaletteHelper
|
||||
{
|
||||
|
||||
public static var gamePalette:Array;
|
||||
|
||||
public static var avatarPalette1:Array;
|
||||
|
||||
public static var avatarPalette2:Array;
|
||||
|
||||
private static const PALETTE_ALPHA_OFFSET:int = 25;
|
||||
|
||||
public function PaletteHelper()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public static function setGamePalette(baPal:ByteArray, baAvatarPal1:ByteArray = null, baAvatarPal2:ByteArray = null) : void
|
||||
{
|
||||
var count:int = baPal.length / 4;
|
||||
gamePalette = new Array(count);
|
||||
var i:int = 0;
|
||||
while(i < count)
|
||||
{
|
||||
gamePalette[i++] = baPal.readUnsignedInt();
|
||||
}
|
||||
if(Boolean(baAvatarPal1))
|
||||
{
|
||||
count = int(baAvatarPal1.length);
|
||||
avatarPalette1 = new Array(count);
|
||||
i = 0;
|
||||
while(i < count)
|
||||
{
|
||||
avatarPalette1[i++] = baAvatarPal1.readUnsignedByte();
|
||||
}
|
||||
}
|
||||
if(Boolean(baAvatarPal2))
|
||||
{
|
||||
baAvatarPal2.position = 0;
|
||||
count = int(baAvatarPal2.length);
|
||||
avatarPalette2 = new Array(count);
|
||||
i = 0;
|
||||
while(i < count)
|
||||
{
|
||||
avatarPalette2[i++] = baAvatarPal2.readUnsignedByte();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function mixColors(color1:uint, color2:uint) : uint
|
||||
{
|
||||
var r1:uint = uint(color1 >> 16 & 0xFF);
|
||||
var g1:uint = uint(color1 >> 8 & 0xFF);
|
||||
var b1:uint = uint(color1 & 0xFF);
|
||||
var r2:uint = uint(color2 >> 16 & 0xFF);
|
||||
var g2:uint = uint(color2 >> 8 & 0xFF);
|
||||
var b2:uint = uint(color2 & 0xFF);
|
||||
r1 = (r1 + r2) / 2;
|
||||
g1 = (g1 + g2) / 2;
|
||||
b1 = (b1 + b2) / 2;
|
||||
return r1 << 16 | g1 << 8 | b1;
|
||||
}
|
||||
|
||||
public static function setColorGradient(palette:Array, color:uint, offset:uint, count:uint) : void
|
||||
{
|
||||
var c:uint = 0;
|
||||
var r:uint = uint(color >> 16 & 0xFF);
|
||||
var g:uint = uint(color >> 8 & 0xFF);
|
||||
var b:uint = uint(color & 0xFF);
|
||||
var r1:uint = r / (count + 2);
|
||||
var g1:uint = g / (count + 2);
|
||||
var b1:uint = b / (count + 2);
|
||||
var index:int = offset + count - 1;
|
||||
for(var i:uint = 0; i < count; i++)
|
||||
{
|
||||
c = uint(r << 16 | g << 8 | b);
|
||||
palette[index] = c | 255 << 24;
|
||||
palette[index + PALETTE_ALPHA_OFFSET] = c | 192 << 24;
|
||||
palette[index + PALETTE_ALPHA_OFFSET * 2] = c | 128 << 24;
|
||||
palette[index + PALETTE_ALPHA_OFFSET * 3] = c | 64 << 24;
|
||||
r -= r1;
|
||||
g -= g1;
|
||||
b -= b1;
|
||||
index--;
|
||||
}
|
||||
}
|
||||
|
||||
public static function AddRGB(color:uint, r:int, g:int, b:int) : uint
|
||||
{
|
||||
var ac:int = color >> 24 & 0xFF;
|
||||
var rc:int = color >> 16 & 0xFF;
|
||||
var gc:int = color >> 8 & 0xFF;
|
||||
var bc:int = color & 0xFF;
|
||||
rc += r;
|
||||
if(rc < 0)
|
||||
{
|
||||
rc = 0;
|
||||
}
|
||||
else if(rc > 255)
|
||||
{
|
||||
rc = 255;
|
||||
}
|
||||
gc += g;
|
||||
if(gc < 0)
|
||||
{
|
||||
gc = 0;
|
||||
}
|
||||
else if(gc > 255)
|
||||
{
|
||||
gc = 255;
|
||||
}
|
||||
bc += b;
|
||||
if(bc < 0)
|
||||
{
|
||||
bc = 0;
|
||||
}
|
||||
else if(bc > 255)
|
||||
{
|
||||
bc = 255;
|
||||
}
|
||||
return uint(ac << 24 | rc << 16 | gc << 8 | bc);
|
||||
}
|
||||
|
||||
public static function getAnimalPalette() : Array
|
||||
{
|
||||
var j:int = 0;
|
||||
var c:uint = 0;
|
||||
var nc:uint = 0;
|
||||
var ret:Array = new Array(uint(4278255615),uint(4286513152),uint(4290707456),uint(4294901760),uint(4278222592),uint(4278238976),uint(4278255360),uint(4278190207),uint(4278190271),uint(4278190335),uint(4286545791),uint(4290756543),uint(4294967295),uint(4282400768),uint(4286611456),uint(4278206528),uint(4278222976),uint(4282384448),uint(4286578816),uint(4286595136),uint(4294934656),uint(4282417216),uint(4286644096),uint(4282400896),uint(4286611711),uint(4278190080));
|
||||
var a:uint = uint(192 << 24);
|
||||
for(var i:int = 0; i < 3; i++)
|
||||
{
|
||||
for(j = 1; j <= PALETTE_ALPHA_OFFSET; j++)
|
||||
{
|
||||
c = uint(ret[j] & 0xFFFFFF);
|
||||
nc = uint(c | a);
|
||||
ret.push(nc);
|
||||
}
|
||||
a -= 64 << 24;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static function BuildAnimalPalette(_palette:Array, color1:uint, color2:uint, color3:uint, color4:uint) : void
|
||||
{
|
||||
setColorGradient(_palette,color1,1,3);
|
||||
setColorGradient(_palette,color2,4,3);
|
||||
setColorGradient(_palette,color3,7,3);
|
||||
setColorGradient(_palette,color4,10,3);
|
||||
setColorGradient(_palette,PaletteHelper.mixColors(color1,color2),13,2);
|
||||
setColorGradient(_palette,PaletteHelper.mixColors(color2,color3),15,2);
|
||||
setColorGradient(_palette,PaletteHelper.mixColors(color1,color3),17,2);
|
||||
setColorGradient(_palette,PaletteHelper.mixColors(color1,color4),19,2);
|
||||
setColorGradient(_palette,PaletteHelper.mixColors(color2,color4),21,2);
|
||||
setColorGradient(_palette,PaletteHelper.mixColors(color3,color4),23,2);
|
||||
}
|
||||
|
||||
public static function adjustPaletteColor(_palette:Array, argb:uint) : void
|
||||
{
|
||||
var i:int = 0;
|
||||
var color:uint = 0;
|
||||
var ac:int = 0;
|
||||
var rc:int = 0;
|
||||
var gc:int = 0;
|
||||
var bc:int = 0;
|
||||
var a:Number = NaN;
|
||||
var percent:Number = (argb >> 24 & 0xFF) / 255;
|
||||
var ra:int = argb >> 16 & 0xFF;
|
||||
var ga:int = argb >> 8 & 0xFF;
|
||||
for(var ba:int = argb & 0xFF; i < _palette.length; )
|
||||
{
|
||||
color = uint(_palette[i]);
|
||||
ac = color >> 24 & 0xFF;
|
||||
rc = color >> 16 & 0xFF;
|
||||
gc = color >> 8 & 0xFF;
|
||||
bc = color & 0xFF;
|
||||
a = ra - rc;
|
||||
a *= percent;
|
||||
rc += a;
|
||||
if(rc < 0)
|
||||
{
|
||||
rc = 0;
|
||||
}
|
||||
else if(rc > 255)
|
||||
{
|
||||
rc = 255;
|
||||
}
|
||||
a = ga - gc;
|
||||
a *= percent;
|
||||
gc += a;
|
||||
if(gc < 0)
|
||||
{
|
||||
gc = 0;
|
||||
}
|
||||
else if(gc > 255)
|
||||
{
|
||||
gc = 255;
|
||||
}
|
||||
a = ba - bc;
|
||||
a *= percent;
|
||||
bc += a;
|
||||
if(bc < 0)
|
||||
{
|
||||
bc = 0;
|
||||
}
|
||||
else if(bc > 255)
|
||||
{
|
||||
bc = 255;
|
||||
}
|
||||
_palette[i] = uint(ac << 24 | rc << 16 | gc << 8 | bc);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
public static function getRGBColors(packedColors:uint) : Array
|
||||
{
|
||||
var colors:Array = [];
|
||||
colors[0] = decToRGBObj(gamePalette[packedColors >> 24 & 0xFF]);
|
||||
colors[1] = decToRGBObj(gamePalette[packedColors >> 16 & 0xFF]);
|
||||
colors[2] = decToRGBObj(gamePalette[packedColors >> 8 & 0xFF]);
|
||||
colors[3] = decToRGBObj(gamePalette[packedColors & 0xFF]);
|
||||
return colors;
|
||||
}
|
||||
|
||||
private static function decToRGBObj(hex:uint) : Object
|
||||
{
|
||||
return {
|
||||
"r":hex >> 16 & 0xFF,
|
||||
"g":hex >> 8 & 0xFF,
|
||||
"b":hex & 0xFF
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,188 @@
|
||||
package com.sbi.graphics
|
||||
{
|
||||
import flash.display.DisplayObject;
|
||||
import flash.display.Sprite;
|
||||
import flash.geom.Rectangle;
|
||||
import flash.utils.Dictionary;
|
||||
|
||||
public class SortLayer extends Sprite
|
||||
{
|
||||
|
||||
private var _bottoms:Dictionary;
|
||||
|
||||
private var _nudge:Number = 0;
|
||||
|
||||
public function SortLayer()
|
||||
{
|
||||
super();
|
||||
this._bottoms = new Dictionary(true);
|
||||
}
|
||||
|
||||
override public function addChild(child:DisplayObject) : DisplayObject
|
||||
{
|
||||
var offset:int = int(child.name);
|
||||
child.name = (this.getNudge() + offset).toString();
|
||||
return super.addChild(child);
|
||||
}
|
||||
|
||||
override public function addChildAt(child:DisplayObject, index:int) : DisplayObject
|
||||
{
|
||||
var offset:int = int(child.name);
|
||||
child.name = (this.getNudge() + offset).toString();
|
||||
return super.addChildAt(child,index);
|
||||
}
|
||||
|
||||
public function release() : void
|
||||
{
|
||||
this._nudge = 0;
|
||||
this._bottoms = new Dictionary(true);
|
||||
}
|
||||
|
||||
public function depthSort(a:DisplayObject, b:DisplayObject) : int
|
||||
{
|
||||
return this._bottoms[a] - this._bottoms[b];
|
||||
}
|
||||
|
||||
public function heartbeat() : void
|
||||
{
|
||||
var i:int = 0;
|
||||
var r:Rectangle = null;
|
||||
var bSort:Boolean = false;
|
||||
var d:DisplayObject = null;
|
||||
var y:Number = NaN;
|
||||
var offset:Number = NaN;
|
||||
var t:int = this.numChildren;
|
||||
var prev:Number = -999999999;
|
||||
if(t < 2)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var sortArray:Array = [t];
|
||||
for(i = 0; i < t; i++)
|
||||
{
|
||||
d = this.getChildAt(i);
|
||||
sortArray[i] = d;
|
||||
r = d.getBounds(this);
|
||||
y = r.bottom;
|
||||
offset = Number(d.name);
|
||||
if(!offset)
|
||||
{
|
||||
offset = this.getNudge();
|
||||
d.name = offset.toString();
|
||||
}
|
||||
y -= offset;
|
||||
this._bottoms[d] = y;
|
||||
if(!bSort)
|
||||
{
|
||||
if(y < prev)
|
||||
{
|
||||
bSort = true;
|
||||
}
|
||||
prev = y;
|
||||
}
|
||||
}
|
||||
if(bSort)
|
||||
{
|
||||
sortArray.sort(this.depthSort);
|
||||
for(i = 0; i < t; i++)
|
||||
{
|
||||
this.setChildIndex(sortArray[i],i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function getNudge() : Number
|
||||
{
|
||||
this._nudge += 0.001;
|
||||
if(this._nudge > 0.9)
|
||||
{
|
||||
this._nudge = 0;
|
||||
}
|
||||
return this._nudge;
|
||||
}
|
||||
|
||||
private function sort(a:Vector.<Number>) : void
|
||||
{
|
||||
this.quickSort(a,0,a.length - 1);
|
||||
this.InsertionSort(a,0,a.length - 1);
|
||||
}
|
||||
|
||||
private function quickSort(a:Vector.<Number>, l:int, r:int) : void
|
||||
{
|
||||
var i:int = 0;
|
||||
var j:int = 0;
|
||||
var v:Number = NaN;
|
||||
var M:int = 4;
|
||||
if(r - l > M)
|
||||
{
|
||||
i = (r + l) / 2;
|
||||
if(a[l] > a[i])
|
||||
{
|
||||
this.swap(a,l,i);
|
||||
}
|
||||
if(a[l] > a[r])
|
||||
{
|
||||
this.swap(a,l,r);
|
||||
}
|
||||
if(a[i] > a[r])
|
||||
{
|
||||
this.swap(a,i,r);
|
||||
}
|
||||
j = r - 1;
|
||||
this.swap(a,i,j);
|
||||
i = l;
|
||||
v = a[j];
|
||||
while(true)
|
||||
{
|
||||
while(a[++i] < v)
|
||||
{
|
||||
}
|
||||
while(a[--j] > v)
|
||||
{
|
||||
}
|
||||
if(j < i)
|
||||
{
|
||||
break;
|
||||
}
|
||||
this.swap(a,i,j);
|
||||
}
|
||||
this.swap(a,i,r - 1);
|
||||
this.quickSort(a,l,j);
|
||||
this.quickSort(a,i + 1,r);
|
||||
}
|
||||
}
|
||||
|
||||
private function swap(a:Vector.<Number>, i:int, j:int) : void
|
||||
{
|
||||
var T:Number = NaN;
|
||||
T = a[i];
|
||||
a[i] = a[j];
|
||||
a[j] = T;
|
||||
swapChildrenAt(i,j);
|
||||
}
|
||||
|
||||
private function move(a:Vector.<Number>, i:int, j:int) : void
|
||||
{
|
||||
a[j] = a[i];
|
||||
this.addChildAt(getChildAt(i),j);
|
||||
}
|
||||
|
||||
private function InsertionSort(a:Vector.<Number>, lo0:int, hi0:int) : void
|
||||
{
|
||||
var i:int = 0;
|
||||
var j:int = 0;
|
||||
var v:Number = NaN;
|
||||
for(i = lo0 + 1; i <= hi0; i++)
|
||||
{
|
||||
v = a[i];
|
||||
j = i;
|
||||
while(j > lo0 && a[j - 1] > v)
|
||||
{
|
||||
this.move(a,j - 1,j);
|
||||
j--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
package com.sbi.graphics
|
||||
{
|
||||
import flash.display.Bitmap;
|
||||
import flash.display.BitmapData;
|
||||
import flash.display.Sprite;
|
||||
import flash.events.Event;
|
||||
import flash.events.MouseEvent;
|
||||
import flash.geom.Rectangle;
|
||||
import flash.system.System;
|
||||
import flash.text.StyleSheet;
|
||||
import flash.text.TextField;
|
||||
import flash.utils.getTimer;
|
||||
|
||||
public class Stats extends Sprite
|
||||
{
|
||||
|
||||
protected const WIDTH:uint = 70;
|
||||
|
||||
protected const HEIGHT:uint = 100;
|
||||
|
||||
protected var xml:XML;
|
||||
|
||||
protected var text:TextField;
|
||||
|
||||
protected var style:StyleSheet;
|
||||
|
||||
protected var timer:uint;
|
||||
|
||||
protected var fps:uint;
|
||||
|
||||
protected var ms:uint;
|
||||
|
||||
protected var ms_prev:uint;
|
||||
|
||||
protected var mem:Number;
|
||||
|
||||
protected var mem_max:Number;
|
||||
|
||||
protected var graph:Bitmap;
|
||||
|
||||
protected var rectangle:Rectangle;
|
||||
|
||||
protected var fps_graph:uint;
|
||||
|
||||
protected var mem_graph:uint;
|
||||
|
||||
protected var mem_max_graph:uint;
|
||||
|
||||
protected var theme:Object = {
|
||||
"bg":51,
|
||||
"fps":16776960,
|
||||
"ms":65280,
|
||||
"mem":65535,
|
||||
"memmax":16711792
|
||||
};
|
||||
|
||||
public function Stats(_theme:Object = null)
|
||||
{
|
||||
super();
|
||||
if(Boolean(_theme))
|
||||
{
|
||||
if(_theme.bg != null)
|
||||
{
|
||||
this.theme.bg = _theme.bg;
|
||||
}
|
||||
if(_theme.fps != null)
|
||||
{
|
||||
this.theme.fps = _theme.fps;
|
||||
}
|
||||
if(_theme.ms != null)
|
||||
{
|
||||
this.theme.ms = _theme.ms;
|
||||
}
|
||||
if(_theme.mem != null)
|
||||
{
|
||||
this.theme.mem = _theme.mem;
|
||||
}
|
||||
if(_theme.memmax != null)
|
||||
{
|
||||
this.theme.memmax = _theme.memmax;
|
||||
}
|
||||
}
|
||||
this.mem_max = 0;
|
||||
this.xml = <xml><fps>FPS:</fps><ms>MS:</ms><mem>MEM:</mem><memMax>MAX:</memMax></xml>;
|
||||
this.style = new StyleSheet();
|
||||
this.style.setStyle("xml",{
|
||||
"fontSize":"9px",
|
||||
"fontFamily":"_sans",
|
||||
"leading":"-2px"
|
||||
});
|
||||
this.style.setStyle("fps",{"color":this.hex2css(this.theme.fps)});
|
||||
this.style.setStyle("ms",{"color":this.hex2css(this.theme.ms)});
|
||||
this.style.setStyle("mem",{"color":this.hex2css(this.theme.mem)});
|
||||
this.style.setStyle("memMax",{"color":this.hex2css(this.theme.memmax)});
|
||||
this.text = new TextField();
|
||||
this.text.width = this.WIDTH;
|
||||
this.text.height = 50;
|
||||
this.text.styleSheet = this.style;
|
||||
this.text.condenseWhite = true;
|
||||
this.text.selectable = false;
|
||||
this.text.mouseEnabled = false;
|
||||
this.graph = new Bitmap();
|
||||
this.graph.y = 50;
|
||||
this.rectangle = new Rectangle(this.WIDTH - 1,0,1,this.HEIGHT - 50);
|
||||
addEventListener(Event.ADDED_TO_STAGE,this.init,false,0,true);
|
||||
addEventListener(Event.REMOVED_FROM_STAGE,this.destroy,false,0,true);
|
||||
}
|
||||
|
||||
private function init(e:Event) : void
|
||||
{
|
||||
graphics.beginFill(this.theme.bg);
|
||||
graphics.drawRect(0,0,this.WIDTH,this.HEIGHT);
|
||||
graphics.endFill();
|
||||
addChild(this.text);
|
||||
this.graph.bitmapData = new BitmapData(this.WIDTH,this.HEIGHT - 50,false,this.theme.bg);
|
||||
addChild(this.graph);
|
||||
addEventListener(MouseEvent.CLICK,this.onClick);
|
||||
addEventListener(Event.ENTER_FRAME,this.update);
|
||||
}
|
||||
|
||||
private function destroy(e:Event) : void
|
||||
{
|
||||
graphics.clear();
|
||||
while(numChildren > 0)
|
||||
{
|
||||
removeChildAt(0);
|
||||
}
|
||||
this.graph.bitmapData.dispose();
|
||||
removeEventListener(MouseEvent.CLICK,this.onClick);
|
||||
removeEventListener(Event.ENTER_FRAME,this.update);
|
||||
}
|
||||
|
||||
private function update(e:Event) : void
|
||||
{
|
||||
this.timer = getTimer();
|
||||
if(this.timer - 1000 > this.ms_prev)
|
||||
{
|
||||
this.ms_prev = this.timer;
|
||||
this.mem = Number((System.totalMemory * 9.54e-7).toFixed(3));
|
||||
this.mem_max = this.mem_max > this.mem ? this.mem_max : this.mem;
|
||||
this.fps_graph = Math.min(this.graph.height,this.fps / stage.frameRate * this.graph.height);
|
||||
this.mem_graph = Math.min(this.graph.height,Math.sqrt(Math.sqrt(this.mem * 5000))) - 2;
|
||||
this.mem_max_graph = Math.min(this.graph.height,Math.sqrt(Math.sqrt(this.mem_max * 5000))) - 2;
|
||||
this.graph.bitmapData.scroll(-1,0);
|
||||
this.graph.bitmapData.fillRect(this.rectangle,this.theme.bg);
|
||||
this.graph.bitmapData.setPixel(this.graph.width - 1,this.graph.height - this.fps_graph,this.theme.fps);
|
||||
this.graph.bitmapData.setPixel(this.graph.width - 1,this.graph.height - (this.timer - this.ms >> 1),this.theme.ms);
|
||||
this.graph.bitmapData.setPixel(this.graph.width - 1,this.graph.height - this.mem_graph,this.theme.mem);
|
||||
this.graph.bitmapData.setPixel(this.graph.width - 1,this.graph.height - this.mem_max_graph,this.theme.memmax);
|
||||
this.xml.fps = "FPS: " + this.fps + " / " + stage.frameRate;
|
||||
this.xml.mem = "MEM: " + this.mem;
|
||||
this.xml.memMax = "MAX: " + this.mem_max;
|
||||
this.fps = 0;
|
||||
}
|
||||
++this.fps;
|
||||
this.xml.ms = "MS: " + (this.timer - this.ms);
|
||||
this.ms = this.timer;
|
||||
this.text.htmlText = this.xml;
|
||||
}
|
||||
|
||||
private function onClick(e:MouseEvent) : void
|
||||
{
|
||||
if(mouseY / height > 0.5)
|
||||
{
|
||||
--stage.frameRate;
|
||||
}
|
||||
else
|
||||
{
|
||||
++stage.frameRate;
|
||||
}
|
||||
this.xml.fps = "FPS: " + this.fps + " / " + stage.frameRate;
|
||||
this.text.htmlText = this.xml;
|
||||
}
|
||||
|
||||
private function hex2css(color:int) : String
|
||||
{
|
||||
return "#" + color.toString(16);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user