Reputation: 422
The guy prior to me on my project wrote some code I try to understand. I'm not really good in C# but try to get my hold of it.
The following code is intended to apply a threshold to the color values but right now i'm trying to simply copy the image to see how all the image manipulation works (those parts were just removed by myself).
Unfortunately 1/4th of my image is simply cut off. I tried to read about strides, scan0 but couldn't find any reason for this behavior. I expected this to be caused by the alpha channel so I changed the following code
public unsafe Bitmap AdjustThreshold(int value)
{
int maxX = _internalBitmapMemory.Width;
int maxY = _internalBitmapMemory.Height;
BitmapData bmpDataImage_1, bmpDataImage_2;
Bitmap newBitmap = new Bitmap(_internalBitmapMemory.Width, _internalBitmapMemory.Height, _internalBitmapMemory.PixelFormat);
bmpDataImage_1 = _internalBitmapMemory.LockBits(new Rectangle(0, 0, maxX, maxY), System.Drawing.Imaging.ImageLockMode.ReadOnly, _internalBitmapMemory.PixelFormat);
bmpDataImage_2 = newBitmap.LockBits(new Rectangle(0, 0, maxX, maxY), System.Drawing.Imaging.ImageLockMode.WriteOnly, newBitmap.PixelFormat);
byte* currentPtrImage_1 = (byte*)bmpDataImage_1.Scan0;
byte* currentPtrImage_2 = (byte*)bmpDataImage_2.Scan0;
int offsetImage_1 = bmpDataImage_1.Stride - maxX * 3;
int offsetImage_2 = bmpDataImage_2.Stride - maxX * 3;
// Color cNow;
for (int currentRow = 0; currentRow < newBitmap.Height; currentRow++)
{
for (int currentColumn = 0; currentColumn < newBitmap.Width; currentColumn++, currentPtrImage_1 += 3, currentPtrImage_2 += 3)
{
// cNow = Color.FromArgb(*(currentPtrImage_1), *(currentPtrImage_1 + 1), *(currentPtrImage_1 + 2));
*(currentPtrImage_2) = *(currentPtrImage_1);
*(currentPtrImage_2 + 1) = *(currentPtrImage_1 + 1);
*(currentPtrImage_2 + 2) = *(currentPtrImage_1 + 2);
}
currentPtrImage_1 += offsetImage_1;
currentPtrImage_2 += offsetImage_2;
}
_internalBitmapMemory.UnlockBits(bmpDataImage_1);
newBitmap.UnlockBits(bmpDataImage_2);
return newBitmap;
}
to
public unsafe Bitmap AdjustThreshold(int value)
{
int maxX = _internalBitmapMemory.Width;
int maxY = _internalBitmapMemory.Height;
BitmapData bmpDataImage_1, bmpDataImage_2;
Bitmap newBitmap = new Bitmap(_internalBitmapMemory.Width, _internalBitmapMemory.Height, _internalBitmapMemory.PixelFormat);
bmpDataImage_1 = _internalBitmapMemory.LockBits(new Rectangle(0, 0, maxX, maxY), System.Drawing.Imaging.ImageLockMode.ReadOnly, _internalBitmapMemory.PixelFormat);
bmpDataImage_2 = newBitmap.LockBits(new Rectangle(0, 0, maxX, maxY), System.Drawing.Imaging.ImageLockMode.WriteOnly, newBitmap.PixelFormat);
byte* currentPtrImage_1 = (byte*)bmpDataImage_1.Scan0;
byte* currentPtrImage_2 = (byte*)bmpDataImage_2.Scan0;
int offsetImage_1 = bmpDataImage_1.Stride - maxX * 4;
int offsetImage_2 = bmpDataImage_2.Stride - maxX * 4;
// Color cNow;
for (int currentRow = 0; currentRow < newBitmap.Height; currentRow++)
{
for (int currentColumn = 0; currentColumn < newBitmap.Width; currentColumn++, currentPtrImage_1 += 4, currentPtrImage_2 += 4)
{
// cNow = Color.FromArgb(*(currentPtrImage_1), *(currentPtrImage_1 + 1), *(currentPtrImage_1 + 2));
*(currentPtrImage_2) = *(currentPtrImage_1);
*(currentPtrImage_2 + 1) = (byte)(*(currentPtrImage_1 + 1) / 2);
*(currentPtrImage_2 + 2) = *(currentPtrImage_1 + 2);
*(currentPtrImage_2 + 3) = *(currentPtrImage_1 + 3);
}
currentPtrImage_1 += offsetImage_1;
currentPtrImage_2 += offsetImage_2;
}
_internalBitmapMemory.UnlockBits(bmpDataImage_1);
newBitmap.UnlockBits(bmpDataImage_2);
return newBitmap;
}
and found it works now.
What really confuses me: Either the code worked before and there is anywhere any reason why it not worked for me, or the code never worked as expected but no one ever found out?!
I fed the function with a 32bbpARGB image. What will happen, if I have a different color dept? For my understanding it will cause an access violation due to the pointer that will exceed the maximum of the bitmap buffer. If I simply create a new bitmap by
Bitmap myBitmap = new Bitmap(Width, Height);
what bbp will be aplied? System-Standard?
And If I will observe the possibility of different color depths, how should I handle this? Shall I simply change the color depth of any bitmap given to 32bbpARGB? Or what is the way that's established here?
Thanks for you input :)
Upvotes: 0
Views: 55
Reputation: 21999
I will answer one question:
Bitmap myBitmap = new Bitmap(Width, Height);
what bbp will be aplied? System-Standard?
If you check Bitmap
consctructor:
This constructor creates a Bitmap with a PixelFormat enumeration value of Format32bppArgb.
which is
Specifies that the format is 32 bits per pixel; 8 bits each are used for the alpha, red, green, and blue components.
Upvotes: 1