首先,我必须承认,我不是一个C专家,当我不得不做这样的转换时,我总是搞混了。我有下一个函数,它接受前两个参数,两个指向无符号ints数组的指针()。如何将算法更改为接受无符号字符数组的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);
}
}
}发布于 2013-05-10 16:40:21
原来的守则有:
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个字节的数据,当input的类型为unsigned int *时,这是很好的。但是,如果将输入类型更改为unsigned char *,则必须决定是否只有蓝色数据(没有红色或绿色组件),或者是否有三个连续字符提供蓝色、红色和绿色组件(或任何其他排序;通常是RGB,不是吗?),还是有其他安排。
如果您有三个连续组件,您可以不使用掩蔽(& 0xFF),但是您需要更频繁地访问数组。
发布于 2013-05-10 15:44:43
由于您的代码需要访问具有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;
//...
}https://stackoverflow.com/questions/16485647
复制相似问题