update: shifted to LVGL's native GIF decoder
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
idf_component_register(SRCS "main.c" "gifdec.c"
|
||||
idf_component_register(SRCS "main.c"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES driver esp_lcd lvgl)
|
||||
|
||||
|
||||
513
main/app_gifdec.c
Normal file
513
main/app_gifdec.c
Normal file
@@ -0,0 +1,513 @@
|
||||
#include "app_gifdec.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MIN(A, B) ((A) < (B) ? (A) : (B))
|
||||
#define MAX(A, B) ((A) > (B) ? (A) : (B))
|
||||
|
||||
typedef struct Entry {
|
||||
uint16_t length;
|
||||
uint16_t prefix;
|
||||
uint8_t suffix;
|
||||
} Entry;
|
||||
|
||||
typedef struct Table {
|
||||
int bulk;
|
||||
int nentries;
|
||||
Entry *entries;
|
||||
} Table;
|
||||
|
||||
/* Memory buffer read helpers */
|
||||
static size_t
|
||||
mem_read(gd_GIF *gif, void *buf, size_t count)
|
||||
{
|
||||
size_t avail = gif->data_size - gif->pos;
|
||||
size_t to_read = (count < avail) ? count : avail;
|
||||
memcpy(buf, gif->data + gif->pos, to_read);
|
||||
gif->pos += to_read;
|
||||
return to_read;
|
||||
}
|
||||
|
||||
static void
|
||||
mem_seek(gd_GIF *gif, size_t offset)
|
||||
{
|
||||
gif->pos = offset;
|
||||
}
|
||||
|
||||
static void
|
||||
mem_skip(gd_GIF *gif, size_t count)
|
||||
{
|
||||
gif->pos += count;
|
||||
if (gif->pos > gif->data_size)
|
||||
gif->pos = gif->data_size;
|
||||
}
|
||||
|
||||
static uint16_t
|
||||
read_num(gd_GIF *gif)
|
||||
{
|
||||
uint8_t bytes[2];
|
||||
mem_read(gif, bytes, 2);
|
||||
return bytes[0] + (((uint16_t) bytes[1]) << 8);
|
||||
}
|
||||
|
||||
gd_GIF *
|
||||
gd_open_gif_mem(const uint8_t *data, size_t size)
|
||||
{
|
||||
uint8_t sigver[3];
|
||||
uint16_t width, height, depth;
|
||||
uint8_t fdsz, bgidx, aspect;
|
||||
int i;
|
||||
uint8_t *bgcolor;
|
||||
int gct_sz;
|
||||
gd_GIF *gif;
|
||||
|
||||
/* Create gd_GIF Structure first to use mem_read */
|
||||
gif = calloc(1, sizeof(*gif));
|
||||
if (!gif) return NULL;
|
||||
|
||||
gif->data = data;
|
||||
gif->data_size = size;
|
||||
gif->pos = 0;
|
||||
|
||||
/* Header */
|
||||
mem_read(gif, sigver, 3);
|
||||
if (memcmp(sigver, "GIF", 3) != 0) {
|
||||
fprintf(stderr, "invalid signature\n");
|
||||
goto fail;
|
||||
}
|
||||
/* Version */
|
||||
mem_read(gif, sigver, 3);
|
||||
if (memcmp(sigver, "89a", 3) != 0) {
|
||||
fprintf(stderr, "invalid version\n");
|
||||
goto fail;
|
||||
}
|
||||
/* Width x Height */
|
||||
width = read_num(gif);
|
||||
height = read_num(gif);
|
||||
/* FDSZ */
|
||||
mem_read(gif, &fdsz, 1);
|
||||
/* Presence of GCT */
|
||||
if (!(fdsz & 0x80)) {
|
||||
fprintf(stderr, "no global color table\n");
|
||||
goto fail;
|
||||
}
|
||||
/* Color Space's Depth */
|
||||
depth = ((fdsz >> 4) & 7) + 1;
|
||||
/* Ignore Sort Flag. */
|
||||
/* GCT Size */
|
||||
gct_sz = 1 << ((fdsz & 0x07) + 1);
|
||||
/* Background Color Index */
|
||||
mem_read(gif, &bgidx, 1);
|
||||
/* Aspect Ratio */
|
||||
mem_read(gif, &aspect, 1);
|
||||
|
||||
gif->width = width;
|
||||
gif->height = height;
|
||||
gif->depth = depth;
|
||||
/* Read GCT */
|
||||
gif->gct.size = gct_sz;
|
||||
mem_read(gif, gif->gct.colors, 3 * gif->gct.size);
|
||||
gif->palette = &gif->gct;
|
||||
gif->bgindex = bgidx;
|
||||
gif->frame = calloc(4, width * height);
|
||||
if (!gif->frame) {
|
||||
goto fail;
|
||||
}
|
||||
gif->canvas = &gif->frame[width * height];
|
||||
if (gif->bgindex)
|
||||
memset(gif->frame, gif->bgindex, gif->width * gif->height);
|
||||
bgcolor = &gif->palette->colors[gif->bgindex*3];
|
||||
if (bgcolor[0] || bgcolor[1] || bgcolor [2])
|
||||
for (i = 0; i < gif->width * gif->height; i++)
|
||||
memcpy(&gif->canvas[i*3], bgcolor, 3);
|
||||
gif->anim_start = gif->pos;
|
||||
return gif;
|
||||
|
||||
fail:
|
||||
free(gif);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void
|
||||
discard_sub_blocks(gd_GIF *gif)
|
||||
{
|
||||
uint8_t size;
|
||||
|
||||
do {
|
||||
mem_read(gif, &size, 1);
|
||||
mem_skip(gif, size);
|
||||
} while (size);
|
||||
}
|
||||
|
||||
static void
|
||||
read_plain_text_ext(gd_GIF *gif)
|
||||
{
|
||||
/* Discard plain text metadata. */
|
||||
mem_skip(gif, 13);
|
||||
/* Discard plain text sub-blocks. */
|
||||
discard_sub_blocks(gif);
|
||||
}
|
||||
|
||||
static void
|
||||
read_graphic_control_ext(gd_GIF *gif)
|
||||
{
|
||||
uint8_t rdit;
|
||||
|
||||
/* Discard block size (always 0x04). */
|
||||
mem_skip(gif, 1);
|
||||
mem_read(gif, &rdit, 1);
|
||||
gif->gce.disposal = (rdit >> 2) & 3;
|
||||
gif->gce.input = rdit & 2;
|
||||
gif->gce.transparency = rdit & 1;
|
||||
gif->gce.delay = read_num(gif);
|
||||
mem_read(gif, &gif->gce.tindex, 1);
|
||||
/* Skip block terminator. */
|
||||
mem_skip(gif, 1);
|
||||
}
|
||||
|
||||
static void
|
||||
read_comment_ext(gd_GIF *gif)
|
||||
{
|
||||
/* Discard comment sub-blocks. */
|
||||
discard_sub_blocks(gif);
|
||||
}
|
||||
|
||||
static void
|
||||
read_application_ext(gd_GIF *gif)
|
||||
{
|
||||
char app_id[8];
|
||||
char app_auth_code[3];
|
||||
|
||||
/* Discard block size (always 0x0B). */
|
||||
mem_skip(gif, 1);
|
||||
/* Application Identifier. */
|
||||
mem_read(gif, app_id, 8);
|
||||
/* Application Authentication Code. */
|
||||
mem_read(gif, app_auth_code, 3);
|
||||
if (!strncmp(app_id, "NETSCAPE", sizeof(app_id))) {
|
||||
/* Discard block size (0x03) and constant byte (0x01). */
|
||||
mem_skip(gif, 2);
|
||||
gif->loop_count = read_num(gif);
|
||||
/* Skip block terminator. */
|
||||
mem_skip(gif, 1);
|
||||
} else {
|
||||
discard_sub_blocks(gif);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
read_ext(gd_GIF *gif)
|
||||
{
|
||||
uint8_t label;
|
||||
|
||||
mem_read(gif, &label, 1);
|
||||
switch (label) {
|
||||
case 0x01:
|
||||
read_plain_text_ext(gif);
|
||||
break;
|
||||
case 0xF9:
|
||||
read_graphic_control_ext(gif);
|
||||
break;
|
||||
case 0xFE:
|
||||
read_comment_ext(gif);
|
||||
break;
|
||||
case 0xFF:
|
||||
read_application_ext(gif);
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "unknown extension: %02X\n", label);
|
||||
}
|
||||
}
|
||||
|
||||
static Table *
|
||||
new_table(int key_size)
|
||||
{
|
||||
int key;
|
||||
int init_bulk = MAX(1 << (key_size + 1), 0x100);
|
||||
Table *table = malloc(sizeof(*table) + sizeof(Entry) * init_bulk);
|
||||
if (table) {
|
||||
table->bulk = init_bulk;
|
||||
table->nentries = (1 << key_size) + 2;
|
||||
table->entries = (Entry *) &table[1];
|
||||
for (key = 0; key < (1 << key_size); key++)
|
||||
table->entries[key] = (Entry) {1, 0xFFF, key};
|
||||
}
|
||||
return table;
|
||||
}
|
||||
|
||||
/* Add table entry. Return value:
|
||||
* 0 on success
|
||||
* +1 if key size must be incremented after this addition
|
||||
* -1 if could not realloc table */
|
||||
static int
|
||||
add_entry(Table **tablep, uint16_t length, uint16_t prefix, uint8_t suffix)
|
||||
{
|
||||
Table *table = *tablep;
|
||||
if (table->nentries == table->bulk) {
|
||||
table->bulk *= 2;
|
||||
table = realloc(table, sizeof(*table) + sizeof(Entry) * table->bulk);
|
||||
if (!table) return -1;
|
||||
table->entries = (Entry *) &table[1];
|
||||
*tablep = table;
|
||||
}
|
||||
table->entries[table->nentries] = (Entry) {length, prefix, suffix};
|
||||
table->nentries++;
|
||||
if ((table->nentries & (table->nentries - 1)) == 0)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint16_t
|
||||
get_key(gd_GIF *gif, int key_size, uint8_t *sub_len, uint8_t *shift, uint8_t *byte)
|
||||
{
|
||||
int bits_read;
|
||||
int rpad;
|
||||
int frag_size;
|
||||
uint16_t key;
|
||||
|
||||
key = 0;
|
||||
for (bits_read = 0; bits_read < key_size; bits_read += frag_size) {
|
||||
rpad = (*shift + bits_read) % 8;
|
||||
if (rpad == 0) {
|
||||
/* Update byte. */
|
||||
if (*sub_len == 0) {
|
||||
mem_read(gif, sub_len, 1); /* Must be nonzero! */
|
||||
if (*sub_len == 0)
|
||||
return 0x1000;
|
||||
}
|
||||
mem_read(gif, byte, 1);
|
||||
(*sub_len)--;
|
||||
}
|
||||
frag_size = MIN(key_size - bits_read, 8 - rpad);
|
||||
key |= ((uint16_t) ((*byte) >> rpad)) << bits_read;
|
||||
}
|
||||
/* Clear extra bits to the left. */
|
||||
key &= (1 << key_size) - 1;
|
||||
*shift = (*shift + key_size) % 8;
|
||||
return key;
|
||||
}
|
||||
|
||||
/* Compute output index of y-th input line, in frame of height h. */
|
||||
static int
|
||||
interlaced_line_index(int h, int y)
|
||||
{
|
||||
int p; /* number of lines in current pass */
|
||||
|
||||
p = (h - 1) / 8 + 1;
|
||||
if (y < p) /* pass 1 */
|
||||
return y * 8;
|
||||
y -= p;
|
||||
p = (h - 5) / 8 + 1;
|
||||
if (y < p) /* pass 2 */
|
||||
return y * 8 + 4;
|
||||
y -= p;
|
||||
p = (h - 3) / 4 + 1;
|
||||
if (y < p) /* pass 3 */
|
||||
return y * 4 + 2;
|
||||
y -= p;
|
||||
/* pass 4 */
|
||||
return y * 2 + 1;
|
||||
}
|
||||
|
||||
/* Decompress image pixels.
|
||||
* Return 0 on success or -1 on out-of-memory (w.r.t. LZW code table). */
|
||||
static int
|
||||
read_image_data(gd_GIF *gif, int interlace)
|
||||
{
|
||||
uint8_t sub_len, shift, byte;
|
||||
int init_key_size, key_size, table_is_full = 0;
|
||||
int frm_off, frm_size, str_len = 0, i, p, x, y;
|
||||
uint16_t key, clear, stop;
|
||||
int ret;
|
||||
Table *table;
|
||||
Entry entry;
|
||||
size_t start, end;
|
||||
|
||||
mem_read(gif, &byte, 1);
|
||||
key_size = (int) byte;
|
||||
if (key_size < 2 || key_size > 8)
|
||||
return -1;
|
||||
|
||||
start = gif->pos;
|
||||
discard_sub_blocks(gif);
|
||||
end = gif->pos;
|
||||
mem_seek(gif, start);
|
||||
clear = 1 << key_size;
|
||||
stop = clear + 1;
|
||||
table = new_table(key_size);
|
||||
key_size++;
|
||||
init_key_size = key_size;
|
||||
sub_len = shift = 0;
|
||||
key = get_key(gif, key_size, &sub_len, &shift, &byte); /* clear code */
|
||||
frm_off = 0;
|
||||
ret = 0;
|
||||
frm_size = gif->fw*gif->fh;
|
||||
while (frm_off < frm_size) {
|
||||
if (key == clear) {
|
||||
key_size = init_key_size;
|
||||
table->nentries = (1 << (key_size - 1)) + 2;
|
||||
table_is_full = 0;
|
||||
} else if (!table_is_full) {
|
||||
ret = add_entry(&table, str_len + 1, key, entry.suffix);
|
||||
if (ret == -1) {
|
||||
free(table);
|
||||
return -1;
|
||||
}
|
||||
if (table->nentries == 0x1000) {
|
||||
ret = 0;
|
||||
table_is_full = 1;
|
||||
}
|
||||
}
|
||||
key = get_key(gif, key_size, &sub_len, &shift, &byte);
|
||||
if (key == clear) continue;
|
||||
if (key == stop || key == 0x1000) break;
|
||||
if (ret == 1) key_size++;
|
||||
entry = table->entries[key];
|
||||
str_len = entry.length;
|
||||
for (i = 0; i < str_len; i++) {
|
||||
p = frm_off + entry.length - 1;
|
||||
x = p % gif->fw;
|
||||
y = p / gif->fw;
|
||||
if (interlace)
|
||||
y = interlaced_line_index((int) gif->fh, y);
|
||||
gif->frame[(gif->fy + y) * gif->width + gif->fx + x] = entry.suffix;
|
||||
if (entry.prefix == 0xFFF)
|
||||
break;
|
||||
else
|
||||
entry = table->entries[entry.prefix];
|
||||
}
|
||||
frm_off += str_len;
|
||||
if (key < table->nentries - 1 && !table_is_full)
|
||||
table->entries[table->nentries - 1].suffix = entry.suffix;
|
||||
}
|
||||
free(table);
|
||||
if (key == stop)
|
||||
mem_read(gif, &sub_len, 1); /* Must be zero! */
|
||||
mem_seek(gif, end);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Read image.
|
||||
* Return 0 on success or -1 on out-of-memory (w.r.t. LZW code table). */
|
||||
static int
|
||||
read_image(gd_GIF *gif)
|
||||
{
|
||||
uint8_t fisrz;
|
||||
int interlace;
|
||||
|
||||
/* Image Descriptor. */
|
||||
gif->fx = read_num(gif);
|
||||
gif->fy = read_num(gif);
|
||||
|
||||
if (gif->fx >= gif->width || gif->fy >= gif->height)
|
||||
return -1;
|
||||
|
||||
gif->fw = read_num(gif);
|
||||
gif->fh = read_num(gif);
|
||||
|
||||
gif->fw = MIN(gif->fw, gif->width - gif->fx);
|
||||
gif->fh = MIN(gif->fh, gif->height - gif->fy);
|
||||
|
||||
mem_read(gif, &fisrz, 1);
|
||||
interlace = fisrz & 0x40;
|
||||
/* Ignore Sort Flag. */
|
||||
/* Local Color Table? */
|
||||
if (fisrz & 0x80) {
|
||||
/* Read LCT */
|
||||
gif->lct.size = 1 << ((fisrz & 0x07) + 1);
|
||||
mem_read(gif, gif->lct.colors, 3 * gif->lct.size);
|
||||
gif->palette = &gif->lct;
|
||||
} else
|
||||
gif->palette = &gif->gct;
|
||||
/* Image Data. */
|
||||
return read_image_data(gif, interlace);
|
||||
}
|
||||
|
||||
static void
|
||||
render_frame_rect(gd_GIF *gif, uint8_t *buffer)
|
||||
{
|
||||
int i, j, k;
|
||||
uint8_t index, *color;
|
||||
i = gif->fy * gif->width + gif->fx;
|
||||
for (j = 0; j < gif->fh; j++) {
|
||||
for (k = 0; k < gif->fw; k++) {
|
||||
index = gif->frame[(gif->fy + j) * gif->width + gif->fx + k];
|
||||
color = &gif->palette->colors[index*3];
|
||||
if (!gif->gce.transparency || index != gif->gce.tindex)
|
||||
memcpy(&buffer[(i+k)*3], color, 3);
|
||||
}
|
||||
i += gif->width;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
dispose(gd_GIF *gif)
|
||||
{
|
||||
int i, j, k;
|
||||
uint8_t *bgcolor;
|
||||
switch (gif->gce.disposal) {
|
||||
case 2: /* Restore to background color. */
|
||||
bgcolor = &gif->palette->colors[gif->bgindex*3];
|
||||
i = gif->fy * gif->width + gif->fx;
|
||||
for (j = 0; j < gif->fh; j++) {
|
||||
for (k = 0; k < gif->fw; k++)
|
||||
memcpy(&gif->canvas[(i+k)*3], bgcolor, 3);
|
||||
i += gif->width;
|
||||
}
|
||||
break;
|
||||
case 3: /* Restore to previous, i.e., don't update canvas.*/
|
||||
break;
|
||||
default:
|
||||
/* Add frame non-transparent pixels to canvas. */
|
||||
render_frame_rect(gif, gif->canvas);
|
||||
}
|
||||
}
|
||||
|
||||
/* Return 1 if got a frame; 0 if got GIF trailer; -1 if error. */
|
||||
int
|
||||
gd_get_frame(gd_GIF *gif)
|
||||
{
|
||||
char sep;
|
||||
|
||||
dispose(gif);
|
||||
mem_read(gif, &sep, 1);
|
||||
while (sep != ',') {
|
||||
if (sep == ';')
|
||||
return 0;
|
||||
if (sep == '!')
|
||||
read_ext(gif);
|
||||
else return -1;
|
||||
mem_read(gif, &sep, 1);
|
||||
}
|
||||
if (read_image(gif) == -1)
|
||||
return -1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void
|
||||
gd_render_frame(gd_GIF *gif, uint8_t *buffer)
|
||||
{
|
||||
memcpy(buffer, gif->canvas, gif->width * gif->height * 3);
|
||||
render_frame_rect(gif, buffer);
|
||||
}
|
||||
|
||||
int
|
||||
gd_is_bgcolor(gd_GIF *gif, uint8_t color[3])
|
||||
{
|
||||
return !memcmp(&gif->palette->colors[gif->bgindex*3], color, 3);
|
||||
}
|
||||
|
||||
void
|
||||
gd_rewind(gd_GIF *gif)
|
||||
{
|
||||
mem_seek(gif, gif->anim_start);
|
||||
}
|
||||
|
||||
void
|
||||
gd_close_gif(gd_GIF *gif)
|
||||
{
|
||||
free(gif->frame);
|
||||
free(gif);
|
||||
}
|
||||
51
main/app_gifdec.h
Normal file
51
main/app_gifdec.h
Normal file
@@ -0,0 +1,51 @@
|
||||
#ifndef APP_GIFDEC_H
|
||||
#define APP_GIFDEC_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct gd_Palette {
|
||||
int size;
|
||||
uint8_t colors[0x100 * 3];
|
||||
} gd_Palette;
|
||||
|
||||
typedef struct gd_GCE {
|
||||
uint16_t delay;
|
||||
uint8_t tindex;
|
||||
uint8_t disposal;
|
||||
int input;
|
||||
int transparency;
|
||||
} gd_GCE;
|
||||
|
||||
typedef struct gd_GIF {
|
||||
const uint8_t *data;
|
||||
size_t data_size;
|
||||
size_t pos;
|
||||
size_t anim_start;
|
||||
uint16_t width, height;
|
||||
uint16_t depth;
|
||||
uint16_t loop_count;
|
||||
gd_GCE gce;
|
||||
gd_Palette *palette;
|
||||
gd_Palette lct, gct;
|
||||
uint16_t fx, fy, fw, fh;
|
||||
uint8_t bgindex;
|
||||
uint8_t *canvas, *frame;
|
||||
} gd_GIF;
|
||||
|
||||
gd_GIF *gd_open_gif_mem(const uint8_t *data, size_t size);
|
||||
int gd_get_frame(gd_GIF *gif);
|
||||
void gd_render_frame(gd_GIF *gif, uint8_t *buffer);
|
||||
int gd_is_bgcolor(gd_GIF *gif, uint8_t color[3]);
|
||||
void gd_rewind(gd_GIF *gif);
|
||||
void gd_close_gif(gd_GIF *gif);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* APP_GIFDEC_H */
|
||||
134
main/main.c
134
main/main.c
@@ -16,7 +16,7 @@
|
||||
#include "esp_heap_caps.h"
|
||||
#include "lvgl.h"
|
||||
|
||||
#include "gifdec.h"
|
||||
#include "src/extra/libs/gif/lv_gif.h"
|
||||
#include "face.h"
|
||||
|
||||
/* LCD size */
|
||||
@@ -52,117 +52,21 @@ static esp_lcd_panel_handle_t lcd_panel = NULL;
|
||||
/* LVGL display */
|
||||
static lv_display_t *lvgl_disp = NULL;
|
||||
|
||||
typedef struct {
|
||||
gd_GIF *gif;
|
||||
lv_obj_t *canvas;
|
||||
lv_timer_t *timer;
|
||||
uint16_t w;
|
||||
uint16_t h;
|
||||
uint8_t *rgb888;
|
||||
lv_color_t *rgb565;
|
||||
} gif_player_t;
|
||||
static lv_obj_t *g_lvgl_gif = NULL;
|
||||
static const lv_img_dsc_t g_lvgl_gif_src = {
|
||||
.header.always_zero = 0,
|
||||
.header.w = 240,
|
||||
.header.h = 135,
|
||||
.header.cf = LV_IMG_CF_RAW,
|
||||
.data_size = sizeof(videoplayback1_ezgif_com_optimize),
|
||||
.data = videoplayback1_ezgif_com_optimize,
|
||||
};
|
||||
|
||||
static gif_player_t g_gif = {0};
|
||||
|
||||
static inline void *lvgl_timer_user_data(lv_timer_t *t)
|
||||
static esp_err_t app_gif_init_lvgl(lv_obj_t *parent)
|
||||
{
|
||||
#if LVGL_VERSION_MAJOR >= 9
|
||||
return lv_timer_get_user_data(t);
|
||||
#else
|
||||
return t->user_data;
|
||||
#endif
|
||||
}
|
||||
|
||||
static void gif_timer_cb(lv_timer_t *t)
|
||||
{
|
||||
gif_player_t *p = (gif_player_t *)lvgl_timer_user_data(t);
|
||||
if (!p || !p->gif) {
|
||||
return;
|
||||
}
|
||||
|
||||
int r = gd_get_frame(p->gif);
|
||||
if (r == 0) {
|
||||
gd_rewind(p->gif);
|
||||
r = gd_get_frame(p->gif);
|
||||
}
|
||||
if (r < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
gd_render_frame(p->gif, p->rgb888);
|
||||
|
||||
const size_t px = (size_t)p->w * (size_t)p->h;
|
||||
const uint8_t *src = p->rgb888;
|
||||
for (size_t i = 0; i < px; i++) {
|
||||
const uint8_t r8 = *src++;
|
||||
const uint8_t g8 = *src++;
|
||||
const uint8_t b8 = *src++;
|
||||
p->rgb565[i] = lv_color_make(r8, g8, b8);
|
||||
}
|
||||
|
||||
/* Schedule next frame based on GIF delay (centiseconds -> ms) */
|
||||
uint32_t delay_ms = (uint32_t)p->gif->gce.delay * 10U;
|
||||
if (delay_ms < 10U) delay_ms = 10U;
|
||||
lv_timer_set_period(t, delay_ms);
|
||||
|
||||
lv_obj_invalidate(p->canvas);
|
||||
}
|
||||
|
||||
static esp_err_t app_gif_init(lv_obj_t *parent)
|
||||
{
|
||||
g_gif.gif = gd_open_gif_mem(videoplayback1_ezgif_com_optimize, sizeof(videoplayback1_ezgif_com_optimize));
|
||||
if (!g_gif.gif) {
|
||||
ESP_LOGE(TAG, "Failed to open GIF from memory");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
g_gif.w = g_gif.gif->width;
|
||||
g_gif.h = g_gif.gif->height;
|
||||
|
||||
const size_t px = (size_t)g_gif.w * (size_t)g_gif.h;
|
||||
|
||||
g_gif.rgb888 = (uint8_t *)heap_caps_malloc(px * 3, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
if (!g_gif.rgb888) {
|
||||
ESP_LOGE(TAG, "No memory for GIF RGB888 buffer");
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
g_gif.rgb565 = (lv_color_t *)heap_caps_aligned_alloc(16, px * sizeof(lv_color_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
|
||||
if (!g_gif.rgb565) {
|
||||
ESP_LOGE(TAG, "No memory for GIF canvas buffer");
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
g_gif.canvas = lv_canvas_create(parent);
|
||||
#if LVGL_VERSION_MAJOR >= 9
|
||||
lv_canvas_set_buffer(g_gif.canvas, g_gif.rgb565, g_gif.w, g_gif.h, LV_COLOR_FORMAT_RGB565);
|
||||
#else
|
||||
lv_canvas_set_buffer(g_gif.canvas, g_gif.rgb565, g_gif.w, g_gif.h, LV_IMG_CF_TRUE_COLOR);
|
||||
#endif
|
||||
lv_obj_set_style_border_width(g_gif.canvas, 0, LV_PART_MAIN);
|
||||
lv_obj_set_style_pad_all(g_gif.canvas, 0, LV_PART_MAIN);
|
||||
lv_obj_set_style_bg_opa(g_gif.canvas, LV_OPA_TRANSP, LV_PART_MAIN);
|
||||
|
||||
/* First frame */
|
||||
if (gd_get_frame(g_gif.gif) <= 0) {
|
||||
gd_rewind(g_gif.gif);
|
||||
(void)gd_get_frame(g_gif.gif);
|
||||
}
|
||||
gd_render_frame(g_gif.gif, g_gif.rgb888);
|
||||
for (size_t i = 0; i < px; i++) {
|
||||
const uint8_t r8 = g_gif.rgb888[i * 3 + 0];
|
||||
const uint8_t g8 = g_gif.rgb888[i * 3 + 1];
|
||||
const uint8_t b8 = g_gif.rgb888[i * 3 + 2];
|
||||
g_gif.rgb565[i] = lv_color_make(r8, g8, b8);
|
||||
}
|
||||
|
||||
/* Place the GIF below the title */
|
||||
lv_obj_align(g_gif.canvas, LV_ALIGN_TOP_MID, 0, 45);
|
||||
|
||||
uint32_t delay_ms = (uint32_t)g_gif.gif->gce.delay * 10U;
|
||||
if (delay_ms < 10U) delay_ms = 10U;
|
||||
g_gif.timer = lv_timer_create(gif_timer_cb, delay_ms, &g_gif);
|
||||
|
||||
g_lvgl_gif = lv_gif_create(parent);
|
||||
lv_gif_set_src(g_lvgl_gif, &g_lvgl_gif_src);
|
||||
lv_obj_align(g_lvgl_gif, LV_ALIGN_TOP_MID, 0, 45);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@@ -306,20 +210,14 @@ void app_main(void)
|
||||
lv_obj_set_style_bg_opa(scr, LV_OPA_COVER, LV_PART_MAIN);
|
||||
|
||||
/* Black text samples */
|
||||
lv_obj_t *title = lv_label_create(scr);
|
||||
lv_label_set_text(title, "Iro Bot.");
|
||||
lv_obj_set_style_text_color(title, lv_color_hex(0xFFFFFF), LV_PART_MAIN);
|
||||
lv_obj_set_style_text_font(title, &lv_font_montserrat_16, LV_PART_MAIN);
|
||||
lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 20);
|
||||
|
||||
/* Add GIF canvas */
|
||||
ESP_ERROR_CHECK(app_gif_init(scr));
|
||||
ESP_ERROR_CHECK(app_gif_init_lvgl(scr));
|
||||
|
||||
lv_obj_t *sample = lv_label_create(scr);
|
||||
lv_label_set_text(sample, "Hey Neil!\nHow are you today?");
|
||||
lv_obj_set_style_text_color(sample, lv_color_hex(0xFFFFFF), LV_PART_MAIN);
|
||||
lv_obj_set_style_text_font(sample, &lv_font_montserrat_16, LV_PART_MAIN);
|
||||
lv_obj_align_to(sample, g_gif.canvas, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
|
||||
lv_obj_align_to(sample, g_lvgl_gif, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
|
||||
|
||||
lvgl_port_unlock();
|
||||
|
||||
|
||||
@@ -2528,7 +2528,7 @@ CONFIG_LV_USE_GRID=y
|
||||
# CONFIG_LV_USE_PNG is not set
|
||||
# CONFIG_LV_USE_BMP is not set
|
||||
# CONFIG_LV_USE_SJPG is not set
|
||||
# CONFIG_LV_USE_GIF is not set
|
||||
CONFIG_LV_USE_GIF=y
|
||||
# CONFIG_LV_USE_QRCODE is not set
|
||||
# CONFIG_LV_USE_FREETYPE is not set
|
||||
# CONFIG_LV_USE_TINY_TTF is not set
|
||||
|
||||
@@ -19,4 +19,6 @@ CONFIG_LV_MEMCPY_MEMSET_STD=y
|
||||
CONFIG_LV_FONT_MONTSERRAT_12=y
|
||||
CONFIG_LV_FONT_MONTSERRAT_16=y
|
||||
CONFIG_LV_FONT_UNSCII_16=y
|
||||
CONFIG_LV_USE_GIF=y
|
||||
CONFIG_FREERTOS_USE_IDLE_HOOK=n
|
||||
|
||||
|
||||
Reference in New Issue
Block a user