Frage

I'm writing a function that halves an image in both dimensions and memory. The image data is declared in my image object as Pixel *data. However, g++ gives me the error below. I find it strange that the error points to [2i*2j] but not [i*j], so it seems that multiplying each index by 2 is what's making the difference somehow.

The function:

void HalveInSize(Image &input, Image &output)
{

    output = Image(input.GetX()/2, input.GetY()/2, (Pixel*) malloc(input.GetX()/2 * input.GetY()/2 * sizeof(Pixel)));

    int i, j;
    for (i = 0; i < output.GetX(); i++)
    {
        for (j=0; j < output.GetY(); j++)
        {
                output.GetData()[i*j] = input.GetData()[2i*2j];
        }   
    }
}

The compiler error:

functions.h: In function ‘void HalveInSize(Image&, Image&)’:
functions.h:65:51: error: invalid types ‘Pixel*[__complex__ int]’ for array subscript
          output.GetData()[i*j] = input.GetData()[2i*2j];
War es hilfreich?

Lösung

2i is a complex number, the positive squareroot of -4. It is nothing to do with your variable i. I think you meant 2*i*2*j (although your loop is not the correct way to iterate over a 2-D array, and you will access outside the bounds that you have malloc'd)

I think you intended

for (i = 0; i < output.GetX(); i++)
{
    for (j=0; j < output.GetY(); j++)
    {
            output.GetData()[i * output.GetY() + j] 
               = input.GetData()[2*i*input.GetY() + 2*j];
    }   
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top