【问题标题】:Supersampling implementation for high quality downscaling用于高质量缩减的超级采样实现
【发布时间】:2013-09-11 14:26:05
【问题描述】:

我想创建具有与使用 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 缩放,右侧由我的算法缩放。看起来不太好...如何改进我的代码?

【问题讨论】:

    标签: android image-processing bitmap antialiasing post-processing


    【解决方案1】:

    要计算平均颜色,您必须分别计算每个 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;
    }
    

    【讨论】:

    • 我们正在使用您的实现,但在某些维度上存在问题。这是你的最终版本吗?在其他情况下,它会产生很好的效果。
    猜你喜欢
    • 1970-01-01
    • 2014-02-18
    • 1970-01-01
    • 2011-04-19
    • 2018-05-18
    • 1970-01-01
    • 1970-01-01
    • 2012-05-29
    • 2011-08-28
    相关资源
    最近更新 更多