Utility for creating TFT graphics

Hi All,

My first post here, so I hope it is useful (although I might have had a login for this site a long time ago, I don't remember). I have used the Arduino in projects for years.

If anybody is interested, I have created a utility which will autogenerate code that can be pasted into the IDE from a source image file. A function is also provided to draw the image on an Elegoo TFT (maybe it will work on others - feedback please). The image is run length encoded to save space and is placed in the Flash memory.

Example:

const PROGMEM uint16_t water_image[] = {
22, //Image Width
19, //Image Height
 10 , 65535 , 3 , 0 , 9 , 65535 ,
 12 , 65535 , 1 , 0 , 9 , 65535 ,
 11 , 65535 , 2 , 0 , 9 , 65535 ,
 10 , 65535 , 2 , 0 , 10 , 65535 ,
 22 , 65535 ,
 10 , 65535 , 2 , 0 , 10 , 65535 ,
 22 , 65535 ,
 9 , 0 , 4 , 65535 , 9 , 0 ,
 1 , 0 , 20 , 65535 , 1 , 0 ,
 1 , 0 , 20 , 65535 , 1 , 0 ,
 1 , 0 , 20 , 65535 , 1 , 0 ,
 1 , 0 , 20 , 65535 , 1 , 0 ,
 1 , 0 , 3 , 65535 , 1 , 0 , 6 , 65535 , 1 , 0 , 5 , 65535 , 1 , 0 , 3 , 65535 , 1 , 0 ,
 1 , 0 , 2 , 65535 , 2 , 0 , 5 , 65535 , 2 , 0 , 5 , 65535 , 2 , 0 , 2 , 65535 , 1 , 0 ,
 6 , 0 , 3 , 65535 , 4 , 0 , 3 , 65535 , 6 , 0 ,
 22 , 0 ,
 22 , 0 ,
 22 , 0 ,
 22 , 0 ,
0,0};

void translateDraw(unsigned int cx, unsigned int cy, unsigned int repeat, uint16_t value)
{
  if (repeat>1)
  {
    tft.drawFastHLine(cx, cy,repeat, value);
  }
  else
  {
    tft.drawPixel(cx,cy,value);
  }
}

void rlebmpdraw(int x, int y, const uint16_t *sram_address, bool inverted)
{
    uint32_t time = millis();
    unsigned int cx,cy;
    
    cx=0;
    cy=0;

    unsigned int arrayIndex=0;

    unsigned int width = pgm_read_word(sram_address+arrayIndex++);
    unsigned int height = pgm_read_word(sram_address+arrayIndex++);
    
    uint16_t white = 0xffff;
    do 
    {
        unsigned int repeat = pgm_read_word(sram_address+arrayIndex);
        unsigned int value = pgm_read_word(sram_address+arrayIndex+1);
        if (repeat==0)
          break;
          
        unsigned int colour = value;
        while ((cx+repeat)>width) 
        {
          
          if (colour!=white)
          {
            translateDraw(x+cx, y+cy, width-cx, colour);
          }
          int newValue=repeat - (width-cx);
          if (newValue<0)
          {
            repeat = 0;
          }
          else
          {
            repeat = newValue;
          }
          cx=0;
          cy++;
          
        } 
        if (colour!=white)
        {
            translateDraw(x+cx, y+cy, repeat, colour);
        }
        cx+=repeat;
        if (cx>=width)
        {
          cx=0;
          cy++;
        }
        
        arrayIndex+=2;
    }while(1);
}

//Call with - rlebmpdraw(xpos, ypos,  water_image, false); //Draw at x,y
//It is expected that tft has been defined and initialised

In summary you select an image file (.jpg, .gif, .png) and it generates code for you.

You can then draw the image with a call to this function,

void rlebmpdraw(int x, int y, const uint16_t *sram_address, bool inverted)

e.g.

rlebmpdraw(20,10,water_image, false); //draw water image (not colour inverted) at 20, 10.

The link to the page with the software on it is here...

https://www.dragons-fire.co.uk/image-to-tft/

(I should mention it is Windows only currently but it is written in Qt, so if anyone wants to convert it... :smile: )

Thanks,

Taf.

1 Like

Then you should share the source code as well. Maybe someone will pick it up.

Your link just provides a ZIP with a SETUP.EXE and an MSI file.
No explanations, examples, videos, ...

I suggest that you provide some sort of reassurance. e.g. a sample JPG input picture, a sample RLE output code, the RLE drawing function.

Otherwise no-one will risk installing this sort of mystery project.

Yes, an Uno can manage a BMP image or a RAW image.
It does not have enough processing power for JPG, GIF, PNG
A RLE image file is much smaller than BMP or RAW pixels image file. And is easily drawn by a Uno.

David.

Thanks for your input david_prentice.

Yes, I thought that might be an issue. I will think about what else to post. I'll update the original post to include the full generated code. Maybe I can generate some images/videos showing what it does and it working on the TFT screen on the webpage.

I think the best place to put useful code is on GitHub. Then you can add, update and so on.

Jean-Marc

A utility to create small graphics image files would be very useful.
RLE works pretty well on many pictures. And is easy to decode.
Of course JPG, PNG, GIF are more efficient but require CPU power to decode.

RAW files seldom have any information header.
RLE files have no standardised format.

JPEG etc are documented and in worldwide use on multiple platforms.

You don't need to put your program source code into the public domain.
Just show that it works. e.g. with some examples.

David.

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