samedi 27 juin 2015

Segmentation Fault C (1d array to 2d array and back)

I am given a 1d array that contains color values for an image's pixels. The array is of size cols*rows. I want to change the color of a specific region. The function call is given the 1d array, its size, left, top, bottom, right, and also the desired color. What I tried to do is copy the values of the 1d array into a 2d array, do the deed on the 2d array then copy back the new values to the 1d array. I keep getting a segmentation fault error. Here's the code:

void region_set( uint8_t array[], 
     unsigned int cols, 
     unsigned int rows,
     unsigned int left,
     unsigned int top,
     unsigned int right,
     unsigned int bottom,
     uint8_t color )

{

if(!(left == right || top == bottom)){


    uint8_t Array2[cols][rows];


    int x, y;

    for(int i= 0; i < rows * cols; i++){            
        x = i / rows;
        y = i % cols;
        Array2[y][x] = array[i];
    }

    for(int y=left; y < right-1; y++){
         for(int x = top; x < bottom-1 ; x++){
            Array2[y][x] = color;
        }
    }

    for(int i= 0; i < rows * cols; i++){            
        x = i / rows;
        y = i % cols;
        array[i] = Array2[y][x];
    }


}

}

Aucun commentaire:

Enregistrer un commentaire