ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
image_format.cpp
Go to the documentation of this file.
4#include "image_format.h"
5
7
8struct MimeLookup {
9 const char *mime_type;
10 ImageFormat format;
11};
12
13// The first entry per format is its canonical MIME type; the rest are aliases
14// seen from real servers (older IIS, CDNs, S3)
15static constexpr MimeLookup MIME_LOOKUP_TABLE[] = {
16#ifdef USE_RUNTIME_IMAGE_BMP
17 {"image/bmp", ImageFormat::BMP}, {"image/x-ms-bmp", ImageFormat::BMP}, {"image/x-bmp", ImageFormat::BMP},
18#endif
19#ifdef USE_RUNTIME_IMAGE_JPEG
20 {"image/jpeg", ImageFormat::JPEG}, {"image/jpg", ImageFormat::JPEG},
21#endif
22#ifdef USE_RUNTIME_IMAGE_PNG
23 {"image/png", ImageFormat::PNG}, {"image/x-png", ImageFormat::PNG},
24#endif
25#ifdef USE_RUNTIME_IMAGE_QOI
26 {"image/qoi", ImageFormat::QOI}, {"image/x-qoi", ImageFormat::QOI},
27#endif
28};
29
30PROGMEM_STRING_TABLE(ImageFormatStrings, "AUTO", "BMP", "JPEG", "PNG", "QOI", "UNKNOWN");
31
33 for (const auto &entry : MIME_LOOKUP_TABLE) {
34 if (entry.format == format) {
35 return entry.mime_type;
36 }
37 }
38 return "image/*"; // AUTO or compiled-out format
39}
40
41std::optional<ImageFormat> get_format_for_mime_type(const char *mime_type) {
42 for (const auto &entry : MIME_LOOKUP_TABLE) {
43 if (str_contains_ignore_case(mime_type, entry.mime_type)) {
44 return entry.format;
45 }
46 }
47 return std::nullopt;
48}
49
51 return ImageFormatStrings::get_log_str(static_cast<uint8_t>(format), ImageFormatStrings::LAST_INDEX);
52}
53
54} // 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.
const LogString * get_format_name(ImageFormat format)
Human-readable name for a format; "UNKNOWN" if not recognized.
PROGMEM_STRING_TABLE(ImageFormatStrings, "AUTO", "BMP", "JPEG", "PNG", "QOI", "UNKNOWN")
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:1094