c++ - arduino,c++的简单位图旋转

标签 c++ bitmap arduino avr

我正在尝试为我的应用程序做出妥协,但到目前为止还没有运气(或者更确切地说是知识)。

我有黑白屏幕的位图,它看起来像这样(我使用arduino字节风格,因为它更具可读性)

{
    B00111100, B01001000,
    B00100100, B01010000,
    B00111100, B01110000,
    B00100100, B01001000
}

它是字节数组,每个字节代表接下来的 8 个水平像素。 问题是我必须使用位图,其中每个字节代表 8 个下一个垂直像素,所以就像这样转动它

{
    B00000000,
    B00000000,
    B11110000,
    B10100000,
    B11110000,
    B00000000,

    B11110000,
    B00100000,
    B01100000,
    B10010000
}

我试过了,但最后完全不知道该怎么做。

编辑。我可能会被误解,所以我在代码中添加了括号,现在更清楚了。

最佳答案

这是一个使用纯 C (gcc) 的示例:

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


typedef uint8_t byte;

void print_bin(byte x) {
    printf("B");
    for (int i = 0; i < 8; i++) {
        printf("%s", (x >> (7-i)) % 2 ? "1" : "0");
    }
    printf("\n");
}

void reverse(byte* in, byte* out, int width, int height) {
    int width_bytes = (width + 7) / 8;
    int height_bytes = (height + 7) / 8;
    // init *out. You can skip the next line if you are sure that *out is clear.
    memset (out, 0, width * height_bytes);
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            if (in[(y * width_bytes + x / 8)] & (1 << (7 - x % 8))) {
                out[(x * height_bytes + y / 8)] |= (1 << (7 - y % 8));
            }
        }
    }
}

#define WIDTH  13
#define HEIGHT  4
#define IN_SIZE  (((WIDTH + 7) / 8) * HEIGHT)
#define OUT_SIZE (((HEIGHT + 7) / 8) * WIDTH)

int main() {
    byte in[IN_SIZE] = {
        0b00111100, 0b01001000,
        0b00100100, 0b01010000,
        0b00111100, 0b01110000,
        0b00100100, 0b01001000
    };

    byte* out = calloc(OUT_SIZE, 1);
    reverse (in, out, WIDTH, HEIGHT);
    for (int i = 0; i < OUT_SIZE; i++) {
        print_bin(out[i]);
    }

}

这是结果:

B00000000
B00000000
B11110000
B10100000
B10100000
B11110000
B00000000
B00000000
B00000000
B11110000
B00100000
B01100000
B10010000

如果速度有问题,您可以进行以下优化:

void reverse(byte* in, byte* out, int width, int height) {
    int width_bytes = (width + 7) / 8;
    int height_bytes = (height + 7) / 8;
    // init *out. You can skip the next line if you are sure that *out is clear.
    memset (out, 0, width * height_bytes);
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int t; // optimisation
            if ((x % 8) == 0) t = in[(y * width_bytes + x / 8)];
            if (t & (1 << (7 - x % 8))) {
                out[(x * height_bytes + y / 8)] |= (1 << (7 - y % 8));
            }
        }
    }
}

关于c++ - arduino,c++的简单位图旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35015264/

相关文章:

java - 构建命令失败,外部 native 问题 android studio

android - 如何在没有标签的情况下获取当前 View 寻呼机 View ?

android - 无法使用蓝牙 HC-06 发送数据 - 应用程序停止工作

计算信号时间

ios - 通过 UDP 在 Arduino 和 iOS 设备之间发送和接收字符串

c++ - this_thread::sleep_for 早睡

c++ - 从继承的函数指针调用基方法

c++ - windows – Qt创建和使用自制静态库

Android:DialogFragment 中的位图大小超过 32 位

linux - 使用 avconv 将(按数字排序的)位图文件转换为视频文件