Repository navigation
Expand file tree
/
Copy pathimageloader.cpp
More file actions
313 lines (254 loc) · 10.5 KB
/
Copy pathimageloader.cpp
File metadata and controls
313 lines (254 loc) · 10.5 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* 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 <vdr/tools.h>
#include <algorithm>
#include <array>
#include "./flat.h"
using Magick::Image;
using Magick::Geometry;
// External declaration of global logo overwrite flag
extern bool g_LogoBgOverwrite;
cImageLoader::cImageLoader() {}
cImageLoader::~cImageLoader() {}
/**
* @brief Load a logo from a file and scale it to the given width and height.
*
* This function tries to load the logo from the configured logo path and
* scales it to the given width and height. If the logo is not found, the
* function logs an error message and returns nullptr.
*
* @param logo The name of the logo (without path).
* @param width The desired width of the logo.
* @param height The desired height of the logo.
* @param Quiet If true, do not log an error message if the logo is not found.
* @return The loaded and scaled logo, or nullptr if the logo could not be loaded.
*/
cImage* cImageLoader::GetLogo(const char *logo, int width, int height, bool Quiet) {
if (width <= 0 || height <= 0 || isempty(logo)) return nullptr;
#ifdef DEBUGIMAGELOADTIME
dsyslog("flatPlus: cImageLoader::GetLogo() '%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 (int8_t 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 '/'
// Only convert '/' to '~' if the logo contains a '/' (e.g. 'logo 1/2.png')
if (LogoLower.find('/') == std::string::npos) {
isyslog("flatPlus: cImageLoader::GetLogo() %s/%s.%s could not be loaded (no '/' in logo name)",
*Config.LogoPath, logo, *m_LogoExtension);
return nullptr;
}
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 && !Quiet) // Third try and not found
isyslog("flatPlus: cImageLoader::GetLogo() %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
}
/**
* @brief Load an icon from a file and scale it to the given width and height.
*
* This function tries to load the icon from the configured icon path and
* scales it to the given width and height. If the icon is not found, the
* function logs an error message and returns nullptr.
*
* @param cIcon The name of the icon (without path).
* @param width The desired width of the icon.
* @param height The desired height of the icon.
* @return The loaded and scaled icon, or nullptr if the icon could not be loaded.
*/
cImage* cImageLoader::GetIcon(const char *cIcon, int width, int height) {
if (width <= 0 || height <= 0 || isempty(cIcon)) return nullptr;
cString File {cString::sprintf("%s/%s/%s.%s", *Config.IconPath, Setup.OSDTheme, cIcon, *m_LogoExtension)};
#ifdef DEBUGIMAGELOADTIME
dsyslog("flatPlus: cImageLoader::GetIcon() '%s'", *File);
cTimeMs Timer; // Start timer
#endif
cImage *img {ImgCache.GetImage(*File, width, height, true)}; // Check if image is in imagecache
#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::GetIcon() '%s'", *File);
Timer.Set(); // Reset timer
#endif
img = ImgCache.GetImage(*File, width, height, true); // Check if image is in imagecache
#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::GetIcon() '%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::GetIcon() '%s' 'CreateImage' failed", *File);
return nullptr;
}
ImgCache.InsertImage(img, *File, width, height, true); // Add image to iconcache
return img;
}
cImage* cImageLoader::GetFile(const char *cFile, int width, int height) {
if (width <= 0 || height <= 0 || isempty(cFile)) return nullptr;
const cString File {cFile};
#ifdef DEBUGIMAGELOADTIME
dsyslog("flatPlus: cImageLoader::GetFile() '%s'", *File);
cTimeMs Timer; // Start timer
#endif
cImage *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::GetFile() '%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::GetFile() '%s' 'CreateImage' failed", *File);
return nullptr;
}
/**
* @brief Load 'logo_background' from the configured logo path or theme directory.
*
* If g_LogoBgOverwrite is true, load the logo from the configured logo path.
* Otherwise, load the logo from the theme directory. g_LogoBgOverwrite is set
* in cFlatDisplayChannel::PreLoadImages().
*
* @param Width The desired width of the logo.
* @param Height The desired height of the logo.
* @return The loaded and scaled logo, or nullptr if the logo could not be loaded.
*/
cImage* cImageLoader::GetLogoBg(int Width, int Height) {
static constexpr const char *LogoBgName {"logo_background"};
return (g_LogoBgOverwrite) ? GetLogo(LogoBgName, Width, Height) : GetIcon(LogoBgName, Width, Height);
}
/**
* @brief Convert a string to lower case.
*
* This function is used to convert file names to lower case when searching for images
*
* @param str The string to convert to lower case
*/
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 |= 0x20; // Bitwise OR operation. ASCII values of uppercase letters are contiguous and can
// be converted to lowercase by setting the 5th bit (0x20)
}
}
// TODO: Improve? What images are to expect?
bool cImageLoader::SearchRecordingPoster(const cString &RecPath, cString &found) {
#ifdef DEBUGFUNCSCALL
dsyslog("flatPlus: cImageLoader::SearchRecordingPoster()");
#endif
static constexpr std::array<const char *, 4> Images {"banner.jpg", "poster.jpg", "fanart.jpg", "cover_vdr.jpg"};
for (const char* const &Image : Images) {
if (CheckImageExistence(RecPath, Image, found)) {
return true;
}
}
dsyslog("flatPlus: cImageLoader::SearchRecordingPoster() No image found for %s", *RecPath);
return false;
}
/**
* @brief Check if a certain image exists in the recording's folder.
*
* Given a recording path and an image name, this function checks if the image exists in the recording's folder,
* in the parent folder, or in the grand parent folder. If the image is found, its full path is stored in @a found.
*
* @param RecPath The path to the recording.
* @param Image The image name to check for.
* @param found The full path to the found image, if any.
*
* @return true if the image exists, false otherwise.
*/
bool cImageLoader::CheckImageExistence(const cString &RecPath, const cString &Image, cString &found) const {
cString ManualPoster = cString::sprintf("%s/%s", *RecPath, *Image);
if (LastModifiedTime(*ManualPoster)) {
found = ManualPoster;
return true;
}
if (Config.TVScraperSearchLocalPosters == 2) return false; // Search only in recording folder
ManualPoster = cString::sprintf("%s/../../../%s", *RecPath, *Image);
if (LastModifiedTime(*ManualPoster)) {
found = ManualPoster;
return true;
}
ManualPoster = cString::sprintf("%s/../../%s", *RecPath, *Image);
if (LastModifiedTime(*ManualPoster)) {
found = ManualPoster;
return true;
}
return false;
}