forked from sle118/FreeTouchDeck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageCache.cpp
84 lines (83 loc) · 3.22 KB
/
ImageCache.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "globals.hpp"
#include "ImageCache.h"
#include "UserConfig.h"
#include "ImageFormatBMP.h"
#include "ImageFormatJPG.h"
#include "Storage.h"
#include "ImageWrapper.h"
static const char *module = "ImageCache";
namespace FreeTouchDeck
{
std::vector<ImageWrapper *> ImageCache::ImageList; // reserve room for 100
ImageInstanceGetMap_t ImageCache::ConstructorList =
{
{"bmp", [](const std::string &fileName)
{
LOC_LOGD(module, "Getting Image instance from BMP constructor");
return (ImageWrapper *)ImageFormatBMP::GetImageInstance(fileName);
}},
{"jpg", [](const std::string &fileName)
{
LOC_LOGD(module, "Getting Image instance from JPG constructor");
return (ImageWrapper *)ImageFormatJPG::GetImageInstance(fileName);
}}};
ImageInstanceGet_t ImageCache::GetConstructorForImage(const std::string &imageName)
{
const std::string ext = ImageWrapper::GetExtension(imageName);
LOC_LOGD(module, "Looking for constructor for extension %s", ext.c_str());
for (auto c : ConstructorList)
{
if (ext == c.first)
{
LOC_LOGD(module, "Found Constructor for image type %s", ext.c_str());
return c.second;
}
}
return NULL;
}
ImageWrapper *ImageCache::GetImage(const std::string &imageName)
{
if (ImageList.size() == 0)
{
// On the first call, insert a generic invalid image as the first element
PrintMemInfo(__FUNCTION__, __LINE__);
ImageList.push_back((ImageWrapper *)new ImageFormatJPG());
PrintMemInfo(__FUNCTION__, __LINE__);
}
if(imageName.empty())
{
return ImageList.front();
}
else
{
LOC_LOGV(module, "Looking for image %s", imageName.c_str());
for (auto i : ImageList)
{
if (i->LogoName == imageName)
{
LOC_LOGV(module, "Returning cache entry for image %s", imageName.c_str());
return i;
}
}
}
LOC_LOGD(module, "Image cache entry not found for %s. Adding it.", imageName.c_str());
PrintMemInfo(__FUNCTION__, __LINE__);
ImageInstanceGet_t constructor = GetConstructorForImage(imageName);
if (!constructor)
{
LOC_LOGE(module, "Unsupported file format %s", ImageWrapper::GetExtension(imageName).c_str());
return ImageList.front();
}
else
{
PrintMemInfo(__FUNCTION__, __LINE__);
ImageWrapper * newImage=constructor(imageName);
LOC_LOGD(module,"Caching image name %s [%s]",newImage->LogoName.c_str(), newImage->valid?"VALID":"INVALID");
ImageList.push_back(newImage);
PrintMemInfo(__FUNCTION__, __LINE__);
}
ImageWrapper * returnedImage=ImageList.back();
LOC_LOGD(module,"Returning image name %s [%s]",returnedImage->LogoName.c_str(), returnedImage->valid?"VALID":"INVALID");
return ImageList.back(); // guaranteed to return at least the empty image
}
}