Miss Rainbowdash
Miss Rainbowdash

Reputation: 205

C++ syntax confusion - declaring unsigned char arrays

I have a function that takes two Image inputs (image and image2), mixing the color values pixel-by-pixel. Factor represents the percentage of the color value from image, so it follows that the remaining value (1-factor) is from image2. I have the pixel buffer represented by unsigned characters, and I'm having difficulty figuring out what syntax I need in order to access and set my values. I've tried a number of things, but right now it gives me the following error:

filters.C:91:41: error: scalar object ‘p’ requires one element in initializer
 unsigned char *p = {red, green, blue};

The function:

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);
            }
        }
    }
}

And this is how I have my image class set up:

#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];
}

What am I overlooking?

Upvotes: 0

Views: 1102

Answers (1)

nneonneo
nneonneo

Reputation: 179592

unsigned char * is not an array, it's a pointer. You want to declare it

unsigned char p[] = {red, green, blue};

Upvotes: 10

Related Questions