bitmap buffer: allow for forcing a destination pixel format (#61)

Otherwise, 32bit images are converted automatically to ARGB4444, and 24bit image are converted to RGB565, thus leading to EdgeTX/edgetx#2261.
This commit is contained in:
Raphael Coeffic
2022-09-05 18:30:52 +10:00
committed by GitHub
parent 199975b95c
commit 5174b58fb9
2 changed files with 20 additions and 17 deletions
+14 -13
View File
@@ -1257,14 +1257,14 @@ void drawSolidRect(BitmapBuffer * dc, coord_t x, coord_t y, coord_t w, coord_t h
//} //}
// //
BitmapBuffer * BitmapBuffer::loadBitmap(const char * filename) BitmapBuffer * BitmapBuffer::loadBitmap(const char * filename, BitmapFormats fmt)
{ {
//TRACE(" BitmapBuffer::loadBitmap(%s)", filename); //TRACE(" BitmapBuffer::loadBitmap(%s)", filename);
const char * ext = getFileExtension(filename); const char * ext = getFileExtension(filename);
if (ext && !strcmp(ext, ".bmp")) if (ext && !strcmp(ext, ".bmp"))
return load_bmp(filename); return load_bmp(filename);
else else
return load_stb(filename); return load_stb(filename, fmt);
} }
BitmapBuffer * BitmapBuffer::loadRamBitmap(const uint8_t * buffer, int len) BitmapBuffer * BitmapBuffer::loadRamBitmap(const uint8_t * buffer, int len)
@@ -1679,7 +1679,7 @@ const stbi_io_callbacks stbCallbacks = {
stbc_eof stbc_eof
}; };
BitmapBuffer * BitmapBuffer::load_stb(const char * filename) BitmapBuffer * BitmapBuffer::load_stb(const char * filename, BitmapFormats fmt)
{ {
//TRACE(" BitmapBuffer::load_stb(%s)", filename); //TRACE(" BitmapBuffer::load_stb(%s)", filename);
@@ -1708,7 +1708,7 @@ BitmapBuffer * BitmapBuffer::load_stb(const char * filename)
} }
//TRACE(" BitmapBuffer::load_stb()----Info File %s, %d, %d, %d/%d", filename, x, y, nn, n); //TRACE(" BitmapBuffer::load_stb()----Info File %s, %d, %d, %d/%d", filename, x, y, nn, n);
BitmapBuffer * bmp = convert_stb_bitmap(img, w, h, n); BitmapBuffer * bmp = convert_stb_bitmap(img, w, h, n, fmt);
stbi_image_free(img); stbi_image_free(img);
return bmp; return bmp;
} }
@@ -1727,22 +1727,24 @@ BitmapBuffer * BitmapBuffer::load_stb_buffer(const uint8_t * buffer, int len)
return bmp; return bmp;
} }
BitmapBuffer * BitmapBuffer::convert_stb_bitmap(uint8_t * img, int w, int h, int n) BitmapBuffer * BitmapBuffer::convert_stb_bitmap(uint8_t * img, int w, int h, int n,
BitmapFormats fmt)
{ {
// convert to RGB565 or ARGB4444 format // convert to RGB565 or ARGB4444 format
//TRACE(" BitmapBuffer::convert_stb_bitmap(%d)", n); BitmapFormats dst_fmt = fmt;
BitmapBuffer * bmp = new BitmapBuffer(n == 4 ? BMP_ARGB4444 : BMP_RGB565, w, h); if (dst_fmt == BMP_INVALID) {
dst_fmt = (n == 4 ? BMP_ARGB4444 : BMP_RGB565);
}
BitmapBuffer * bmp = new BitmapBuffer(dst_fmt, w, h);
if (bmp == nullptr) { if (bmp == nullptr) {
TRACE("convert_stn_bitmap: malloc failed"); TRACE("convert_stn_bitmap: malloc failed");
return nullptr; return nullptr;
} }
#if 0 // use Stb's "stbi__vertically_flip_on_load" instead of this?
DMABitmapConvert(bmp->data, img, w, h, n == 4 ? DMA2D_ARGB4444 : DMA2D_RGB565);
#else
pixel_t * dest = bmp->getPixelPtrAbs(0, 0); pixel_t * dest = bmp->getPixelPtrAbs(0, 0);
const uint8_t * p = img; const uint8_t * p = img;
if (n == 4) { if (dst_fmt == BMP_ARGB4444) {
for (int row = 0; row < h; ++row) { for (int row = 0; row < h; ++row) {
for (int col = 0; col < w; ++col) { for (int col = 0; col < w; ++col) {
*dest = ARGB(p[3], p[0], p[1], p[2]); *dest = ARGB(p[3], p[0], p[1], p[2]);
@@ -1751,7 +1753,7 @@ BitmapBuffer * BitmapBuffer::convert_stb_bitmap(uint8_t * img, int w, int h, int
} }
} }
} }
else { // assume 3 bytes, packed in groups of 4, I guess else { // assume 3 bytes, packed in groups of 4
for (int row = 0; row < h; ++row) { for (int row = 0; row < h; ++row) {
for (int col = 0; col < w; ++col) { for (int col = 0; col < w; ++col) {
*dest = RGB(p[0], p[1], p[2]); *dest = RGB(p[0], p[1], p[2]);
@@ -1760,7 +1762,6 @@ BitmapBuffer * BitmapBuffer::convert_stb_bitmap(uint8_t * img, int w, int h, int
} }
} }
} }
#endif
return bmp; return bmp;
} }
+6 -4
View File
@@ -48,7 +48,8 @@ constexpr uint8_t STASHED = 0x33;
enum BitmapFormats enum BitmapFormats
{ {
BMP_8BIT, BMP_INVALID = -1,
BMP_8BIT = 0,
BMP_RGB565, BMP_RGB565,
BMP_ARGB4444 BMP_ARGB4444
}; };
@@ -309,7 +310,7 @@ class BitmapBuffer: public BitmapBufferBase<pixel_t>
void drawBitmapPatternPie(coord_t x0, coord_t y0, const uint8_t * img, LcdFlags flags, int startAngle, int endAngle); void drawBitmapPatternPie(coord_t x0, coord_t y0, const uint8_t * img, LcdFlags flags, int startAngle, int endAngle);
static BitmapBuffer * loadBitmap(const char * filename); static BitmapBuffer * loadBitmap(const char * filename, BitmapFormats fmt = BMP_INVALID);
static BitmapBuffer * loadRamBitmap(const uint8_t * buffer, int len); static BitmapBuffer * loadRamBitmap(const uint8_t * buffer, int len);
static BitmapBuffer * loadMask(const char * filename); static BitmapBuffer * loadMask(const char * filename);
@@ -360,9 +361,10 @@ class BitmapBuffer: public BitmapBufferBase<pixel_t>
protected: protected:
static BitmapBuffer * load_bmp(const char * filename); static BitmapBuffer * load_bmp(const char * filename);
static BitmapBuffer * load_stb(const char * filename); static BitmapBuffer * load_stb(const char * filename, BitmapFormats fmt = BMP_INVALID);
static BitmapBuffer * load_stb_buffer(const uint8_t * buffer, int len); static BitmapBuffer * load_stb_buffer(const uint8_t * buffer, int len);
static BitmapBuffer * convert_stb_bitmap(uint8_t * img, int w, int h, int n); static BitmapBuffer * convert_stb_bitmap(uint8_t * img, int w, int h, int n,
BitmapFormats fmt = BMP_INVALID);
inline bool applyClippingRect(coord_t & x, coord_t & y, coord_t & w, coord_t & h) const inline bool applyClippingRect(coord_t & x, coord_t & y, coord_t & w, coord_t & h) const
{ {