我首先必须承认我不是C专家,而且在进行此类转换时我总是感到困惑。 我有下一个函数,它接收前2个参数,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);
}
}
}
答案 0 :(得分: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;
//...
}
答案 1 :(得分: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
的转换操作意味着代码假设input
的每个值中有3个字节的数据,当{{1}的类型时这是正常的是input
。但是,如果将输入类型更改为unsigned int *
,则必须确定是否只有蓝色数据(没有红色或绿色组件),或者是否有三个连续字符提供蓝色,红色和绿色组件(或任何其他排序;它通常是RGB,不是吗?),或者是否有其他排列。
如果您有3个连续的组件,则可以不使用屏蔽(unsigned char *
),但您需要更频繁地访问该阵列。