algorithm - 水平翻转一位位图线

标签 algorithm bitmap

我正在寻找一种算法来水平翻转 1 位位图线。请记住,这些行是 DWORD 对齐的!

我目前正在将 RLE 流取消编码为每像素 8 位缓冲区,然后重新编码为 1 位行,但是,我想尝试将其全部保留在 1 位空间中来提高它的速度。分析表明程序的这一部分与其余部分相比相对较慢。

示例行(翻转前):

FF FF FF FF 77 AE F0 00

示例行(翻转后):

F7 5E EF FF FF FF F0 00

最佳答案

创建转换表以交换字节中的位:

byte[] convert = new byte[256];
for (int i = 0; i < 256; i++) {
  int value = 0;
  for (int bit = 1; bit <= 128; bit<<=1) {
    value <<= 1;
    if ((i & bit) != 0) value++;
  }
  convert[i] = (byte)value;
}

现在您可以使用表来交换一个字节,然后您只需将字节存储在结果中的正确位置即可:

byte[] data = { 0xFF, 0xFF, 0xFF, 0xFF, 0x77, 0xAE, 0xF0, 0x00 };
int width = 52;

int shift = data.Length * 8 - width;
int shiftBytes = data.Length - 1 - shift / 8;
int shiftBits = shift % 8;

byte[] result = new byte[data.Length];
for (int i = 0; i < data.Length; i++) {
  byte swap = convert[data[i]];
  if (shiftBits == 0) {
    result[shiftBYtes - i] = swap;
  } else {
    if (shiftBytes - i >= 0) {
      result[shiftBytes - i] |= (byte)(swap << shiftBits);
    }
    if (shiftBytes - i - 1 >= 0) {
      result[shiftBytes - i - 1] |= (byte)(swap >> (8 - shiftBits));
    }
  }
}

Console.WriteLine(BitConverter.ToString(result));

输出:

F7-5E-EF-FF-FF-FF-F0-00

关于algorithm - 水平翻转一位位图线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2776925/

相关文章:

c++ - std::max_element 编译错误,C++

android - 截取 LinearLayout 和 RecyclerView 的屏幕截图

android - 使用设备存储中的图像填充 ListView 中的 ImageView

ios - 在 OpenGL ES (iOS) 中绘制二维位图

algorithm - 具有多个目的地的 K 条不连贯的路径

c - 从 c 中的算法实现 for 循环

量化线段路径差异的算法

algorithm - 如何在golang中按字符a对字符串数组进行排序

android - 加载位图时出现 "BitmapFactory.decodeResource"应用程序崩溃

android - 在 Surfaceview 的 Canvas 上显示位图