Pngdec not working

hi,
i want show in my display the png image store in sd card, i used PNGDEC but not working not show image call seek and stop

File dir = SD.open("/");
  while (true) {
    File entry = dir.openNextFile();
    if (!entry) break;
    if (entry.isDirectory() == false) {
      const char *name = entry.name();
      const int len = strlen(name);
      if (len > 3 && strcmp(name + len - 3, "png") == 0) {
        ///Serial.print("File: ");
        //Serial.println(name);
        filename = name;
      }      



int16_t rc = png.open(strname.c_str(), myOpen, myClose, myRead, mySeek, pngDraw);


if (rc == PNG_SUCCESS) {
        tft.startWrite();
        Serial.printf("image specs: (%d x %d), %d bpp, pixel type: %d\n", png.getWidth(), png.getHeight(), png.getBpp(), png.getPixelType());
        uint32_t dt = millis();
        if (png.getWidth() > MAX_IMAGE_WDITH) {
          Serial.println("Image too wide for allocated lin buffer!");
        }
        else {
          rc = png.decode(NULL, 0);
          png.close();
        }
        tft.endWrite();
        // How long did rendering take...
        Serial.print(millis()-dt); Serial.println("ms");
      }


this is log:

 Using the PNGdec library
SD Card Type: SDHC
SD Card Size: 14988MB
close file
image.png
Attempting to open /image.png
read
0
success file
image specs: (200 x 200), 8 bpp, pixel type: 3
ror seek file

Welcome to the forum

Please post a complete sketch that illustrates the problem rather than just a snippet of one

You left out the part of the sketch where you set 'strname' so I don't know if that is a mistake or not.

thank you sorry, this is my complete code is example tft_espi


#include <SD.h>

// Include the PNG decoder library
#include <PNGdec.h>

PNG png; // PNG decoder inatance

#define MAX_IMAGE_WDITH 256 // Adjust for your images

int16_t xpos = 0;
int16_t ypos = 0;

// Include the TFT library https://github.com/Bodmer/TFT_eSPI
#include "SPI.h"
#include <TFT_eSPI.h>              // Hardware-specific library
TFT_eSPI tft = TFT_eSPI();         // Invoke custom library

// Functions to access a file on the SD card
File myfile;

const char *filename;



//====================================================================================
//                                    Setup
//====================================================================================
void setup()
{
  Serial.begin(115200);
  Serial.println("\n\n Using the PNGdec library");


  // Set all chip selects high to avoid bus contention during initialisation of each peripheral
  digitalWrite(22, HIGH); // Touch controller chip select (if used)
  digitalWrite(15, HIGH); // TFT screen chip select
  digitalWrite( 5, HIGH); // SD card chips select, must use GPIO 5 (ESP32 SS)

  // Initialise the TFT
  tft.begin();
  tft.fillScreen(TFT_BLACK);

  if (!SD.begin()) {
    Serial.println("Card Mount Failed");
    return;
  }
  uint8_t cardType = SD.cardType();

  if (cardType == CARD_NONE) {
    Serial.println("No SD card attached");
    return;
  }

  Serial.print("SD Card Type: ");
  if (cardType == CARD_MMC) {
    Serial.println("MMC");
  } else if (cardType == CARD_SD) {
    Serial.println("SDSC");
  } else if (cardType == CARD_SDHC) {
    Serial.println("SDHC");
  } else {
    Serial.println("UNKNOWN");
  }

  uint64_t cardSize = SD.cardSize() / (1024 * 1024);
  Serial.printf("SD Card Size: %lluMB\n", cardSize);


  File dir = SD.open("/");
  while (true) {
    File entry = dir.openNextFile();
    if (!entry) break;
    if (entry.isDirectory() == false) {
      const char *name = entry.name();
      const int len = strlen(name);
      if (len > 3 && strcmp(name + len - 3, "png") == 0) {
        ///Serial.print("File: ");
        //Serial.println(name);
        filename = name;
      }      
      // close the file:
      entry.close();
      Serial.println("close file");
      
      }
    }
  Serial.println(filename);
  String strname = filename;
  strname = "/" + strname;
  int16_t rc = png.open(strname.c_str(), myOpen, myClose, myRead, mySeek, pngDraw);
  Serial.println(rc);
  if(rc == PNG_INVALID_FILE){
    Serial.println("invalid file");
  }
  if(rc == PNG_SUCCESS) {
    Serial.println("success file");
  }
  if (rc == PNG_SUCCESS) {
        tft.startWrite();
        Serial.printf("image specs: (%d x %d), %d bpp, pixel type: %d\n", png.getWidth(), png.getHeight(), png.getBpp(), png.getPixelType());
        uint32_t dt = millis();
        if (png.getWidth() > MAX_IMAGE_WDITH) {
          Serial.println("Image too wide for allocated lin buffer!");
        }
        else {
          rc = png.decode(NULL, 0);
          png.close();
        }
        tft.endWrite();
        // How long did rendering take...
        Serial.print(millis()-dt); Serial.println("ms");
      }

  Serial.println("\r\nInitialisation done.");
}

//====================================================================================
//                                    Loop
//====================================================================================
void loop()
{
  
}




void * myOpen(const char *filename, int32_t *size) {
  Serial.printf("Attempting to open %s\n", filename);
  myfile = SD.open(filename);
  if (!myfile) {
   Serial.println("Failed to open file");
 }
  *size = myfile.size();
  return &myfile;
}
void myClose(void *handle) {
  Serial.println("close");
  if (myfile) myfile.close();
}
int32_t myRead(PNGFILE *handle, uint8_t *buffer, int32_t length) {
  Serial.println("read");
  if (!myfile) return 0;
  return myfile.read(buffer, length);
}
int32_t mySeek(PNGFILE *handle, int32_t position) {
  Serial.println("seek:"+ position);
  if (!myfile)
  {
    Serial.println("Error seek file");
    return 0;
  }
  return myfile.seek(position);
}

//=========================================v==========================================
//                                      pngDraw
//====================================================================================
// This next function will be called during decoding of the png file to
// render each image line to the TFT.  If you use a different TFT library
// you will need to adapt this function to suit.
// Callback function to draw pixels to the display
void pngDraw1(PNGDRAW *pDraw) {
  Serial.println("pDraw");
  uint16_t usPixels[png.getWidth()];  
  uint16_t lineBuffer[MAX_IMAGE_WDITH];
  png.getLineAsRGB565(pDraw, usPixels, PNG_RGB565_BIG_ENDIAN, 0xffffffff);
  tft.pushImage(0, 0 + pDraw->y, pDraw->iWidth, 1, usPixels);
}

void pngDraw(PNGDRAW *pDraw) {
  uint16_t lineBuffer[MAX_IMAGE_WDITH];
  static uint16_t dmaBuffer[MAX_IMAGE_WDITH]; // static so buffer persists after fn exit

  png.getLineAsRGB565(pDraw, lineBuffer, PNG_RGB565_BIG_ENDIAN, 0xffffffff);
  tft.pushImageDMA(xpos, ypos + pDraw->y, pDraw->iWidth, 1, lineBuffer, dmaBuffer);
}


Addition does not work on string literals. Try:

  Serial.print("seek:");
  Serial.println(position);

hello,
i modify the seek code but not showing image display, this is the log

Using the PNGdec library
SD Card Type: SDHC
SD Card Size: 14988MB
close file
image.png
Attempting to open /image.png
read
0
success file
image specs: (200 x 200), 8 bpp, pixel type: 3
seek:8

if change this code

int16_t rc = png.open(strname.c_str(), myOpen, myClose, myRead, mySeek, pngDraw);

in

int16_t rc = png.open(panda, myOpen, myClose, myRead, mySeek, pngDraw);

and panda is include "panda.h" an c image work

Your complete sketch (above) does not have a variable named 'panda'. Please show your new sketch.

this is the example
https://github.com/Bodmer/TFT_eSPI/blob/master/examples/PNG%20Images/Flash_PNG/Flash_PNG.ino

the panda file is a .h file

// array size is 182750
static const unsigned char panda[] PROGMEM  = {
  0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 
  0x00, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x01, 0x40, 0x08, 0x06, 0x00, 0x00, 0x00, 0x82, 0xe8, 0xf1, 
  0x53, 0x00, 0x00, 0x00, 0x01, 0x73, 0x52, 0x47, 0x42, 0x00, 0xae, 0xce, 0x1c, 0xe9, 0x00, 0x00, 
  0x00, 0x04, 0x67, 0x41, 0x4d, 0x41, 0x00, 0x00, 0xb1, 0x8f, 0x0b, 0xfc, 0x61, 0x05, 0x00, 0x00, 
  0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x1b, 0xae, 0x00, 0x00, 0x1b, 0xae, 0x01, 0x8c... etc etc
};

and complete sketch is

#include <SD.h>
#include "panda.h"

// Include the PNG decoder library
#include <PNGdec.h>

PNG png; // PNG decoder inatance

#define MAX_IMAGE_WDITH 256 // Adjust for your images

int16_t xpos = 0;
int16_t ypos = 0;

// Include the TFT library https://github.com/Bodmer/TFT_eSPI
#include "SPI.h"
#include <TFT_eSPI.h>              // Hardware-specific library
TFT_eSPI tft = TFT_eSPI();         // Invoke custom library

// Functions to access a file on the SD card
File myfile;

const char *filename;



//====================================================================================
//                                    Setup
//====================================================================================
void setup()
{
  Serial.begin(115200);
  Serial.println("\n\n Using the PNGdec library");


  // Set all chip selects high to avoid bus contention during initialisation of each peripheral
  digitalWrite(22, HIGH); // Touch controller chip select (if used)
  digitalWrite(15, HIGH); // TFT screen chip select
  digitalWrite( 5, HIGH); // SD card chips select, must use GPIO 5 (ESP32 SS)

  // Initialise the TFT
  tft.begin();
  tft.fillScreen(TFT_BLACK);

  if (!SD.begin()) {
    Serial.println("Card Mount Failed");
    return;
  }
  uint8_t cardType = SD.cardType();

  if (cardType == CARD_NONE) {
    Serial.println("No SD card attached");
    return;
  }

  Serial.print("SD Card Type: ");
  if (cardType == CARD_MMC) {
    Serial.println("MMC");
  } else if (cardType == CARD_SD) {
    Serial.println("SDSC");
  } else if (cardType == CARD_SDHC) {
    Serial.println("SDHC");
  } else {
    Serial.println("UNKNOWN");
  }

  uint64_t cardSize = SD.cardSize() / (1024 * 1024);
  Serial.printf("SD Card Size: %lluMB\n", cardSize);


  File dir = SD.open("/");
  while (true) {
    File entry = dir.openNextFile();
    if (!entry) break;
    if (entry.isDirectory() == false) {
      const char *name = entry.name();
      const int len = strlen(name);
      if (len > 3 && strcmp(name + len - 3, "png") == 0) {
        ///Serial.print("File: ");
        //Serial.println(name);
        filename = name;
      }      
      // close the file:
      entry.close();
      Serial.println("close file");
      
      }
    }
  Serial.println(filename);
  String strname = filename;
  strname = "/" + strname;
  //int16_t rc = png.open(strname.c_str(), myOpen, myClose, myRead, mySeek, pngDraw);
  int16_t rc = png.openFLASH((uint8_t *)panda, sizeof(panda), pngDraw);
  Serial.println(rc);
  if(rc == PNG_INVALID_FILE){
    Serial.println("invalid file");
  }
  if(rc == PNG_SUCCESS) {
    Serial.println("success file");
  }
  if (rc == PNG_SUCCESS) {
        tft.startWrite();
        Serial.printf("image specs: (%d x %d), %d bpp, pixel type: %d\n", png.getWidth(), png.getHeight(), png.getBpp(), png.getPixelType());
        uint32_t dt = millis();
        if (png.getWidth() > MAX_IMAGE_WDITH) {
          Serial.println("Image too wide for allocated lin buffer!");
        }
        else {
          rc = png.decode(NULL, 0);
          png.close();
        }
        tft.endWrite();
        // How long did rendering take...
        Serial.print(millis()-dt); Serial.println("ms");
      }

  Serial.println("\r\nInitialisation done.");
}

//====================================================================================
//                                    Loop
//====================================================================================
void loop()
{
  
}




void * myOpen(const char *filename, int32_t *size) {
  Serial.printf("Attempting to open %s\n", filename);
  myfile = SD.open(filename);
  if (!myfile) {
   Serial.println("Failed to open file");
 }
  *size = myfile.size();
  return &myfile;
}
void myClose(void *handle) {
  Serial.println("close");
  if (myfile) myfile.close();
}
int32_t myRead(PNGFILE *handle, uint8_t *buffer, int32_t length) {
  Serial.println("read");
  if (!myfile) return 0;
  return myfile.read(buffer, length);
}
int32_t mySeek(PNGFILE *handle, int32_t position) {
  Serial.println("seek:"+ position);
  if (!myfile)
  {
    Serial.println("Error seek file");
    return 0;
  }
  return myfile.seek(position);
}

//=========================================v==========================================
//                                      pngDraw
//====================================================================================
// This next function will be called during decoding of the png file to
// render each image line to the TFT.  If you use a different TFT library
// you will need to adapt this function to suit.
// Callback function to draw pixels to the display
void pngDraw1(PNGDRAW *pDraw) {
  Serial.println("pDraw");
  uint16_t usPixels[png.getWidth()];  
  uint16_t lineBuffer[MAX_IMAGE_WDITH];
  png.getLineAsRGB565(pDraw, usPixels, PNG_RGB565_BIG_ENDIAN, 0xffffffff);
  tft.pushImage(0, 0 + pDraw->y, pDraw->iWidth, 1, usPixels);
}

void pngDraw(PNGDRAW *pDraw) {
  uint16_t lineBuffer[MAX_IMAGE_WDITH];
  static uint16_t dmaBuffer[MAX_IMAGE_WDITH]; // static so buffer persists after fn exit

  png.getLineAsRGB565(pDraw, lineBuffer, PNG_RGB565_BIG_ENDIAN, 0xffffffff);
  tft.pushImageDMA(xpos, ypos + pDraw->y, pDraw->iWidth, 1, lineBuffer, dmaBuffer);
}

That's the wrong example. FLASH is not a file system so it doesn't have files you can open. You should be starting with: https://github.com/Bodmer/TFT_eSPI/blob/master/examples/PNG%20Images/LittleFS_PNG/LittleFS_PNG.ino

Then you should only have to replace "LittleFS" (file system in FLASH) with the functions for a file system on an SD card.

notthing not work .. littlefs not read sdcard :frowning:

do you know another library to show pmg on display

Why would you think it could read/write from/to an SD card ?

Little FS is not written to write to SD cards. Post#8 does not show the use of LitteFS, post the latest code.

Which MCU is being used?

Note the Runs on any MCU with at least 48K of free RAM?

When you ran the example code included with the pngc library, did it work?

That is why I said:

@Idahowalker idahowalker did you succeed now ? I am trying to achieve something very similar and seem to fail at same point.
@Johnwasser : can you give a little exmaple how to "replace "LittleFS" (file system in FLASH) with the functions for a file system on an SD card." that is not obvious to me

Regards

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.