如何更正此LockBits数学错误?

时间:2015-06-02 02:22:29

标签: c# image-processing rgb getpixel lockbits

使用GetPixel / SetPixel,我使用以下功能为强调红色和紫色的图像过滤器提供动力:

for (int y = 0; y < bmpMain.Height; y++)
    for (int x = 0; x < bmpMain.Width; x++)
    {
        bmpMain.GetPixel(x, y);
        Color c = bmpMain.GetPixel(x, y);
        int myRed = c.R, myGreen = c.G, myBlue = c.B;
        myGreen -= 128;
        if (myGreen < 0) myGreen = 0;
        bmpMain.SetPixel(x, y, Color.FromArgb(255, myRed, myGreen, myBlue));
    }

使用LockBits,我已将其替换为以下内容:

for (int counter = 1; counter < rgbValues.Length; counter += 3)
{
    rgbValues[counter] -= 128;
    if (rgbValues[counter] < 0) rgbValues[counter] = 0;  
}

但是,除了将绿色像素值减去128之外,128还是添加到绿色值。

如果我这样做:

for (int counter = 1; counter < rgbValues.Length; counter += 3)
{
    rgbValues[counter] += 128;
    if (rgbValues[counter] < 0) rgbValues[counter] = 0;  
}

128也被添加到绿色值。生成的图像与我减去128的图像相同。

那么,我如何才能获得简单的数学运算才能在LockBits中正常工作?

1 个答案:

答案 0 :(得分:1)

假设rgbValues是一个字节数组,语句

rgbValues[counter] -= 128;

相当于

rgbValues[counter] = (byte)(rgbValues[counter] - 128);

因此,如果rgbValues[counter]等于零,则设置为(byte)(-128)。问题是,与int不同,byte数据类型为无符号,并且不能表示负值。正如EBrown所说,减法溢出并回绕到128。

修复代码的一种方法是引入类型为int的中间变量,以便您可以安全地容纳负值:

int myGreen = rgbValues[counter];
myGreen -= 128;
if (myGreen < 0) myGreen = 0;  
rgbValues[counter] = (byte)myGreen;

另一种方法是重写代码并首先避免负值:

rgbValues[counter] = rgbValues[counter] > 128
    ? (byte)(rgbValues[counter] - 128)
    : (byte)0;