【问题标题】:Convert raster byte[] image data to column Format in C#在 C# 中将光栅字节 [] 图像数据转换为列格式
【发布时间】:2020-05-24 23:16:35
【问题描述】:

我正在尝试将字节数组从 Raster 格式的图像(从左到右读取)转换为 Column 格式(从上到下读取)。

问题看起来很简单,我们有二维位数组(图像的宽度/高度)。在Raster 格式中,您从左到右读取位,在Column 格式中,您从上到下读取位。

我尝试这样做以支持ESC/POS 协议的Column 格式打印。我已经有了Raster 格式的图像,现在我正在尝试将其转换为Column 格式。

ESC/POSRaster 打印的文档:

ESC/POSColumn 打印的文档:

目前,我通过直接使用BitArray 处理位来进行转换。这个解决方案不是最优的,我认为有必要直接在Byte 中工作。

private byte[] ConvertRasterToColumnFormat(byte[] rasterData, int width, int height)
{
    var finalHeight = height;
    while (finalHeight % 8 != 0) finalHeight++;

    var finalWidth = width;
    while (finalWidth % 8 != 0) finalWidth++;

    var rasterBitArray = new BitArray(rasterData);
    var columnBitArray = new BitArray(finalHeight * finalWidth);

    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            var rasterPosition = y * finalWidth;
            var columnPosition = x * finalHeight;

            rasterPosition += (x / 8) * 8;
            columnPosition += (y / 8) * 8;

            rasterPosition += 7 - x % 8;
            columnPosition += 7 - y % 8;

            var value = rasterBitArray[rasterPosition];
            columnBitArray[columnPosition] = value;
        }
    }

    var result = new byte[columnBitArray.Length / 8];
    columnBitArray.CopyTo(result, 0);
    return result;
}

.NET 测试:https://dotnetfiddle.net/NBRBgt

有人有更优化的解决方案吗?

【问题讨论】:

    标签: c# image image-processing raster escpos


    【解决方案1】:

    一种可能的方法是获取一个 8x8 的位块,使用bitboard techniques 对其进行转置,然后存储结果的字节。它不是很漂亮,但它避免了处理单个位,当然也避免了BitArray(即使在逐位方法中也很慢)。

    以下代码通过了您的测试用例,但可能应该更好地测试..

    private static byte[] ConvertRasterToColumnFormat(byte[] rasterData, int width, int height)
    {
        int h = height + 7 & -8;
        int w = (width + 7) >> 3;
        int hsmall = h >> 3;
    
        var result = new byte[h * w];
    
        for (int y = 0; y < height; y += 8)
        {
            for (int x = 0; x < w; x++)
            {
                // grab 8x8 block of bits
                int i = x + w * y;
                ulong block = rasterData[i];
                if (i + w < rasterData.Length)
                    block |= (ulong)rasterData[i + w] << 8;
                if (i + w * 2 < rasterData.Length)
                    block |= (ulong)rasterData[i + w * 2] << 16;
                if (i + w * 3 < rasterData.Length)
                    block |= (ulong)rasterData[i + w * 3] << 24;
                if (i + w * 4 < rasterData.Length)
                    block |= (ulong)rasterData[i + w * 4] << 32;
                if (i + w * 5 < rasterData.Length)
                    block |= (ulong)rasterData[i + w * 5] << 40;
                if (i + w * 6 < rasterData.Length)
                    block |= (ulong)rasterData[i + w * 6] << 48;
                if (i + w * 7 < rasterData.Length)
                    block |= (ulong)rasterData[i + w * 7] << 56;
    
                // transpose 8x8
                // https://www.chessprogramming.org/Flipping_Mirroring_and_Rotating#Anti-Diagonal
                ulong t;
                const ulong k1 = 0xaa00aa00aa00aa00;
                const ulong k2 = 0xcccc0000cccc0000;
                const ulong k4 = 0xf0f0f0f00f0f0f0f;
                t = block ^ (block << 36);
                block ^= k4 & (t ^ (block >> 36));
                t = k2 & (block ^ (block << 18));
                block ^= t ^ (t >> 18);
                t = k1 & (block ^ (block << 9));
                block ^= t ^ (t >> 9);
    
                // write block to columns
                int j = (y >> 3) + h * x;
                result[j] = (byte)block;
                result[j + hsmall] = (byte)(block >> 8);
                result[j + hsmall * 2] = (byte)(block >> 16);
                result[j + hsmall * 3] = (byte)(block >> 24);
                result[j + hsmall * 4] = (byte)(block >> 32);
                result[j + hsmall * 5] = (byte)(block >> 40);
                result[j + hsmall * 6] = (byte)(block >> 48);
                result[j + hsmall * 7] = (byte)(block >> 56);
            }
        }
    
        return result;
    }
    

    我在 256x256 阵列上的设置(Intel 4770K、x64、.NET Core 3.0)的简单基准测试给出:

    old: 1103 ticks
    new:   56 ticks
    

    所以非常明显的区别。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-27
      • 1970-01-01
      • 2015-07-07
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      • 2012-10-28
      相关资源
      最近更新 更多