forked from vdr-projects/vdr-plugin-skinflatplus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimageloader.c
239 lines (185 loc) · 7.31 KB
/
imageloader.c
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* Skin flatPlus: A plugin for the Video Disk Recorder
*
* See the README file for copyright information and how to reach the author.
*
* $Id$
*/
#include "./imageloader.h"
#include <dirent.h>
#include <math.h>
#include <string>
#include "./flat.h"
using Magick::Image;
using Magick::Geometry;
cImageLoader::cImageLoader() {}
cImageLoader::~cImageLoader() {}
cImage* cImageLoader::LoadLogo(const char *logo, int width, int height) {
if (width == 0 || height == 0) return nullptr;
#ifdef DEBUGIMAGELOADTIME
dsyslog("flatPlus: cImageLoader::LoadLogo() '%s' %dx%d", logo, width, height);
cTimeMs Timer; // Start timer
#endif
// Plain logo without converting to lower including '/'
cString File = cString::sprintf("%s/%s.%s", *Config.LogoPath, logo, *m_LogoExtension);
std::string LogoLower {""};
bool success {false};
cImage *img {nullptr};
for (uint i {0}; i < 3; ++i) { // Run up to three times (0..2)
if (i == 1) { // Second try (Plain logo not found)
LogoLower.reserve(64); // Filename without path
LogoLower = logo;
ToLowerCase(LogoLower); // Convert to lowercase (A-Z)
File = cString::sprintf("%s/%s.%s", *Config.LogoPath, LogoLower.c_str(), *m_LogoExtension);
} else if (i == 2) { // Third try. Search for lowercase logo with '~' for path '/'
std::replace(LogoLower.begin(), LogoLower.end(), '/', '~');
File = cString::sprintf("%s/%s.%s", *Config.LogoPath, LogoLower.c_str(), *m_LogoExtension);
}
img = ImgCache.GetImage(*File, width, height); // Check if image is in imagecache
#ifdef DEBUGIMAGELOADTIME
dsyslog(" search in cache #%d: %ld ms", i + 1, Timer.Elapsed());
Timer.Set(); // Reset timer
#endif
if (img) return img; // Image found in imagecache
success = LoadImage(*File); // Try to load image from disk
if (!success) { // Image not found on disk
if (i == 2) // Third try and not found
isyslog("flatPlus: cImageLoader::LoadLogo() %s/%s.%s could not be loaded", *Config.LogoPath, logo,
*m_LogoExtension);
continue;
}
#ifdef DEBUGIMAGELOADTIME
dsyslog(" load file from disk #%d: %ld ms", i + 1, Timer.Elapsed());
Timer.Set(); // Reset timer
#endif
img = CreateImage(width, height);
if (!img) continue;
#ifdef DEBUGIMAGELOADTIME
dsyslog(" scale logo #%d: %ld ms", i + 1, Timer.Elapsed());
#endif
ImgCache.InsertImage(img, *File, width, height); // Add image to imagecache
return img; // Image loaded from disk
} // for
return nullptr; // No image; so return nullptr
}
cImage* cImageLoader::LoadIcon(const char *cIcon, int width, int height) {
if (width == 0 || height == 0) return nullptr;
cString File = cString::sprintf("%s/%s/%s.%s", *Config.IconPath, Setup.OSDTheme, cIcon, *m_LogoExtension);
#ifdef DEBUGIMAGELOADTIME
dsyslog("flatPlus: cImageLoader::LoadIcon() '%s'", *File);
cTimeMs Timer; // Start timer
#endif
cImage *img {nullptr};
img = ImgCache.GetImage(*File, width, height);
#ifdef DEBUGIMAGELOADTIME
dsyslog(" search in cache: %d ms", Timer.Elapsed());
Timer.Set(); // Reset timer
#endif
if (img) return img;
bool success = LoadImage(*File);
#ifdef DEBUGIMAGELOADTIME
dsyslog(" load file from disk: %d ms", Timer.Elapsed());
#endif
if (!success) { // Search for logo in default folder
File = cString::sprintf("%s/%s/%s.%s", *Config.IconPath, "default", cIcon, *m_LogoExtension);
#ifdef DEBUGIMAGELOADTIME
dsyslog("flatPlus: cImageLoader::LoadIcon() '%s'", *File);
Timer.Set(); // Reset timer
#endif
img = ImgCache.GetImage(*File, width, height);
#ifdef DEBUGIMAGELOADTIME
dsyslog(" search in cache: %d ms", Timer.Elapsed());
Timer.Set(); // Reset timer
#endif
if (img) return img;
success = LoadImage(*File);
#ifdef DEBUGIMAGELOADTIME
dsyslog(" load file from disk: %d ms", Timer.Elapsed());
Timer.Set(); // Reset timer
#endif
if (!success) {
isyslog("flatPlus: cImageLoader::LoadIcon() '%s' could not be loaded", *File);
return nullptr;
}
}
img = CreateImage(width, height);
#ifdef DEBUGIMAGELOADTIME
dsyslog(" scale logo: %d ms", Timer.Elapsed());
#endif
if (!img) {
dsyslog("flatPlus: cImageLoader::LoadIcon() '%s' 'CreateImage' failed", *File);
return nullptr;
}
ImgCache.InsertImage(img, *File, width, height);
return img;
}
cImage* cImageLoader::LoadFile(const char *cFile, int width, int height) {
if (width == 0 || height == 0) return nullptr;
const cString File = cFile;
cImage *img {nullptr};
#ifdef DEBUGIMAGELOADTIME
dsyslog("flatPlus: cImageLoader::LoadFile() '%s'", *File);
cTimeMs Timer; // Start timer
#endif
img = ImgCache.GetImage(*File, width, height);
#ifdef DEBUGIMAGELOADTIME
dsyslog(" search in cache: %ld ms", Timer.Elapsed());
Timer.Set(); // Reset timer
#endif
if (img) return img; // Image found in imagecache
const bool success = LoadImage(*File);
if (!success) {
isyslog("flatPlus: cImageLoader::LoadFile() '%s' could not be loaded", *File);
return nullptr;
}
#ifdef DEBUGIMAGELOADTIME
dsyslog(" load file from disk: %ld ms", Timer.Elapsed());
Timer.Set(); // Reset timer
#endif
img = CreateImage(width, height);
#ifdef DEBUGIMAGELOADTIME
dsyslog(" scale image: %ld ms", Timer.Elapsed());
#endif
if (img) {
ImgCache.InsertImage(img, *File, width, height);
return img;
}
dsyslog("flatPlus: cImageLoader::LoadFile() '%s' 'CreateImage' failed", *File);
return nullptr;
}
void cImageLoader::ToLowerCase(std::string &str) {
for (auto &ch : str) {
if (ch <= 'Z' && ch >= 'A') // Check for 'Z' first. Small letters start at 97
ch += 32; // Or: ch ^= 1 << 5;
}
}
// TODO: Improve? What images are to expect?
bool cImageLoader::SearchRecordingPoster(const cString &RecPath, cString &found) {
#ifdef DEBUGFUNCSCALL
dsyslog("flatPlus: cImageLoader::SearchRecordingPoster()");
#endif
//* Search for cover_vdr.jpg, poster.jpg, banner.jpg and fanart.jpg
cString ManualPoster {""};
for (const cString &Image : RecordingImages) {
ManualPoster = cString::sprintf("%s/%s", *RecPath, *Image);
if (std::filesystem::exists(*ManualPoster)) {
// dsyslog("flatPlus: Image found in %s/%s", *RecPath, *Image);
found = ManualPoster;
return true;
}
ManualPoster = cString::sprintf("%s/../../../%s", *RecPath, *Image);
if (std::filesystem::exists(*ManualPoster)) {
// dsyslog("flatPlus: Image found in %s/../../../%s", *RecPath, *Image);
found = ManualPoster;
return true;
}
ManualPoster = cString::sprintf("%s/../../%s", *RecPath, *Image);
if (std::filesystem::exists(*ManualPoster)) {
// dsyslog("flatPlus: Image found in %s/../../%s", *RecPath, *Image);
found = ManualPoster;
return true;
}
}
dsyslog("flatPlus: cImageLoader::SearchRecordingPoster() No image found in %s or above.", *RecPath);
return false;
}