Pergunta

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.

Foi útil?

Solução 2

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.

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top