0

我首先必须承认我不是 C 专家,在必须进行此类转换时我总是会搞混。我有下一个函数,它接受前 2 个参数,2 个指向unsigned ints数组的指针。如何更改算法以接受 2 个指向unsigned chars数组的指针,当然还要对这 2 个 char 指针数组进行操作?(我的意思是我知道不仅要更改签名,还应该更改算法中的哪些内容?)

这就是我需要的:

void resize(unsigned char *input, unsigned char *output, int sourceWidth, int sourceHeight, int targetWidth, int targetHeight) 

这就是我所拥有的:

void resize(unsigned int *input, unsigned 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);
        }
    }
}
4

2 回答 2

0

原始代码有:

int a, b, c, d, x, y, index;
...

for (int i = 0; i < targetHeight; i++) 
{
    for (int j = 0; j < targetWidth; j++) 
    {
        ...            
        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);

green和的移位操作red意味着代码假定来自 的每个值中有 3 个字节的数据,当 的类型为时input这很好。但是,如果将输入类型更改为,则必须确定是否只有蓝色数据(没有红色或绿色分量),或者是否会有三个连续的字符提供蓝色、红色和绿色分量(或任何其他顺序;它是通常是RGB,不是吗?),或者是否有其他安排。inputunsigned int *unsigned char *

如果您有 3 个连续的组件,则可以不用屏蔽 ( & 0xFF),但您需要更频繁地访问该数组。

于 2013-05-10T16:40:21.250 回答
0

由于您的代码需要访问具有 4 字节数据类型的像素,因此最好在函数开头进行强制转换:

void resize(unsigned char *input_char, unsigned char *output_char, int sourceWidth, int sourceHeight, int targetWidth, int targetHeight) 
{    
    unsigned int* input = reinterpret_cast<unsigned int*>(input_char);        
    unsigned int* output = reinterpret_cast<unsigned int*>(output_char);
    int a, b, c, d, x, y, index;
    //...
}

注意:值 int sourceWidth、int sourceHeight、int targetWidth、int targetHeight 必须引用数组的整数大小,否则您的代码将无法正常工作或引发错误。

如果你不能使用 C++ 风格转换,这里的代码:

void resize(unsigned char *input_char, unsigned char *output_char, int sourceWidth, int sourceHeight, int targetWidth, int targetHeight) 
{    
    unsigned int* input = (unsigned int*)(input_char);        
    unsigned int* output = (unsigned int*)(output_char);
    int a, b, c, d, x, y, index;
    //...
}
于 2013-05-10T15:44:43.780 回答