memory image cache

This commit is contained in:
mrbesen 2022-04-23 22:28:01 +02:00
parent 6daa3d5d91
commit 24425de9e8
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
5 changed files with 94 additions and 4 deletions

View File

@ -7,6 +7,7 @@
#include <opencv2/opencv.hpp>
#include "datadragonimagecache.h"
#include "memoryimagecache.h"
class DataDragon {
public:
@ -50,6 +51,7 @@ private:
CURL* curl = nullptr; // the curl (does curling)
DataDragonImageCache cache[3];
MemoryImageCache memcache;
};

View File

@ -0,0 +1,28 @@
#pragma once
#include <functional>
#include <string>
#include <opencv2/opencv.hpp>
class MemoryImageCache {
public:
MemoryImageCache(size_t maxsize = 25);
void addImage(cv::Mat, const std::string& title, int type);
cv::Mat getImage(const std::string& title, int type);
private:
void cleanUp();
struct CachedImage {
time_t lastaccessed = 0;
cv::Mat imageref;
std::string title;
int type;
bool operator<(const CachedImage& other) const;
};
static std::function<bool(const CachedImage&)> getImageMatcher(const std::string& title, int type);
std::list<CachedImage> cache;
size_t maxsize;
};

View File

@ -30,6 +30,7 @@ SOURCES += \
src/main.cpp \
src/mainwindow.cpp \
src/matcher.cpp \
src/memoryimagecache.cpp \
src/scaleableinputs.cpp \
src/screen.cpp \
src/stagesettings.cpp \
@ -44,6 +45,7 @@ HEADERS += \
include/lolautoaccept.h \
include/mainwindow.h \
include/matcher.h \
include/memoryimagecache.h \
include/scaleableinputs.h \
include/screen.h \
include/stagesettings.h \

View File

@ -112,10 +112,17 @@ const std::vector<DataDragon::ChampData>& DataDragon::getChamps() {
}
cv::Mat DataDragon::getImage(const std::string& champid, ImageType imgtype) {
// query cache
cv::Mat img = cache[(int) imgtype].getImage(champid);
if(!img.empty())
// query mem cache
cv::Mat img = memcache.getImage(champid, (int) imgtype);
if(!img.empty()) return img;
// query HDD cache
img = cache[(int) imgtype].getImage(champid);
if(!img.empty()) {
// update mem cache
memcache.addImage(img, champid, (int) imgtype);
return img;
}
std::string url;
if(imgtype == ImageType::SQUARE) {
@ -146,13 +153,17 @@ cv::Mat DataDragon::getImage(const std::string& champid, ImageType imgtype) {
return {};
}
// store cache
// store HDD cache
cache[(int) imgtype].addImageRaw(arr, champid);
cv::Mat rawData(1, arr.size(), CV_8UC1, (void*) arr.data());
cv::Mat decodedImage = cv::imdecode(rawData, cv::IMREAD_COLOR);
cv::cvtColor(decodedImage, decodedImage, cv::COLOR_BGR2RGB);
// store mem cache
memcache.addImage(decodedImage, champid, (int) imgtype);
return decodedImage;
}

47
src/memoryimagecache.cpp Normal file
View File

@ -0,0 +1,47 @@
#include "memoryimagecache.h"
#include <algorithm>
#include <Log.h>
MemoryImageCache::MemoryImageCache(size_t maxsize) : maxsize(maxsize) {}
void MemoryImageCache::addImage(cv::Mat img, const std::string& title, int type) {
auto it = std::find_if(cache.begin(), cache.end(), getImageMatcher(title, type));
if(it == cache.end()) {
// insert new
cache.push_back({time(0), img, title, type});
cleanUp();
return;
}
// update old
it->lastaccessed = time(0);
it->imageref = img;
}
cv::Mat MemoryImageCache::getImage(const std::string& title, int type) {
auto it = std::find_if(cache.begin(), cache.end(), getImageMatcher(title, type));
if(it == cache.end()) {
return {};
}
it->lastaccessed = time(0);
return it->imageref;
}
void MemoryImageCache::cleanUp() {
if(cache.size() < maxsize) return;
auto oldest = std::min_element(cache.begin(), cache.end());
cache.erase(oldest);
}
bool MemoryImageCache::CachedImage::operator<(const MemoryImageCache::CachedImage& other) const {
return lastaccessed < other.lastaccessed;
}
std::function<bool(const MemoryImageCache::CachedImage&)> MemoryImageCache::getImageMatcher(const std::string& title, int type) {
return [title, type](const MemoryImageCache::CachedImage& img) -> bool {
return img.type == type && img.title == title;
};
}