2023-08-02 16:36:34 +02:00
|
|
|
#include "./message_image_loader.hpp"
|
|
|
|
|
|
|
|
#include "./image_loader_sdl_bmp.hpp"
|
2024-03-04 13:38:55 +01:00
|
|
|
#include "./image_loader_qoi.hpp"
|
2023-08-02 16:36:34 +02:00
|
|
|
#include "./image_loader_webp.hpp"
|
2024-04-16 10:23:17 +02:00
|
|
|
#include "./image_loader_sdl_image.hpp"
|
2023-12-12 16:39:37 +01:00
|
|
|
#include "./media_meta_info_loader.hpp"
|
2023-08-02 16:36:34 +02:00
|
|
|
|
|
|
|
#include <solanaceae/message3/components.hpp>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <cassert>
|
|
|
|
#include <vector>
|
|
|
|
|
2024-01-12 16:45:52 +01:00
|
|
|
// fwd
|
|
|
|
namespace Message {
|
|
|
|
uint64_t getTimeMS(void);
|
|
|
|
}
|
|
|
|
|
2023-08-02 16:36:34 +02:00
|
|
|
MessageImageLoader::MessageImageLoader(void) {
|
|
|
|
_image_loaders.push_back(std::make_unique<ImageLoaderSDLBMP>());
|
2024-03-04 13:38:55 +01:00
|
|
|
_image_loaders.push_back(std::make_unique<ImageLoaderQOI>());
|
2023-08-02 16:36:34 +02:00
|
|
|
_image_loaders.push_back(std::make_unique<ImageLoaderWebP>());
|
2024-04-16 10:23:17 +02:00
|
|
|
_image_loaders.push_back(std::make_unique<ImageLoaderSDLImage>());
|
2023-08-02 16:36:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<TextureEntry> MessageImageLoader::load(TextureUploaderI& tu, Message3Handle m) {
|
|
|
|
if (!static_cast<bool>(m)) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2023-12-12 16:39:37 +01:00
|
|
|
if (m.all_of<Message::Components::TagNotImage>()) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2023-08-02 16:36:34 +02:00
|
|
|
if (m.all_of<Message::Components::Transfer::FileInfoLocal>()) {
|
|
|
|
const auto& file_list = m.get<Message::Components::Transfer::FileInfoLocal>().file_list;
|
|
|
|
assert(!file_list.empty());
|
|
|
|
const auto& file_path = file_list.front();
|
|
|
|
|
|
|
|
std::ifstream file(file_path, std::ios::binary);
|
|
|
|
if (file.is_open()) {
|
|
|
|
std::vector<uint8_t> tmp_buffer;
|
|
|
|
while (file.good()) {
|
|
|
|
auto ch = file.get();
|
|
|
|
if (ch == EOF) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
tmp_buffer.push_back(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// try all loaders after another
|
|
|
|
for (auto& il : _image_loaders) {
|
|
|
|
auto res = il->loadFromMemoryRGBA(tmp_buffer.data(), tmp_buffer.size());
|
|
|
|
if (res.frames.empty() || res.height == 0 || res.width == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextureEntry new_entry;
|
2024-01-12 16:45:52 +01:00
|
|
|
new_entry.timestamp_last_rendered = Message::getTimeMS();
|
2023-08-02 16:36:34 +02:00
|
|
|
new_entry.current_texture = 0;
|
|
|
|
for (const auto& [ms, data] : res.frames) {
|
|
|
|
const auto n_t = tu.uploadRGBA(data.data(), res.width, res.height);
|
|
|
|
new_entry.textures.push_back(n_t);
|
|
|
|
new_entry.frame_duration.push_back(ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
new_entry.width = res.width;
|
|
|
|
new_entry.height = res.height;
|
|
|
|
|
|
|
|
std::cout << "MIL: loaded image file " << file_path << "\n";
|
|
|
|
|
|
|
|
return new_entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-02 19:24:51 +02:00
|
|
|
std::cerr << "MIL: failed to load message\n";
|
|
|
|
|
2023-08-02 16:36:34 +02:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|