Initial commit
This commit is contained in:
259
T-RGB/lib/Arduino_GFX/examples/ImgViewer/ImgViewerBmp/BmpClass.h
Normal file
259
T-RGB/lib/Arduino_GFX/examples/ImgViewer/ImgViewerBmp/BmpClass.h
Normal file
@@ -0,0 +1,259 @@
|
||||
/*******************************************************************************
|
||||
* BMP Class
|
||||
*
|
||||
* Rewrite from: https://github.com/Jaycar-Electronics/Arduino-Picture-Frame.git
|
||||
******************************************************************************/
|
||||
#ifndef _BMPCLASS_H_
|
||||
#define _BMPCLASS_H_
|
||||
|
||||
/* Wio Terminal */
|
||||
#if defined(ARDUINO_ARCH_SAMD) && defined(SEEED_GROVE_UI_WIRELESS)
|
||||
#include <Seeed_FS.h>
|
||||
#elif defined(ESP32) || defined(ESP8266)
|
||||
#include <FS.h>
|
||||
#else
|
||||
#include <SD.h>
|
||||
#endif
|
||||
|
||||
typedef void(BMP_DRAW_CALLBACK)(int16_t x, int16_t y, uint16_t *bitmap, int16_t w, int16_t h);
|
||||
|
||||
class BmpClass
|
||||
{
|
||||
public:
|
||||
void draw(
|
||||
File *f, BMP_DRAW_CALLBACK *bmpDrawCallback, bool useBigEndian,
|
||||
int16_t x, int16_t y, int16_t widthLimit, int16_t heightLimit)
|
||||
{
|
||||
_bmpDrawCallback = bmpDrawCallback;
|
||||
_useBigEndian = useBigEndian;
|
||||
_heightLimit = heightLimit;
|
||||
|
||||
int16_t u, v;
|
||||
uint32_t xend;
|
||||
|
||||
getbmpparms(f);
|
||||
|
||||
//validate bitmap
|
||||
if ((bmtype == 19778) && (bmwidth > 0) && (bmheight > 0) && (bmbpp > 0))
|
||||
{
|
||||
//centre image
|
||||
u = (widthLimit - bmwidth) / 2;
|
||||
v = (heightLimit - bmheight) / 2;
|
||||
u = (u < 0) ? x : x + u;
|
||||
v = (v < 0) ? y : y + v;
|
||||
xend = (bmwidth > widthLimit) ? widthLimit : bmwidth;
|
||||
|
||||
bmpRow = (uint16_t *)malloc(xend * 2);
|
||||
if (!bmpRow)
|
||||
{
|
||||
Serial.println(F("bmpRow malloc failed."));
|
||||
}
|
||||
if (bmbpp < 9)
|
||||
{
|
||||
bmplt = (uint16_t *)malloc(bmpltsize * 2);
|
||||
if (!bmplt)
|
||||
{
|
||||
Serial.println(F("bmplt malloc failed."));
|
||||
}
|
||||
bmloadplt(f); //load palette if palettized
|
||||
drawbmpal(f, u, v, xend);
|
||||
free(bmplt);
|
||||
}
|
||||
else if (bmbpp == 16)
|
||||
{
|
||||
// TODO: bpp 16 should have 3 pixel types
|
||||
drawbmRgb565(f, u, v, xend);
|
||||
}
|
||||
else
|
||||
{
|
||||
drawbmtrue(f, u, v, xend);
|
||||
}
|
||||
free(bmpRow);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
void bmloadplt(File *f)
|
||||
{
|
||||
byte r, g, b;
|
||||
if (bmpltsize == 0)
|
||||
{
|
||||
bmpltsize = 1 << bmbpp; //load default palette size
|
||||
}
|
||||
f->seek(54); //palette position in type 0x28 bitmaps
|
||||
for (int16_t i = 0; i < bmpltsize; i++)
|
||||
{
|
||||
b = f->read();
|
||||
g = f->read();
|
||||
r = f->read();
|
||||
f->read(); //dummy byte
|
||||
bmplt[i] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
|
||||
}
|
||||
}
|
||||
|
||||
void drawbmpal(File *f, int16_t u, int16_t v, uint32_t xend)
|
||||
{
|
||||
byte bmbitmask;
|
||||
int16_t i, ystart, bmppb, p, d;
|
||||
int16_t x, y;
|
||||
uint16_t c;
|
||||
bmbpl = ((bmbpp * bmwidth + 31) / 32) * 4; //bytes per line
|
||||
bmppb = 8 / bmbpp; //pixels/byte
|
||||
bmbitmask = ((1 << bmbpp) - 1); //mask for each pixel
|
||||
ystart = 0;
|
||||
if (bmheight > _heightLimit)
|
||||
{
|
||||
ystart = bmheight - _heightLimit; //don't draw if it's outside screen
|
||||
}
|
||||
for (y = ystart; y < bmheight; y++)
|
||||
{ //invert in calculation (y=0 is bottom)
|
||||
f->seek(bmdataptr + y * bmbpl); //seek to start of line
|
||||
x = 0;
|
||||
p = 0;
|
||||
while (x < xend)
|
||||
{
|
||||
if (p < 1)
|
||||
{
|
||||
d = f->read();
|
||||
p = bmppb;
|
||||
}
|
||||
d = d << bmbpp;
|
||||
c = bmplt[(bmbitmask & (d >> 8))];
|
||||
bmpRow[x] = (_useBigEndian) ? ((c >> 8) | (c << 8)) : c;
|
||||
|
||||
p--;
|
||||
x++;
|
||||
}
|
||||
_bmpDrawCallback(u, v + bmheight - 1 - y, bmpRow, xend, 1);
|
||||
}
|
||||
}
|
||||
|
||||
// draw 16-bit colour (RGB565) bitmap
|
||||
void drawbmRgb565(File *f, int16_t u, int16_t v, uint32_t xend)
|
||||
{
|
||||
int16_t i, ystart;
|
||||
uint32_t x, y;
|
||||
byte lo, hi;
|
||||
bmbpl = ((bmbpp * bmwidth + 31) / 32) * 4; //bytes per line, due to 32bit chunks
|
||||
ystart = 0;
|
||||
if (bmheight > _heightLimit)
|
||||
{
|
||||
ystart = bmheight - _heightLimit; //don't draw if it's outside screen
|
||||
}
|
||||
Serial.println(xend);
|
||||
for (y = ystart; y < bmheight; y++)
|
||||
{ //invert in calculation (y=0 is bottom)
|
||||
f->seek(bmdataptr + (y * bmbpl)); //seek at start of line
|
||||
for (x = 0; x < xend; x++)
|
||||
{
|
||||
lo = f->read();
|
||||
hi = f->read();
|
||||
if (_useBigEndian)
|
||||
{
|
||||
bmpRow[x] = hi | lo << 8;
|
||||
}
|
||||
else
|
||||
{
|
||||
bmpRow[x] = lo | hi << 8;
|
||||
}
|
||||
}
|
||||
_bmpDrawCallback(u, v + bmheight - 1 - y, bmpRow, xend, 1);
|
||||
}
|
||||
}
|
||||
|
||||
// draw true colour bitmap at (u,v) handles 24/32 not 16bpp yet
|
||||
void drawbmtrue(File *f, int16_t u, int16_t v, uint32_t xend)
|
||||
{
|
||||
int16_t i, ystart;
|
||||
uint32_t x, y;
|
||||
byte r, g, b;
|
||||
bmbpl = ((bmbpp * bmwidth + 31) / 32) * 4; //bytes per line, due to 32bit chunks
|
||||
ystart = 0;
|
||||
if (bmheight > _heightLimit)
|
||||
{
|
||||
ystart = bmheight - _heightLimit; //don't draw if it's outside screen
|
||||
}
|
||||
for (y = ystart; y < bmheight; y++)
|
||||
{ //invert in calculation (y=0 is bottom)
|
||||
f->seek(bmdataptr + y * bmbpl); //seek at start of line
|
||||
for (x = 0; x < xend; x++)
|
||||
{
|
||||
b = f->read();
|
||||
g = f->read();
|
||||
r = f->read();
|
||||
if (bmbpp == 32)
|
||||
{
|
||||
f->read(); //dummy byte for 32bit
|
||||
}
|
||||
bmpRow[x] = (_useBigEndian) ? ((r & 0xf8) | (g >> 5) | ((g & 0x1c) << 11) | ((b & 0xf8) << 5)) : (((r & 0xf8) << 8) | ((g & 0xfc) << 3) | (b >> 3));
|
||||
}
|
||||
_bmpDrawCallback(u, v + bmheight - 1 - y, bmpRow, xend, 1);
|
||||
}
|
||||
}
|
||||
|
||||
void getbmpparms(File *f)
|
||||
{ //load into globals as ints-some parameters are 32 bit, but we can't handle this size anyway
|
||||
byte h[48]; //header is 54 bytes typically, but we don't need it all
|
||||
int16_t i;
|
||||
f->seek(0); //set start of file
|
||||
for (i = 0; i < 48; i++)
|
||||
{
|
||||
h[i] = f->read(); //read header
|
||||
}
|
||||
bmtype = h[0] + (h[1] << 8); //offset 0 'BM'
|
||||
bmdataptr = h[10] + (h[11] << 8); //offset 0xA pointer to image data
|
||||
bmhdrsize = h[14] + (h[15] << 8); //dib header size (0x28 is usual)
|
||||
//files may vary here, if !=28, unsupported type, put default values
|
||||
bmwidth = 0;
|
||||
bmheight = 0;
|
||||
bmbpp = 0;
|
||||
bmpltsize = 0;
|
||||
if ((bmhdrsize == 0x28) || (bmhdrsize == 0x38))
|
||||
{
|
||||
bmwidth = h[18] + (h[19] << 8); //width
|
||||
bmheight = h[22] + (h[23] << 8); //height
|
||||
bmbpp = h[28] + (h[29] << 8); //bits per pixel
|
||||
bmpltsize = h[46] + (h[47] << 8); //palette size
|
||||
}
|
||||
// Serial.printf("bmtype: %d, bmhdrsize: %d, bmwidth: %d, bmheight: %d, bmbpp: %d\n", bmtype, bmhdrsize, bmwidth, bmheight, bmbpp);
|
||||
}
|
||||
|
||||
byte isbmp(char n[])
|
||||
{ //check if bmp extension
|
||||
int16_t k;
|
||||
k = strlen(n);
|
||||
if (k < 5)
|
||||
{
|
||||
return 0; //name not long enough
|
||||
}
|
||||
if (n[k - 1] != 'P')
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (n[k - 2] != 'M')
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (n[k - 3] != 'B')
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (n[k - 4] != '.')
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return 1; //passes all tests
|
||||
}
|
||||
|
||||
BMP_DRAW_CALLBACK *_bmpDrawCallback;
|
||||
bool _useBigEndian;
|
||||
int16_t _heightLimit;
|
||||
|
||||
uint16_t bmtype, bmdataptr; //from header
|
||||
uint32_t bmhdrsize, bmwidth, bmheight, bmbpp, bmpltsize; //from DIB Header
|
||||
uint16_t bmbpl; //bytes per line- derived
|
||||
uint16_t *bmplt; //palette- stored encoded for LCD
|
||||
uint16_t *bmpRow;
|
||||
};
|
||||
|
||||
#endif // _BMPCLASS_H_
|
||||
@@ -0,0 +1,170 @@
|
||||
/*******************************************************************************
|
||||
* BMP Image Viewer
|
||||
* This is a simple BMP image viewer example
|
||||
* Image Source: https://github.blog/2014-11-24-from-sticker-to-sculpture-the-making-of-the-octocat-figurine/
|
||||
*
|
||||
* Setup steps:
|
||||
* 1. Change your LCD parameters in Arduino_GFX setting
|
||||
* 2. Upload BMP file
|
||||
* FFat (ESP32):
|
||||
* upload FFat (FatFS) data with ESP32 Sketch Data Upload:
|
||||
* ESP32: https://github.com/lorol/arduino-esp32fs-plugin
|
||||
* LittleFS (ESP32 / ESP8266 / Pico):
|
||||
* upload LittleFS data with ESP8266 LittleFS Data Upload:
|
||||
* ESP32: https://github.com/lorol/arduino-esp32fs-plugin
|
||||
* ESP8266: https://github.com/earlephilhower/arduino-esp8266littlefs-plugin
|
||||
* Pico: https://github.com/earlephilhower/arduino-pico-littlefs-plugin.git
|
||||
* SPIFFS (ESP32):
|
||||
* upload SPIFFS data with ESP32 Sketch Data Upload:
|
||||
* ESP32: https://github.com/lorol/arduino-esp32fs-plugin
|
||||
* SD:
|
||||
* Most Arduino system built-in support SD file system.
|
||||
* Wio Terminal require extra dependant Libraries:
|
||||
* - Seeed_Arduino_FS: https://github.com/Seeed-Studio/Seeed_Arduino_FS.git
|
||||
* - Seeed_Arduino_SFUD: https://github.com/Seeed-Studio/Seeed_Arduino_SFUD.git
|
||||
******************************************************************************/
|
||||
// #define BMP_FILENAME "/octocatS.bmp" // 243x128@IndexedColor
|
||||
#define BMP_FILENAME "/octocatM.bmp" // 456x240@16bit
|
||||
// #define BMP_FILENAME "/octocatL.bmp" // 608x320@24bit
|
||||
|
||||
/*******************************************************************************
|
||||
* Start of Arduino_GFX setting
|
||||
*
|
||||
* Arduino_GFX try to find the settings depends on selected board in Arduino IDE
|
||||
* Or you can define the display dev kit not in the board list
|
||||
* Defalult pin list for non display dev kit:
|
||||
* Arduino Nano, Micro and more: CS: 9, DC: 8, RST: 7, BL: 6, SCK: 13, MOSI: 11, MISO: 12
|
||||
* ESP32 various dev board : CS: 5, DC: 27, RST: 33, BL: 22, SCK: 18, MOSI: 23, MISO: nil
|
||||
* ESP32-C3 various dev board : CS: 7, DC: 2, RST: 1, BL: 3, SCK: 4, MOSI: 6, MISO: nil
|
||||
* ESP32-S2 various dev board : CS: 34, DC: 35, RST: 33, BL: 21, SCK: 36, MOSI: 35, MISO: nil
|
||||
* ESP32-S3 various dev board : CS: 40, DC: 41, RST: 42, BL: 48, SCK: 36, MOSI: 35, MISO: nil
|
||||
* ESP8266 various dev board : CS: 15, DC: 4, RST: 2, BL: 5, SCK: 14, MOSI: 13, MISO: 12
|
||||
* Raspberry Pi Pico dev board : CS: 17, DC: 27, RST: 26, BL: 28, SCK: 18, MOSI: 19, MISO: 16
|
||||
* RTL8720 BW16 old patch core : CS: 18, DC: 17, RST: 2, BL: 23, SCK: 19, MOSI: 21, MISO: 20
|
||||
* RTL8720_BW16 Official core : CS: 9, DC: 8, RST: 6, BL: 3, SCK: 10, MOSI: 12, MISO: 11
|
||||
* RTL8722 dev board : CS: 18, DC: 17, RST: 22, BL: 23, SCK: 13, MOSI: 11, MISO: 12
|
||||
* RTL8722_mini dev board : CS: 12, DC: 14, RST: 15, BL: 13, SCK: 11, MOSI: 9, MISO: 10
|
||||
* Seeeduino XIAO dev board : CS: 3, DC: 2, RST: 1, BL: 0, SCK: 8, MOSI: 10, MISO: 9
|
||||
* Teensy 4.1 dev board : CS: 39, DC: 41, RST: 40, BL: 22, SCK: 13, MOSI: 11, MISO: 12
|
||||
******************************************************************************/
|
||||
#include <Arduino_GFX_Library.h>
|
||||
|
||||
#define GFX_BL DF_GFX_BL // default backlight pin, you may replace DF_GFX_BL to actual backlight pin
|
||||
|
||||
/* More dev device declaration: https://github.com/moononournation/Arduino_GFX/wiki/Dev-Device-Declaration */
|
||||
#if defined(DISPLAY_DEV_KIT)
|
||||
Arduino_GFX *gfx = create_default_Arduino_GFX();
|
||||
#else /* !defined(DISPLAY_DEV_KIT) */
|
||||
|
||||
/* More data bus class: https://github.com/moononournation/Arduino_GFX/wiki/Data-Bus-Class */
|
||||
Arduino_DataBus *bus = create_default_Arduino_DataBus();
|
||||
|
||||
/* More display class: https://github.com/moononournation/Arduino_GFX/wiki/Display-Class */
|
||||
Arduino_GFX *gfx = new Arduino_ILI9341(bus, DF_GFX_RST, 3 /* rotation */, false /* IPS */);
|
||||
|
||||
#endif /* !defined(DISPLAY_DEV_KIT) */
|
||||
/*******************************************************************************
|
||||
* End of Arduino_GFX setting
|
||||
******************************************************************************/
|
||||
|
||||
/* Wio Terminal */
|
||||
#if defined(ARDUINO_ARCH_SAMD) && defined(SEEED_GROVE_UI_WIRELESS)
|
||||
#include <Seeed_FS.h>
|
||||
#include <SD/Seeed_SD.h>
|
||||
#elif defined(ARDUINO_RASPBERRY_PI_PICO) || defined(ARDUINO_RASPBERRY_PI_PICO_W)
|
||||
#include <LittleFS.h>
|
||||
#include <SD.h>
|
||||
#elif defined(ESP32)
|
||||
#include <FFat.h>
|
||||
#include <LittleFS.h>
|
||||
#include <SPIFFS.h>
|
||||
#include <SD.h>
|
||||
#elif defined(ESP8266)
|
||||
#include <LittleFS.h>
|
||||
#include <SD.h>
|
||||
#else
|
||||
#include <SD.h>
|
||||
#endif
|
||||
|
||||
#include "BmpClass.h"
|
||||
static BmpClass bmpClass;
|
||||
|
||||
// pixel drawing callback
|
||||
static void bmpDrawCallback(int16_t x, int16_t y, uint16_t *bitmap, int16_t w, int16_t h)
|
||||
{
|
||||
// Serial.printf("Draw pos = %d, %d. size = %d x %d\n", x, y, w, h);
|
||||
gfx->draw16bitRGBBitmap(x, y, bitmap, w, h);
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
// while (!Serial);
|
||||
Serial.println("BMP Image Viewer");
|
||||
|
||||
// Init Display
|
||||
gfx->begin();
|
||||
gfx->fillScreen(BLACK);
|
||||
|
||||
#ifdef GFX_BL
|
||||
pinMode(GFX_BL, OUTPUT);
|
||||
digitalWrite(GFX_BL, HIGH);
|
||||
#endif
|
||||
|
||||
/* Wio Terminal */
|
||||
#if defined(ARDUINO_ARCH_SAMD) && defined(SEEED_GROVE_UI_WIRELESS)
|
||||
if (!SD.begin(SDCARD_SS_PIN, SDCARD_SPI, 4000000UL))
|
||||
#elif defined(ARDUINO_RASPBERRY_PI_PICO) || defined(ARDUINO_RASPBERRY_PI_PICO_W)
|
||||
if (!LittleFS.begin())
|
||||
// if (!SD.begin(SS))
|
||||
#elif defined(ESP32)
|
||||
// if (!FFat.begin())
|
||||
if (!LittleFS.begin())
|
||||
// if (!SPIFFS.begin())
|
||||
// if (!SD.begin(SS))
|
||||
#elif defined(ESP8266)
|
||||
if (!LittleFS.begin())
|
||||
// if (!SD.begin())
|
||||
#else
|
||||
if (!SD.begin())
|
||||
#endif
|
||||
{
|
||||
Serial.println(F("ERROR: File System Mount Failed!"));
|
||||
gfx->println(F("ERROR: File System Mount Failed!"));
|
||||
}
|
||||
else
|
||||
{
|
||||
unsigned long start = millis();
|
||||
|
||||
#if defined(ARDUINO_ARCH_SAMD) && defined(SEEED_GROVE_UI_WIRELESS)
|
||||
File bmpFile = SD.open(BMP_FILENAME, "r");
|
||||
#elif defined(ARDUINO_RASPBERRY_PI_PICO) || defined(ARDUINO_RASPBERRY_PI_PICO_W)
|
||||
File bmpFile = LittleFS.open(BMP_FILENAME, "r");
|
||||
// File bmpFile = SD.open(BMP_FILENAME, "r");
|
||||
#elif defined(ESP32)
|
||||
// File bmpFile = FFat.open(BMP_FILENAME, "r");
|
||||
File bmpFile = LittleFS.open(BMP_FILENAME, "r");
|
||||
// File bmpFile = SPIFFS.open(BMP_FILENAME, "r");
|
||||
// File bmpFile = SD.open(BMP_FILENAME, "r");
|
||||
#elif defined(ESP8266)
|
||||
File bmpFile = LittleFS.open(BMP_FILENAME, "r");
|
||||
// File bmpFile = SD.open(BMP_FILENAME, "r");
|
||||
#else
|
||||
File bmpFile = SD.open(BMP_FILENAME, FILE_READ);
|
||||
#endif
|
||||
|
||||
// read BMP file header
|
||||
bmpClass.draw(
|
||||
&bmpFile, bmpDrawCallback, false /* useBigEndian */,
|
||||
0 /* x */, 0 /* y */, gfx->width() /* widthLimit */, gfx->height() /* heightLimit */);
|
||||
|
||||
bmpFile.close();
|
||||
|
||||
Serial.print("Time used: ");
|
||||
Serial.println(millis() - start);
|
||||
}
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 570 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 214 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 31 KiB |
Reference in New Issue
Block a user