From 03c9b2c4a20ef26e992bb369035f3c120d5c00b7 Mon Sep 17 00:00:00 2001 From: Damjan Adamic Date: Sat, 2 Jan 2016 20:21:30 +0100 Subject: [PATCH] PNG loading works --- radio/src/bmp.cpp | 88 ++++++++++++++----- radio/src/gui/horus/lcd.h | 2 + .../src/gui/horus/menu_general_sdmanager.cpp | 10 +++ 3 files changed, 80 insertions(+), 20 deletions(-) diff --git a/radio/src/bmp.cpp b/radio/src/bmp.cpp index f2e28961d..9e5267b05 100644 --- a/radio/src/bmp.cpp +++ b/radio/src/bmp.cpp @@ -1,23 +1,23 @@ -/* - * Copyright (C) OpenTX - * - * Based on code named - * th9x - http://code.google.com/p/th9x - * er9x - http://code.google.com/p/er9x - * gruvin9x - http://code.google.com/p/gruvin9x - * - * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - */ - +/* + * Copyright (C) OpenTX + * + * Based on code named + * th9x - http://code.google.com/p/th9x + * er9x - http://code.google.com/p/er9x + * gruvin9x - http://code.google.com/p/gruvin9x + * + * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + #include "opentx.h" #if defined(PCBTARANIS) @@ -422,3 +422,51 @@ const char *writeScreenshot() return NULL; } + + +#define STB_IMAGE_IMPLEMENTATION +#define STBI_ONLY_PNG +#include "thirdparty/Stb/stb_image.h" + + +const char * pngLoad(uint8_t * bmp, const char * filename, uint16_t width, uint16_t height) +{ + int x,y,n; + unsigned char *data = stbi_load(filename, &x, &y, &n, 3); + if (!data) { + return "stb error"; + } + + //convert to 565 fromat + // todo use dma2d for conversion from 888 to 565 + unsigned char *p = data; + uint16_t * dest = (uint16_t *)bmp; + + *dest++ = min(width, x); + *dest++ = min(height, y); + + for(int row = 0; row < min(height, y); ++row) { + unsigned char *l = p; + for(int col = 0; col < min(width, x); ++col) { + *dest = RGB(l[0], l[1], l[2]); + ++dest; + l += 3; + } + p += 3 * x; + } + stbi_image_free(data); + return 0; +// // ... process data if not NULL ... +// // ... x = width, y = height, n = # 8-bit components per pixel ... +// // ... replace '0' with '1'..'4' to force that many components per pixel +// // ... but 'n' will always be the number that it would have been if you said 0 +// stbi_image_free(data) +// +// Standard parameters: +// int *x -- outputs image width in pixels +// int *y -- outputs image height in pixels +// int *comp -- outputs # of image components in image file +// int req_comp -- if non-zero, # of image components requested in result + + +} \ No newline at end of file diff --git a/radio/src/gui/horus/lcd.h b/radio/src/gui/horus/lcd.h index 2eadee4f1..042147a8d 100644 --- a/radio/src/gui/horus/lcd.h +++ b/radio/src/gui/horus/lcd.h @@ -311,6 +311,8 @@ void lcdSetContrast(); #define lcdOff(...) const char * bmpLoad(uint8_t * dest, const char * filename, uint16_t width, uint16_t height); +const char * pngLoad(uint8_t * bmp, const char * filename, uint16_t width, uint16_t height); + #if defined(BOOT) #define BLINK_ON_PHASE (0) diff --git a/radio/src/gui/horus/menu_general_sdmanager.cpp b/radio/src/gui/horus/menu_general_sdmanager.cpp index e62755885..a1b7a88ed 100644 --- a/radio/src/gui/horus/menu_general_sdmanager.cpp +++ b/radio/src/gui/horus/menu_general_sdmanager.cpp @@ -366,6 +366,16 @@ bool menuGeneralSdManager(evt_t _event) } lcdDrawBitmap(LCD_W/2, (LCD_H-MODEL_BITMAP_HEIGHT)/2, modelBitmap); } + + if (ext && !strcasecmp(ext, ".png")) { + if (lastBitmap != menuVerticalPosition) { + lastBitmap = menuVerticalPosition; + if (pngLoad(modelBitmap, reusableBuffer.sdmanager.lines[index], MODEL_BITMAP_WIDTH, MODEL_BITMAP_HEIGHT)) { + ((uint32_t *)modelBitmap)[0] = 0; + } + } + lcdDrawBitmap(LCD_W/2, (LCD_H-MODEL_BITMAP_HEIGHT)/2, modelBitmap); + } return true; }