I’m trying to make a program that, using pointers, detects lines in an image and removes those lines. Currently, the detecting lines part is working really well, and for the most part the removing the lines part is working as well. However, after around 150-200 images, the program will throw random AccessViolationExceptions in places that have nothing to do with the unsafe bits of the code.
This is the bit that does the line removal:
static unsafe Bitmap RemoveLines(Bitmap input, int[] horizontalLines, int[] verticalLines)
{
Bitmap output;
if (input.PixelFormat == PixelFormat.Format24bppRgb)
{
output = (Bitmap) input.Clone();
}
else
{
output = ConvertTo24bpp((Bitmap)input.Clone());
}
BitmapData bitmapData = output.LockBits(new Rectangle(0, 0, output.Width, output.Height), ImageLockMode.ReadWrite, output.PixelFormat);
int w = output.Width;
int h = output.Height;
int bpp = 3;
int s = bitmapData.Stride;
byte* p = (byte*) bitmapData.Scan0;
for (int r = 0; r < h; r++)
{
for (int c = 0; c < h; c++)
{
if (horizontalLines.Contains(r) || verticalLines.Contains(c))
{
int i = (r * s) + c * bpp;
p[i + 0] = 255;
p[i + 1] = 255;
p[i + 2] = 255;
}
}
}
output.UnlockBits(bitmapData);
return output;
}
After this code, I save the resulting Bitmap as well as embedding it in another Bitmap for comparison purposes:
// ... Detect lines and such
Bitmap export = new Bitmap(bitmap.Width * 3, bitmap.Height, PixelFormat.Format24bppRgb);
Graphics fg = Graphics.FromImage(export);
fg.DrawImage(bitmap, 0, 0); // Draw the original input bitmap
fg.DrawImage(edited, bitmap.Width, 0); // Draw the input after processing (Line Detection)
try
{
Bitmap lineRemoved = RemoveLines(bitmap, horizontalLines.ToArray(), verticalLines.ToArray()); // Remove lines based on earlier detection
lineRemoved.Save(cellDirectory + "\\Lines\\cell_lr_" + i.ToString("D2") + j.ToString("D2") + ".gif", ImageFormat.Gif); // Save image after removal
fg.DrawImage(lineRemoved, bitmap.Width * 2, 0); // Add image to composite for comparison; This line is what throws the error most of the time
lineRemoved.Dispose();
export.Save(cellDirectory + "\\Lines\\cell" + i.ToString("D2") + j.ToString("D2") + ".gif", ImageFormat.Gif);
}
catch (Exception ex)
{ }
The DrawImage call is what throws errors, and it is always an AccessViolationException followed by an InvalidOperationException. Looking at lineRemoved during the error shows that most of its members have “threw exception of type ‘InvalidOperationException'” rather than actual values, even though one line before the same Bitmap saved just fine on its own. The input bitmap remains unchanged throughout the code, and is always Cloned or drawn to a different bitmap when I need to alter it in any way.
I’ve tried commenting out the lines after saving lineRemoved, but then the same error pops up later in the code. What’s more, that try/catch doesn’t actually catch the Exception – it always says unhandled. It’s got to be something to do with the pointers, but otherwise I am completely lost as to what is causing this.
Your code contains a subtle one-character bug. The line that reads
should be
If the image is in landscape orientation, your bug would cause the right part of the image not being processed.
If the image is in protrait orientation, it would cause the buffer to overflow, leading to an access violation exception (if you’re lucky) or memory corruption (if you’re not).
That being said, your algorithm is not very efficient. For example, you are doing the calculation
for every pixel you’re drawing, while obviously (r * s) doesn’t change in the inner loop, and c * bpp can be replaced by something like currentPixel += bpp.
In fact it would probably be more efficient to loop over horizontalLines and verticalLines.