Skip to content

Instantly share code, notes, and snippets.

@Jerrylum
Last active June 1, 2019 06:47
Show Gist options
  • Save Jerrylum/5d4f3168a596dfbcd87e8d5f81236fcc to your computer and use it in GitHub Desktop.
Save Jerrylum/5d4f3168a596dfbcd87e8d5f81236fcc to your computer and use it in GitHub Desktop.
A display test for BMP file (includes 24bit RGB and 32bit RGBA) using LVGL Canvas function
#include "main.h"
void drawBmp(lv_obj_t* canvas, const char* filename){
printf("Start: %d\n",pros::millis());
FILE* f = fopen(filename, "rb");
unsigned char info[54];
fread(info, sizeof(unsigned char), 54, f); // read the 54-byte header
// extract image height and width from header
int width = *(int*)&info[18];
int height = *(int*)&info[22];
int pixelbit = info[28] / 8;
int linebyte = width * pixelbit;
int offset = linebyte % 4;
if (offset != 0)
offset = 4 - offset;
bool isBottom = false;
if (height > 0) // read from bottom
isBottom = true;
printf("%d %d %d\n",offset,linebyte,pixelbit);
int size = pixelbit * width * height;
unsigned char* data = new unsigned char[size]; // allocate 3 bytes per pixel
fread(data, sizeof(unsigned char), size, f); // read the rest of the data at once
fclose(f);
//lv_color_t pbuf[width][height] = {0};
printf("Finish read: %d\n",pros::millis());
int ing_w = 0;
int ing_h = 0;
for(int i = 0; i < size; i += 3) {
//BGR color mode
//i+2 = red, i+1 = green, i = blue
int x = ing_w;
int y = isBottom ? (height - ing_h - 1) : ing_h;
lv_color_t c = LV_COLOR_MAKE(data[i+2],data[i+1],data[i]);
ing_w += 1;
if(ing_w >= width){
ing_w = 0;
ing_h += 1;
}
lv_canvas_set_px(canvas,x,y,c);//Maybe change
}
printf("Finish draw: %d\n",pros::millis());
}
void draw32BitBmp(lv_obj_t* canvas, const char* filename){
printf("Start: %d\n",pros::millis());
FILE* f = fopen(filename, "rb");
unsigned char info[54];
fread(info, sizeof(unsigned char), 54, f); // read the 54-byte header
// extract image height and width from header
int width = *(int*)&info[18];
int height = *(int*)&info[22];
int pixelbit = info[28] / 8;
int linebyte = width * pixelbit;
int offset = linebyte % 4;
if (offset != 0)
offset = 4 - offset;
bool isBottom = false;
if (height > 0) // read from bottom
isBottom = true;
printf("%d %d %d\n",offset,linebyte,pixelbit);
int size = pixelbit * width * height;
unsigned char* data = new unsigned char[size]; // allocate 3 bytes per pixel
fread(data, sizeof(unsigned char), size, f); // read the rest of the data at once
fclose(f);
//lv_color_t pbuf[width][height] = {0};
printf("Finish read: %d\n",pros::millis());
int ing_w = 0;
int ing_h = 0;
for(int i = 0; i < size; i += pixelbit) {
//BGR color mode
//i+2 = red, i+1 = green, i = blue
int x = ing_w;
int y = isBottom ? (height - ing_h - 1) : ing_h;
int a = pixelbit == 4 ? data[i+3] : 255;
int r = data[i+2];
int g = data[i+1];
int b = data[i];
float a_float = a / 2.55 / 100;
float a_m_float = 1 - a_float;
lv_color_t bg_c = lv_canvas_get_px(canvas, x, y);
bg_c.red = bg_c.red * a_m_float + r * a_float;
bg_c.green = bg_c.green * a_m_float + g * a_float;
bg_c.blue = bg_c.blue * a_m_float + b * a_float;
ing_w += 1;
if(ing_w >= width){
ing_w = 0;
ing_h += 1;
}
lv_canvas_set_px(canvas,x,y,bg_c);//Maybe change
}
printf("Finish draw: %d\n",pros::millis());
}
void initialize() {
pros::delay(100);
printf("Dummy init");
lv_obj_t * jcd_dummy = lv_canvas_create(lv_scr_act(), NULL);
lv_obj_set_size(jcd_dummy, 480, 240);
printf("Canvas init");
static lv_color_t cbuf[sizeof(lv_color_t) * 480 * 240];
uint32_t i;
for(i = 0; i < sizeof(cbuf) / sizeof(cbuf[0]); i++) {
cbuf[i] = LV_COLOR_HEX3(0xacf);
}
/*Create the canvas object*/
lv_obj_t * canvas = lv_canvas_create(jcd_dummy, NULL);
/*Assign the buffer to the canvas*/
lv_canvas_set_buffer(canvas, cbuf, 480, 240, LV_IMG_CF_TRUE_COLOR);
printf("Draw test");
//drawBmp(canvas,"/usd/pros-icon.bmp");
draw32BitBmp(canvas,"/usd/pros-icon-rgba.bmp");
}
void disabled() {}
void competition_initialize() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment