将光栅字节[]图像数据转换为C#中的列格式

Convert raster byte[] image data to column Format in C#

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

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

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

ESC/POS Raster 打印文档:

ESC/POS Column 打印文档:

目前,我通过直接使用 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 Fiddle 测试:https://dotnetfiddle.net/NBRBgt

有没有人有更好的解决方案?

一种可能的方法是获取一个 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

非常明显的区别。