ESPHome 2026.9.0-dev
Loading...
Searching...
No Matches
image_format.cpp
Go to the documentation of this file.
3#include "image_format.h"
4
6
7struct MimeLookup {
8 const char *mime_type;
9 ImageFormat format;
10};
11
12// The first entry per format is its canonical MIME type; the rest are aliases
13// seen from real servers (older IIS, CDNs, S3)
14static constexpr MimeLookup MIME_LOOKUP_TABLE[] = {
15#ifdef USE_RUNTIME_IMAGE_BMP
16 {"image/bmp", ImageFormat::BMP}, {"image/x-ms-bmp", ImageFormat::BMP}, {"image/x-bmp", ImageFormat::BMP},
17#endif
18#ifdef USE_RUNTIME_IMAGE_JPEG
19 {"image/jpeg", ImageFormat::JPEG}, {"image/jpg", ImageFormat::JPEG},
20#endif
21#ifdef USE_RUNTIME_IMAGE_PNG
22 {"image/png", ImageFormat::PNG}, {"image/x-png", ImageFormat::PNG},
23#endif
24#ifdef USE_RUNTIME_IMAGE_QOI
25 {"image/qoi", ImageFormat::QOI}, {"image/x-qoi", ImageFormat::QOI},
26#endif
27};
28
30 for (const auto &entry : MIME_LOOKUP_TABLE) {
31 if (entry.format == format) {
32 return entry.mime_type;
33 }
34 }
35 return "image/*"; // AUTO or compiled-out format
36}
37
38std::optional<ImageFormat> get_format_for_mime_type(const char *mime_type) {
39 for (const auto &entry : MIME_LOOKUP_TABLE) {
40 if (str_contains_ignore_case(mime_type, entry.mime_type)) {
41 return entry.format;
42 }
43 }
44 return std::nullopt;
45}
46
47} // namespace esphome::runtime_image
const char * format
const char * get_mime_type_for_format(ImageFormat format)
Canonical MIME type for a format; "image/*" for AUTO/unknown.
std::optional< ImageFormat > get_format_for_mime_type(const char *mime_type)
Case-insensitive substring match of known media types; nullopt if none found.
ImageFormat
Image format types that can be decoded dynamically.
bool str_contains_ignore_case(const char *haystack, const char *needle)
Case-insensitive check if needle string is contained in haystack (no heap allocation).
Definition helpers.h:998