#include "./media_meta_info_loader.hpp" #include "./image_loader_webp.hpp" #include "./image_loader_sdl_bmp.hpp" #include "./image_loader_stb.hpp" #include #include #include MediaMetaInfoLoader::MediaMetaInfoLoader(RegistryMessageModel& rmm) : _rmm(rmm) { // HACK: make them be added externally? _image_loaders.push_back(std::make_unique()); _image_loaders.push_back(std::make_unique()); _image_loaders.push_back(std::make_unique()); _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) { return false; } bool MediaMetaInfoLoader::onEvent(const Message::Events::MessageUpdated& e) { if (e.e.any_of()) { return false; } if (!e.e.all_of()) { return false; } const auto& fil = e.e.get(); if (fil.file_list.size() != 1) { return false; } std::ifstream file(fil.file_list.front(), std::ios::binary); if (file.is_open()) { std::vector 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) { auto res = il->loadInfoFromMemory(tmp_buffer.data(), tmp_buffer.size()); if (res.height == 0 || res.width == 0) { continue; } e.e.emplace(res.width, res.height); could_load = true; std::cout << "MMIL loaded image info " << fil.file_list.front() << "\n"; _rmm.throwEventUpdate(e.e); break; } if (!could_load) { e.e.emplace(); std::cout << "MMIL loading failed image info " << fil.file_list.front() << "\n"; _rmm.throwEventUpdate(e.e); } } return false; }