0

I've created a program that processes images in c#. For some reason, I've made pretty much the most simple double for loop possible, and it refuses to work. This loop ends at x=78 and y=78, reliably every time. I've been looking at this forever, and I can not figure it out! Code:

try 
{
     for (int y = 0; y < inputHeight; y++)
     {
         for (int x = 0; x < inputWidth; x++)
         {
             if (x == 0 && y == 0)
                 continue;
             int xpix = x;
             int ypix = y;
             Color color = inputImage.GetPixel(xpix, ypix);
             colorBucket[counter] = color.R;
             if (counter < byteSize - 1)
                 colorBucket[counter + 1] = color.G;
             if (counter < byteSize - 2)
                 colorBucket[counter + 2] = color.B;
             counter += 3;        
         }
    }
}
catch (Exception e)
{
    System.Diagnostics.Debugger.Log(1, "Error", e.ToString());
}

Visual studio specifically tells me that both conditions are true, regardless of the fact that the loop has exited, and they are also true when looping. The size of colorBucket is huge, and the loop exits when count is about a third of the size of colorBucket through itself. Along with that, no exceptions are thrown, and the debug point I have set in my catch statement is never called.

EDIT: the width and height of the image are 844 pixels, also, the loop still appears to fail once the inner code is removed.

EDIT 2: So I've figured out something pretty important, the loop doesn't actually end prematurely, but somehow, counter is not equal to the number of colors in the colorbucket.

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
Dylan Katz
  • 194
  • 2
  • 14
  • You're probably getting an exception during the loop. Wrap the loop in a `try/catch` block and see what happens. – Dai Dec 15 '13 at 03:58
  • Use breakpoints and check the inputHeight and inputWidth variable when it hits your first for loop. That will tell you if the loops are really ending prematurely or set to 78x78 as @dasblinkenlight mentioned – BRogers Dec 15 '13 at 04:02
  • both conditions in the for loops are true when I put a breakpoint after the loop. inputHeight is NOT 78, neither is inputWidth, I stepped through the loop approximatley 20 times. Also, visual studios tells me that even though we've left the loops, the conditions are still true. – Dylan Katz Dec 15 '13 at 04:05
  • There's no way to see if the conditions are true **after** the loop, because loop variables are out of scope by then. – Sergey Kalinichenko Dec 15 '13 at 04:07
  • @dasblinkenlight odd, when I mouse over the condition, it says "true" Also, I can view the values of x and y after the loop is over. – Dylan Katz Dec 15 '13 at 04:08
  • When you step through it... what is the value of `inputHeight` and `inputWidth`? hover over them when it hits the for loop... – BRogers Dec 15 '13 at 04:15
  • @Brogers see my edit. Dimensions are 844x844 – Dylan Katz Dec 15 '13 at 04:20
  • Is `colorBucket`'s size at least `2137008` then? – Sergey Kalinichenko Dec 15 '13 at 04:29
  • @dasblinkenlight colorBucket has a size of 6414336 – Dylan Katz Dec 15 '13 at 04:29
  • @dasblinkenlight oops, forgot the brace, but yes i did remove some code, though I ran the code in the post with the same results. No exceptions are thrown. – Dylan Katz Dec 15 '13 at 04:42

2 Answers2

3

It appears that your inputHeight and inputWidth are defined incorrectly. Try changing the loop headers to use the properties of your inputImage, like this:

for (int y = 0; y < inputImage.Height; y++)
{
    for (int x = 0; x < inputImage.Width; x++)
    {
        ...
    }
}

Another problem is the size of your colorBucket array: your loops cannot run more than one third its size, combined. For example, if the size of the colorBucket array is 18252, your loop will run exactly 78x78 times before throwing an exception.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

Well, it turns out that It had to do with the fact that I was multiplying the size of colorBucket in to thirds outside of this code. I understand if you find in necessary to close this question, as it's quite specific to this situation.

Dylan Katz
  • 194
  • 2
  • 14