我想要创建低比例尺位图(原始位图从资产加载)的高质量比例尺比例尺与专业软件,如Paint.net (在Paint.NET中,缩放算法被选择在一个领域称为插值。最高的设置使用超采样)?
我知道如何实现抗混叠的超采样。为了达到抗混叠的目的,原始图像以更高的分辨率呈现,然后被降采样.例如,要获得100x100的目标图像,您可以将场景渲染到200x200,然后用2x2网格对其进行下采样。
但是,该算法如何处理从400x400到175x175的降采样,以达到缩放的目的。在这种情况下,网格必须是2.285x2.285。那么,如何实现超采样以达到缩放的目的呢?
thx
编辑:我目前的算法如下:
private Bitmap downscale(Bitmap src, int targetWidth, int targetHeight){
Bitmap target = Bitmap.createBitmap(targetWidth, targetHeight, Config.ARGB_8888);
float w = src.getWidth()/(float)targetWidth;
float s = src.getHeight()/(float)targetHeight;
int color = 0;
int g = 0;
for(int i=0;i<target.getWidth();++i){
for(int j=0;j<target.getHeight();++j){
color = 0;
g = 0;
for(float k =i*w;k<roundUp((i+1)*w);++k){
for(float l=j*w;l<roundUp((j+1)*s);++l){
++g;
color+=src.getPixel((int)k, (int)l);
}
}
target.setPixel(i, j, color/g);
}
}
return target;
}
图像显示与54x54相同的100x100位图。左边的是Paint.Net缩放的,右边的是我的算法。看起来不太好。如何改进我的代码?
发布于 2013-09-10 16:08:27
计算每个a-,r-,g-,b-值的平均颜色,分别计算。
我修改了我的代码,它现在运行得很好。
public static Bitmap downscaleBitmap(Bitmap src, int targetWidth, int targetHeight, int off){
float r = (src.getWidth()-off)/(float)targetWidth;
float s = (src.getHeight()-off)/(float)targetHeight;
Bitmap target = Bitmap.createBitmap(Math.round(src.getWidth()/r), Math.round(src.getHeight()/s), Config.ARGB_8888);
r = src.getWidth()/(float)target.getWidth();
s = src.getHeight()/(float)target.getHeight();
int argb;
int red;
int green;
int blue;
int alpha;
float wx;
float wy;
float n;
for(int i=0;i<target.getWidth();++i){
for(int j=0;j<target.getHeight();++j){
red = 0;
green = 0;
blue = 0;
alpha = 0;
n=0;
for(int k =(int)(i*r);k<roundUp((i+1)*r);++k){
if(k<i*r){
wx = k-i*r+1;
}else{
if(k+1>(i+1)*r)
wx = (i+1)*r-k;
else
wx = 1;
}
for(int l=(int)(j*s);l<roundUp((j+1)*s);++l){
if(l<j*s){
wy = l-j*s+1;
}else{
if(l+1>(j+1)*s)
wy = (j+1)*s-l;
else
wy = 1;
}
n+=wy*wx;
argb=src.getPixel(k, l);
red += wx*wy*Color.red(argb);
green += wx*wy*Color.green(argb);
blue += wx*wy*Color.blue(argb);
alpha += wx*wy*Color.alpha(argb);
}
}
target.setPixel(i, j, Color.argb((int)(alpha/n), (int)(red/n), (int)(green/n), (int)(blue/n)));
}
}
return target;
}
https://stackoverflow.com/questions/18675521
复制相似问题