c - 在 C 中优化双线性调整大小算法

标签 c performance optimization resize image-resizing

任何人都可以在下一个双线性调整算法中发现任何提高速度的方法吗? 我需要提高速度,因为这是保持良好图像质量的关键。预计用于具有低速 CPU 的移动设备。 该算法主要用于放大调整大小。任何其他更快的双线性算法也将不胜感激。谢谢

void resize(int* input, int* output, int sourceWidth, int sourceHeight, int targetWidth, int targetHeight) 
{    
    int a, b, c, d, x, y, index;
    float x_ratio = ((float)(sourceWidth - 1)) / targetWidth;
    float y_ratio = ((float)(sourceHeight - 1)) / targetHeight;
    float x_diff, y_diff, blue, red, green ;
    int offset = 0 ;

    for (int i = 0; i < targetHeight; i++) 
    {
        for (int j = 0; j < targetWidth; j++) 
        {
            x = (int)(x_ratio * j) ;
            y = (int)(y_ratio * i) ;
            x_diff = (x_ratio * j) - x ;
            y_diff = (y_ratio * i) - y ;
            index = (y * sourceWidth + x) ;                
            a = input[index] ;
            b = input[index + 1] ;
            c = input[index + sourceWidth] ;
            d = input[index + sourceWidth + 1] ;

            // blue element
            blue = (a&0xff)*(1-x_diff)*(1-y_diff) + (b&0xff)*(x_diff)*(1-y_diff) +
                   (c&0xff)*(y_diff)*(1-x_diff)   + (d&0xff)*(x_diff*y_diff);

            // green element
            green = ((a>>8)&0xff)*(1-x_diff)*(1-y_diff) + ((b>>8)&0xff)*(x_diff)*(1-y_diff) +
                    ((c>>8)&0xff)*(y_diff)*(1-x_diff)   + ((d>>8)&0xff)*(x_diff*y_diff);

            // red element
            red = ((a>>16)&0xff)*(1-x_diff)*(1-y_diff) + ((b>>16)&0xff)*(x_diff)*(1-y_diff) +
                  ((c>>16)&0xff)*(y_diff)*(1-x_diff)   + ((d>>16)&0xff)*(x_diff*y_diff);

            output [offset++] = 
                    0x000000ff | // alpha
                    ((((int)red)   << 24)&0xff0000) |
                    ((((int)green) << 16)&0xff00) |
                    ((((int)blue)  << 8)&0xff00);
        }
    }
}

最佳答案

在我的脑海中:

  1. 停止使用浮点,除非您确定您的 objective-c PU 在硬件中具有良好的性能。
  2. 确保内存访问是缓存优化的,即聚集在一起。
  3. 尽可能使用最快的数据类型。有时这意味着最小,有时意味着“最原生,需要最少的开销”。
  4. 调查有符号/无符号整数运算是否会在您的平台上产生性能成本。
  5. 调查查找表而不是计算是否能为您带来任何好处(但这些可能会破坏缓存,所以要小心)。

当然,还要进行大量分析和测量。

关于c - 在 C 中优化双线性调整大小算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11363023/

相关文章:

C# LockBits 性能(int[,] 到 byte[])

jquery setInterval或滚动

Mysql多列索引使用错误的索引

c - C 是否在第一个元素之前填充结构?

c - 错误 : permission denied for language c

performance - 如何使用 matlab 更快地更新图像中的数据?

c# - 对不同 DC 中的 Azure 表存储的请求变得越来越慢

python - 使用 numpy 数组优化 python 函数

c - strncpy 行为与 sprintf 不同

c - 如何写一个类似sftp的密码提示?