PNG DEcoder and ESP32-S3

LVGL Ver 9.4.0
This is the code
in main setup sdAnalyzer.CreateLVGLPNG(“/moon/test.png”);


void SDCardAnalyzer::CreateLVGLPNG(const char* path) {

    lv_fs_sdfat_init();


    lv_obj_t* img = lv_image_create(lv_screen_active());

    // KORREKT: Füge den führenden Slash hinzu, wenn 'path' ihn nicht hat
    // Dein Dump zeigt "/moon/test.png", also muss der Pfad mit '/' beginnen
    String fullPath;
    
        fullPath = String("S:") + path;  
    

    Serial.printf("Lade Bild: %s\n\n\n\r", fullPath.c_str());


    lv_image_set_src(img, fullPath.c_str());

    // WICHTIG: LVGL muss das Layout aktualisieren, bevor die Größe gültig ist
    lv_obj_update_layout(img);

    // Debug: Aktuelle Widget-Größe ausgeben (sollte 0 sein, bevor Bild geladen)
    Serial.printf("Image Objekt Größe : width %d height %d\n\r", 
                  lv_obj_get_width(img), lv_obj_get_height(img));

    // Bild zentrieren (funktioniert erst NACH erfolgreichem Laden)
    lv_obj_center(img);

    // Layout erneut aktualisieren, um Zentrierung zu berechnen
    lv_obj_update_layout(img);

    // Überprüfen, ob Bild erfolgreich geladen wurde
    lv_image_dsc_t* dsc = (lv_image_dsc_t*)lv_image_get_src(img);
    if (!dsc || dsc->data == nullptr) {
        Serial.println("Bild-Dekodierung fehlgeschlagen! (dsc == NULL oder data == NULL)");
        
        // Zusätzliche Debug-Info: Versuche, Datei direkt zu öffnen
        lv_fs_file_t file;
        lv_fs_res_t res = lv_fs_open(&file, fullPath.c_str(), LV_FS_MODE_RD);
        if (res == LV_FS_RES_OK) {
            Serial.println("FEHLER: Datei kann geöffnet werden, aber PNG-Decoder schlägt fehl!");
            uint32_t size;
            lv_fs_res_t size_res = lv_fs_get_size(&file, &size); // <-- Corrected function
                if (size_res == LV_FS_RES_OK) {
                    Serial.printf("File size: %u bytes\n\r", size);
                    } else {
                Serial.printf(_RED "Could not get file size. Error: %d\n\r\n" RESET, size_res);
    }
            lv_fs_close(&file);
            
            // Mögliche Ursache: PNG ist korrupt oder verwendet nicht unterstütztes Format
            // (z.B. indizierte Farben/Palette statt RGB/RGBA)
        } else {
            Serial.printf(_RED "FEHLER: Datei kann nicht geöffnet werden! Fehlercode: %d\n\r" RESET, res);
        }
        return;
    }

    // Erfolg! Jetzt hat das Bild seine echten Dimensionen
    Serial.printf("PNG erfolgreich geladen! %dx%d CF=%d\n\r", 
                  dsc->header.w, dsc->header.h, dsc->header.cf);
    
    // Jetzt kannst du die tatsächlichen Pixel-Dimensionen verwenden
    uint16_t img_width = dsc->header.w;   // 10 Pixel für deine test.png
    uint16_t img_height = dsc->header.h;  // 10 Pixel für deine test.png
    
    // Das Widget sollte jetzt automatisch diese Größe haben
    Serial.printf("Image Widget Größe nach Laden: width %d height %d\n\r",
                  lv_obj_get_width(img), lv_obj_get_height(img));
}


#pragma once
#include "lvgl.h"
#include <SdFat.h>

extern SdFat sd;

#ifdef __cplusplus
extern "C" {
#endif

void lv_fs_sdfat_init(void);

#ifdef __cplusplus
}
#endif
/* -------------------------
   FILE: open / close / read / write / seek / tell
   ------------------------- */

#include "lv_sdfat_fs.h"
#include <string.h>
#include <SdFat.h>

extern SdFat sd;

// Ein statisches File-Objekt (einfach für Demo, mehrere Dateien → eigene Struktur)
static SdFile file;

