gifdecoder speedup and bugfixes

* fixed a bug that caused wrong behavior with segment mirroring
    (effects must use virtualHeight() /  virtualWidth() instead of height() / width())
* added image blur as an option (second slider)
* added very basic error reporting for users
*  up to 25% faster, especially with big animated gifs

* made all local variables "static" (don't pollute global namespace)
* drawPixelCallback: cache calculation that do not depend on x/y position
* reduced memory allocations on boards without PSRAM, to avoid crashes
This commit is contained in:
Frank
2025-11-06 22:50:59 +01:00
parent 5177ebd271
commit 10b3ac0eb1
2 changed files with 70 additions and 21 deletions

View File

@@ -4693,9 +4693,11 @@ static const char _data_FX_MODE_WASHING_MACHINE[] PROGMEM = "Washing Machine@!,!
Draws a .gif image from filesystem on the matrix/strip Draws a .gif image from filesystem on the matrix/strip
*/ */
uint16_t mode_image(void) { uint16_t mode_image(void) {
if (!strip.isMatrix) return mode_oops(); // not a 2D set-up
#ifndef WLED_ENABLE_GIF #ifndef WLED_ENABLE_GIF
return mode_oops(); return mode_oops();
#else #else
if (max(SEGMENT.virtualWidth(),SEGMENT.virtualHeight()) < 4) return mode_oops(); // too small
renderImageToSegment(SEGMENT); renderImageToSegment(SEGMENT);
return FRAMETIME; return FRAMETIME;
#endif #endif
@@ -4704,7 +4706,7 @@ uint16_t mode_image(void) {
// Serial.println(status); // Serial.println(status);
// } // }
} }
static const char _data_FX_MODE_IMAGE[] PROGMEM = "Image@!,;;;12;sx=128"; static const char _data_FX_MODE_IMAGE[] PROGMEM = "Image@!,Blur,;;;12;sx=128,ix=0";
/* /*
Blends random colors across palette Blends random colors across palette

View File

@@ -9,11 +9,15 @@
* Functions to render images from filesystem to segments, used by the "Image" effect * Functions to render images from filesystem to segments, used by the "Image" effect
*/ */
File file; static File file;
char lastFilename[34] = "/"; static char lastFilename[34] = "/";
GifDecoder<320,320,12,true> decoder; #if !defined(BOARD_HAS_PSRAM)
bool gifDecodeFailed = false; static GifDecoder<256,256,11,true> decoder; // WLEDMM use less RAM on boards without PSRAM - avoids crashes due to out-of-memory
unsigned long lastFrameDisplayTime = 0, currentFrameDelay = 0; #else
static GifDecoder<320,320,12,true> decoder;
#endif
static bool gifDecodeFailed = false;
static unsigned long lastFrameDisplayTime = 0, currentFrameDelay = 0;
bool fileSeekCallback(unsigned long position) { bool fileSeekCallback(unsigned long position) {
return file.seek(position); return file.seek(position);
@@ -35,29 +39,49 @@ int fileSizeCallback(void) {
return file.size(); return file.size();
} }
bool openGif(const char *filename) { bool openGif(const char *filename) { // side-effect: updates "file"
file = WLED_FS.open(filename, "r"); file = WLED_FS.open(filename, "r");
DEBUG_PRINTF("opening GIF file %s\n", filename);
if (!file) return false; if (!file) return false;
return true; return true;
} }
Segment* activeSeg; static Segment* activeSeg;
uint16_t gifWidth, gifHeight; static uint16_t gifWidth, gifHeight; // these two must stay uint16_t, because they are passed by reference
static unsigned seg_cols = 1;
static unsigned seg_rows = 1;
static int expandX = 1;
static int expandY = 1;
static int lastX = -1, lastY = -1;
void screenClearCallback(void) { void screenClearCallback(void) {
activeSeg->fill(0); activeSeg->fill(0);
} }
void updateScreenCallback(void) {} void updateScreenCallback(void) {
// this callback runs when the decoder has finished painting all pixels
// perfect time for adding blur
if (activeSeg->intensity > 1) {
uint8_t blurAmount = activeSeg->intensity >> 2;
if ((blurAmount < 24) && (activeSeg->is2D())) activeSeg->blurRows(activeSeg->intensity >> 1); // some blur - fast
else activeSeg->blur(blurAmount); // more blur - slower
}
lastX = lastY = -1; // invalidate last position
}
void drawPixelCallback(int16_t x, int16_t y, uint8_t red, uint8_t green, uint8_t blue) { void drawPixelCallback(int16_t x, int16_t y, uint8_t red, uint8_t green, uint8_t blue) {
// simple nearest-neighbor scaling // simple nearest-neighbor downscaling
int16_t outY = y * activeSeg->height() / gifHeight; int outY = y * seg_rows / gifHeight;
int16_t outX = x * activeSeg->width() / gifWidth; int outX = x * seg_cols / gifWidth;
if ((unsigned(outX) >= seg_cols) || (unsigned(outY) >= seg_rows)) return; // out of range
if ((lastX == outX) && (lastY == outY)) return; // downscaling optimization: skip re-painting same pixel
lastX = outX; lastY = outY;
// set multiple pixels if upscaling // set multiple pixels if upscaling
for (int16_t i = 0; i < (activeSeg->width()+(gifWidth-1)) / gifWidth; i++) { // softhack007: changed loop x/y order -> minor speedup from better cache locality
for (int16_t j = 0; j < (activeSeg->height()+(gifHeight-1)) / gifHeight; j++) { for (int j = 0; j < expandY; j++) {
for (int i = 0; i < expandX; i++) {
activeSeg->setPixelColorXY(outX + i, outY + j, gamma8(red), gamma8(green), gamma8(blue)); activeSeg->setPixelColorXY(outX + i, outY + j, gamma8(red), gamma8(green), gamma8(blue));
} }
} }
@@ -81,17 +105,24 @@ byte renderImageToSegment(Segment &seg) {
// TODO: if (seg.mode != seg.currentMode()) return IMAGE_ERROR_WAITING; // TODO: if (seg.mode != seg.currentMode()) return IMAGE_ERROR_WAITING;
if (activeSeg && activeSeg != &seg) return IMAGE_ERROR_SEG_LIMIT; // only one segment at a time if (activeSeg && activeSeg != &seg) return IMAGE_ERROR_SEG_LIMIT; // only one segment at a time
activeSeg = &seg; activeSeg = &seg;
seg_cols = activeSeg->virtualWidth();
seg_rows = activeSeg->virtualHeight();
if (strncmp(lastFilename +1, seg.name, 32) != 0) { // segment name changed, load new image if (strncmp(lastFilename +1, seg.name, 32) != 0) { // segment name changed, load new image
strncpy(lastFilename +1, seg.name, 32); strncpy(lastFilename +1, seg.name, 32);
gifDecodeFailed = false; gifDecodeFailed = false;
if (strcmp(lastFilename + strlen(lastFilename) - 4, ".gif") != 0) { if (strcmp(lastFilename + strlen(lastFilename) - 4, ".gif") != 0) {
gifDecodeFailed = true; gifDecodeFailed = true;
USER_PRINTF("Unsupported format: %s\n", lastFilename);
return IMAGE_ERROR_UNSUPPORTED_FORMAT; return IMAGE_ERROR_UNSUPPORTED_FORMAT;
} }
if (file) file.close(); if (file) file.close();
openGif(lastFilename);
if (!file) { gifDecodeFailed = true; return IMAGE_ERROR_FILE_MISSING; } if (!openGif(lastFilename)) {
gifDecodeFailed = true;
USER_PRINTF("GIF file not found: %s\n", lastFilename);
return IMAGE_ERROR_FILE_MISSING;
}
decoder.setScreenClearCallback(screenClearCallback); decoder.setScreenClearCallback(screenClearCallback);
decoder.setUpdateScreenCallback(updateScreenCallback); decoder.setUpdateScreenCallback(updateScreenCallback);
decoder.setDrawPixelCallback(drawPixelCallback); decoder.setDrawPixelCallback(drawPixelCallback);
@@ -100,9 +131,18 @@ byte renderImageToSegment(Segment &seg) {
decoder.setFileReadCallback(fileReadCallback); decoder.setFileReadCallback(fileReadCallback);
decoder.setFileReadBlockCallback(fileReadBlockCallback); decoder.setFileReadBlockCallback(fileReadBlockCallback);
decoder.setFileSizeCallback(fileSizeCallback); decoder.setFileSizeCallback(fileSizeCallback);
decoder.alloc(); decoder.alloc(); // WLEDMM this function may throw out-of memory and cause a crash
DEBUG_PRINTLN(F("Starting decoding")); DEBUG_PRINTLN(F("Starting decoding"));
if(decoder.startDecoding() < 0) { gifDecodeFailed = true; return IMAGE_ERROR_GIF_DECODE; } int derr = 0;
if((derr = decoder.startDecoding()) < 0) {
gifDecodeFailed = true;
USER_PRINTF("GIF Decoding error %d\n", derr);
if ((derr == ERROR_GIF_TOO_WIDE) || (derr == ERROR_GIF_UNSUPPORTED_FEATURE) || (derr == ERROR_GIF_INVALID_PARAMETER))
errorFlag = ERR_NORAM_PX;
return IMAGE_ERROR_GIF_DECODE;
}
if ((errorFlag == ERR_NORAM_PX) || (errorFlag == ERR_NORAM)) errorFlag = ERR_NONE; // success -> reset previous memory error codes
DEBUG_PRINTLN(F("Decoding started")); DEBUG_PRINTLN(F("Decoding started"));
} }
@@ -118,9 +158,16 @@ byte renderImageToSegment(Segment &seg) {
if (millis() - lastFrameDisplayTime < wait) return IMAGE_ERROR_WAITING; if (millis() - lastFrameDisplayTime < wait) return IMAGE_ERROR_WAITING;
decoder.getSize(&gifWidth, &gifHeight); decoder.getSize(&gifWidth, &gifHeight);
// softhack007: pre-calculate upscaling for speedup
expandX = (seg_cols+(gifWidth-1)) / gifWidth;
expandY = (seg_rows+(gifHeight-1)) / gifHeight;
int result = decoder.decodeFrame(false); int result = decoder.decodeFrame(false);
if (result < 0) { gifDecodeFailed = true; return IMAGE_ERROR_FRAME_DECODE; } if (result < 0) {
gifDecodeFailed = true;
USER_PRINTF("GIF Frame decode failed %d\n", result);
return IMAGE_ERROR_FRAME_DECODE;
}
currentFrameDelay = decoder.getFrameDelay_ms(); currentFrameDelay = decoder.getFrameDelay_ms();
unsigned long tooSlowBy = (millis() - lastFrameDisplayTime) - wait; // if last frame was longer than intended, compensate unsigned long tooSlowBy = (millis() - lastFrameDisplayTime) - wait; // if last frame was longer than intended, compensate