0

我想创建具有与使用 Paint.net 等专业软件缩放的位图质量相当的高质量的缩小位图(从资产加载的原始位图)(在 Paint.NET 中,在称为插值的字段中选择了缩放算法。最高设置使用超级采样)?

我了解如何为抗锯齿实现超级采样。出于抗锯齿的目的,原始图像以更高分辨率渲染,然后进行下采样。例如,要获得 100x100 的目标图像,您可以将场景渲染为 200x200,然后使用 2x2 网格对其进行下采样。

但是,该算法如何处理从 400x400 到 175x175 的下采样以实现缩放目的。在这种情况下,网格必须为 ~ 2.285x2.285。那么如何为缩放目的实现超级采样呢?

谢谢

编辑:我当前的算法如下所示:

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;

}

该图像显示了相同的 100x100 位图,缩放为 54x54。左侧由 Paint.Net 缩放,右侧由我的算法缩放。看起来不太好...如何改进我的代码?

4

1 回答 1

0

To calculate the average color you have to calculate each a-,r-,g-,b-value seperately

I changed my code and it works pretty good now.

    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;
}
于 2013-09-10T16:08:27.877 回答