双线性插值缩放(ActionScript版)

自己用ActionScript写了个双线性插值缩放的代码,供大家参考

 

package action { import flash.display.Bitmap; import flash.display.BitmapData; /** * 双线性插值算法用于图片缩放 * */ public class DoubleLinearScale { public function DoubleLinearScale() { } /* 输入一个颜色,将它拆成三个部分: * 红色,绿色和蓝色 */ public static function retrieveRGBComponent( color:uint ):Array { var r:Number = (color >> 16) & 0xff; var g:Number = (color >> 8) & 0xff; var b:Number = color & 0xff; return [r, g, b]; } /* * 红色,绿色和蓝色三色组合 */ public static function generateFromRGBComponent( rgb:Array ):uint { if( rgb == null || rgb.length != 3 || rgb[0] < 0 || rgb[0] > 255 || rgb[1] < 0 || rgb[1] > 255 || rgb[2] < 0 || rgb[2] > 255 ) return 0xFFFFFF; return rgb[0] << 16 | rgb[1] << 8 | rgb[2]; } /* * 双线性插值缩放 */ public static function scale(srcBMP:Bitmap,destW:Number,destH:Number):Bitmap { var destBMP:Bitmap = null; var destData:BitmapData = new BitmapData(destW,destH); var srcData:BitmapData = srcBMP.bitmapData; var srcW:Number = srcBMP.width; var srcH:Number = srcBMP.height; var scaleX:Number = srcW/destW; var scaleY:Number = srcH/destH for(var j:int=0;j<=destH;++j){ //j为目标图像的纵坐标 for(var i:int=0;i<=destW;++i){ //i为目标图像的横坐标 //坐标进行反向变化 var srcX:Number = i*scaleX; var srcY:Number = j*scaleY; var m:int = int(srcX); //m为源图像横坐标的整数部分 var n:int = int(srcY); //n为源图像纵坐标的整数部分 var u:Number = srcX-m; //p为源图像横坐标小数部分 var v:Number = srcY-n; //r为源图像纵坐标的小数部分 //公式 f(m+p,n+r) = (1-p)(1-r)*f(m,n) + (1-p)r*f(m,n+1) + p(1-r)f(m+1,n) + p*r*f(m+1,n+1) //将颜色分解成 r,g,b var aArray:Array = retrieveRGBComponent(srcData.getPixel(m,n)); var bArray:Array = retrieveRGBComponent(srcData.getPixel(m,n+1)); var cArray:Array = retrieveRGBComponent(srcData.getPixel(m+1,n)); var dArray:Array = retrieveRGBComponent(srcData.getPixel(m+1,n+1)); var r:Number = (1-u)*(1-v)*aArray[0]+(1-u)*v*bArray[0]+u*(1-v)*cArray[0]+ u*v*dArray[0]; var g:Number = (1-u)*(1-v)*aArray[1]+(1-u)*v*bArray[1]+u*(1-v)*cArray[1]+ u*v*dArray[1]; var b:Number = (1-u)*(1-v)*aArray[2]+(1-u)*v*bArray[2]+u*(1-v)*cArray[2]+ u*v*dArray[2]; var colorArray:Array = new Array(r,g,b); // r,g,b合成颜色 var color:uint = generateFromRGBComponent(colorArray); //设置目标图像某点的颜色 destData.setPixel(i,j,color); } } destBMP = new Bitmap(destData); return destBMP; } } } 

你可能感兴趣的:(function,null,Class,action,import,actionscript)