// ----------------- Callbacks -----------------
static void* fs_open_cb(lv_fs_drv_t* drv, const char* path, lv_fs_mode_t mode) {
    // Wenn der Pfad NICHT mit "/" beginnt, füge "/" hinzu
    char full_path[256];
    if(path[0] != '/') {
        snprintf(full_path, sizeof(full_path), "/%s", path);
        path = full_path;
    }
    
    SdFile* f = new SdFile();
    int flags = O_RDONLY;
    if(mode & LV_FS_MODE_WR) {
        flags = O_WRONLY | O_CREAT;
    }
    
    if(!f->open(path, flags)) {
        Serial.printf("FEHLER: Datei %s konnte nicht geöffnet werden\n", path);
        delete f;
        return nullptr;
    }
    
    return f;
}



static lv_fs_res_t fs_close_cb(lv_fs_drv_t* drv, void* file_p)
{
    SdFile* f = static_cast<SdFile*>(file_p);
    if(f) {
        f->close();
        delete f;   // Speicher freigeben
    }
    return LV_FS_RES_OK;
}




static lv_fs_res_t fs_read_cb(lv_fs_drv_t* drv, void* file_p, void* buf, uint32_t btr, uint32_t* br)
{
    SdFile* f = static_cast<SdFile*>(file_p);
    int32_t n = f->read(buf, btr);
    if(n < 0) { *br = 0; return LV_FS_RES_FS_ERR; }
    *br = n;
    return LV_FS_RES_OK;
}

static lv_fs_res_t fs_write_cb(lv_fs_drv_t* drv, void* file_p, const void* buf, uint32_t btw, uint32_t* bw)
{
    SdFile* f = static_cast<SdFile*>(file_p);
    int32_t n = f->write(buf, btw);
    if(n < 0) { *bw = 0; return LV_FS_RES_FS_ERR; }
    *bw = n;
    return LV_FS_RES_OK;
}

static lv_fs_res_t fs_seek_cb(lv_fs_drv_t* drv, void* file_p, uint32_t pos, lv_fs_whence_t whence)
{
    SdFile* f = static_cast<SdFile*>(file_p);
    bool ok = false;
    if(whence == LV_FS_SEEK_SET) ok = f->seekSet(pos);
    else if(whence == LV_FS_SEEK_CUR) ok = f->seekCur(pos);
    else if(whence == LV_FS_SEEK_END) ok = f->seekEnd(pos);
    return ok ? LV_FS_RES_OK : LV_FS_RES_FS_ERR;
}

static lv_fs_res_t fs_tell_cb(lv_fs_drv_t* drv, void* file_p, uint32_t* pos)
{
    SdFile* f = static_cast<SdFile*>(file_p);
    *pos = f->curPosition();
    return LV_FS_RES_OK;
}

// ----------------- Init -----------------
void lv_fs_sdfat_init(void)
{
    static lv_fs_drv_t drv;
    lv_fs_drv_init(&drv);

    drv.letter     = 'S';
    drv.open_cb    = fs_open_cb;
    drv.close_cb   = fs_close_cb;
    drv.read_cb    = fs_read_cb;
    drv.write_cb   = fs_write_cb;
    drv.seek_cb    = fs_seek_cb;
    drv.tell_cb    = fs_tell_cb;
    drv.ready_cb   = nullptr;  // optional

    lv_fs_drv_register(&drv);
}

Blockquote
Lade Bild: S:/moon/test.png

Image Objekt Größe : width 50 height 50
PNG erfolgreich geladen! 28527x12142 CF=58
Image Widget Größe nach Laden: width 50 height 50
FEHLER: Datei /moon/test.png konnte nicht geöffnet werden
[Error] (0.000, +0) image_decoder_get_info: File open failed: 12 lv_image_decoder.c:368
[Warn] (0.000, +0) lv_draw_image: Couldn’t get info about the image lv_draw_image.c:117
[Error] (0.000, +0) image_decoder_get_info: File open failed: 1FEHLER: Datei /moon/test.png konnte nicht geöffnet werden
2 lv_image_decoder.c:368
[Warn] (0.000, +0) lv_draw_image: Couldn’t get info about the image lv_draw_image.c:117
FEHLER: Datei /moon/test.png konnte nicht geöffnet werden
[Error] (0.000, +0) image_decoder_get_info: File open failed: 12 lv_image_decoder.c:368
[Warn] (0.000, +0) lv_draw_image: Couldn’t get info about the image lv_draw_image.c:117
[Error] (0.000, +0) image_decoder_get_info: File open failed: 1FEHLER: Datei /moon/test.png konnte nicht geöffnet werden
2 lv_image_decoder.c:368
[Warn] (0.000, +0) lv_draw_image: Couldn’t get info about the image lv_draw_image.c:117
will see if someone is smarter then all the major KIs

And what? Seems you try open one file twice… Nothing about PNG and nothing about S3…