pSully
pSully

Reputation: 1

2d array flip vertical

I cannot get my program to flip a ppm image vertically. As of now my function only spits out the exact image that way inputted. I have this function that executes the flipping...or it suppose to. Then I have a function immediately following that calls for the image to be read, then flipped, then written out again. I know that function is okay because I've tested it. Any ideas of what to do?

void ppmFlipVertical(int image[MAXROWS][MAXCOLS], int numRows, int numCols)
{
int temp, N,L,c;
N=3*numRows;
L=3*numCols;
c=0;

    for(int r=0; r<(N/2); r++)
    {
        temp=image[r][c];
        image[r][c] = image[N-r][c]; 
        temp=image[N-r][c];
    }



}

Upvotes: 0

Views: 5264

Answers (2)

Jarod42
Jarod42

Reputation: 217205

Following may help:

//void ppmFlipVertical(int image[][], int maxRows, int MaxColumn)
void ppmFlipVertical(int (&image)[MAXROWS][MAXCOLS])
{
    for (int r = 0; r < (MAXROWS/2); r++)
    {
        for (int c = 0; c != MAXCOLUMNS; ++c)
        {
             std::swap(image[r][c], image[MAXROWS - 1 - r][c]);
        }
    }
}

Your prototype is misleading as parameter int image[MAXROWS][MAXCOLS] is equivalent to int image[][].

You may use std::swap to swap two values.

The last valid index for row is MAXROWS -1.

Upvotes: 1

Nathan Wride
Nathan Wride

Reputation: 965

Try this, it flips all the elements methodically. The only problem is i'm not sure how your image is stored so this is for flipping single cells/items. I'm sure you could easily extrapolate this item flipper for multiple items at a time.

void ppmFlipVertical(int image[MAXROWS][MAXCOLS], int numRows, int numCols)
{
    int tempTop, tempBottom, c;
    for( c = 0; c < numCols; c++ ) //Heh c++
    {
        int r, rMax;
        rMax = numRows / 2;
        for( r = 0; r < rMax; r ++ )
        {
            tempTop=image[r][c];
            tempBottom=image[numRows-r][c];

            image[r][c] = tempBottom; 
            image[numRows-r][c] = tempTop;
        }
    }
}

Upvotes: 0

Related Questions