chore(color): Cleanup bitmap code (#4319)

This commit is contained in:
philmoz
2023-11-17 10:36:05 +10:00
committed by GitHub
parent b1ad3ad771
commit b0e43e09ea
6 changed files with 56 additions and 252 deletions
+1 -1
View File
@@ -234,7 +234,7 @@ void drawCurveRef(BitmapBuffer * dc, coord_t x, coord_t y, const CurveRef & curv
break;
case CURVE_REF_FUNC:
dc->drawTextAtIndex(x, y, STR_VCURVEFUNC, curve.value, flags);
dc->drawText(x, y, STR_VCURVEFUNC[curve.value], flags);
break;
case CURVE_REF_CUSTOM:
+1 -1
View File
@@ -408,7 +408,7 @@ static int luaLcdDrawNumber(lua_State *L)
char s[49];
int val = luaL_checkinteger(L, 3);
LcdFlags flags = luaL_optunsigned(L, 4, 0);
BitmapBuffer::formatNumberAsString(s, 49, val, flags & 0xFFFF);
formatNumberAsString(s, 49, val, flags & 0xFFFF);
drawString(L, s, flags);
return 0;
}
+51 -10
View File
@@ -407,7 +407,7 @@ char *getValueOrGVarString(char *dest, size_t len, gvar_t value, gvar_t vmin, gv
}
value += offset;
BitmapBuffer::formatNumberAsString(dest, len, value, flags, 0, nullptr, suffix);
formatNumberAsString(dest, len, value, flags, 0, nullptr, suffix);
return dest;
}
#endif
@@ -808,9 +808,9 @@ char *getValueWithUnit(char *dest, size_t len, int32_t val, uint8_t unit,
if (unit == UNIT_CELLS) unit = UNIT_VOLTS;
if ((flags & NO_UNIT) || (unit == UNIT_RAW)) {
flags = flags & (~NO_UNIT);
BitmapBuffer::formatNumberAsString(dest, len, val, flags);
formatNumberAsString(dest, len, val, flags);
} else {
BitmapBuffer::formatNumberAsString(dest, len, val, flags, 0, nullptr,
formatNumberAsString(dest, len, val, flags, 0, nullptr,
STR_VTELEMUNIT[unit]);
}
@@ -861,7 +861,7 @@ char *getSourceCustomValueString(char (&dest)[L], source_t source, int32_t val,
return getTimerString(dest, val, timerOptions);
}
else if (source == MIXSRC_TX_VOLTAGE) {
BitmapBuffer::formatNumberAsString(dest, len, val, flags | PREC1);
formatNumberAsString(dest, len, val, flags | PREC1);
return dest;
}
#if defined(INTERNAL_GPS)
@@ -884,34 +884,75 @@ char *getSourceCustomValueString(char (&dest)[L], source_t source, int32_t val,
#endif
#if defined(LUA_INPUTS)
else if (source >= MIXSRC_FIRST_LUA && source <= MIXSRC_LAST_LUA) {
BitmapBuffer::formatNumberAsString(dest, len, val, flags);
formatNumberAsString(dest, len, val, flags);
}
#endif
else if (source < MIXSRC_FIRST_CH) {
val = calcRESXto100(val);
BitmapBuffer::formatNumberAsString(dest, len, val, flags);
formatNumberAsString(dest, len, val, flags);
}
else if (source <= MIXSRC_LAST_CH) {
if (g_eeGeneral.ppmunit == PPM_PERCENT_PREC1) {
val = calcRESXto1000(val);
BitmapBuffer::formatNumberAsString(dest, len, val, flags | PREC1);
formatNumberAsString(dest, len, val, flags | PREC1);
} else {
val = calcRESXto100(val);
BitmapBuffer::formatNumberAsString(dest, len, val, flags);
formatNumberAsString(dest, len, val, flags);
}
}
else {
BitmapBuffer::formatNumberAsString(dest, len, val, flags);
formatNumberAsString(dest, len, val, flags);
}
return dest;
}
void formatNumberAsString(char *buffer, uint8_t buffer_size, int32_t val, LcdFlags flags, uint8_t len, const char * prefix, const char * suffix)
{
if (buffer) {
char str[48+1]; // max=16 for the prefix, 16 chars for the number, 16 chars for the suffix
char *s = str + 32;
*s = '\0';
int idx = 0;
int mode = MODE(flags);
bool neg = false;
if (val < 0) {
val = -val;
neg = true;
}
do {
*--s = '0' + (val % 10);
++idx;
val /= 10;
if (mode != 0 && idx == mode) {
mode = 0;
*--s = '.';
if (val == 0)
*--s = '0';
}
} while (val != 0 || mode > 0 || (mode == MODE(LEADING0) && idx < len));
if (neg) *--s = '-';
// TODO needs check on all string lengths ...
if (prefix) {
int len = strlen(prefix);
if (len <= 16) {
s -= len;
strncpy(s, prefix, len);
}
}
if (suffix) {
strncpy(&str[32], suffix, 16);
}
strncpy(buffer, s, buffer_size);
}
}
std::string formatNumberAsString(int32_t val, LcdFlags flags, uint8_t len,
const char *prefix, const char *suffix)
{
char s[49];
BitmapBuffer::formatNumberAsString(s, 49, val, flags, len, prefix, suffix);
formatNumberAsString(s, 49, val, flags, len, prefix, suffix);
return std::string(s);
}
+1
View File
@@ -80,6 +80,7 @@ char *strSetCursor(char *dest, int position);
char *strAppendDate(char *str, bool time = false);
char *strAppendFilename(char *dest, const char *filename, const int size);
std::string formatNumberAsString(int32_t val, LcdFlags flags = 0, uint8_t len = 0, const char * prefix = nullptr, const char * suffix = nullptr);
void formatNumberAsString(char *buffer, const uint8_t buffer_size, int32_t val, LcdFlags flags = 0, uint8_t len = 0, const char * prefix = nullptr, const char * suffix = nullptr);
#if !defined(BOOT)
char *getStringAtIndex(char *dest, const char *const *s, int idx);
+1 -214
View File
@@ -17,7 +17,6 @@
*/
#include <math.h>
#include <string.h>
#include "board.h"
#include "bitmapbuffer.h"
@@ -25,6 +24,7 @@
#include "libopenui_file.h"
#include "font.h"
#include "dma2d.h"
#include "strhelpers.h"
#include "lvgl/src/draw/sw/lv_draw_sw.h"
@@ -990,55 +990,6 @@ void BitmapBuffer::drawMask(coord_t x, coord_t y, const BitmapBuffer * mask, Lcd
}
}
void BitmapBuffer::drawMask(coord_t x, coord_t y, const BitmapBuffer * mask, const BitmapBuffer * srcBitmap, coord_t offsetX, coord_t offsetY, coord_t width, coord_t height)
{
if (!mask || !srcBitmap)
return;
APPLY_OFFSET();
coord_t maskWidth = mask->width();
coord_t maskHeight = mask->height();
if (!width || width > maskWidth) {
width = maskWidth;
}
if (!height || height > maskHeight) {
height = maskHeight;
}
if (x + width > xmax) {
width = xmax - x;
}
if (x < xmin) {
width += x - xmin;
offsetX -= x - xmin;
x = xmin;
}
if (y >= ymax || x >= xmax || width <= 0 || x + width < xmin || y + height < ymin)
return;
// TODO: This should be doable the same way as with
// drawBitmapPattern() (just with ARGB as input).
//
DMAWait();
for (coord_t row = 0; row < height; row++) {
if (y + row < ymin || y + row >= ymax)
continue;
pixel_t * p = getPixelPtrAbs(x, y + row);
const pixel_t * q = mask->getPixelPtrAbs(offsetX, offsetY + row);
for (coord_t col = 0; col < width; col++) {
drawAlphaPixel(p, *((uint8_t *)q), *srcBitmap->getPixelPtrAbs(row, col));
MOVE_TO_NEXT_RIGHT_PIXEL(p);
MOVE_TO_NEXT_RIGHT_PIXEL(q);
}
}
}
// Apply a mask ('bmp') + color ('flags') on top of current pixels:
//
// drawAlphaPixel(bmp[x][y], pixel(x,y), color)
@@ -1161,49 +1112,6 @@ coord_t BitmapBuffer::drawSizedText(coord_t x, coord_t y, const char *s,
return ((flags & RIGHT) ? orig_pos : pos) - offsetX;
}
void BitmapBuffer::formatNumberAsString(char *buffer, uint8_t buffer_size, int32_t val, LcdFlags flags, uint8_t len, const char * prefix, const char * suffix)
{
if (buffer == nullptr) {
return;
}
char str[48+1]; // max=16 for the prefix, 16 chars for the number, 16 chars for the suffix
char *s = str + 32;
*s = '\0';
int idx = 0;
int mode = MODE(flags);
bool neg = false;
if (val < 0) {
val = -val;
neg = true;
}
do {
*--s = '0' + (val % 10);
++idx;
val /= 10;
if (mode != 0 && idx == mode) {
mode = 0;
*--s = '.';
if (val == 0)
*--s = '0';
}
} while (val != 0 || mode > 0 || (mode == MODE(LEADING0) && idx < len));
if (neg) *--s = '-';
// TODO needs check on all string lengths ...
if (prefix) {
int len = strlen(prefix);
if (len <= 16) {
s -= len;
strncpy(s, prefix, len);
}
}
if (suffix) {
strncpy(&str[32], suffix, 16);
}
strncpy(buffer, s, buffer_size);
}
coord_t BitmapBuffer::drawNumber(coord_t x, coord_t y, int32_t val, LcdFlags flags, uint8_t len, const char * prefix, const char * suffix)
{
char s[49];
@@ -1219,39 +1127,6 @@ void drawSolidRect(BitmapBuffer * dc, coord_t x, coord_t y, coord_t w, coord_t h
dc->drawSolidFilledRect(x, y+h-thickness, w, thickness, flags);
}
//void BitmapBuffer::drawBitmapPie(int x0, int y0, const uint16_t * img, int startAngle, int endAngle)
//{
// const uint16_t * q = img;
// coord_t width = *q++;
// coord_t height = *q++;
//
// int slopes[4];
// if (!evalSlopes(slopes, startAngle, endAngle))
// return;
//
// int w2 = width/2;
// int h2 = height/2;
//
// for (int y=h2-1; y>=0; y--) {
// for (int x=w2-1; x>=0; x--) {
// int slope = (x==0 ? 99000 : y*100/x);
// if (slope >= slopes[0] && slope < slopes[1]) {
// *getPixelPtr(x0+w2+x, y0+h2-y) = q[(h2-y)*width + w2+x];
// }
// if (-slope >= slopes[0] && -slope < slopes[1]) {
// *getPixelPtr(x0+w2+x, y0+h2+y) = q[(h2+y)*width + w2+x];
// }
// if (slope >= slopes[2] && slope < slopes[3]) {
// *getPixelPtr(x0+w2-x, y0+h2-y) = q[(h2-y)*width + w2-x];
// }
// if (-slope >= slopes[2] && -slope < slopes[3]) {
// *getPixelPtr(x0+w2-x, y0+h2+y) = q[(h2+y)*width + w2-x];
// }
// }
// }
//}
//
BitmapBuffer * BitmapBuffer::loadBitmap(const char * filename, BitmapFormats fmt)
{
//TRACE(" BitmapBuffer::loadBitmap(%s)", filename);
@@ -1262,11 +1137,6 @@ BitmapBuffer * BitmapBuffer::loadBitmap(const char * filename, BitmapFormats fmt
return load_stb(filename, fmt);
}
BitmapBuffer * BitmapBuffer::loadRamBitmap(const uint8_t * buffer, int len)
{
return load_stb_buffer(buffer, len);
}
BitmapBuffer * BitmapBuffer::loadMask(const char * filename)
{
BitmapBuffer * bitmap = BitmapBuffer::loadBitmap(filename);
@@ -1324,77 +1194,6 @@ BitmapBuffer* BitmapBuffer::load8bitMaskLZ4(const uint8_t* compressed_data)
return bitmap;
}
BitmapBuffer * BitmapBuffer::invertMask() const
{
BitmapBuffer * result = new BitmapBuffer(format, width(), height());
pixel_t * srcData = data;
pixel_t * destData = result->data;
for (auto y = 0; y < height(); y++) {
for (auto x = 0; x < width(); x++) {
destData[x] = OPACITY_MAX - (uint8_t)srcData[x];
}
srcData += width();
destData += width();
}
return result;
}
BitmapBuffer * BitmapBuffer::horizontalFlip() const
{
BitmapBuffer * result = new BitmapBuffer(format, width(), height());
pixel_t * srcData = data;
pixel_t * destData = result->data;
for (uint8_t y = 0; y < height(); y++) {
for (uint8_t x = 0; x < width(); x++) {
destData[x] = srcData[width() - 1 - x];
}
srcData += width();
destData += width();
}
return result;
}
BitmapBuffer * BitmapBuffer::verticalFlip() const
{
BitmapBuffer * result = new BitmapBuffer(format, width(), height());
for (uint8_t y = 0; y < height(); y++) {
for (uint8_t x = 0; x < width(); x++) {
result->data[y * width() + x] = data[(height() - 1 - y) * width() + x];
}
}
return result;
}
BitmapBuffer * BitmapBuffer::loadMaskOnBackground(const char * filename, LcdFlags foreground, LcdFlags background)
{
BitmapBuffer * result = nullptr;
BitmapBuffer * mask = BitmapBuffer::loadMask(filename);
if (mask) {
result = new BitmapBuffer(BMP_RGB565, mask->width(), mask->height());
if (result) {
result->clear(background);
result->drawMask(0, 0, mask, foreground);
}
delete mask;
}
return result;
}
BitmapBuffer * BitmapBuffer::load8bitMaskOnBackground(const uint8_t * lbm, LcdFlags foreground, LcdFlags background)
{
BitmapBuffer * result = nullptr;
BitmapBuffer * mask = BitmapBuffer::load8bitMask(lbm);
if (mask) {
result = new BitmapBuffer(BMP_RGB565, mask->width(), mask->height());
if (result) {
result->clear(background);
result->drawMask(0, 0, mask, foreground);
}
delete mask;
}
return result;
}
FIL imgFile __DMA;
BitmapBuffer * BitmapBuffer::load_bmp(const char * filename)
@@ -1761,18 +1560,6 @@ BitmapBuffer * BitmapBuffer::convert_stb_bitmap(uint8_t * img, int w, int h, int
return bmp;
}
uint8_t * BitmapBuffer::loadFont(const uint8_t * lbm, int len, int& w, int& h)
{
int n;
unsigned char * font = stbi_load_from_memory(lbm, len, &w, &h, &n, 1);
if (!font) {
TRACE("load_stb_buffer(%p,%d) failed: %s", font, len, stbi_failure_reason());
return nullptr;
}
return font;
}
#include "../thirdparty/lz4/lz4.h"
LZ4Bitmap::LZ4Bitmap(uint8_t format, const uint8_t* compressed_data) :
+1 -26
View File
@@ -21,10 +21,7 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "opentx_types.h"
#include "libopenui_defines.h"
#include "opentx_helpers.h"
#include "debug.h"
constexpr uint8_t SOLID = 0xFF;
constexpr uint8_t DOTTED = 0x55;
@@ -212,6 +209,7 @@ class BitmapBuffer: public BitmapBufferBase<pixel_t>
~BitmapBuffer();
void setDrawCtx(lv_draw_ctx_t* ctx) { draw_ctx = ctx; }
void setData(uint16_t* d) {
if (!dataAllocated) {
data = d;
@@ -304,28 +302,18 @@ class BitmapBuffer: public BitmapBufferBase<pixel_t>
void drawAnnulusSector(coord_t x, coord_t y, coord_t internalRadius, coord_t externalRadius, int startAngle, int endAngle, LcdFlags flags = 0);
void drawBitmapPie(int x0, int y0, const uint16_t * img, 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, BitmapFormats fmt = BMP_INVALID);
static BitmapBuffer * loadRamBitmap(const uint8_t * buffer, int len);
static BitmapBuffer * loadMask(const char * filename);
static BitmapBuffer * load8bitMask(const uint8_t * lbm);
static BitmapBuffer * load8bitMaskLZ4(const uint8_t * compressed_data);
static BitmapBuffer * loadMaskOnBackground(const char * filename, LcdFlags foreground, LcdFlags background);
static BitmapBuffer * load8bitMaskOnBackground(const uint8_t * lbm, LcdFlags foreground, LcdFlags background);
static uint8_t * loadFont(const uint8_t * lbm, int len, int& w, int& h);
uint8_t * to8bitMask(size_t* size) const;
void drawMask(coord_t x, coord_t y, const BitmapBuffer * mask, LcdFlags flags, coord_t offsetX = 0, coord_t width = 0);
void drawMask(coord_t x, coord_t y, const BitmapBuffer * mask, const BitmapBuffer * srcBitmap, coord_t offsetX = 0, coord_t offsetY = 0, coord_t width = 0, coord_t height = 0);
void drawBitmapPattern(coord_t x, coord_t y, const uint8_t * bmp, LcdFlags flags, coord_t offset=0, coord_t width=0);
coord_t drawSizedText(coord_t x, coord_t y, const char * s, uint8_t len, LcdFlags flags=0);
@@ -336,13 +324,6 @@ class BitmapBuffer: public BitmapBufferBase<pixel_t>
return drawSizedText(x, y, s, strlen(s), flags);
}
coord_t drawTextAtIndex(coord_t x, coord_t y, const char *const *s, uint8_t idx, LcdFlags flags = 0)
{
return drawText(x, y, s[idx], flags);
}
static void formatNumberAsString(char *buffer, const uint8_t buffer_size, int32_t val, LcdFlags flags = 0, uint8_t len = 0, const char * prefix = nullptr, const char * suffix = nullptr);
coord_t drawNumber(coord_t x, coord_t y, int32_t val, LcdFlags flags = 0, uint8_t len = 0, const char * prefix = nullptr, const char * suffix = nullptr);
template<class T>
@@ -351,12 +332,6 @@ class BitmapBuffer: public BitmapBufferBase<pixel_t>
template<class T>
void drawScaledBitmap(const T * bitmap, coord_t x, coord_t y, coord_t w, coord_t h);
BitmapBuffer * horizontalFlip() const;
BitmapBuffer * verticalFlip() const;
BitmapBuffer * invertMask() const;
protected:
static BitmapBuffer * load_bmp(const char * filename);
static BitmapBuffer * load_stb(const char * filename, BitmapFormats fmt = BMP_INVALID);