misschuer

常用链接

统计

积分与排名

百事通

最新评论

as 图片的缩放算法 (二次线性 和 三次卷积)

package {

import flash.display.BitmapData;
public class ImageScaling {
public static var PI:Number = Math.PI;
public static function getA(val:uint):uint {
return val >>> 24;
}
public static function getR(val:uint):uint {
return (val & 0x00ffffff) >>> 16;
}
public static function getG(val:uint):uint {
return (val & 0x0000ffff) >>> 8;
}
public static function getB(val:uint):uint {
return val & 0x000000ff;
}
public static function getAA(val:Array):Array {
var ret:Array = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]];
for (var i:uint = 0; i < 4; ++ i) {
for (var j:uint = 0; j < 4; ++ j) {
ret[ i ][ j ] = val[ i ][ j ] >>> 24;
}
}
return ret;
}
public static function getRR(val:Array):Array {
var ret:Array = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]];
for (var i:uint = 0; i < 4; ++ i) {
for (var j:uint = 0; j < 4; ++ j) {
ret[ i ][ j ] = (val[ i ][ j ] & 0x00ffffff) >>> 16;
}
}
return ret;
}
public static function getGG(val:Array):Array {
var ret:Array = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]];
for (var i:uint = 0; i < 4; ++ i) {
for (var j:uint = 0; j < 4; ++ j) {
ret[ i ][ j ] = (val[ i ][ j ] & 0x0000ffff) >>> 8;
}
}
return ret;
}
public static function getBB(val:Array):Array {
var ret:Array = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]];
for (var i:uint = 0; i < 4; ++ i) {
for (var j:uint = 0; j < 4; ++ j) {
ret[ i ][ j ] = (val[ i ][ j ] & 0x000000ff);
}
}
return ret;
}
public static function availablePixel (bitmapData:BitmapData, x:uint, y:uint):uint {
var flag:Boolean = true;
if (x < 0) { x = 0; flag = false; }
else if (x >= bitmapData.width) { x = bitmapData.width - 1; flag = false; }
else if (y < 0) { y = 0; flag = false; }
else if (y >= bitmapData.height) { y = bitmapData.height - 1; flag = false; }
var ret:uint = bitmapData.getPixel32(x, y);
if (!flag) ret = ret & 0x00ffffff;
return ret;
}
public static function SinXDivX(x:Number):Number {
var a:Number = -1;
if (x < 0) x = -x;
var x2:Number = x*x;
var x3:Number = x2*x;
if (x <= 1)
return (a + 2) * x3 - (a + 3) * x2 + 1;
else if (x <= 2) 
return a * x3 - 5 * a * x2 + 8 * a * x - 4 * a;
return 0;
}
public static function getPixels(a:Array, b:Array, c:Array):uint {
var arfa:Array = ImageScaling.getAA(b);
var R:Array = ImageScaling.getRR(b);
var G:Array = ImageScaling.getGG(b);
var B:Array = ImageScaling.getBB(b);
var aa:uint = ImageScaling.MatrixMutiple(a, arfa, c) << 24;
var rr:uint = ImageScaling.MatrixMutiple(a, R, c) << 16;
var gg:uint = ImageScaling.MatrixMutiple(a, G, c) << 8;
var bb:uint = ImageScaling.MatrixMutiple(a, B, c);
//trace (aa + " " + rr + " " + gg + " " + bb);
return aa + rr + gg + bb;
}
public static function border_color(Color:Number):uint {
if (Color <= 0) return uint(0);
if (Color >= 255) return uint(255);
return uint(Color);
}
public static function MatrixMutiple(a:Array, b:Array, c:Array):uint {
var ret:Number = 0;
for (var i:int = 0; i < 4; ++ i) {
var tmp:Number = 0;
for (var j:int = 0; j < 4; ++ j) {
tmp += a[ j ] * b[ j ][ i ];
}
ret += c[ i ] * tmp;
}
return border_color(ret + 0.5);
}
public static function thirdConvolution (bitmapData:BitmapData, scalingWidth:uint, scalingHeight:uint):BitmapData {
var widthFactor:Number = bitmapData.width / scalingWidth;
var heightFactor:Number = bitmapData.height / scalingHeight;
var i:uint = 0, j:uint = 0;
var nbd:BitmapData = new BitmapData(scalingWidth, scalingHeight, true, 0xffffffff);
for (i = 0; i < scalingWidth; ++ i) {
for (j = 0; j < scalingHeight; ++ j) {
var tx:Number = (i + 0.5) * widthFactor - 0.5;
var ty:Number = (j + 0.5) * heightFactor - 0.5;
if (tx < 0) tx = -tx;
if (ty < 0) ty = -ty;
var x:uint = Math.floor(tx);
var y:uint = Math.floor(ty);
var u:Number = tx - x;
var v:Number = ty - y;
var A:Array = [0.0, 0.0, 0.0, 0.0]; 
var B:Array = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]; 
var C:Array = [0.0, 0.0, 0.0, 0.0];
for (var k:int = 0; k < 4; ++ k) {
A[ k ] = ImageScaling.SinXDivX(u+1.0-k);
C[ k ] = ImageScaling.SinXDivX(v+1.0-k);
for (var z:int = 0; z < 4; ++ z) {
B[ k ][ z ] = ImageScaling.availablePixel(bitmapData, x+k-1, y+z-1);
}
}
var p:uint = ImageScaling.getPixels(A, B, C);
nbd.setPixel32(i, j, p);
}
}
return nbd;
}
public static function calculator(p:Array, c:Array):uint {
return uint(p[0]*c[0]+p[1]*c[1]+p[2]*c[2]+p[3]*c[3]);
}
public static function mixedPixel(p:Array, c:Array):uint {
var aa:Array = [0, 0, 0, 0];
var ra:Array = [0, 0, 0, 0];
var ga:Array = [0, 0, 0, 0];
var ba:Array = [0, 0, 0, 0];
for (var i:uint = 0; i < 4; ++ i) {
aa[ i ] = ImageScaling.getA(p[ i ]);
ra[ i ] = ImageScaling.getR(p[ i ]);
ga[ i ] = ImageScaling.getG(p[ i ]);
ba[ i ] = ImageScaling.getB(p[ i ]);
}
return (ImageScaling.calculator(aa, c) << 24) + (ImageScaling.calculator(ra, c) << 16) + (ImageScaling.calculator(ga, c) << 8) + ImageScaling.calculator(ba, c);
}
public static function secondLinear (bitmapData:BitmapData, scalingWidth:uint, scalingHeight:uint):BitmapData {
var widthFactor:Number = bitmapData.width / scalingWidth;
var heightFactor:Number = bitmapData.height / scalingHeight;
var i:uint = 0, j:uint = 0;
var nbd:BitmapData = new BitmapData(scalingWidth, scalingHeight, true, 0xffffffff);
for (i = 0; i < scalingWidth; ++ i) {
for (j = 0; j < scalingHeight; ++ j) {
var tx:Number = i * widthFactor - 0.5;
var ty:Number = j * heightFactor - 0.5;
if (tx < 0) tx = -tx;
if (ty < 0) ty = -ty;
var x:uint = Math.floor(tx);
var y:uint = Math.floor(ty);
var u:Number = tx - x;
var v:Number = ty - y;
var ti:uint = x;
var tj:uint = y;

var p0:uint = ImageScaling.availablePixel(bitmapData, x, y);
var p1:uint = ImageScaling.availablePixel(bitmapData, x, y+1);
var p2:uint = ImageScaling.availablePixel(bitmapData, x+1, y);
var p3:uint = ImageScaling.availablePixel(bitmapData, x+1, y+1);
var pa:Array = [p0, p1, p2, p3];
var c0:Number = (1.0-u)*(1.0-v);
var c1:Number = (1.0-u)*v;
var c2:Number = u*(1.0-v);
var c3:Number = u*v;
var ca:Array = [c0, c1, c2, c3];
nbd.setPixel32(i, j, ImageScaling.mixedPixel(pa, ca));
}
}
return nbd;
}
}
}

测试:
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.geom.Rectangle;
import flash.system.ImageDecodingPolicy;
import flash.utils.ByteArray;

[SWF(width="1000", height="600", backgroundColor="#cacaca", frameRate="24")]
public class Main extends Sprite {
[Embed(source='dog.png')]
public static const image:Class;
public function Main() {
var bitmapdata:BitmapData = (new image() as Bitmap).bitmapData
var bitmap:Bitmap = new Bitmap(showImage2(bitmapdata));
bitmap.x = 0;
bitmap.y = 0;
this.addChild(bitmap);
var bitmap2:Bitmap = new Bitmap(showImage1(bitmapdata));
bitmap2.x = 300;
bitmap2.y = 300;
this.addChild(bitmap2);
}
public static function showImage1(bitmapdata:BitmapData):BitmapData {
return ImageScaling.secondLinear(bitmapdata, 286, 300);
}
public static function showImage2(bitmapdata:BitmapData):BitmapData {
return ImageScaling.thirdConvolution(bitmapdata, 286, 300);
}
}
}



__________________________________________________
参考h ttp://blog.csdn.net/housisong/article/details/1452249 

posted on 2012-08-07 09:58 此最相思 阅读(469) 评论(0)  编辑 收藏 引用 所属分类: as


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理