3

我有 3 个 System.Drawing.Bitmap 对象。RGB 前景、RGB 背景和每像素一个字节的掩码图像,其中 0 表示采用背景像素,1 表示采用前景像素。这三个都是相同的尺寸。

我选择使用 Bitmap 对象是因为我最终需要在 MonoTouch 和 MonoDroid 中运行此代码。如果需要,我可以重新考虑。

我的代码已经过优化,但似乎仍然很慢。这也是整个操作中最慢的部分,所以我想对其进行更多优化。我也忍不住想有一些秘密方法可以为我做这一切。

如果有帮助。在此之前,所有 3 个都是字节 [],我将其转换为图像并重新调整为统一尺寸。如果我能后退一步做得更好,请告诉我。

下面的代码是我目前正在使用的代码,它本质上是用适当的前景像素更新背景图像。

        BitmapData backgroundData = background.LockBits(new System.Drawing.Rectangle(0, 0, background.Width, background.Height), ImageLockMode.ReadOnly, background.PixelFormat);
        int backgroundPixelSize = GetPixelSize(backgroundData);

        BitmapData foregroundData = foreground.LockBits(new System.Drawing.Rectangle(0, 0, foreground.Width, foreground.Height), ImageLockMode.ReadOnly, foreground.PixelFormat);
        int foregroundPixelSize = GetPixelSize(foregroundData);

        BitmapData maskData = mask.LockBits(new System.Drawing.Rectangle(0, 0, mask.Width, mask.Height), ImageLockMode.ReadOnly, mask.PixelFormat);
        //int maskPixelSize = GetPixelSize(maskData);

        for (int y = 0; y < background.Height; y++)
        {
            byte* backgroundRow = (byte*)backgroundData.Scan0 + (y * backgroundData.Stride);
            byte* foregroundRow = (byte*)foregroundData.Scan0 + (y * foregroundData.Stride);
            byte* maskRow = (byte*)maskData.Scan0 + (y * maskData.Stride);

            for (int x = 0; x < background.Width; x++)
            {
                // Check if the mask byte is set
                if (maskRow[x] > 0)
                {
                    // Copy the bytes over
                    for (int p = 0; p < backgroundPixelSize; p++)
                    {
                        backgroundRow[x * backgroundPixelSize + p] = foregroundRow[x * foregroundPixelSize + p];
                    }
                }
            }
        }

更新: 这两个图像是每像素 3 个字节,而掩码图像是每像素 1 个字节。

4

1 回答 1

1

你可以试试这段代码。我认为它更快更清晰(如果所有三张图像都具有相同的尺寸)。

BitmapData backgroundData = background.LockBits(
    new System.Drawing.Rectangle(0, 0, background.Width, background.Height),
    ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

BitmapData foregroundData = foreground.LockBits(
    new System.Drawing.Rectangle(0, 0, foreground.Width, foreground.Height),
    ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

BitmapData maskData = mask.LockBits(
    new System.Drawing.Rectangle(0, 0, mask.Width, mask.Height), 
    ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

uint* backgroundPtr = (uint*)backgroundData.Scan0;
uint* foregroundPtr = (uint*)foregroundData.Scan0;
uint* maskPtr = (uint*)maskData.Scan0;

int dataLength = backgroundData.Height * backgroundData.Width;

for (int i = 0; i < dataLength; i++)
    if (maskPtr[i] > 0)
        backgroundPtr[i] = foregroundPtr[i];

更新

也可以使用掩码 PixelFormat:

BitmapData maskData = mask.LockBits(
    new System.Drawing.Rectangle(0, 0, mask.Width, mask.Height), 
    ImageLockMode.ReadOnly, mask.PixelFormat);
byte* maskPtr = (byte*)maskData.Scan0;

int dataLength = backgroundData.Height * backgroundData.Width;
for (int i = 0; i < dataLength; i++)
    if (maskPtr[i] > 0)
        backgroundPtr[i] = foregroundPtr[i];
于 2012-12-26T20:52:42.400 回答