Merge pull request #6002 from opentx/raphaelcoeffic/rle_bitmaps
[Horus] save space in bootloader and firmware by using RLE compression
This commit is contained in:
@@ -489,6 +489,8 @@ void BitmapBuffer::drawBitmapPatternPie(coord_t x0, coord_t y0, const uint8_t *
|
||||
}
|
||||
}
|
||||
|
||||
#if !defined(BOOT)
|
||||
|
||||
BitmapBuffer * BitmapBuffer::load(const char * filename)
|
||||
{
|
||||
const char * ext = getFileExtension(filename);
|
||||
@@ -743,7 +745,6 @@ void *stb_realloc(void *ptr, unsigned int oldsz, unsigned int newsz)
|
||||
}
|
||||
#endif // #if defined(TRACE_STB_MALLOC)
|
||||
|
||||
|
||||
#include "thirdparty/Stb/stb_image.h"
|
||||
|
||||
// fill 'data' with 'size' bytes. return number of bytes actually read
|
||||
@@ -831,3 +832,4 @@ BitmapBuffer * BitmapBuffer::load_stb(const char * filename)
|
||||
stbi_image_free(img);
|
||||
return bmp;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
|
||||
#include <inttypes.h>
|
||||
#include "colors.h"
|
||||
#include "rle.h"
|
||||
|
||||
#if defined(PCBX10) && !defined(SIMU)
|
||||
#define MOVE_PIXEL_RIGHT(p, count) p -= count
|
||||
@@ -59,6 +60,16 @@ class BitmapBufferBase
|
||||
{
|
||||
}
|
||||
|
||||
BitmapBufferBase(uint8_t format, T * data):
|
||||
format(format),
|
||||
data(data),
|
||||
data_end(NULL)
|
||||
{
|
||||
width = *((uint16_t*)data);
|
||||
height = *(((uint16_t*)data)+1);
|
||||
data_end = data + 4 + (width * height);
|
||||
}
|
||||
|
||||
inline uint8_t getFormat() const
|
||||
{
|
||||
return format;
|
||||
@@ -103,6 +114,28 @@ class BitmapBufferBase
|
||||
|
||||
typedef BitmapBufferBase<const uint16_t> Bitmap;
|
||||
|
||||
class RLEBitmap:
|
||||
public BitmapBufferBase<uint16_t>
|
||||
{
|
||||
public:
|
||||
RLEBitmap(uint8_t format, const uint8_t* rle_data)
|
||||
: BitmapBufferBase<uint16_t>(format, 0, 0, NULL)
|
||||
{
|
||||
width = *((uint16_t *)rle_data);
|
||||
height = *(((uint16_t *)rle_data)+1);
|
||||
|
||||
uint32_t pixels = width * height;
|
||||
data = (uint16_t*)malloc(pixels * sizeof(uint16_t));
|
||||
rle_decode_8bit((uint8_t*)data, pixels * sizeof(uint16_t), rle_data+4);
|
||||
data_end = data + pixels;
|
||||
}
|
||||
|
||||
~RLEBitmap()
|
||||
{
|
||||
free(data);
|
||||
}
|
||||
};
|
||||
|
||||
class BitmapBuffer: public BitmapBufferBase<uint16_t>
|
||||
{
|
||||
private:
|
||||
@@ -231,11 +264,13 @@ class BitmapBuffer: public BitmapBufferBase<uint16_t>
|
||||
|
||||
void drawBitmapPatternPie(coord_t x0, coord_t y0, const uint8_t * img, LcdFlags flags, int startAngle, int endAngle);
|
||||
|
||||
#if !defined(BOOT)
|
||||
static BitmapBuffer * load(const char * filename);
|
||||
|
||||
static BitmapBuffer * loadMask(const char * filename);
|
||||
|
||||
static BitmapBuffer * loadMaskOnBackground(const char * filename, LcdFlags foreground, LcdFlags background);
|
||||
#endif
|
||||
|
||||
void drawMask(coord_t x, coord_t y, BitmapBuffer * mask, LcdFlags flags, coord_t offset=0, coord_t width=0);
|
||||
|
||||
@@ -325,8 +360,10 @@ class BitmapBuffer: public BitmapBufferBase<uint16_t>
|
||||
}
|
||||
|
||||
protected:
|
||||
#if !defined(BOOT)
|
||||
static BitmapBuffer * load_bmp(const char * filename);
|
||||
static BitmapBuffer * load_stb(const char * filename);
|
||||
#endif
|
||||
};
|
||||
|
||||
extern BitmapBuffer * lcd;
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
*/
|
||||
|
||||
#include "opentx.h"
|
||||
#include "rle.h"
|
||||
|
||||
#if !defined(BOOT)
|
||||
const uint16_t font_tinsize_specs[] = {
|
||||
@@ -82,17 +83,18 @@ const pm_uchar font_stdsizebold[] = {
|
||||
|
||||
#if !defined(BOOT)
|
||||
const uint16_t * const fontspecsTable[16] = {
|
||||
font_stdsize_specs, font_tinsize_specs, font_smlsize_specs, font_midsize_specs, font_dblsize_specs, font_xxlsize_specs, font_stdsize_specs, font_stdsize_specs,
|
||||
font_stdsize_specs, font_tinsize_specs, font_smlsize_specs, font_midsize_specs, font_dblsize_specs, font_xxlsize_specs, font_stdsize_specs, font_stdsize_specs,
|
||||
font_stdsizebold_specs, font_tinsize_specs, font_smlsize_specs, font_midsize_specs, font_dblsize_specs, font_xxlsize_specs, font_stdsize_specs, font_stdsize_specs
|
||||
};
|
||||
|
||||
const uint8_t * const fontsTable[16] = {
|
||||
font_stdsize, font_tinsize, font_smlsize, font_midsize, font_dblsize, font_xxlsize, font_stdsize, font_stdsize,
|
||||
const uint8_t * fontsTable[16] = {
|
||||
font_stdsize, font_tinsize, font_smlsize, font_midsize, font_dblsize, font_xxlsize, font_stdsize, font_stdsize,
|
||||
font_stdsizebold, font_tinsize, font_smlsize, font_midsize, font_dblsize, font_xxlsize, font_stdsize, font_stdsize
|
||||
};
|
||||
|
||||
#else
|
||||
const uint16_t * const fontspecsTable[1] = { font_stdsize_specs };
|
||||
const uint8_t * const fontsTable[1] = { font_stdsize };
|
||||
const uint8_t * fontsTable[1] = { font_stdsize };
|
||||
#endif
|
||||
|
||||
BitmapBuffer * fontCache[2] = { NULL, NULL };
|
||||
@@ -117,3 +119,38 @@ void loadFontCache()
|
||||
fontCache[0] = createFontCache(fontsTable[0], TEXT_COLOR, TEXT_BGCOLOR);
|
||||
fontCache[1] = createFontCache(fontsTable[0], TEXT_INVERTED_COLOR, TEXT_INVERTED_BGCOLOR);
|
||||
}
|
||||
|
||||
|
||||
static uint8_t* decompressFont(const uint8_t* font)
|
||||
{
|
||||
uint16_t width = *((uint16_t *)font);
|
||||
uint16_t height = *(((uint16_t *)font)+1);
|
||||
|
||||
size_t font_size = width * height;
|
||||
uint8_t* dec_buf = (uint8_t*)malloc(font_size + 4);
|
||||
|
||||
// copy width / height
|
||||
memcpy(dec_buf,font,4);
|
||||
|
||||
rle_decode_8bit(dec_buf+4, font_size, font+4);
|
||||
return dec_buf;
|
||||
}
|
||||
|
||||
void loadFonts()
|
||||
{
|
||||
static bool fonts_loaded = false;
|
||||
if (fonts_loaded) return;
|
||||
|
||||
#if !defined(BOOT)
|
||||
int i=0;
|
||||
for (; i<9; i++)
|
||||
fontsTable[i] = decompressFont(fontsTable[i]);
|
||||
|
||||
for (; i<16; i++)
|
||||
fontsTable[i] = fontsTable[i-9];
|
||||
#else
|
||||
fontsTable[0] = decompressFont(fontsTable[0]);
|
||||
#endif
|
||||
|
||||
fonts_loaded = true;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
#include "rle.h"
|
||||
#include "debug.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
int rle_decode_8bit(unsigned char* dest, unsigned int dest_size, const unsigned char* src)
|
||||
{
|
||||
//unsigned char count = 0;
|
||||
unsigned char prev_byte = 0;
|
||||
bool prev_byte_valid = false;
|
||||
|
||||
const unsigned char* dest_end = dest + dest_size;
|
||||
unsigned char* d = dest;
|
||||
|
||||
while(d < dest_end) {
|
||||
|
||||
*d++ = *src;
|
||||
if (prev_byte_valid && (*src == prev_byte)) {
|
||||
|
||||
src++;
|
||||
|
||||
if (d + *src > dest + dest_size) {
|
||||
TRACE("rle_decode_8bit: destination overflow!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(d, prev_byte, *src);
|
||||
d += *src++;
|
||||
prev_byte_valid = false;
|
||||
}
|
||||
else {
|
||||
prev_byte = *src++;
|
||||
prev_byte_valid = true;
|
||||
}
|
||||
}
|
||||
|
||||
return d - dest;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
#ifndef _rle_h_
|
||||
#define _rle_h_
|
||||
|
||||
int rle_decode_8bit(unsigned char* dest, unsigned int dest_size, const unsigned char* src);
|
||||
|
||||
|
||||
#endif
|
||||
@@ -22,7 +22,10 @@
|
||||
|
||||
#if defined(SPLASH)
|
||||
|
||||
const uint8_t __bmp_splash[] {
|
||||
#include "bmp_splash.lbm"
|
||||
};
|
||||
Bitmap BMP_SPLASH(BMP_RGB565, (const uint16_t*)__bmp_splash);
|
||||
|
||||
void drawSplash()
|
||||
{
|
||||
@@ -45,6 +48,7 @@ void drawSplash()
|
||||
splashImg);
|
||||
}
|
||||
else {
|
||||
// TODO: verify / load / decompress bitmap
|
||||
lcd->drawBitmap((LCD_W - BMP_SPLASH.getWidth())/2,
|
||||
(LCD_H - BMP_SPLASH.getHeight())/2,
|
||||
&BMP_SPLASH);
|
||||
|
||||
@@ -34,8 +34,15 @@ void drawColumnHeader(const char * const * headers, const char * const * descrip
|
||||
}
|
||||
}
|
||||
|
||||
const uint8_t __alpha_button_on[] {
|
||||
#include "alpha_button_on.lbm"
|
||||
};
|
||||
Bitmap ALPHA_BUTTON_ON(BMP_ARGB4444, (const uint16_t*)__alpha_button_on);
|
||||
|
||||
const uint8_t __alpha_button_off[] {
|
||||
#include "alpha_button_off.lbm"
|
||||
};
|
||||
Bitmap ALPHA_BUTTON_OFF(BMP_ARGB4444, (const uint16_t*)__alpha_button_off);
|
||||
|
||||
void drawButton(coord_t x, coord_t y, const char * label, LcdFlags attr)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user