[Horus] More bitmaps on SD. PNG format used instead of BMP. And yes it's a bad idea given the resources we have.

This commit is contained in:
Bertrand Songis
2016-02-29 22:17:47 +01:00
parent f65e42833e
commit 1fd1e141f2
34 changed files with 274 additions and 98 deletions
+15 -16
View File
@@ -461,7 +461,7 @@ const stbi_io_callbacks stbCallbacks = {
stbc_eof
};
BitmapBuffer * BitmapBuffer::load(const char * filename, bool transparent)
BitmapBuffer * BitmapBuffer::load(const char * filename)
{
FIL imgFile;
@@ -471,46 +471,45 @@ BitmapBuffer * BitmapBuffer::load(const char * filename, bool transparent)
}
int w, h, n;
unsigned char *img = stbi_load_from_callbacks(&stbCallbacks, &imgFile, &w, &h, &n, transparent ? 4 : 3);
unsigned char * img = stbi_load_from_callbacks(&stbCallbacks, &imgFile, &w, &h, &n, 4);
f_close(&imgFile);
if (!img) {
return NULL;
}
//convert to RGB565 or ARGB4444 fromat
// todo use dma2d for conversion from 888 to 565
unsigned char * p = img;
BitmapBuffer * bmp = new BitmapBuffer(w, h);
uint16_t * dest = bmp->data;
// convert to RGB565 or ARGB4444 format
BitmapBuffer * bmp = new BitmapBuffer(n == 4 ? BMP_ARGB4444 : BMP_RGB565, w, h);
if (bmp == NULL) {
TRACE("load_stb() malloc failed");
stbi_image_free(img);
return NULL;
}
if (transparent) {
#if 0
DMABitmapConvert(bmp->data, img, w, h, n == 4 ? DMA2D_ARGB4444 : DMA2D_RGB565);
#else
uint16_t * dest = bmp->getData();
const uint8_t * p = img;
if (n == 4) {
for(int row = 0; row < h; ++row) {
unsigned char *l = p;
for(int col = 0; col < w; ++col) {
*dest = ARGB(l[3], l[0], l[1], l[2]);
*dest = ARGB(p[3], p[0], p[1], p[2]);
++dest;
l += 4;
p += 4;
}
p += 4 * w;
}
}
else {
for(int row = 0; row < h; ++row) {
unsigned char *l = p;
for(int col = 0; col < w; ++col) {
*dest = RGB(l[0], l[1], l[2]);
*dest = RGB(p[0], p[1], p[2]);
++dest;
l += 3;
p += 4;
}
p += 3 * w;
}
}
#endif
stbi_image_free(img);
return bmp;
+41 -29
View File
@@ -25,38 +25,54 @@
#include "colors.h"
#define USE_STB
// TODO should go to lcd.h again
typedef int coord_t;
typedef uint32_t LcdFlags;
typedef uint16_t display_t;
enum BitmapFormats
{
BMP_RGB565,
BMP_ARGB4444
};
template<class T>
class BitmapBufferBase
{
public:
BitmapBufferBase(int width, int height, T * data):
BitmapBufferBase(uint8_t format, uint16_t width, uint16_t height, T * data):
format(format),
width(width),
height(height),
data(data)
{
}
int getWidth() const
inline uint8_t getFormat() const
{
return format;
}
inline uint16_t getWidth() const
{
return width;
}
int getHeight() const
inline uint16_t getHeight() const
{
return height;
}
protected:
int width;
int height;
inline T * getData() const
{
return data;
}
public: // TODO protected
protected:
uint8_t format;
uint16_t width;
uint16_t height;
T * data;
};
@@ -69,22 +85,24 @@ class BitmapBuffer: public BitmapBufferBase<uint16_t>
public:
BitmapBuffer(int width, int height):
BitmapBufferBase<uint16_t>(width, height, NULL),
BitmapBuffer(uint8_t format, uint16_t width, uint16_t height):
BitmapBufferBase<uint16_t>(format, width, height, NULL),
dataAllocated(true)
{
data = (uint16_t *)malloc(width*height*sizeof(uint16_t));
}
BitmapBuffer(int width, int height, uint16_t * data):
BitmapBufferBase<uint16_t>(width, height, data),
BitmapBuffer(uint8_t format, uint16_t width, uint16_t height, uint16_t * data):
BitmapBufferBase<uint16_t>(format, width, height, data),
dataAllocated(false)
{
}
~BitmapBuffer()
{
if (dataAllocated) free(data);
if (dataAllocated) {
free(data);
}
}
inline void clear()
@@ -137,7 +155,7 @@ class BitmapBuffer: public BitmapBufferBase<uint16_t>
void drawBitmapPatternPie(coord_t x0, coord_t y0, const uint8_t * img, LcdFlags flags, int startAngle, int endAngle);
static BitmapBuffer * load(const char * filename, bool transparent = false);
static BitmapBuffer * load(const char * filename);
void drawBitmapPattern(coord_t x, coord_t y, const uint8_t * bmp, LcdFlags flags, coord_t offset=0, coord_t width=0);
@@ -148,6 +166,8 @@ class BitmapBuffer: public BitmapBufferBase<uint16_t>
template<class T>
void drawBitmap(coord_t x, coord_t y, const T * bmp, coord_t srcx=0, coord_t srcy=0, coord_t w=0, coord_t h=0, float scale=0)
{
if (!bmp) return;
int srcw = bmp->getWidth();
int srch = bmp->getHeight();
@@ -167,7 +187,12 @@ class BitmapBuffer: public BitmapBufferBase<uint16_t>
if (y + h > height) {
h = height - y;
}
DMACopyBitmap(data, width, x, y, bmp->data, srcw, srcx, srcy, w, h);
if (bmp->getFormat() == BMP_ARGB4444) {
DMACopyAlphaBitmap(data, this->width, x, y, bmp->getData(), srcw, srcx, srcy, w, h);
}
else {
DMACopyBitmap(data, width, x, y, bmp->getData(), srcw, srcx, srcy, w, h);
}
}
else {
int scaledw = w * scale;
@@ -180,7 +205,7 @@ class BitmapBuffer: public BitmapBufferBase<uint16_t>
for (int i = 0; i < scaledh; i++) {
uint16_t * p = &data[(y + i) * width + x];
const uint16_t * qstart = &bmp->data[(srcy + int(i / scale)) * bmp->getWidth() + srcx];
const uint16_t * qstart = &bmp->getData()[(srcy + int(i / scale)) * bmp->getWidth() + srcx];
for (int j = 0; j < scaledw; j++) {
const uint16_t * q = qstart + int(j / scale);
*p = *q;
@@ -205,19 +230,6 @@ class BitmapBuffer: public BitmapBufferBase<uint16_t>
drawBitmap(x+(w-width)/2, y, bitmap, 0, 0, 0, 0, scale);
}
}
template<class T>
void drawAlphaBitmap(coord_t x, coord_t y, const T * bmp)
{
int width = bmp->getWidth();
int height = bmp->getHeight();
if (width == 0 || height == 0) {
return;
}
DMACopyAlphaBitmap(data, this->width, x, y, bmp->data, width, height);
}
};
extern BitmapBuffer * lcd;
+4
View File
@@ -248,6 +248,10 @@ const uint8_t LBM_LIBRARY_SLOT[] = {
#include "mask_library_slot.lbm"
};
const uint8_t LBM_ACTIVE_MODEL[] = {
#include "mask_library_active_model.lbm"
};
const uint8_t LBM_LIBRARY_CURSOR[] = {
#include "mask_library_category_index.lbm"
};
+1
View File
@@ -59,6 +59,7 @@ extern const uint8_t * const LBM_MAINVIEWS_ICONS[];
// Model selection icons
extern const uint8_t LBM_LIBRARY_ICON[];
extern const uint8_t LBM_LIBRARY_SLOT[];
extern const uint8_t LBM_ACTIVE_MODEL[];
extern const uint8_t LBM_LIBRARY_CURSOR[];
extern const uint8_t LBM_SCORE0[];
extern const uint8_t LBM_SCORE1[];
@@ -100,3 +100,4 @@ void Layout2P1::refresh()
}
BaseLayoutFactory<Layout2P1> layout2P1("Layout2P1", LBM_LAYOUT_2P1, OPTIONS_LAYOUT_2P1);
const LayoutFactory * defaultLayout = &layout2P1;
+26 -4
View File
@@ -659,7 +659,7 @@ void lcdDrawBlackOverlay()
}
#if defined(SIMU)
BitmapBuffer _lcd(LCD_W, LCD_H, displayBuf);
BitmapBuffer _lcd(BMP_RGB565, LCD_W, LCD_H, displayBuf);
BitmapBuffer * lcd = &_lcd;
void DMAFillRect(uint16_t * dest, uint16_t destw, uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color)
@@ -678,12 +678,12 @@ void DMACopyBitmap(uint16_t * dest, uint16_t destw, uint16_t x, uint16_t y, cons
}
}
void DMACopyAlphaBitmap(uint16_t * dest, uint16_t destw, uint16_t x, uint16_t y, const uint16_t * src, uint16_t srcw, uint16_t h)
void DMACopyAlphaBitmap(uint16_t * dest, uint16_t destw, uint16_t x, uint16_t y, const uint16_t * src, uint16_t srcw, uint16_t srcx, uint16_t srcy, uint16_t w, uint16_t h)
{
for (coord_t line=0; line<h; line++) {
uint16_t * p = dest + (y+line)*destw + x;
const uint16_t * q = src + line*srcw;
for (coord_t col=0; col<srcw; col++) {
const uint16_t * q = src + (srcy+line)*srcw + srcx;
for (coord_t col=0; col<w; col++) {
uint8_t alpha = *q >> 12;
uint8_t red = ((((*q >> 8) & 0x0f) << 1) * alpha + (*p >> 11) * (0x0f-alpha)) / 0x0f;
uint8_t green = ((((*q >> 4) & 0x0f) << 2) * alpha + ((*p >> 5) & 0x3f) * (0x0f-alpha)) / 0x0f;
@@ -693,4 +693,26 @@ void DMACopyAlphaBitmap(uint16_t * dest, uint16_t destw, uint16_t x, uint16_t y,
}
}
}
void DMABitmapConvert(uint16_t * dest, const uint8_t * src, uint16_t w, uint16_t h, uint32_t format)
{
if (format == DMA2D_ARGB4444) {
for (int row = 0; row < h; ++row) {
for (int col = 0; col < w; ++col) {
*dest = ARGB(src[0], src[1], src[2], src[3]);
++dest;
src += 4;
}
}
}
else {
for (int row = 0; row < h; ++row) {
for(int col = 0; col < w; ++col) {
*dest = RGB(src[1], src[2], src[3]);
++dest;
src += 4;
}
}
}
}
#endif
+2 -2
View File
@@ -77,7 +77,7 @@
#if defined(SIMU)
extern display_t displayBuf[DISPLAY_BUFFER_SIZE];
#else
#define displayBuf lcd->data
#define displayBuf lcd->getData()
#endif
#define lcdRefreshWait()
@@ -157,7 +157,7 @@ inline void lcdDrawAlphaPixel(coord_t x, coord_t y, uint8_t opacity, uint16_t co
#if !defined(SIMU)
inline void lcdDrawSolidFilledRect(coord_t x, coord_t y, coord_t w, coord_t h, LcdFlags flags)
{
DMAFillRect(lcd->data, LCD_W, x, y, w, h, lcdColorTable[COLOR_IDX(flags)]);
DMAFillRect(lcd->getData(), LCD_W, x, y, w, h, lcdColorTable[COLOR_IDX(flags)]);
}
#else
void lcdDrawSolidFilledRect(coord_t x, coord_t y, coord_t w, coord_t h, LcdFlags flags);
+4 -3
View File
@@ -63,8 +63,6 @@ void drawPots()
OPTION_SLIDER_SQUARE_BUTTON);
}
#include "alpha_horus.lbm"
bool menuCommonCalib(evt_t event)
{
drawScreenTemplate(NULL, LBM_CALIBRATION_ICON, OPTION_MENU_NO_FOOTER);
@@ -194,7 +192,10 @@ bool menuCommonCalib(evt_t event)
break;
}
lcd->drawAlphaBitmap((LCD_W-206)/2, LCD_H-220, &ALPHA_HORUS);
static const BitmapBuffer * horus = BitmapBuffer::load(getThemePath("horus.png"));
if (horus) {
lcd->drawBitmap((LCD_W-horus->getWidth())/2, LCD_H-20-horus->getHeight(), horus);
}
drawSticks();
drawPots();
@@ -366,11 +366,11 @@ bool menuGeneralSdManager(evt_t _event)
if (currentBitmapIndex != menuVerticalPosition) {
currentBitmapIndex = menuVerticalPosition;
delete currentBitmap;
currentBitmap = BitmapBuffer::load(reusableBuffer.sdmanager.lines[index], true);
currentBitmap = BitmapBuffer::load(reusableBuffer.sdmanager.lines[index]);
// TODO scale in case of a too large bitmap
}
if (currentBitmap) {
lcd->drawAlphaBitmap(LCD_W / 2, LCD_H / 2, currentBitmap);
lcd->drawBitmap(LCD_W / 2, LCD_H / 2, currentBitmap);
}
}
+1 -1
View File
@@ -18,7 +18,7 @@
* GNU General Public License for more details.
*/
#include "../../opentx.h"
#include "opentx.h"
const char *warningText = NULL;
const char *warningInfoText;
+6 -8
View File
@@ -52,11 +52,9 @@ void Theme::drawThumb(uint16_t x, uint16_t y, uint32_t flags)
#define THUMB_WIDTH 51
#define THUMB_HEIGHT 31
if (!thumb) {
thumb = BitmapBuffer::load(getFilePath("thumb.bmp"));
}
if (thumb) {
lcd->drawBitmap(x, y, thumb);
thumb = BitmapBuffer::load(getFilePath("thumb.png"));
}
lcd->drawBitmap(x, y, thumb);
if (flags == LINE_COLOR) {
lcdDrawFilledRect(x, y, THUMB_WIDTH, THUMB_HEIGHT, SOLID, OVERLAY_COLOR | OPACITY(10));
}
@@ -72,20 +70,20 @@ void Theme::drawAboutBackground() const
drawBackground();
}
#include "alpha_asterisk.lbm"
void Theme::drawMessageBox(const char * title, const char * text, const char * action, uint32_t flags) const
{
static const BitmapBuffer * asterisk = BitmapBuffer::load(getThemePath("asterisk.png"));
//if (flags & MESSAGEBOX_TYPE_ALERT) {
drawBackground();
lcdDrawFilledRect(0, POPUP_Y, LCD_W, POPUP_H, SOLID, TEXT_INVERTED_COLOR | OPACITY(8));
//}
if ((flags & MESSAGEBOX_TYPE_ALERT) || (flags & MESSAGEBOX_TYPE_WARNING)) {
lcd->drawAlphaBitmap(POPUP_X-80, POPUP_Y+12, &ALPHA_ASTERISK);
lcd->drawBitmap(POPUP_X-80, POPUP_Y+12, asterisk);
}
else {
lcd->drawAlphaBitmap(POPUP_X-80, POPUP_Y+12, &ALPHA_ASTERISK);
lcd->drawBitmap(POPUP_X-80, POPUP_Y+12, asterisk);
}
#if defined(TRANSLATIONS_FR) || defined(TRANSLATIONS_IT) || defined(TRANSLATIONS_CZ)
+1 -1
View File
@@ -66,7 +66,7 @@ class DarkblueTheme: public Theme
lcdDrawBitmapPattern(5, 7, icon, MENU_TITLE_COLOR);
}
else {
static BitmapBuffer * thumb = BitmapBuffer::load(getFilePath("topmenu_opentx.bmp"));
static BitmapBuffer * thumb = BitmapBuffer::load(getFilePath("topmenu_opentx.png"));
lcd->drawBitmap(5, 7, thumb);
}
+3 -5
View File
@@ -68,7 +68,7 @@ class DefaultTheme: public Theme
virtual void drawBackground() const
{
static BitmapBuffer * backgroundBitmap = BitmapBuffer::load(getThemePath("mainbg.bmp"));
static BitmapBuffer * backgroundBitmap = BitmapBuffer::load(getThemePath("mainbg.png"));
if (backgroundBitmap) {
lcd->drawBitmap(0, 0, backgroundBitmap);
}
@@ -80,10 +80,8 @@ class DefaultTheme: public Theme
virtual void drawAboutBackground() const
{
static BitmapBuffer * backgroundBitmap = BitmapBuffer::load(getThemePath("aboutbg.bmp"));
if (backgroundBitmap) {
lcd->drawBitmap(0, 0, backgroundBitmap);
}
static BitmapBuffer * backgroundBitmap = BitmapBuffer::load(getThemePath("aboutbg.png"));
lcd->drawBitmap(0, 0, backgroundBitmap);
}
virtual void drawTopbarBackground(const uint8_t * icon) const
+1 -1
View File
@@ -29,7 +29,7 @@
#define TRIM_LEN 80
#define POTS_LINE_Y 252
Layout * customScreens[MAX_CUSTOM_SCREENS];
Layout * customScreens[MAX_CUSTOM_SCREENS] = { 0, 0, 0, 0, 0 };
Topbar * topbar;
void drawMainPots()
+9 -8
View File
@@ -52,8 +52,8 @@ void drawTopbarDatetime()
#define STICK_PANEL_WIDTH 68
void drawStick(coord_t x, coord_t y, int16_t xval, int16_t yval)
{
lcd->drawAlphaBitmap(x, y, &ALPHA_STICK_BACKGROUND);
lcd->drawAlphaBitmap(x + 2 + STICK_PANEL_WIDTH/2 + STICK_PANEL_WIDTH/2 * xval/RESX, y + 2 + STICK_PANEL_WIDTH/2 - STICK_PANEL_WIDTH/2 * yval/RESX, &ALPHA_STICK_POINTER);
lcd->drawBitmap(x, y, &ALPHA_STICK_BACKGROUND);
lcd->drawBitmap(x + 2 + STICK_PANEL_WIDTH/2 + STICK_PANEL_WIDTH/2 * xval/RESX, y + 2 + STICK_PANEL_WIDTH/2 - STICK_PANEL_WIDTH/2 * yval/RESX, &ALPHA_STICK_POINTER);
}
#include "alpha_button_on.lbm"
@@ -75,9 +75,9 @@ void drawButton(coord_t x, coord_t y, const char * label, LcdFlags attr)
lcdDrawText(x+padding+8, y, label, TEXT_COLOR);
}
if (attr & BUTTON_OFF)
lcd->drawAlphaBitmap(x-6, y+3, &ALPHA_BUTTON_OFF);
lcd->drawBitmap(x-6, y+3, &ALPHA_BUTTON_OFF);
else if (attr & BUTTON_ON)
lcd->drawAlphaBitmap(x-6, y+3, &ALPHA_BUTTON_ON);
lcd->drawBitmap(x-6, y+3, &ALPHA_BUTTON_ON);
}
void drawCheckBox(coord_t x, coord_t y, uint8_t value, LcdFlags attr)
@@ -424,23 +424,24 @@ int16_t editGVarFieldValue(coord_t x, coord_t y, int16_t value, int16_t min, int
void drawSleepBitmap()
{
lcd->clear();
const BitmapBuffer * bitmap = BitmapBuffer::load(getThemePath("sleep.bmp"));
const BitmapBuffer * bitmap = BitmapBuffer::load(getThemePath("sleep.png"));
if (bitmap) {
lcd->drawBitmap((LCD_W-bitmap->getWidth())/2, (LCD_H-bitmap->getHeight())/2, bitmap);
}
lcdRefresh();
}
#include "alpha_shutdown.lbm"
#define SHUTDOWN_CIRCLE_DIAMETER 150
void drawShutdownBitmap(uint32_t index)
{
static uint32_t last_index = 0xffffffff;
static const BitmapBuffer * shutdown = BitmapBuffer::load(getThemePath("shutdown.png"));
if (index < last_index) {
theme->drawBackground();
lcd->drawAlphaBitmap((LCD_W-ALPHA_SHUTDOWN.getWidth())/2, (LCD_H-ALPHA_SHUTDOWN.getHeight())/2, &ALPHA_SHUTDOWN);
if (shutdown) {
lcd->drawBitmap((LCD_W-shutdown->getWidth())/2, (LCD_H-shutdown->getHeight())/2, shutdown);
}
lcdStoreBackupBuffer();
}
else {
+2 -1
View File
@@ -41,7 +41,7 @@ class ModelBitmapWidget: public Widget
void refreshBuffer()
{
delete buffer;
buffer = new BitmapBuffer(zone.w, zone.h);
buffer = new BitmapBuffer(BMP_RGB565, zone.w, zone.h);
if (buffer) {
buffer->drawBitmap(0, 0, lcd, zone.x, zone.y, zone.w, zone.h);
GET_FILENAME(filename, BITMAPS_PATH, g_model.header.bitmap, BITMAPS_EXT);
@@ -82,3 +82,4 @@ class ModelBitmapWidget: public Widget
};
BaseWidgetFactory<ModelBitmapWidget> modelBitmapWidget("ModelBmp", NULL);
const WidgetFactory * defaultWidget = &modelBitmapWidget;