Search code examples
c++bitmaparduinoavr

Simple bitmap rotation for arduino, c++


I'm trying to achieve compromise for my app, but got no luck (or rather knowledge) so far.

I've got bitmap for black-white screen, it looks like this (I use arduino byte style, because it's more readable)

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

It is array of bytes, each byte representing 8 next horizontal pixels. Problem is that I have to use bitmap, where each byte represents 8 next vertical pixels, so it's like turning it this way

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

    B11110000,
    B00100000,
    B01100000,
    B10010000
}

I tried, but ended up completely without any idea how to do this.

Edit. I could be misunderstod, so I added brackets to code, It's more clear right now.


Solution

  • Here is an example using plain 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]);
        }
    
    }
    

    And this is the result:

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

    If speed is an issue, you can do the following optimisation:

    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));
                }
            }
        }
    }