[英]C++ syntax confusion - declaring unsigned char arrays
我有一個函數需要兩個Image輸入(image和image2),逐像素混合顏色值。 因數表示圖像中顏色值的百分比,因此可以得出剩余值(1因數)來自圖像2。 我有用無符號字符表示的像素緩沖區,並且在弄清楚訪問和設置值所需的語法時遇到了困難。 我已經嘗試了很多方法,但是現在它給了我以下錯誤:
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};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.