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

Committer:
WiredHome
Date:
Sat Aug 06 14:50:25 2016 +0000
Revision:
15:f33dcd24d8d1
Parent:
14:694822610507
Child:
16:987892bebe05
Update to latest libraries and test with both 480x272 and 800x480 screens.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 0:9ecf49d75058 1 // ______________ ______________ ______________ _______________
WiredHome 0:9ecf49d75058 2 // /_____ _____/ / ___________/ / ___________/ /_____ ______/
WiredHome 0:9ecf49d75058 3 // / / / / / / / /
WiredHome 0:9ecf49d75058 4 // / / / /___ / /__________ / /
WiredHome 0:9ecf49d75058 5 // / / / ____/ /__________ / / /
WiredHome 0:9ecf49d75058 6 // / / / / / / / /
WiredHome 0:9ecf49d75058 7 // / / / /__________ ___________/ / / /
WiredHome 0:9ecf49d75058 8 // /__/ /_____________/ /_____________/ /__/
WiredHome 0:9ecf49d75058 9 //
WiredHome 0:9ecf49d75058 10
WiredHome 14:694822610507 11 #include "mbed.h" // v122
WiredHome 14:694822610507 12 #include "RA8875.h" // v125
WiredHome 0:9ecf49d75058 13 #include "WebColors.h"
WiredHome 0:9ecf49d75058 14 #include <algorithm>
WiredHome 0:9ecf49d75058 15
WiredHome 15:f33dcd24d8d1 16 //#define JPEG_TEST
WiredHome 15:f33dcd24d8d1 17
WiredHome 15:f33dcd24d8d1 18
WiredHome 15:f33dcd24d8d1 19 // These two defines can be enabled, or commented out
WiredHome 15:f33dcd24d8d1 20 //#define BIG_SCREEN
WiredHome 15:f33dcd24d8d1 21 //#define CAP_TOUCH
WiredHome 15:f33dcd24d8d1 22 #define LCD_C 16 // color - bits per pixel
WiredHome 15:f33dcd24d8d1 23
WiredHome 15:f33dcd24d8d1 24 #ifdef CAP_TOUCH
WiredHome 15:f33dcd24d8d1 25 RA8875 lcd(p5, p6, p7, p12, NC, p9,p10,p13, "tft"); // MOSI,MISO,SCK,/ChipSelect,/reset, SDA,SCL,/IRQ, name
WiredHome 15:f33dcd24d8d1 26 #else
WiredHome 15:f33dcd24d8d1 27 RA8875 lcd(p5, p6, p7, p12, NC, "tft"); //MOSI, MISO, SCK, /ChipSelect, /reset, name
WiredHome 15:f33dcd24d8d1 28 LocalFileSystem local("local"); // access to calibration file for resistive touch.
WiredHome 15:f33dcd24d8d1 29 #endif
WiredHome 15:f33dcd24d8d1 30
WiredHome 15:f33dcd24d8d1 31 #ifdef BIG_SCREEN
WiredHome 15:f33dcd24d8d1 32 #define LCD_W 800
WiredHome 15:f33dcd24d8d1 33 #define LCD_H 480
WiredHome 15:f33dcd24d8d1 34 #else
WiredHome 15:f33dcd24d8d1 35 #define LCD_W 480
WiredHome 15:f33dcd24d8d1 36 #define LCD_H 272
WiredHome 15:f33dcd24d8d1 37 #endif
WiredHome 15:f33dcd24d8d1 38
WiredHome 15:f33dcd24d8d1 39
WiredHome 1:84fb3e9adaaf 40
WiredHome 12:93adb3470150 41 //#define DEBUG "Main"
WiredHome 1:84fb3e9adaaf 42 // ...
WiredHome 1:84fb3e9adaaf 43 // INFO("Stuff to show %d", var); // new-line is automatically appended
WiredHome 1:84fb3e9adaaf 44 //
WiredHome 1:84fb3e9adaaf 45 #if (defined(DEBUG) && !defined(TARGET_LPC11U24))
WiredHome 1:84fb3e9adaaf 46 #define INFO(x, ...) std::printf("[INF %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
WiredHome 1:84fb3e9adaaf 47 #define WARN(x, ...) std::printf("[WRN %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
WiredHome 1:84fb3e9adaaf 48 #define ERR(x, ...) std::printf("[ERR %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
WiredHome 1:84fb3e9adaaf 49 static void HexDump(const char * title, const uint8_t * p, int count)
WiredHome 1:84fb3e9adaaf 50 {
WiredHome 1:84fb3e9adaaf 51 int i;
WiredHome 1:84fb3e9adaaf 52 char buf[100] = "0000: ";
WiredHome 1:84fb3e9adaaf 53
WiredHome 1:84fb3e9adaaf 54 if (*title)
WiredHome 1:84fb3e9adaaf 55 INFO("%s", title);
WiredHome 1:84fb3e9adaaf 56 for (i=0; i<count; ) {
WiredHome 1:84fb3e9adaaf 57 sprintf(buf + strlen(buf), "%02X ", *(p+i));
WiredHome 1:84fb3e9adaaf 58 if ((++i & 0x0F) == 0x00) {
WiredHome 1:84fb3e9adaaf 59 INFO("%s", buf);
WiredHome 1:84fb3e9adaaf 60 if (i < count)
WiredHome 1:84fb3e9adaaf 61 sprintf(buf, "%04X: ", i);
WiredHome 1:84fb3e9adaaf 62 else
WiredHome 1:84fb3e9adaaf 63 buf[0] = '\0';
WiredHome 1:84fb3e9adaaf 64 }
WiredHome 1:84fb3e9adaaf 65 }
WiredHome 1:84fb3e9adaaf 66 if (strlen(buf))
WiredHome 1:84fb3e9adaaf 67 INFO("%s", buf);
WiredHome 1:84fb3e9adaaf 68 }
WiredHome 1:84fb3e9adaaf 69 #else
WiredHome 1:84fb3e9adaaf 70 #define INFO(x, ...)
WiredHome 1:84fb3e9adaaf 71 #define WARN(x, ...)
WiredHome 1:84fb3e9adaaf 72 #define ERR(x, ...)
WiredHome 1:84fb3e9adaaf 73 #define HexDump(a, b, c)
WiredHome 1:84fb3e9adaaf 74 #endif
WiredHome 0:9ecf49d75058 75
WiredHome 0:9ecf49d75058 76 Serial pc(USBTX, USBRX);
WiredHome 0:9ecf49d75058 77 extern "C" void mbed_reset();
WiredHome 0:9ecf49d75058 78
WiredHome 0:9ecf49d75058 79
WiredHome 0:9ecf49d75058 80 typedef struct {
WiredHome 0:9ecf49d75058 81 loc_t x;
WiredHome 0:9ecf49d75058 82 loc_t y;
WiredHome 0:9ecf49d75058 83 char * filename;
WiredHome 0:9ecf49d75058 84 } TestImage_T;
WiredHome 0:9ecf49d75058 85
WiredHome 0:9ecf49d75058 86 TestImage_T TestImage[] = {
WiredHome 0:9ecf49d75058 87 { 0, 0, "/local/01601602.bmp"},
WiredHome 14:694822610507 88 { 0, 0, "/local/48027202.bmp"},
WiredHome 0:9ecf49d75058 89 { 0, 0, "/local/48027204.bmp"},
WiredHome 14:694822610507 90 { 0, 0, "/local/48027208.bmp"},
WiredHome 14:694822610507 91 { 0, 0, "/local/48027224.bmp"},
WiredHome 14:694822610507 92 { 0, 0, "/local/p480272.jpg"},
WiredHome 14:694822610507 93 { 0, 0, "/local/p480272.bmp"}
WiredHome 0:9ecf49d75058 94 };
WiredHome 0:9ecf49d75058 95
WiredHome 0:9ecf49d75058 96
WiredHome 1:84fb3e9adaaf 97 #ifdef JPEG_TEST
WiredHome 2:1e1dd697d12d 98 //#include "tjpgd.h"
WiredHome 2:1e1dd697d12d 99 //JDEC jdec;
WiredHome 2:1e1dd697d12d 100 //#define JPEG_WORK_SPACE_SIZE 3100
WiredHome 2:1e1dd697d12d 101 //uint16_t work[JPEG_WORK_SPACE_SIZE/sizeof(uint16_t)];
WiredHome 2:1e1dd697d12d 102 //FILE *fh;
WiredHome 1:84fb3e9adaaf 103
WiredHome 1:84fb3e9adaaf 104 uint16_t jpeg_input_func(JDEC *jd, uint8_t *buff, uint16_t ndata)
WiredHome 1:84fb3e9adaaf 105 {
WiredHome 1:84fb3e9adaaf 106 //INFO("Read in %p count %d", buff, ndata);
WiredHome 1:84fb3e9adaaf 107 if (buff) {
WiredHome 2:1e1dd697d12d 108 size_t n = fread(buff, 1, ndata, (FILE *)jd->device);
WiredHome 1:84fb3e9adaaf 109 //INFO("fread returned %d", n);
WiredHome 1:84fb3e9adaaf 110 //HexDump("buf", buff, (ndata > 32) ? 32 : ndata);
WiredHome 1:84fb3e9adaaf 111 return n == (size_t)-1 ? 0 : n;
WiredHome 1:84fb3e9adaaf 112 } else {
WiredHome 2:1e1dd697d12d 113 off_t t = fseek((FILE *)jd->device, ndata, SEEK_CUR);
WiredHome 1:84fb3e9adaaf 114 //INFO("Seek returned %d", t);
WiredHome 1:84fb3e9adaaf 115 return t == (off_t)-1 ? 0 : ndata;
WiredHome 1:84fb3e9adaaf 116 }
WiredHome 1:84fb3e9adaaf 117 }
WiredHome 1:84fb3e9adaaf 118
WiredHome 1:84fb3e9adaaf 119 uint16_t jpeg_output_func(JDEC *jd, void *bitmap, JRECT *rect)
WiredHome 1:84fb3e9adaaf 120 {
WiredHome 1:84fb3e9adaaf 121 #if JD_FORMAT == 1
WiredHome 1:84fb3e9adaaf 122 uint16_t *src = (uint16_t *)bitmap;
WiredHome 1:84fb3e9adaaf 123 #else
WiredHome 1:84fb3e9adaaf 124 uint8_t *src = (uint8_t *)bitmap;
WiredHome 1:84fb3e9adaaf 125 #endif
WiredHome 1:84fb3e9adaaf 126 int x0 = rect->left;
WiredHome 1:84fb3e9adaaf 127 int x1 = rect->right;
WiredHome 1:84fb3e9adaaf 128 int y0 = rect->top;
WiredHome 1:84fb3e9adaaf 129 int y1 = rect->bottom;
WiredHome 1:84fb3e9adaaf 130
WiredHome 1:84fb3e9adaaf 131 if (y0 >= lcd.height() || x0 >= lcd.width())
WiredHome 1:84fb3e9adaaf 132 return 1;
WiredHome 1:84fb3e9adaaf 133
WiredHome 1:84fb3e9adaaf 134 if (x1 > lcd.width()-1) x1 = lcd.width() - 1;
WiredHome 1:84fb3e9adaaf 135 if (y1 > lcd.height()-1) y1 = lcd.height() - 1;
WiredHome 1:84fb3e9adaaf 136
WiredHome 1:84fb3e9adaaf 137 if (x0 == 0)
WiredHome 1:84fb3e9adaaf 138 printf("\r %d", y0);
WiredHome 1:84fb3e9adaaf 139
WiredHome 1:84fb3e9adaaf 140 int w = x1 - x0 + 1;
WiredHome 1:84fb3e9adaaf 141 for (int y= y0; y <= y1; y++) {
WiredHome 1:84fb3e9adaaf 142 lcd.SetGraphicsCursor(x0, y);
WiredHome 1:84fb3e9adaaf 143 lcd._StartGraphicsStream();
WiredHome 1:84fb3e9adaaf 144 #if JD_FORMAT == 1
WiredHome 1:84fb3e9adaaf 145 uint16_t *p = src + w * (y - y0);
WiredHome 1:84fb3e9adaaf 146 #else
WiredHome 1:84fb3e9adaaf 147 uint8_t *p = src + 3 * (w * (y - y0));
WiredHome 1:84fb3e9adaaf 148 #endif
WiredHome 1:84fb3e9adaaf 149 for (int x=x0; x <= x1; x++) {
WiredHome 1:84fb3e9adaaf 150 #if JD_FORMAT == 1
WiredHome 1:84fb3e9adaaf 151 lcd._putp(*p++);
WiredHome 1:84fb3e9adaaf 152 #else
WiredHome 1:84fb3e9adaaf 153 lcd._putp(RGB(*p, *(p+1), *(p+2)));
WiredHome 1:84fb3e9adaaf 154 p += 3;
WiredHome 1:84fb3e9adaaf 155 #endif
WiredHome 1:84fb3e9adaaf 156 }
WiredHome 1:84fb3e9adaaf 157 lcd._EndGraphicsStream();
WiredHome 1:84fb3e9adaaf 158 }
WiredHome 1:84fb3e9adaaf 159 return 1;
WiredHome 1:84fb3e9adaaf 160 }
WiredHome 1:84fb3e9adaaf 161 #endif
WiredHome 1:84fb3e9adaaf 162
WiredHome 1:84fb3e9adaaf 163
WiredHome 0:9ecf49d75058 164 int main()
WiredHome 0:9ecf49d75058 165 {
WiredHome 0:9ecf49d75058 166 pc.baud(460800); // I like a snappy terminal, so crank it up!
WiredHome 0:9ecf49d75058 167 pc.printf("\r\nRA8875 Bitmap Test - Build " __DATE__ " " __TIME__ "\r\n");
WiredHome 0:9ecf49d75058 168
WiredHome 15:f33dcd24d8d1 169 lcd.init(LCD_W, LCD_H, LCD_C);
WiredHome 2:1e1dd697d12d 170 lcd.Backlight(0.5f);
WiredHome 2:1e1dd697d12d 171
WiredHome 0:9ecf49d75058 172 int count = sizeof(TestImage)/sizeof(TestImage[0]);
WiredHome 14:694822610507 173 while (1) {
WiredHome 14:694822610507 174 for (int i=0; i<count; i++) {
WiredHome 14:694822610507 175 lcd.cls();
WiredHome 14:694822610507 176 printf("RenderBitmapFile(%d, %d, %s) ", TestImage[i].x, TestImage[i].y, TestImage[i].filename);
WiredHome 14:694822610507 177 RetCode_t r = lcd.RenderImageFile(TestImage[i].x, TestImage[i].y, TestImage[i].filename);
WiredHome 14:694822610507 178 printf("returned %d\r\n", r);
WiredHome 14:694822610507 179 wait(5);
WiredHome 14:694822610507 180 }
WiredHome 0:9ecf49d75058 181 }
WiredHome 0:9ecf49d75058 182 }