c++语法混淆-声明无符号字符数组

C++ syntax confusion - declaring unsigned char arrays

本文关键字:无符号 字符 数组 声明 语法 c++      更新时间:2023-10-16

我有一个函数,它接受两个图像输入(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};