Demonstration of the Bitmap rendering - reads BMP and JPG files in many format variants and renders to the screen.

Dependencies:   FlashFileSystem mbed RA8875

Bitmap and JPEG Demo

This demo publishes each graphic file from a list of files to the display. The graphic files can be BMP, JPG, or ICO. On the terminal it displays the filename and the return-code (in case of an error). This demo works as intended on the 480x272 display in either 8 or 16 bit per pixel mode and it should work on larger displays.

Items of interest to change and rebuild/test:

// Not all systems have the LocalFileSystem. Plug in the library and driver for yours.
LocalFileSystem local("local");             // Image source

TestImage_T TestImage[] = {
    { 0, 0, "/local/01601602.bmp"},
    { 0, 0, "/local/48027202.bmp"},
    { 0, 0, "/local/48027204.bmp"},
    { 0, 0, "/local/48027208.bmp"},
    { 0, 0, "/local/48027224.bmp"},
    { 0, 0, "/local/p480272.jpg"},
    { 0, 0, "/local/p480272.bmp"}
};

// deefine your screen size and color depth. 
#define SCREEN_W 480
#define SCREEN_H 272
#define SCREEN_BPP 16

LocalFileSystem may need to be altered to SDFileSystem (for example) and the corresponding paths in the TestImage array.

Also, the Screen size and bit per pixel color depth.

Images

The following image files are saved in different resolution and color depth. The first, and smallest, was simply for developing the monochrome support. Others are 4-bit color, 8-bit color, 24-bit color, and a mislabeled 1-bit color (shown as 02).

/media/uploads/WiredHome/testimages.zip

main.cpp

Committer:
WiredHome
Date:
2016-08-06
Revision:
15:f33dcd24d8d1
Parent:
14:694822610507
Child:
16:987892bebe05

File content as of revision 15:f33dcd24d8d1:

//      ______________  ______________  ______________  _______________
//     /_____   _____/ /  ___________/ /  ___________/ /_____   ______/
//          /  /      /  /            /  /                  /  /
//         /  /      /  /___         /  /__________        /  /
//        /  /      /  ____/        /__________   /       /  /
//       /  /      /  /                       /  /       /  /
//      /  /      /  /__________  ___________/  /       /  /
//     /__/      /_____________/ /_____________/       /__/
//

#include "mbed.h"           // v122
#include "RA8875.h"         // v125
#include "WebColors.h"
#include <algorithm>

//#define JPEG_TEST


// These two defines can be enabled, or commented out
//#define BIG_SCREEN
//#define CAP_TOUCH
#define LCD_C 16         // color - bits per pixel

#ifdef CAP_TOUCH
RA8875 lcd(p5, p6, p7, p12, NC, p9,p10,p13, "tft"); // MOSI,MISO,SCK,/ChipSelect,/reset, SDA,SCL,/IRQ, name
#else
RA8875 lcd(p5, p6, p7, p12, NC, "tft");             //MOSI, MISO, SCK, /ChipSelect, /reset, name
LocalFileSystem local("local");                     // access to calibration file for resistive touch.
#endif

#ifdef BIG_SCREEN
    #define LCD_W 800
    #define LCD_H 480
#else
    #define LCD_W 480
    #define LCD_H 272
#endif



//#define DEBUG "Main"
// ...
// INFO("Stuff to show %d", var); // new-line is automatically appended
//
#if (defined(DEBUG) && !defined(TARGET_LPC11U24))
#define INFO(x, ...) std::printf("[INF %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
#define WARN(x, ...) std::printf("[WRN %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
#define ERR(x, ...)  std::printf("[ERR %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
static void HexDump(const char * title, const uint8_t * p, int count)
{
    int i;
    char buf[100] = "0000: ";

    if (*title)
        INFO("%s", title);
    for (i=0; i<count; ) {
        sprintf(buf + strlen(buf), "%02X ", *(p+i));
        if ((++i & 0x0F) == 0x00) {
            INFO("%s", buf);
            if (i < count)
                sprintf(buf, "%04X: ", i);
            else
                buf[0] = '\0';
        }
    }
    if (strlen(buf))
        INFO("%s", buf);
}
#else
#define INFO(x, ...)
#define WARN(x, ...)
#define ERR(x, ...)
#define HexDump(a, b, c)
#endif

