tomato-testing/src/media_meta_info_loader.cpp

99 lines
2.5 KiB
C++
Raw Normal View History

2023-08-02 19:24:51 +02:00
#include "./media_meta_info_loader.hpp"
#include "./image_loader_webp.hpp"
#include "./image_loader_sdl_bmp.hpp"
#include "./image_loader_stb.hpp"
#include <solanaceae/message3/components.hpp>
#include <fstream>
#include <iostream>
2023-08-02 20:58:16 +02:00
void MediaMetaInfoLoader::handleMessage(const Message3Handle& m) {
if (m.any_of<Message::Components::TagNotImage, Message::Components::FrameDims>()) {
return;
2023-08-02 19:24:51 +02:00
}
2023-08-02 20:58:16 +02:00
if (!m.all_of<Message::Components::Transfer::FileInfoLocal, Message::Components::Transfer::TagHaveAll>()) {
return;
2023-08-02 19:24:51 +02:00
}
2023-08-02 20:58:16 +02:00
const auto& fil = m.get<Message::Components::Transfer::FileInfoLocal>();
2023-08-02 19:24:51 +02:00
if (fil.file_list.size() != 1) {
2023-08-02 20:58:16 +02:00
return;
2023-08-02 19:24:51 +02:00
}
std::ifstream file(fil.file_list.front(), std::ios::binary);
if (file.is_open()) {
// figure out size
file.seekg(0, file.end);
if (file.tellg() > 50*1024*1024) {
// TODO: conf
// dont try load files larger 50mb
return;
}
file.seekg(0, file.beg);
2023-08-02 19:24:51 +02:00
std::vector<uint8_t> tmp_buffer;
while (file.good()) {
auto ch = file.get();
if (ch == EOF) {
break;
} else {
tmp_buffer.push_back(ch);
}
}
bool could_load {false};
// try all loaders after another
for (auto& il : _image_loaders) {
// TODO: impl callback based load
2023-08-02 19:24:51 +02:00
auto res = il->loadInfoFromMemory(tmp_buffer.data(), tmp_buffer.size());
if (res.height == 0 || res.width == 0) {
continue;
}
2023-08-02 20:58:16 +02:00
m.emplace<Message::Components::FrameDims>(res.width, res.height);
2023-08-02 19:24:51 +02:00
could_load = true;
std::cout << "MMIL loaded image info " << fil.file_list.front() << "\n";
2023-08-02 20:58:16 +02:00
_rmm.throwEventUpdate(m);
2023-08-02 19:24:51 +02:00
break;
}
if (!could_load) {
2023-08-02 20:58:16 +02:00
m.emplace<Message::Components::TagNotImage>();
2023-08-02 19:24:51 +02:00
std::cout << "MMIL loading failed image info " << fil.file_list.front() << "\n";
2023-08-02 20:58:16 +02:00
_rmm.throwEventUpdate(m);
2023-08-02 19:24:51 +02:00
}
}
2023-08-02 20:58:16 +02:00
}
2023-08-02 19:24:51 +02:00
2023-08-02 20:58:16 +02:00
MediaMetaInfoLoader::MediaMetaInfoLoader(RegistryMessageModel& rmm) : _rmm(rmm) {
// HACK: make them be added externally?
_image_loaders.push_back(std::make_unique<ImageLoaderWebP>());
_image_loaders.push_back(std::make_unique<ImageLoaderSDLBMP>());
_image_loaders.push_back(std::make_unique<ImageLoaderSTB>());
_rmm.subscribe(this, RegistryMessageModel_Event::message_construct);
_rmm.subscribe(this, RegistryMessageModel_Event::message_updated);
}
MediaMetaInfoLoader::~MediaMetaInfoLoader(void) {
}
bool MediaMetaInfoLoader::onEvent(const Message::Events::MessageConstruct& e) {
handleMessage(e.e);
return false;
}
bool MediaMetaInfoLoader::onEvent(const Message::Events::MessageUpdated& e) {
handleMessage(e.e);
2023-08-02 19:24:51 +02:00
return false;
}