我有一个函数接受两个图像输入(image 和 image2),逐个像素地混合颜色值。 Factor 表示图像颜色值的百分比,因此剩余值(1-factor)来自 image2。我有由无符号字符表示的像素缓冲区,我很难弄清楚我需要什么语法来访问和设置我的值。我已经尝试了很多事情,但现在它给了我以下错误:
filters.C:91:41: error: scalar object ‘p’ requires one element in initializer
unsigned char *p = {red, green, blue};
函数:
void Filter::Execute()
{
if (input->GetWidth() == input2->GetWidth() && input->GetHeight() == input2->GetHeight())
{
int width = input->GetWidth();
int height = input->GetHeight();
output.ResetSize(width, height);
for (int w = 0; w < width; w++)
{
for (int h = 0; h < height; h++)
{
unsigned char red = input->GetPixel(w, h)[0]*factor+input2->GetPixel(w, h)[0]*(1-factor);
unsigned char green = input->GetPixel(w, h)[1]*factor+input2->GetPixel(w, h)[1]*(1-factor);
unsigned char blue = input->GetPixel(w, h)[2]*factor+input2->GetPixel(w, h)[2]*(1-factor);
unsigned char *p = {red, green, blue};
output.SetPixel(w, h, p);
}
}
}
}
这就是我设置图像类的方式:
#include <image.h>
#include <stdlib.h>
Image::Image()
{
width = 0;
height = 0;
buffer = NULL;
}
Image::~Image()
{
if (buffer != NULL)
{
delete[] buffer;
}
}
void Image::ResetSize(int w, int h)
{
width = w;
height = h;
if (buffer != NULL)
{
delete[] buffer;
}
else
{
buffer = new unsigned char[3*width*height];
}
}
unsigned char * Image::GetPixel(int w, int h)
{
int index = h*width + w;
return buffer+3*index;
}
void Image::SetPixel(int w, int h, unsigned char *p)
{
int index = h*width + w;
buffer[3*index+0] = p[0];
buffer[3*index+1] = p[1];
buffer[3*index+2] = p[2];
}
我忽略了什么?
最佳答案
unsigned char *
不是一个数组,它是一个指针。你要声明它
unsigned char p[] = {red, green, blue};
关于C++语法混淆——声明unsigned char数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24133413/