4

so I am having a few issues with this program used to pixelate an image. One issue is that I get a "Stack around the variable 'pixArray' was corrupted" and then, when I click continue after breaking it gives the error in the title.

I'm not sure if it is acceptable to use pastebin, but I'll use it for the sake of having a "short" post.

The Code

The Image Being Used

Also, when it runs through, all of the pixelated squares are one pixel too short on the left and top of the squares. It is just using the original data when it writes to the outFile. If you could try to figure out why this is happening, you get bonus points.

Finally, the averages don't seem to be averaging correctly, as you can see by the squares around the right-most side of the image after running the program.

Any help with any of these problems would be greatly appreciated. Thanks in advance!

EDIT: I sorted through the code, commenting out sections that use pixArray, and the section that, when commented out, fixes the problem is at the bottom of the function, getAveragesForRGB

start = 0;//reset start number
for(int row = 0; row < squareSize; row++) {
    if(row != 0)
        start = ((square * MAXROWS) / (MAXCOLS / squareSize)) + 1;
    stop = (((square + 1) * MAXROWS) / (MAXCOLS / squareSize));
    for (int col = start; col < stop; col++) {
        //write each average into each piece of the array
        pixArray[row][col].red = redAvg;
        pixArray[row][col].green = greenAvg;
        pixArray[row][col].blue = blueAvg;
    }
}

EDIT 2: I got it all running smoothly now. Just in case anyone ever runs into this exact problem for whatever reason, here is the new getAveragesForRGB where all of my problems were.

void getAveragesForRGB(Pixel pixArray[][MAXCOLS], int squareSize, int square, int numSquaresPerStripe) {

    //initialize variables needed for function
    int start, stop;
    int redAvg, greenAvg, blueAvg;

    //reset averages for current square's usage
    redAvg = 0;
    greenAvg = 0;
    blueAvg = 0;

    start = 0; //reset start number
    for (int row=0; row < squareSize; row++) {
        if (row != 0)
            start = ((square * MAXROWS) / (MAXCOLS / squareSize)) - 1;  //starting point for loop over the columns
        stop = start + squareSize;//stopping point for   ^^^
        for (int col = start; col < stop - 1; col++) {
            //add each rgb value to the sum to be divided later
            redAvg += pixArray[row][col].red;
            greenAvg += pixArray[row][col].green;
            blueAvg += pixArray[row][col].blue;
        }
    }

    //divide by number of pixels in square for average
    redAvg /= (squareSize * squareSize);
    greenAvg /= (squareSize * squareSize);
    blueAvg /= (squareSize * squareSize);


    start = 0;//reset start number
    for (int row = 0; row < squareSize; row++) {
        if (row != 0)
            start = ((square * MAXROWS) / (MAXCOLS / squareSize)) - 1;  //starting point for loop over the columns
        stop = (((square + 1) * MAXROWS) / (MAXCOLS / squareSize));  //stopping point for   ^^^
        for (int col = start; col < stop - 1; col++) {
            //write each average into each piece of the array
            pixArray[row][col].red = redAvg;
            pixArray[row][col].green = greenAvg;
            pixArray[row][col].blue = blueAvg;
        }
    }
}
Chris Krycho
  • 3,125
  • 1
  • 23
  • 35
Alex Braniff
  • 41
  • 1
  • 1
  • 3
  • It seems you are overwriting a local array in some function. Double-check all loop limits. – Some programmer dude Feb 13 '13 at 10:11
  • 2
    No one on SO is going to play "spot the error" on 300+ lines of code. Simplify your code until it still produces the error, but without all the "extra" code. Particularly as there is a dozen uses of "pixArray" in the code, it's hard to say which function actually causes the problem. – Mats Petersson Feb 13 '13 at 11:10
  • That is a very good point. Sorry about that. – Alex Braniff Feb 13 '13 at 16:00
  • 3
    In case anyone else gets here, I ran into a seemingly rare error when I changed from VS2010 to VS2012 and didn't upgrade my DLLs. In SFML specifically. – escapecharacter Jun 21 '14 at 21:08

0 Answers0