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

时间:2020-02-09 12:50:54

标签: c# image image-processing raster escpos

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

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

我尝试执行此操作以支持Column协议的ESC/POS格式打印。我已经有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 Fiddle测试:https://dotnetfiddle.net/NBRBgt

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

1 个答案:

答案 0 :(得分: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阵列上使用我的设置(英特尔4770K,x64,.NET Core 3.0)的简单基准测试得出:

old: 1103 ticks
new:   56 ticks

非常明显的区别。