ESPHome 2025.6.3
Loading...
Searching...
No Matches
online_image.cpp
Go to the documentation of this file.
1#include "online_image.h"
2
3#include "esphome/core/log.h"
4
5static const char *const TAG = "online_image";
6static const char *const ETAG_HEADER_NAME = "etag";
7static const char *const IF_NONE_MATCH_HEADER_NAME = "if-none-match";
8static const char *const LAST_MODIFIED_HEADER_NAME = "last-modified";
9static const char *const IF_MODIFIED_SINCE_HEADER_NAME = "if-modified-since";
10
11#include "image_decoder.h"
12
13#ifdef USE_ONLINE_IMAGE_BMP_SUPPORT
14#include "bmp_image.h"
15#endif
16#ifdef USE_ONLINE_IMAGE_JPEG_SUPPORT
17#include "jpeg_image.h"
18#endif
19#ifdef USE_ONLINE_IMAGE_PNG_SUPPORT
20#include "png_image.h"
21#endif
22
23namespace esphome {
24namespace online_image {
25
27
28inline bool is_color_on(const Color &color) {
29 // This produces the most accurate monochrome conversion, but is slightly slower.
30 // return (0.2125 * color.r + 0.7154 * color.g + 0.0721 * color.b) > 127;
31
32 // Approximation using fast integer computations; produces acceptable results
33 // Equivalent to 0.25 * R + 0.5 * G + 0.25 * B
34 return ((color.r >> 2) + (color.g >> 1) + (color.b >> 2)) & 0x80;
35}
36
37OnlineImage::OnlineImage(const std::string &url, int width, int height, ImageFormat format, ImageType type,
38 image::Transparency transparency, uint32_t download_buffer_size)
39 : Image(nullptr, 0, 0, type, transparency),
40 buffer_(nullptr),
41 download_buffer_(download_buffer_size),
42 download_buffer_initial_size_(download_buffer_size),
43 format_(format),
44 fixed_width_(width),
45 fixed_height_(height) {
46 this->set_url(url);
47}
48
49void OnlineImage::draw(int x, int y, display::Display *display, Color color_on, Color color_off) {
50 if (this->data_start_) {
51 Image::draw(x, y, display, color_on, color_off);
52 } else if (this->placeholder_) {
53 this->placeholder_->draw(x, y, display, color_on, color_off);
54 }
55}
56
58 if (this->buffer_) {
59 ESP_LOGV(TAG, "Deallocating old buffer");
60 this->allocator_.deallocate(this->buffer_, this->get_buffer_size_());
61 this->data_start_ = nullptr;
62 this->buffer_ = nullptr;
63 this->width_ = 0;
64 this->height_ = 0;
65 this->buffer_width_ = 0;
66 this->buffer_height_ = 0;
67 this->last_modified_ = "";
68 this->etag_ = "";
69 this->end_connection_();
70 }
71}
72
73size_t OnlineImage::resize_(int width_in, int height_in) {
74 int width = this->fixed_width_;
75 int height = this->fixed_height_;
76 if (this->is_auto_resize_()) {
77 width = width_in;
78 height = height_in;
79 if (this->width_ != width && this->height_ != height) {
80 this->release();
81 }
82 }
83 size_t new_size = this->get_buffer_size_(width, height);
84 if (this->buffer_) {
85 // Buffer already allocated => no need to resize
86 return new_size;
87 }
88 ESP_LOGD(TAG, "Allocating new buffer of %zu bytes", new_size);
89 this->buffer_ = this->allocator_.allocate(new_size);
90 if (this->buffer_ == nullptr) {
91 ESP_LOGE(TAG, "allocation of %zu bytes failed. Biggest block in heap: %zu Bytes", new_size,
93 this->end_connection_();
94 return 0;
95 }
96 this->buffer_width_ = width;
97 this->buffer_height_ = height;
98 this->width_ = width;
99 ESP_LOGV(TAG, "New size: (%d, %d)", width, height);
100 return new_size;
101}
102
104 if (this->decoder_) {
105 ESP_LOGW(TAG, "Image already being updated.");
106 return;
107 }
108 ESP_LOGI(TAG, "Updating image %s", this->url_.c_str());
109
110 std::list<http_request::Header> headers = {};
111
112 http_request::Header accept_header;
113 accept_header.name = "Accept";
114 std::string accept_mime_type;
115 switch (this->format_) {
116#ifdef USE_ONLINE_IMAGE_BMP_SUPPORT
117 case ImageFormat::BMP:
118 accept_mime_type = "image/bmp";
119 break;
120#endif // USE_ONLINE_IMAGE_BMP_SUPPORT
121#ifdef USE_ONLINE_IMAGE_JPEG_SUPPORT
123 accept_mime_type = "image/jpeg";
124 break;
125#endif // USE_ONLINE_IMAGE_JPEG_SUPPORT
126#ifdef USE_ONLINE_IMAGE_PNG_SUPPORT
127 case ImageFormat::PNG:
128 accept_mime_type = "image/png";
129 break;
130#endif // USE_ONLINE_IMAGE_PNG_SUPPORT
131 default:
132 accept_mime_type = "image/*";
133 }
134 accept_header.value = accept_mime_type + ",*/*;q=0.8";
135
136 if (!this->etag_.empty()) {
137 headers.push_back(http_request::Header{IF_NONE_MATCH_HEADER_NAME, this->etag_});
138 }
139
140 if (!this->last_modified_.empty()) {
141 headers.push_back(http_request::Header{IF_MODIFIED_SINCE_HEADER_NAME, this->last_modified_});
142 }
143
144 headers.push_back(accept_header);
145
146 for (auto &header : this->request_headers_) {
147 headers.push_back(http_request::Header{header.first, header.second.value()});
148 }
149
150 this->downloader_ = this->parent_->get(this->url_, headers, {ETAG_HEADER_NAME, LAST_MODIFIED_HEADER_NAME});
151
152 if (this->downloader_ == nullptr) {
153 ESP_LOGE(TAG, "Download failed.");
154 this->end_connection_();
155 this->download_error_callback_.call();
156 return;
157 }
158
159 int http_code = this->downloader_->status_code;
160 if (http_code == HTTP_CODE_NOT_MODIFIED) {
161 // Image hasn't changed on server. Skip download.
162 ESP_LOGI(TAG, "Server returned HTTP 304 (Not Modified). Download skipped.");
163 this->end_connection_();
164 this->download_finished_callback_.call(true);
165 return;
166 }
167 if (http_code != HTTP_CODE_OK) {
168 ESP_LOGE(TAG, "HTTP result: %d", http_code);
169 this->end_connection_();
170 this->download_error_callback_.call();
171 return;
172 }
173
174 ESP_LOGD(TAG, "Starting download");
175 size_t total_size = this->downloader_->content_length;
176
177#ifdef USE_ONLINE_IMAGE_BMP_SUPPORT
178 if (this->format_ == ImageFormat::BMP) {
179 ESP_LOGD(TAG, "Allocating BMP decoder");
180 this->decoder_ = make_unique<BmpDecoder>(this);
181 }
182#endif // USE_ONLINE_IMAGE_BMP_SUPPORT
183#ifdef USE_ONLINE_IMAGE_JPEG_SUPPORT
184 if (this->format_ == ImageFormat::JPEG) {
185 ESP_LOGD(TAG, "Allocating JPEG decoder");
187 }
188#endif // USE_ONLINE_IMAGE_JPEG_SUPPORT
189#ifdef USE_ONLINE_IMAGE_PNG_SUPPORT
190 if (this->format_ == ImageFormat::PNG) {
191 ESP_LOGD(TAG, "Allocating PNG decoder");
192 this->decoder_ = make_unique<PngDecoder>(this);
193 }
194#endif // USE_ONLINE_IMAGE_PNG_SUPPORT
195
196 if (!this->decoder_) {
197 ESP_LOGE(TAG, "Could not instantiate decoder. Image format unsupported: %d", this->format_);
198 this->end_connection_();
199 this->download_error_callback_.call();
200 return;
201 }
202 auto prepare_result = this->decoder_->prepare(total_size);
203 if (prepare_result < 0) {
204 this->end_connection_();
205 this->download_error_callback_.call();
206 return;
207 }
208 ESP_LOGI(TAG, "Downloading image (Size: %zu)", total_size);
209 this->start_time_ = ::time(nullptr);
210}
211
213 if (!this->decoder_) {
214 // Not decoding at the moment => nothing to do.
215 return;
216 }
217 if (!this->downloader_ || this->decoder_->is_finished()) {
218 this->data_start_ = buffer_;
219 this->width_ = buffer_width_;
220 this->height_ = buffer_height_;
221 ESP_LOGD(TAG, "Image fully downloaded, read %zu bytes, width/height = %d/%d", this->downloader_->get_bytes_read(),
222 this->width_, this->height_);
223 ESP_LOGD(TAG, "Total time: %lds", ::time(nullptr) - this->start_time_);
224 this->etag_ = this->downloader_->get_response_header(ETAG_HEADER_NAME);
225 this->last_modified_ = this->downloader_->get_response_header(LAST_MODIFIED_HEADER_NAME);
226 this->download_finished_callback_.call(false);
227 this->end_connection_();
228 return;
229 }
230 if (this->downloader_ == nullptr) {
231 ESP_LOGE(TAG, "Downloader not instantiated; cannot download");
232 return;
233 }
234 size_t available = this->download_buffer_.free_capacity();
235 if (available) {
236 // Some decoders need to fully download the image before downloading.
237 // In case of huge images, don't wait blocking until the whole image has been downloaded,
238 // use smaller chunks
239 available = std::min(available, this->download_buffer_initial_size_);
240 auto len = this->downloader_->read(this->download_buffer_.append(), available);
241 if (len > 0) {
243 auto fed = this->decoder_->decode(this->download_buffer_.data(), this->download_buffer_.unread());
244 if (fed < 0) {
245 ESP_LOGE(TAG, "Error when decoding image.");
246 this->end_connection_();
247 this->download_error_callback_.call();
248 return;
249 }
250 this->download_buffer_.read(fed);
251 }
252 }
253}
254
257 if (color.g == 1 && color.r == 0 && color.b == 0) {
258 color.g = 0;
259 }
260 if (color.w < 0x80) {
261 color.r = 0;
262 color.g = this->type_ == ImageType::IMAGE_TYPE_RGB565 ? 4 : 1;
263 color.b = 0;
264 }
265 }
266}
267
268void OnlineImage::draw_pixel_(int x, int y, Color color) {
269 if (!this->buffer_) {
270 ESP_LOGE(TAG, "Buffer not allocated!");
271 return;
272 }
273 if (x < 0 || y < 0 || x >= this->buffer_width_ || y >= this->buffer_height_) {
274 ESP_LOGE(TAG, "Tried to paint a pixel (%d,%d) outside the image!", x, y);
275 return;
276 }
277 uint32_t pos = this->get_position_(x, y);
278 switch (this->type_) {
280 const uint32_t width_8 = ((this->width_ + 7u) / 8u) * 8u;
281 pos = x + y * width_8;
282 auto bitno = 0x80 >> (pos % 8u);
283 pos /= 8u;
284 auto on = is_color_on(color);
285 if (this->has_transparency() && color.w < 0x80)
286 on = false;
287 if (on) {
288 this->buffer_[pos] |= bitno;
289 } else {
290 this->buffer_[pos] &= ~bitno;
291 }
292 break;
293 }
295 uint8_t gray = static_cast<uint8_t>(0.2125 * color.r + 0.7154 * color.g + 0.0721 * color.b);
297 if (gray == 1) {
298 gray = 0;
299 }
300 if (color.w < 0x80) {
301 gray = 1;
302 }
304 if (color.w != 0xFF)
305 gray = color.w;
306 }
307 this->buffer_[pos] = gray;
308 break;
309 }
311 this->map_chroma_key(color);
312 uint16_t col565 = display::ColorUtil::color_to_565(color);
313 this->buffer_[pos + 0] = static_cast<uint8_t>((col565 >> 8) & 0xFF);
314 this->buffer_[pos + 1] = static_cast<uint8_t>(col565 & 0xFF);
316 this->buffer_[pos + 2] = color.w;
317 }
318 break;
319 }
321 this->map_chroma_key(color);
322 this->buffer_[pos + 0] = color.r;
323 this->buffer_[pos + 1] = color.g;
324 this->buffer_[pos + 2] = color.b;
326 this->buffer_[pos + 3] = color.w;
327 }
328 break;
329 }
330 }
331}
332
334 if (this->downloader_) {
335 this->downloader_->end();
336 this->downloader_ = nullptr;
337 }
338 this->decoder_.reset();
339 this->download_buffer_.reset();
340}
341
342bool OnlineImage::validate_url_(const std::string &url) {
343 if ((url.length() < 8) || (url.find("http") != 0) || (url.find("://") == std::string::npos)) {
344 ESP_LOGE(TAG, "URL is invalid and/or must be prefixed with 'http://' or 'https://'");
345 return false;
346 }
347 return true;
348}
349
350void OnlineImage::add_on_finished_callback(std::function<void(bool)> &&callback) {
351 this->download_finished_callback_.add(std::move(callback));
352}
353
354void OnlineImage::add_on_error_callback(std::function<void()> &&callback) {
355 this->download_error_callback_.add(std::move(callback));
356}
357
358} // namespace online_image
359} // namespace esphome
esphome::http_request::HttpRequestComponent * parent_
Definition helpers.h:550
void deallocate(T *p, size_t n)
Definition helpers.h:742
size_t get_max_free_block_size() const
Return the maximum size block this allocator could allocate.
Definition helpers.h:770
T * allocate(size_t n)
Definition helpers.h:704
static uint16_t color_to_565(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
std::shared_ptr< HttpContainer > get(const std::string &url)
const uint8_t * data_start_
Definition image.h:55
bool has_transparency() const
Definition image.h:41
ImageType type_
Definition image.h:54
Transparency transparency_
Definition image.h:56
void draw(int x, int y, display::Display *display, Color color_on, Color color_off) override
Definition image.cpp:9
uint8_t * data(size_t offset=0)
std::string etag_
The value of the ETag HTTP header provided in the last response.
size_t resize_(int width, int height)
Resize the image buffer to the requested dimensions.
bool validate_url_(const std::string &url)
int get_position_(int x, int y) const
CallbackManager< void(bool)> download_finished_callback_
void draw_pixel_(int x, int y, Color color)
Draw a pixel into the buffer.
std::vector< std::pair< std::string, TemplatableValue< std::string > > > request_headers_
friend void ImageDecoder::draw(int x, int y, int w, int h, const Color &color)
void add_on_error_callback(std::function< void()> &&callback)
std::unique_ptr< ImageDecoder > decoder_
RAMAllocator< uint8_t > allocator_
void add_on_finished_callback(std::function< void(bool)> &&callback)
int buffer_width_
Actual width of the current image.
ESPHOME_ALWAYS_INLINE bool is_auto_resize_() const
void set_url(const std::string &url)
Set the URL to download the image from.
std::shared_ptr< http_request::HttpContainer > downloader_
const int fixed_width_
width requested on configuration, or 0 if non specified.
CallbackManager< void()> download_error_callback_
const int fixed_height_
height requested on configuration, or 0 if non specified.
int buffer_height_
Actual height of the current image.
size_t download_buffer_initial_size_
This is the initial size of the download buffer, not the current size.
std::string last_modified_
The value of the Last-Modified HTTP header provided in the last response.
OnlineImage(const std::string &url, int width, int height, ImageFormat format, image::ImageType type, image::Transparency transparency, uint32_t buffer_size)
Construct a new OnlineImage object.
void release()
Release the buffer storing the image.
uint8_t type
@ TRANSPARENCY_ALPHA_CHANNEL
Definition image.h:22
@ TRANSPARENCY_CHROMA_KEY
Definition image.h:21
@ IMAGE_TYPE_GRAYSCALE
Definition image.h:14
@ IMAGE_TYPE_BINARY
Definition image.h:13
@ IMAGE_TYPE_RGB565
Definition image.h:16
@ IMAGE_TYPE_RGB
Definition image.h:15
bool is_color_on(const Color &color)
ImageFormat
Format that the image is encoded with.
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:302
std::unique_ptr< T > make_unique(Args &&...args)
Definition helpers.h:86
uint8_t w
Definition color.h:26
uint8_t g
Definition color.h:18
uint8_t b
Definition color.h:22
uint8_t r
Definition color.h:14
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6