cancel
Showing results for 
Search instead for 
Did you mean: 

How would you go about retrieving a PNG image from a USB stick and displaying it on screen?

GMeur
Senior

Hello,

I would like to know if you could give me any tips about how to display with TouchGFX an image on screen that comes from a USB stick?

Thanks in advance for your help.

6 REPLIES 6
cameronf
Senior

I've used LodePNG for decoding PNG files and like it so far.

https://lodev.org/lodepng/

The sequence would be to read the PNG file off the USB stick, decode the file using LodePNG, then copy the decoded data into a dynamic bitmap.

The library uses quite a bit of dynamic memory as does temporarily storing the PNG data (depending on size of images) so you'll want to make sure your heap is large enough to handle it. In my case when doing this I place the heap in my larger external RAM.

Martin KJELDSEN
Chief III

@Community member​ is right. That's the procedure. We use something else for decoding pngs i think, and this is not something we're pushing officially. So, what me and Cameron have not done now is to load the pngs from a USB stick. But if you manage to do that we may be able to help you from there.

Let us know!

/Martin

cameronf
Senior

If you're having trouble reading the file off the USB stick, ST's example projects are a good resource. There should be a few USB examples for most dev kits. Specifically look for USB bootloader example projects for a reference project that reads files off a USB drive.

GMeur
Senior

Hej,

I'm back to this matter after a long break.

What I would like to know is how to copy the decoded data into my dynamic bitmap easily. Here's my current code:

if(f_open(&MyFile, "0:key.png", FA_READ) == FR_OK){
          
            uint16_t read;
            size_t png_size = f_size(&MyFile); 
                
            unsigned char* img = 0;
            unsigned width, height;
            unsigned error;
            unsigned char* png_file = (unsigned char*)malloc(png_size);
 
            if(png_file != NULL){
                res = f_read(&MyFile, png_file, png_size, (UINT*) &read);
            }
            
           error = lodepng_decode24(&img, &width, &height, png_file, png_size); 
           free(png_file);
            
            BitmapId bmpId = Bitmap::dynamicBitmapCreate(width, height, Bitmap::RGB888);
            
            
            //xQueueSend(bitmap_msg_q, &bmpId,0);
            f_close(&MyFile);
        }
       }

Thanks.

GMeur
Senior

Hej,

I'm back to this matter after a long break.

What I would like to know is how to copy the decoded data into my dynamic bitmap easily. Here's my current code:

if(f_open(&MyFile, "0:key.png", FA_READ) == FR_OK){
              
    uint16_t read;
    size_t png_size = f_size(&MyFile); 
                    
    unsigned char* img = 0;
    unsigned width, height;
    unsigned error;
    unsigned char* png_file = (unsigned char*)malloc(png_size);
     
    if(png_file != NULL){
          res = f_read(&MyFile, png_file, png_size, (UINT*) &read);
    }
                
   error = lodepng_decode24(&img, &width, &height, png_file, png_size); 
   free(png_file);
                
   BitmapId bmpId = Bitmap::dynamicBitmapCreate(width, height, Bitmap::RGB888);
                
                
  //xQueueSend(bitmap_msg_q, &bmpId,0);
  f_close(&MyFile);
}

Thanks.

Hi,

Bitmap has the following method:

const uint8_t* Bitmap::getData() const;

It will return a pointer to the data store for the Bitmap. You can just memcpy your data into this region (taking care only to copy exactly what's needed or risk overriding things in the bitmap cache).

/Martin