sipeed / Maixduino

Arduino port on Maix board ( k210 )
https://maixduino.sipeed.com
Other
216 stars 96 forks source link

Fix color inversion inside a buffer #143

Open dimitre opened 5 months ago

dimitre commented 5 months ago

Maixduino use to handle flipped colors inside a image buffer. By removing a byte swap from lcd we are able now to handle colors correctly for objects and image buffer. it is now possible to make a gradient from black to white inside one image with the changes in this PR.

closes https://github.com/sipeed/Maixduino/issues/141 closes https://github.com/sipeed/Maixduino/issues/138

dimitre commented 5 months ago

We have here a minimal example to test the color fixes:

#include <Sipeed_ST7789.h>
#include <Sipeed_OV2640.h>

SPIClass spi_(SPI0);
Sipeed_ST7789 lcd(320, 240, spi_);
Sipeed_OV2640 camera(FRAMESIZE_QQVGA, PIXFORMAT_RGB565);
GFXcanvas16 canvas(160, 120);

uint16_t rgbto565(uint8_t r, uint8_t g, uint8_t b) {
    return ((r & 0b11111000) << 8) | ((g & 0b11111100) << 3) | (b >> 3);
}

uint16_t rgbto565(uint8_t v) {
    return rgbto565(v, v, v);
}

void setup() {
    lcd.begin(15000000, COLOR_RED);
    lcd.println("hello Sipeed Maix");
    camera.begin();
    camera.run(true);
}

void loop() {
    // This should be Green and it is another color
    canvas.fillScreen(COLOR_GREEN);
    uint8_t * img = camera.snapshot();
    for (uint16_t x=0; x<camera.width(); x++) {
        for (uint16_t y=0; y<15; y++) {
            uint32_t index = x + y * camera.width();
            uint8_t gray = x * 255 / camera.width();
            // this should be a smooth gradient from black to white. 
            camera.getRGB565()[index] = rgbto565(gray);
            canvas.getBuffer()[index] = rgbto565(gray);
        }
    }
    lcd.drawImage(0, 30, camera.width(), camera.height(), camera.getRGB565());
    lcd.drawImage(camera.width(), 30, camera.width(), camera.height(), (uint16_t*)canvas.getBuffer());
}