【问题标题】:Simple bitmap rotation for arduino, c++arduino,c ++的简单位图旋转
【发布时间】:2016-01-26 13:45:44
【问题描述】:

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

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

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

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

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

    B11110000,
    B00100000,
    B01100000,
    B10010000
}

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

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

【问题讨论】:

    标签: c++ bitmap arduino avr


    【解决方案1】:

    这是一个使用纯 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));
                }
            }
        }
    }
    

    【讨论】:

    • 哦,我忘了说,对不起:{B00111100, B01001000, B00100100, B01010000, B00111100, B01110000, B00100100, B01001000}
    • 谢谢!我会尽快检查并告诉它是如何工作的!
    • 哦,它可以工作,但是对于宽度超过 8 像素的位图,它不起作用,对吧?这就是棘手的部分——甚至可能有 84x48 位图
    • 现在变得有趣了。我只是用更通用的解决方案更新解决方案。
    • 像魅力一样工作,非常感谢!真的,我欠你一杯啤酒。一大堆啤酒。
    猜你喜欢
    • 2016-01-30
    • 2014-03-13
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    • 2022-07-10
    • 2023-03-18
    • 1970-01-01
    • 2011-01-14
    相关资源
    最近更新 更多