ฉันมีฟังก์ชันขีดจำกัดที่ค่อนข้างง่ายซึ่งใช้โค้ดที่ไม่ปลอดภัย สิ่งนี้ใช้ได้ผลดีใน Windows อย่างไรก็ตามใน Mono บน linux ไม่มีการกำหนดขีดจำกัดเกิดขึ้น เป็นการยากที่จะแก้ไขข้อบกพร่องเนื่องจากใช้งานได้บน Linux เท่านั้น ฉันได้ตรวจสอบแล้วว่า bitsPerPixel นั้นถูกต้องพร้อมกับความสูง h
ความกว้าง w
และก้าวย่าง ws
ก็ถูกต้องเช่นกัน
ฉันจะทำอะไรได้อีกเพื่อจำกัดขอบเขตให้แคบลง หรือมี gotcha ทั่วไปอยู่ที่นี่
public void threshold(Bitmap bmp, int thresh)
{
var bitsPerPixel = Image.GetPixelFormatSize(bmp.PixelFormat) / 8;
BitmapData bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.ReadOnly, bmp.PixelFormat);
unsafe
{
byte* p = (byte*)(void*)bmData.Scan0.ToPointer();
int h = bmp.Height;
int w = bmp.Width;
int ws = bmData.Stride;
for (int i = 0; i < h; i++)
{
byte* row = &p[i * ws];
for (int j = 0; j < w * bitsPerPixel; j += bitsPerPixel)
{
for (var k = 0; k < bitsPerPixel; k++)
{
row[j + k] = (byte)((row[j + k] > (byte)thresh) ? 255 : 0);
}
}
}
}
bmp.UnlockBits(bmData);
}