mirror of
https://github.com/opentx/opentx.git
synced 2025-07-16 12:55:12 +03:00
Re #3167: Code cleanup, old bitmap loading code removed, now stb_image used exclusively. Malloc traces disabled.
This commit is contained in:
parent
3660101e37
commit
cefc78dbe8
2 changed files with 3 additions and 174 deletions
|
@ -388,169 +388,6 @@ void BitmapBuffer::drawBitmapPatternPie(coord_t x0, coord_t y0, const uint8_t *
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BitmapBuffer * BitmapBuffer::load_bmp(const char * filename)
|
|
||||||
{
|
|
||||||
FIL bmpFile;
|
|
||||||
UINT read;
|
|
||||||
uint8_t palette[16];
|
|
||||||
uint8_t bmpBuf[LCD_W]; /* maximum with LCD_W */
|
|
||||||
uint8_t * buf = &bmpBuf[0];
|
|
||||||
|
|
||||||
FRESULT result = f_open(&bmpFile, filename, FA_OPEN_EXISTING | FA_READ);
|
|
||||||
if (result != FR_OK) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (f_size(&bmpFile) < 14) {
|
|
||||||
f_close(&bmpFile);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
result = f_read(&bmpFile, buf, 14, &read);
|
|
||||||
if (result != FR_OK || read != 14) {
|
|
||||||
f_close(&bmpFile);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (buf[0] != 'B' || buf[1] != 'M') {
|
|
||||||
f_close(&bmpFile);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t fsize = *((uint32_t *)&buf[2]);
|
|
||||||
uint32_t hsize = *((uint32_t *)&buf[10]); /* header size */
|
|
||||||
|
|
||||||
uint32_t len = limit((uint32_t)4, (uint32_t)(hsize-14), (uint32_t)32);
|
|
||||||
result = f_read(&bmpFile, buf, len, &read);
|
|
||||||
if (result != FR_OK || read != len) {
|
|
||||||
f_close(&bmpFile);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t ihsize = *((uint32_t *)&buf[0]); /* more header size */
|
|
||||||
|
|
||||||
/* invalid header size */
|
|
||||||
if (ihsize + 14 > hsize) {
|
|
||||||
f_close(&bmpFile);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* sometimes file size is set to some headers size, set a real size in that case */
|
|
||||||
if (fsize == 14 || fsize == ihsize + 14)
|
|
||||||
fsize = f_size(&bmpFile) - 2;
|
|
||||||
|
|
||||||
/* declared file size less than header size */
|
|
||||||
if (fsize <= hsize) {
|
|
||||||
f_close(&bmpFile);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t w, h;
|
|
||||||
|
|
||||||
switch (ihsize){
|
|
||||||
case 40: // windib
|
|
||||||
case 56: // windib v3
|
|
||||||
case 64: // OS/2 v2
|
|
||||||
case 108: // windib v4
|
|
||||||
case 124: // windib v5
|
|
||||||
w = *((uint32_t *)&buf[4]);
|
|
||||||
h = *((uint32_t *)&buf[8]);
|
|
||||||
buf += 12;
|
|
||||||
break;
|
|
||||||
case 12: // OS/2 v1
|
|
||||||
w = *((uint16_t *)&buf[4]);
|
|
||||||
h = *((uint16_t *)&buf[6]);
|
|
||||||
buf += 8;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
f_close(&bmpFile);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (*((uint16_t *)&buf[0]) != 1) { /* planes */
|
|
||||||
f_close(&bmpFile);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint16_t depth = *((uint16_t *)&buf[2]);
|
|
||||||
|
|
||||||
buf = &bmpBuf[0];
|
|
||||||
|
|
||||||
if (depth == 4) {
|
|
||||||
if (f_lseek(&bmpFile, hsize-64) != FR_OK || f_read(&bmpFile, buf, 64, &read) != FR_OK || read != 64) {
|
|
||||||
f_close(&bmpFile);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
for (uint8_t i=0; i<16; i++) {
|
|
||||||
palette[i] = buf[4*i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (f_lseek(&bmpFile, hsize) != FR_OK) {
|
|
||||||
f_close(&bmpFile);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
BitmapBuffer * bmp = new BitmapBuffer(w, h);
|
|
||||||
if (bmp == NULL) {
|
|
||||||
f_close(&bmpFile);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint16_t * dest = bmp->data;
|
|
||||||
uint32_t rowSize;
|
|
||||||
|
|
||||||
switch (depth) {
|
|
||||||
case 32:
|
|
||||||
for (int i=h-1; i>=0; i--) {
|
|
||||||
uint8_t * dst = ((uint8_t *)dest) + i*w*2;
|
|
||||||
for (unsigned int j=0; j<w; j++) {
|
|
||||||
uint32_t pixel;
|
|
||||||
result = f_read(&bmpFile, (uint8_t *)&pixel, 4, &read);
|
|
||||||
if (result != FR_OK || read != 4) {
|
|
||||||
f_close(&bmpFile);
|
|
||||||
delete bmp;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
*((uint16_t *)dst) = RGB((pixel>>24) & 0xff, (pixel>>16) & 0xff, (pixel>>8) & 0xff);
|
|
||||||
dst += 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 1:
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 4:
|
|
||||||
rowSize = ((4*w+31)/32)*4;
|
|
||||||
for (int32_t i=h-1; i>=0; i--) {
|
|
||||||
result = f_read(&bmpFile, buf, rowSize, &read);
|
|
||||||
if (result != FR_OK || read != rowSize) {
|
|
||||||
f_close(&bmpFile);
|
|
||||||
delete bmp;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
uint8_t * dst = ((uint8_t *)dest) + i*w*2;
|
|
||||||
for (uint32_t j=0; j<w; j++) {
|
|
||||||
uint8_t index = (buf[j/2] >> ((j & 1) ? 0 : 4)) & 0x0F;
|
|
||||||
uint8_t val = palette[index];
|
|
||||||
*((uint16_t *)dst) = RGB(val, val, val);
|
|
||||||
dst += 2;
|
|
||||||
// *dst++ = 0x0F;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
f_close(&bmpFile);
|
|
||||||
delete bmp;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
f_close(&bmpFile);
|
|
||||||
return bmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
#define STB_IMAGE_IMPLEMENTATION
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
#define STBI_ONLY_PNG
|
#define STBI_ONLY_PNG
|
||||||
|
@ -559,7 +396,7 @@ BitmapBuffer * BitmapBuffer::load_bmp(const char * filename)
|
||||||
#define STBI_ONLY_GIF
|
#define STBI_ONLY_GIF
|
||||||
#define STBI_NO_STDIO
|
#define STBI_NO_STDIO
|
||||||
|
|
||||||
#define TRACE_STB_MALLOC
|
// #define TRACE_STB_MALLOC
|
||||||
|
|
||||||
#if defined(TRACE_STB_MALLOC)
|
#if defined(TRACE_STB_MALLOC)
|
||||||
#define STBI_MALLOC(sz) stb_malloc(sz)
|
#define STBI_MALLOC(sz) stb_malloc(sz)
|
||||||
|
@ -628,7 +465,7 @@ const stbi_io_callbacks stbCallbacks = {
|
||||||
stbc_eof
|
stbc_eof
|
||||||
};
|
};
|
||||||
|
|
||||||
BitmapBuffer * BitmapBuffer::load_stb(const char * filename)
|
BitmapBuffer * BitmapBuffer::load(const char * filename)
|
||||||
{
|
{
|
||||||
FIL imgFile;
|
FIL imgFile;
|
||||||
|
|
||||||
|
|
|
@ -132,15 +132,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);
|
void drawBitmapPatternPie(coord_t x0, coord_t y0, const uint8_t * img, LcdFlags flags, int startAngle, int endAngle);
|
||||||
|
|
||||||
static BitmapBuffer * load_bmp(const char * filename);
|
static BitmapBuffer * load(const char * filename);
|
||||||
static BitmapBuffer * load_stb(const char * filename);
|
|
||||||
static BitmapBuffer * load(const char * filename) {
|
|
||||||
#if defined(USE_STB)
|
|
||||||
return load_stb(filename);
|
|
||||||
#else
|
|
||||||
return load_bmp(filename);
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
|
|
||||||
void drawBitmapPattern(coord_t x, coord_t y, const uint8_t * bmp, LcdFlags flags, coord_t offset=0, coord_t width=0);
|
void drawBitmapPattern(coord_t x, coord_t y, const uint8_t * bmp, LcdFlags flags, coord_t offset=0, coord_t width=0);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue