Adafruit library - loading a 565 tft raw file instead of bmp

Hi all,

I am using the adafruit library and while its performance, in general, is satisfactory loading of .bmp files is significantly slow.
I would like to modify the sketch below to load raw images. Therefore the conversion to the 16bit TFT format could be done previosuly, increasing rendering speed.

I use the sketch below to load images into the screen:

void bmpDraw(char *filename, int x, int y) {

  File     bmpFile;
  int      bmpWidth, bmpHeight;   // W+H in pixels
  uint8_t  bmpDepth;              // Bit depth (currently must be 24)
  uint32_t bmpImageoffset;        // Start of image data in file
  uint32_t rowSize;               // Not always = bmpWidth; may have padding
  uint8_t  sdbuffer[3 * BUFFPIXEL]; // pixel in buffer (R+G+B per pixel)
  uint16_t lcdbuffer[BUFFPIXEL];  // pixel out buffer (16-bit per pixel)
  uint8_t  buffidx = sizeof(sdbuffer); // Current position in sdbuffer
  boolean  goodBmp = false;       // Set to true on valid header parse
  boolean  flip    = true;        // BMP is stored bottom-to-top
  int      w, h, row, col;
  uint8_t  r, g, b;
  uint32_t pos = 0, startTime = millis();
  uint8_t  lcdidx = 0;
  boolean  first = true;

  if ((x >= tft.width()) || (y >= tft.height())) return;

  //Open requested file on SD card
  if ((bmpFile = SD.open(filename)) == NULL) {
    //Serial.println(F("File not found"));
    return;
  }

  // Parse BMP header
  if (read16(bmpFile) == 0x4D42) { // BMP signature
    Serial.println(F("File size: ")); Serial.println(read32(bmpFile));
    (void)read32(bmpFile); // Read & ignore creator bytes
    bmpImageoffset = read32(bmpFile); // Start of image data
    Serial.print(F("Image Offset: ")); Serial.println(bmpImageoffset, DEC);
    // Read DIB header
    Serial.print(F("Header size: ")); Serial.println(read32(bmpFile));
    bmpWidth  = read32(bmpFile);
    bmpHeight = read32(bmpFile);
    if (read16(bmpFile) == 1) { // # planes -- must be '1'
      bmpDepth = read16(bmpFile); // bits per pixel
      Serial.print(F("Bit Depth: ")); Serial.println(bmpDepth);
      if ((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed

        goodBmp = true; // Supported BMP format -- proceed!
        Serial.print(F("Image size: "));
        Serial.print(bmpWidth);
        Serial.print('x');
        Serial.println(bmpHeight);

        // BMP rows are padded (if needed) to 4-byte boundary
        rowSize = (bmpWidth * 3 + 3) & ~3;

        // If bmpHeight is negative, image is in top-down order.
        // This is not canon but has been observed in the wild.
        if (bmpHeight < 0) {
          bmpHeight = -bmpHeight;
          flip      = false;
        }

        // Crop area to be loaded
        w = bmpWidth;
        h = bmpHeight;
        if ((x + w - 1) >= tft.width())  w = tft.width()  - x;
        if ((y + h - 1) >= tft.height()) h = tft.height() - y;

        // Set TFT address window to clipped image bounds
        tft.setAddrWindow(x, y, x + w - 1, y + h - 1);

        for (row = 0; row < h; row++) { // For each scanline...
          // Seek to start of scan line.  It might seem labor-
          // intensive to be doing this on every line, but this
          // method covers a lot of gritty details like cropping
          // and scanline padding.  Also, the seek only takes
          // place if the file position actually needs to change
          // (avoids a lot of cluster math in SD library).
          if (flip) // Bitmap is stored bottom-to-top order (normal BMP)
            pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
          else     // Bitmap is stored top-to-bottom
            pos = bmpImageoffset + row * rowSize;
          if (bmpFile.position() != pos) { // Need seek?
            bmpFile.seek(pos);
            buffidx = sizeof(sdbuffer); // Force buffer reload
          }

          for (col = 0; col < w; col++) { // For each column...
            // Time to read more pixel data?
            if (buffidx >= sizeof(sdbuffer)) { // Indeed
              // Push LCD buffer to the display first
              if (lcdidx > 0) {
                tft.pushColors(lcdbuffer, lcdidx, first);
                lcdidx = 0;
                first  = false;
              }
              bmpFile.read(sdbuffer, sizeof(sdbuffer));
              buffidx = 0; // Set index to beginning
            }

            // Convert pixel from BMP to TFT format
            b = sdbuffer[buffidx++];
            g = sdbuffer[buffidx++];
            r = sdbuffer[buffidx++];
            lcdbuffer[lcdidx++] = tft.color565(r, g, b);
          } // end pixel
        } // end scanline
        // Write any remaining data to LCD
        if (lcdidx > 0) {
          tft.pushColors(lcdbuffer, lcdidx, first);
        }
        Serial.print(F("Loaded in "));
        Serial.print(millis() - startTime);
        Serial.println(" ms");
      } // end goodBmp
    }
  }

  bmpFile.close();
  if (!goodBmp) Serial.println(F("BMP format not recognized."));
}


// These read 16- and 32-bit types from the SD card file.
// BMP data is stored little-endian, Arduino is little-endian too.
// May need to reverse subscript order if porting elsewhere.

uint16_t read16(File f) {
  uint16_t result;
  ((uint8_t *)&result)[0] = f.read(); // LSB
  ((uint8_t *)&result)[1] = f.read(); // MSB
  return result;
}

uint32_t read32(File f) {
  uint32_t result;
  ((uint8_t *)&result)[0] = f.read(); // LSB
  ((uint8_t *)&result)[1] = f.read();
  ((uint8_t *)&result)[2] = f.read();
  ((uint8_t *)&result)[3] = f.read(); // MSB
  return result;
}

Grateful for any help.

You might try to reuse some of the parts from ILI934_due library:

  1. Convert 24bit bmp images with BMP24toILI565 tool from ILI9341_due library tools folder to 565 format and copy them to SD card
  2. Use SdFatTftBitmap sketch from ILI9341_due library examples. You’ll need SdFat library (GitHub - greiman/SdFat-beta: Beta SdFat for test of new features)
  3. In SdFatTftBitmap.ino
    3a. remove all includes that start with ILI…and add the adafruit ones
    3b. replace
    ILI9341_due tft(LCD_CS, LCD_DC);
    with
    Adafruit_ILI9341 tft(LCD_CS, LCD_DC);
    3c. modify CS and DC pins for TFT and SD card to the ones you use
    3d. modify the image names in loop()
  4. Add this function to Adafruit_ILI9341.cpp (and a function declaration to Adafruit_ILI9341.h)
void Adafruit_ILI9341::pushColors565(uint8_t *colors, uint16_t offset, uint16_t len) {
  if (hwSPI) spi_begin();
  *dcport |=  dcpinmask;
  *csport &= ~cspinmask;

  colors = colors + offset;

  for (uint16_t i = 0; i < len; i++) {
		spiwrite(colors[i]);
	}

  *csport |= cspinmask;
  if (hwSPI) spi_end();
}
  1. Hopefully it’ll compile and work.

And for pushColors?

In your original sketch you are already calling pushColors so I assume you have it available in the (modified) Adafruit library?
This is what I have found:

void pushColors(uint16_t *colors, uint8_t offset, uint8_t len) {
  *rsport |=  rspinmask;
  *csport &= ~cspinmask;
  
  colors = colors + offset*2;
  for (uint8_t i = 0; i < len; ++i) {
    uint16_t color = *colors;
    spiwrite(color >> 8);
    spiwrite(color);
    colors++;
  }

  *csport |= cspinmask;

MarekB, first of all, many thanks for sparing some time to look at this.

I totally missed this post, so I' need to see if I find some free time to get it working.

My modified library loads a 150*150px in 890ms, lets see how much we can improve things :wink:

Hi, It's a few days since the last post, but I'm looking for something similar so this sounds like a good start:
I have a ILI9341 TFT with Atmega1284p and a modified Adafruid Libary (from casemod here). Works perfect!
Now I want to display small (50x50px) Icons on the display without SD (loading from progmem). I have the Icon as .c file, converted by Henning Karlson (UTFT) for an other TFT. I'm just stucked to rewrite drawBitmap to be able to use my raw data images...

Hope you have a solution. :wink:

Hey, here is a small extension for Casemod ILI9341 libarie and maybe also for other Adafruid_GFX:

first open Adafruit_GFX.h and add new funktion:

    drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap,
      int16_t w, int16_t h, uint16_t color),      // Original by Adafruit
 drawBitmap(int16_t x, int16_t y, int16_t w, int16_t h, unsigned int* data),

After this edit Adafruit_GFX.cpp and add the new function:

void Adafruit_GFX::drawBitmap(int16_t x, int16_t y, int16_t w, int16_t h, unsigned int* data){
 unsigned int col;
 int tx, ty,tc;
 for (ty=0; ty<h; ty++){   
 for (tx=0; tx<w; tx++){ 
 tc = (ty*w)+tx;
 col=pgm_read_word(&data[tc]);
 drawPixel(x+tx,y+ty,col);
 }
 }
}

Now you can use it in your sketch. For this first add a new tab and call it “tux.c”:

// Generated by  : ImageConverter 565 v1.0
// Generated from: tux.png
// Time generated: 11.10.2010 22:51:32
// Size          : 2 048 Bytes

#include <avr/pgmspace.h>

const unsigned short tux[0x400] PROGMEM ={

SEE ATTACHED FILE FOR CODE!!!

}

after this go to main.ino and add:

extern unsigned int tux[0x400];

Now you can use:

tft.drawBitmap (287, 97, 32, 32, tux);

Example is taken from Henning Karlson (UTFT) and used his Image converter to do so.

Hope it helps somebody…

tux.c (198 KB)

You might like to have a look as my instructable.

These adapted Adafruit libraries and the sketch will load a 150 x 150 pixel 565 raw image from SD card and display it in 194ms using an UNO.

Use IDE 1.6.x and compiler option -O2 as in this Instructable. The standard -Os option will be slower at maybe 250ms to pull off SD and display.

A 50 x 50 pixel may take around 20ms but I have not tried that.

I suspect here is little room for improvement on this for an UNO...