mirror of
https://github.com/celisej567/abaddon.git
synced 2026-09-13 20:18:50 +03:00
maybe slightly better image loading maybe hopefully
This commit is contained in:
@@ -1,44 +1,68 @@
|
||||
#include "imgmanager.hpp"
|
||||
#include "util.hpp"
|
||||
|
||||
ImageManager::ImageManager() {
|
||||
m_cb_dispatcher.connect(sigc::mem_fun(*this, &ImageManager::RunCallbacks));
|
||||
}
|
||||
|
||||
Cache &ImageManager::GetCache() {
|
||||
return m_cache;
|
||||
}
|
||||
|
||||
void ImageManager::LoadFromURL(std::string url, std::function<void(Glib::RefPtr<Gdk::Pixbuf>)> cb) {
|
||||
/*if (m_pixs.find(url) != m_pixs.end()) {
|
||||
cb(m_pixs.at(url));
|
||||
return;
|
||||
}*/
|
||||
Glib::RefPtr<Gdk::Pixbuf> ImageManager::ReadFileToPixbuf(std::string path) {
|
||||
const auto &data = ReadWholeFile(path);
|
||||
if (data.size() == 0) return Glib::RefPtr<Gdk::Pixbuf>(nullptr);
|
||||
auto loader = Gdk::PixbufLoader::create();
|
||||
loader->signal_size_prepared().connect([&loader](int w, int h) {
|
||||
int cw, ch;
|
||||
GetImageDimensions(w, h, cw, ch); // what could go wrong
|
||||
loader->set_size(cw, ch);
|
||||
});
|
||||
loader->write(static_cast<const guint8 *>(data.data()), data.size());
|
||||
loader->close();
|
||||
auto buf = loader->get_pixbuf();
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
void ImageManager::LoadFromURL(std::string url, callback_type cb) {
|
||||
m_cache.GetFileFromURL(url, [this, url, cb](std::string path) {
|
||||
try {
|
||||
auto buf = Gdk::Pixbuf::create_from_file(path);
|
||||
//m_pixs[url] = buf;
|
||||
cb(buf);
|
||||
auto buf = ReadFileToPixbuf(path);
|
||||
m_cb_mutex.lock();
|
||||
m_cb_queue.push(std::make_pair(buf, cb));
|
||||
m_cb_dispatcher.emit();
|
||||
m_cb_mutex.unlock();
|
||||
} catch (std::exception &e) {
|
||||
fprintf(stderr, "err loading pixbuf from %s: %s\n", path.c_str(), e.what());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void ImageManager::RunCallbacks() {
|
||||
m_cb_mutex.lock();
|
||||
const auto &pair = m_cb_queue.front();
|
||||
pair.second(pair.first);
|
||||
m_cb_queue.pop();
|
||||
m_cb_mutex.unlock();
|
||||
}
|
||||
|
||||
Glib::RefPtr<Gdk::Pixbuf> ImageManager::GetFromURLIfCached(std::string url) {
|
||||
std::string path = m_cache.GetPathIfCached(url);
|
||||
if (path != "")
|
||||
return Gdk::Pixbuf::create_from_file(path);
|
||||
//if (m_pixs.find(url) != m_pixs.end())
|
||||
// return m_pixs.at(url);
|
||||
return ReadFileToPixbuf(path);
|
||||
|
||||
return Glib::RefPtr<Gdk::Pixbuf>(nullptr);
|
||||
}
|
||||
|
||||
Glib::RefPtr<Gdk::Pixbuf> ImageManager::GetPlaceholder(int size) {
|
||||
std::string name = "/placeholder" + std::to_string(size);
|
||||
//if (m_pixs.find(name) != m_pixs.end())
|
||||
// return m_pixs.at(name);
|
||||
if (m_pixs.find(name) != m_pixs.end())
|
||||
return m_pixs.at(name);
|
||||
|
||||
try {
|
||||
auto buf = Gdk::Pixbuf::create_from_file("res/decamarks.png", size, size);
|
||||
// m_pixs[name] = buf;
|
||||
m_pixs[name] = buf;
|
||||
return buf;
|
||||
} catch (std::exception &e) {
|
||||
fprintf(stderr, "error loading placeholder\n");
|
||||
|
||||
Reference in New Issue
Block a user