使用索引的8bpp图像和像素数组而不使用GetPixel函数

3

我需要获取调色板中每个像素在位图中的索引(我有一个索引为8bpp的图像)。现在我使用以下方法:

List<byte> data = new List<byte>(); // indexes
List<Color> pixels = bitmap.Palette.Entries.ToList(); // palette

for (int i = 0; i <bitmap.Height; i++)
    for (int j = 0; j < bitmap.Width; j++)
        data.Add((byte)pixels[k].IndexOf(bitmap.GetPixel(j, i)));

但是这种方法非常慢,因为我使用了几张高分辨率图片。

我的问题是:

1)有没有一种方法可以加快循环过程,以获取每个像素的RGBA值?

2)也许有一种更优化的方法可以获取调色板中图像的颜色索引?


在每个循环中向列表添加数据。在构造函数中预先分配大小:List<byte> data = new List<byte>(1000); 其中1000是位图的高度*宽度。 - jdweng
@jdweng,我不明白它如何能帮助我。 - Vlad i Slav
添加到列表的方法很慢,因为在每个循环中都必须调用类的构造函数。在进入循环之前创建列表会更快。然后使用:data[(i * bitmap.Width) + j] = (byte)pixels[k].IndexOf(bitmap.GetPixel(j, i)); - jdweng
你看到这篇帖子了吗?Jose的回答可能是你想要的。 - TaW
@jdweng 首先,必须使用字节数组而不是列表,因为每次列表都有0个元素,并且在 data[(i * bitmap.Width) + j] = (byte)pixels[k].IndexOf(bitmap.GetPixel(j, i));期间会抛出ArgumentOutOfRangeException异常。其次,这种方式写入的数组二进制数据不正确,与我接收到的数据不同。第三,我没有注意到显着的时间差异。 - Vlad i Slav
2个回答

4
也许像这样做会更快。原因是,GetbitsSetbits非常慢,每次都会内部调用lockbits锁定内存。最好一次性完成所有操作,使用LockBitsunsafefixedDictionary

为了测试结果,我使用了这张图片:

来源:

我将结果与您的原始版本进行了比较,结果相同。

基准测试

----------------------------------------------------------------------------
Mode             : Release (64Bit)
Test Framework   : .NET Framework 4.7.1 (CLR 4.0.30319.42000)
----------------------------------------------------------------------------
Operating System : Microsoft Windows 10 Pro
Version          : 10.0.17134
----------------------------------------------------------------------------
CPU Name         : Intel(R) Core(TM) i7-2600 CPU @ 3.40GHz
Description      : Intel64 Family 6 Model 42 Stepping 7
Cores (Threads)  : 4 (8)      : Architecture  : x64
Clock Speed      : 3401 MHz   : Bus Speed     : 100 MHz
L2Cache          : 1 MB       : L3Cache       : 8 MB
----------------------------------------------------------------------------

测试1

--- Random Set ------------------------------------------------------------
| Value    |   Average |   Fastest |    Cycles | Garbage | Test |    Gain |
--- Scale 1 ------------------------------------------------ Time 8.894 ---
| Mine1    |  5.211 ms |  4.913 ms |  17.713 M | 0.000 B | Pass | 93.50 % |
| Original | 80.107 ms | 75.131 ms | 272.423 M | 0.000 B | Base |  0.00 % |
---------------------------------------------------------------------------

完整代码

public unsafe byte[] Convert(string input)
{
   using (var bmp = new Bitmap(input))
   {
      var pixels = bmp.Palette.Entries.Select((color, i) => new {x = color,i})
                                      .ToDictionary(arg => arg.x.ToArgb(), x => x.i);

      // lock the image data for direct access
      var bits = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppPArgb);


      // create array as we know the size
      var data = new byte[bmp.Height * bmp.Width];

      // pin the data array
      fixed (byte* pData = data)
      {
         // just getting a pointer we can increment
         var d = pData;

         // store the max length so we don't have to recalculate it
         var length = (int*)bits.Scan0 + bmp.Height * bmp.Width;

         // Iterate through the scanlines of the image as contiguous memory by pointer 
         for (var p = (int*)bits.Scan0; p < length; p++, d++)

            //the magic, get the pixel, lookup the Dict, assign the values
            *d = (byte)pixels[*p];
      }

      // unlock the bitmap
      bmp.UnlockBits(bits);
      return data;
   }
}

