mirror of
https://github.com/esphome/esphome.git
synced 2025-02-04 23:51:40 +01:00
[online_image] Code Improvements (#8130)
This commit is contained in:
parent
7fccc9ff86
commit
fc847c1de8
@ -52,6 +52,14 @@ class Format:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class BMPFormat(Format):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__("BMP")
|
||||||
|
|
||||||
|
def actions(self):
|
||||||
|
cg.add_define("USE_ONLINE_IMAGE_BMP_SUPPORT")
|
||||||
|
|
||||||
|
|
||||||
class PNGFormat(Format):
|
class PNGFormat(Format):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__("PNG")
|
super().__init__("PNG")
|
||||||
@ -61,20 +69,12 @@ class PNGFormat(Format):
|
|||||||
cg.add_library("pngle", "1.0.2")
|
cg.add_library("pngle", "1.0.2")
|
||||||
|
|
||||||
|
|
||||||
class BMPFormat(Format):
|
|
||||||
def __init__(self):
|
|
||||||
super().__init__("BMP")
|
|
||||||
|
|
||||||
def actions(self):
|
|
||||||
cg.add_define("USE_ONLINE_IMAGE_BMP_SUPPORT")
|
|
||||||
|
|
||||||
|
|
||||||
# New formats can be added here.
|
# New formats can be added here.
|
||||||
IMAGE_FORMATS = {
|
IMAGE_FORMATS = {
|
||||||
x.image_type: x
|
x.image_type: x
|
||||||
for x in (
|
for x in (
|
||||||
PNGFormat(),
|
|
||||||
BMPFormat(),
|
BMPFormat(),
|
||||||
|
PNGFormat(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ class ImageDecoder {
|
|||||||
*
|
*
|
||||||
* @param download_size The total number of bytes that need to be downloaded for the image.
|
* @param download_size The total number of bytes that need to be downloaded for the image.
|
||||||
*/
|
*/
|
||||||
virtual void prepare(uint32_t download_size) { this->download_size_ = download_size; }
|
virtual void prepare(size_t download_size) { this->download_size_ = download_size; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Decode a part of the image. It will try reading from the buffer.
|
* @brief Decode a part of the image. It will try reading from the buffer.
|
||||||
@ -75,8 +75,8 @@ class ImageDecoder {
|
|||||||
OnlineImage *image_;
|
OnlineImage *image_;
|
||||||
// Initializing to 1, to ensure it is distinguishable from initial "decoded_bytes_".
|
// Initializing to 1, to ensure it is distinguishable from initial "decoded_bytes_".
|
||||||
// Will be overwritten anyway once the download size is known.
|
// Will be overwritten anyway once the download size is known.
|
||||||
uint32_t download_size_ = 1;
|
size_t download_size_ = 1;
|
||||||
uint32_t decoded_bytes_ = 0;
|
size_t decoded_bytes_ = 0;
|
||||||
double x_scale_ = 1.0;
|
double x_scale_ = 1.0;
|
||||||
double y_scale_ = 1.0;
|
double y_scale_ = 1.0;
|
||||||
};
|
};
|
||||||
|
@ -6,13 +6,12 @@ static const char *const TAG = "online_image";
|
|||||||
|
|
||||||
#include "image_decoder.h"
|
#include "image_decoder.h"
|
||||||
|
|
||||||
#ifdef USE_ONLINE_IMAGE_PNG_SUPPORT
|
|
||||||
#include "png_image.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef USE_ONLINE_IMAGE_BMP_SUPPORT
|
#ifdef USE_ONLINE_IMAGE_BMP_SUPPORT
|
||||||
#include "bmp_image.h"
|
#include "bmp_image.h"
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_ONLINE_IMAGE_PNG_SUPPORT
|
||||||
|
#include "png_image.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace online_image {
|
namespace online_image {
|
||||||
@ -122,16 +121,16 @@ void OnlineImage::update() {
|
|||||||
ESP_LOGD(TAG, "Starting download");
|
ESP_LOGD(TAG, "Starting download");
|
||||||
size_t total_size = this->downloader_->content_length;
|
size_t total_size = this->downloader_->content_length;
|
||||||
|
|
||||||
#ifdef USE_ONLINE_IMAGE_PNG_SUPPORT
|
|
||||||
if (this->format_ == ImageFormat::PNG) {
|
|
||||||
this->decoder_ = make_unique<PngDecoder>(this);
|
|
||||||
}
|
|
||||||
#endif // ONLINE_IMAGE_PNG_SUPPORT
|
|
||||||
#ifdef USE_ONLINE_IMAGE_BMP_SUPPORT
|
#ifdef USE_ONLINE_IMAGE_BMP_SUPPORT
|
||||||
if (this->format_ == ImageFormat::BMP) {
|
if (this->format_ == ImageFormat::BMP) {
|
||||||
this->decoder_ = make_unique<BmpDecoder>(this);
|
this->decoder_ = make_unique<BmpDecoder>(this);
|
||||||
}
|
}
|
||||||
#endif // ONLINE_IMAGE_BMP_SUPPORT
|
#endif // ONLINE_IMAGE_BMP_SUPPORT
|
||||||
|
#ifdef USE_ONLINE_IMAGE_PNG_SUPPORT
|
||||||
|
if (this->format_ == ImageFormat::PNG) {
|
||||||
|
this->decoder_ = make_unique<PngDecoder>(this);
|
||||||
|
}
|
||||||
|
#endif // ONLINE_IMAGE_PNG_SUPPORT
|
||||||
|
|
||||||
if (!this->decoder_) {
|
if (!this->decoder_) {
|
||||||
ESP_LOGE(TAG, "Could not instantiate decoder. Image format unsupported.");
|
ESP_LOGE(TAG, "Could not instantiate decoder. Image format unsupported.");
|
||||||
|
@ -41,7 +41,7 @@ static void draw_callback(pngle_t *pngle, uint32_t x, uint32_t y, uint32_t w, ui
|
|||||||
decoder->draw(x, y, w, h, color);
|
decoder->draw(x, y, w, h, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PngDecoder::prepare(uint32_t download_size) {
|
void PngDecoder::prepare(size_t download_size) {
|
||||||
ImageDecoder::prepare(download_size);
|
ImageDecoder::prepare(download_size);
|
||||||
pngle_set_user_data(this->pngle_, this);
|
pngle_set_user_data(this->pngle_, this);
|
||||||
pngle_set_init_callback(this->pngle_, init_callback);
|
pngle_set_init_callback(this->pngle_, init_callback);
|
||||||
@ -51,7 +51,7 @@ void PngDecoder::prepare(uint32_t download_size) {
|
|||||||
int HOT PngDecoder::decode(uint8_t *buffer, size_t size) {
|
int HOT PngDecoder::decode(uint8_t *buffer, size_t size) {
|
||||||
if (!this->pngle_) {
|
if (!this->pngle_) {
|
||||||
ESP_LOGE(TAG, "PNG decoder engine not initialized!");
|
ESP_LOGE(TAG, "PNG decoder engine not initialized!");
|
||||||
return -1;
|
return DECODE_ERROR_OUT_OF_MEMORY;
|
||||||
}
|
}
|
||||||
if (size < 256 && size < this->download_size_ - this->decoded_bytes_) {
|
if (size < 256 && size < this->download_size_ - this->decoded_bytes_) {
|
||||||
ESP_LOGD(TAG, "Waiting for data");
|
ESP_LOGD(TAG, "Waiting for data");
|
||||||
|
@ -21,7 +21,7 @@ class PngDecoder : public ImageDecoder {
|
|||||||
PngDecoder(OnlineImage *image) : ImageDecoder(image), pngle_(pngle_new()) {}
|
PngDecoder(OnlineImage *image) : ImageDecoder(image), pngle_(pngle_new()) {}
|
||||||
~PngDecoder() override { pngle_destroy(this->pngle_); }
|
~PngDecoder() override { pngle_destroy(this->pngle_); }
|
||||||
|
|
||||||
void prepare(uint32_t download_size) override;
|
void prepare(size_t download_size) override;
|
||||||
int HOT decode(uint8_t *buffer, size_t size) override;
|
int HOT decode(uint8_t *buffer, size_t size) override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
Loading…
Reference in New Issue
Block a user