Serial pc(USBTX, USBRX);
extern "C" void mbed_reset();


typedef struct {
    loc_t x;
    loc_t y;
    char * filename;
} TestImage_T;

TestImage_T TestImage[] = {
    { 0, 0, "/local/01601602.bmp"},
    { 0, 0, "/local/48027202.bmp"},
    { 0, 0, "/local/48027204.bmp"},
    { 0, 0, "/local/48027208.bmp"},
    { 0, 0, "/local/48027224.bmp"},
    { 0, 0, "/local/p480272.jpg"},
    { 0, 0, "/local/p480272.bmp"}
};


#ifdef JPEG_TEST
//#include "tjpgd.h"
//JDEC jdec;
//#define JPEG_WORK_SPACE_SIZE 3100
//uint16_t work[JPEG_WORK_SPACE_SIZE/sizeof(uint16_t)];
//FILE *fh;

uint16_t jpeg_input_func(JDEC *jd, uint8_t *buff, uint16_t ndata)
{
    //INFO("Read in %p count %d", buff, ndata);
    if (buff) {
        size_t n = fread(buff, 1, ndata, (FILE *)jd->device);
        //INFO("fread returned %d", n);
        //HexDump("buf", buff, (ndata > 32) ? 32 : ndata);
        return n == (size_t)-1 ? 0 : n;
    } else {
        off_t t = fseek((FILE *)jd->device, ndata, SEEK_CUR);
        //INFO("Seek returned %d", t);
        return t == (off_t)-1 ? 0 : ndata;
    }
}

uint16_t jpeg_output_func(JDEC *jd, void *bitmap, JRECT *rect)
{
    #if JD_FORMAT == 1
    uint16_t *src = (uint16_t *)bitmap;
    #else
    uint8_t *src = (uint8_t *)bitmap;
    #endif
    int x0 = rect->left;
    int x1 = rect->right;
    int y0 = rect->top;
    int y1 = rect->bottom;
 
    if (y0 >= lcd.height() || x0 >= lcd.width())
        return 1;
 
    if (x1 > lcd.width()-1) x1 = lcd.width() - 1;
    if (y1 > lcd.height()-1) y1 = lcd.height() - 1;
 
    if (x0 == 0)
        printf("\r %d", y0);
 
    int w = x1 - x0 + 1;
    for (int y= y0; y <= y1; y++) {
        lcd.SetGraphicsCursor(x0, y);
        lcd._StartGraphicsStream();
        #if JD_FORMAT == 1
        uint16_t *p = src + w * (y - y0);
        #else
        uint8_t *p = src + 3 * (w * (y - y0));
        #endif
        for (int x=x0; x <= x1; x++) {
            #if JD_FORMAT == 1
            lcd._putp(*p++);
            #else
            lcd._putp(RGB(*p, *(p+1), *(p+2)));
            p += 3;
            #endif
        }
        lcd._EndGraphicsStream();
    }
    return 1;
}
#endif


int main()
{
    pc.baud(460800);    // I like a snappy terminal, so crank it up!
    pc.printf("\r\nRA8875 Bitmap Test - Build " __DATE__ " " __TIME__ "\r\n");

    lcd.init(LCD_W, LCD_H, LCD_C);
    lcd.Backlight(0.5f);
    
    int count = sizeof(TestImage)/sizeof(TestImage[0]);
    while (1) {
        for (int i=0; i<count; i++) {
            lcd.cls();
            printf("RenderBitmapFile(%d, %d, %s) ", TestImage[i].x, TestImage[i].y, TestImage[i].filename);
            RetCode_t r = lcd.RenderImageFile(TestImage[i].x, TestImage[i].y, TestImage[i].filename);
            printf("returned %d\r\n", r);
            wait(5);
        }
    }
}