摘要

我并不是一个图像专家,如果这种方法行不通,那么你的索引图像可能有一些我不理解的不同之处。

更新

要检查像素格式和调色板是否存在,您可以使用以下方法:

bmp.PixelFormat
bmp.Palette.Entries.Any()

更新2

Vlad i Slav 提供的有效解决方案如下:

我需要将 PixelFormat.Format32bppPArgb替换为 Format32bppArgb 并添加此检查。

if (pixels.ContainsKey(*p)) 
   *d = (byte)pixels[*p];
else 
   *d = 0;. 

此外,需要从调色板中获取不同的值,因为我在那里遇到了一些错误。

1
@VladiSlav 为了方便起见,仍然应该快1000倍,再次声明这未经测试,但应该可以工作。 - TheGeneral
1
Vlad和Slav,请给我一个小时回家,我会为你写一个可工作且经过测试的版本。 - TheGeneral
1
@VladiSlav,你需要给我提供一个链接到失败的图片,这样我才能查看它,也许调色板中没有包含 alpha 通道? - TheGeneral
1
@VladiSlav 调色板为空。 - TheGeneral
1
@VladiSlav 如果这个解决方案解决了您的问题,请自由地回答您自己的问题。 - TheGeneral
显示剩余13条评论

1
如果您事先检查像素格式并确定它是PixelFormat.Format8bppIndexed,则可以在LockBits调用中使用bmp.PixelFormat,直接获取真实的像素值。这也避免了在调色板包含重复颜色的情况下获取错误的索引。
但是,您需要小心步幅;在.Net中,图像中每行的字节长度都会向上舍入到下一个4个字节的倍数。因此,在具有例如宽度为386(如您的企鹅)的图像上,实际数据每行将为388字节。这就是所谓的“步幅”。为避免出现问题,并获得完全紧凑的数据,请按每行复制输入数据,以实际使用的行数据长度为块大小,同时将读取指针向前移动给定的BitmapData对象的完整步幅。

最小步长通常计算为(bpp * width + 7) / 8。当然,对于8位图像,这将等于宽度,因为每个像素是1字节,但我编写的函数支持任何比特深度,甚至低于8bpp,因此需要进行该计算。

完整函数:

/// <summary>
/// Gets the raw bytes from an image.
/// </summary>
/// <param name="sourceImage">The image to get the bytes from.</param>
/// <param name="stride">Stride of the retrieved image data.</param>
/// <param name="collapseStride">Collapse the stride to the minimum required for the image data.</param>
/// <returns>The raw bytes of the image.</returns>
public static Byte[] GetImageData(Bitmap sourceImage, out Int32 stride, Boolean collapseStride)
{
    if (sourceImage == null)
        throw new ArgumentNullException("sourceImage", "Source image is null!");
    Int32 width = sourceImage.Width;
    Int32 height = sourceImage.Height;
    BitmapData sourceData = sourceImage.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, sourceImage.PixelFormat);
    stride = sourceData.Stride;
    Byte[] data;
    if (collapseStride)
    {
        Int32 actualDataWidth = ((Image.GetPixelFormatSize(sourceImage.PixelFormat) * width) + 7) / 8;
        Int64 sourcePos = sourceData.Scan0.ToInt64();
        Int32 destPos = 0;
        data = new Byte[actualDataWidth * height];
        for (Int32 y = 0; y < height; ++y)
        {
            Marshal.Copy(new IntPtr(sourcePos), data, destPos, actualDataWidth);
            sourcePos += stride;
            destPos += actualDataWidth;
        }
        stride = actualDataWidth;
    }
    else
    {
        data = new Byte[stride * height];
        Marshal.Copy(sourceData.Scan0, data, 0, data.Length);
    }
    sourceImage.UnlockBits(sourceData);
    return data;
}

在你的情况下,这可以简单地称为这样:
if (image.PixelFormat == PixelFormat.Format8bppIndexed)
{
    Int32 stride;
    Byte[] rawData = GetImageData(image, out stride, true);
    // ... do whatever you want with that image data.
}

请注意,正如我在对另一个答案的评论中所说,.Net框架存在一个错误,阻止包含调色板透明信息的8位PNG图像正确加载为8位。要解决这个问题,请参考这个答案:《A: 如何仅将8位PNG图像读取为8位PNG图像?》。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接