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-05-17
Revision:
11:0c2ff405c1c1
Parent:
1:84fb3e9adaaf

File content as of revision 11:0c2ff405c1c1:

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

#include "mbed.h"           // v118
#include "RA8875.h"         // v111
#include "WebColors.h"
#include <algorithm>

#define JPEG_TEST

#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

RA8875 lcd(p5, p6, p7, p12, NC, "tft");     // MOSI, MISO, SCK, /ChipSelect, /reset, name
Serial pc(USBTX, USBRX);
extern "C" void mbed_reset();


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

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

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

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

#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, fh);
        INFO("fread returned %d of %d", n, ndata);
        HexDump("buf", buff, (ndata > 32) ? 32 : ndata);
        return n == (size_t)-1 ? 0 : n;
    } else {
        off_t t = fseek(fh, 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(SCREEN_W, SCREEN_H, SCREEN_BPP);

#ifdef JPEG_TEST
    const char * path = "/local/testimg.jpg";
    fh = fopen(path, "rb");
    if (fh == NULL) error("%s not found.", path);

    JRESULT r = jd_prepare(&jdec, jpeg_input_func, work, JPEG_WORK_SPACE_SIZE, fh);
    if(r != JDR_OK) error("jd_prepare error:%d", r);
    r = jd_decomp(&jdec, jpeg_output_func, 0);
    fclose(fh);
    if( r != JDR_OK) error("jd_decomp error:%d", r);    
    pc.printf("done.\r\n");
    while (1)
        ;
#else
    int count = sizeof(TestImage)/sizeof(TestImage[0]);
    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.RenderBitmapFile(TestImage[i].x, TestImage[i].y, TestImage[i].filename);
        printf("returned %d\r\n", r);
        wait(5);
    }
#endif    
}