summaryrefslogtreecommitdiff
path: root/imageloader.c
blob: a5e3a91defc6fb796840c713847d01abb2202b1e (plain)
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
#include "imageloader.h"
#include <math.h>
#include <string>
#include <dirent.h>
#include <iostream>
#include "flat.h"

using namespace Magick;

cImageLoader::cImageLoader() {
    InitializeMagick(NULL);
    logoExtension = "png";
}

cImageLoader::~cImageLoader() {
}

cImage* cImageLoader::LoadLogo(const char *logo, int width, int height) {
    if( (width == 0) || (height==0) )
        return NULL;
    std::string logoLower = logo;
    toLowerCase(logoLower);
    cString File = cString::sprintf("%s/%s.%s", *Config.logoPath, logoLower.c_str(), *logoExtension);
    #ifdef DEBUGIMAGELOADTIME
        dsyslog("imageloader load logo %s", *File);
    #endif

    cImage *img;
    #ifdef DEBUGIMAGELOADTIME
        uint32_t tick1 = GetMsTicks();
    #endif

    img = imgCache.GetImage( *File, width, height );

    #ifdef DEBUGIMAGELOADTIME
        uint32_t tick2 = GetMsTicks();
        dsyslog("   search in cache: %d ms", tick2 - tick1);
    #endif
    if( img != NULL )
        return img;

    #ifdef DEBUGIMAGELOADTIME
        uint32_t tick3 = GetMsTicks();
    #endif

    bool success = LoadImage(File);

    if( !success ) {
        dsyslog("imageloader LoadLogo: %s could not be loaded", *File);
        return NULL;
    }
    #ifdef DEBUGIMAGELOADTIME
        uint32_t tick4 = GetMsTicks();
        dsyslog("   load file from disk: %d ms", tick4 - tick3);
    #endif

    #ifdef DEBUGIMAGELOADTIME
        uint32_t tick5 = GetMsTicks();
    #endif

    img = CreateImage(width, height);

    if( img == NULL )
        return NULL;

    #ifdef DEBUGIMAGELOADTIME
        uint32_t tick6 = GetMsTicks();
        dsyslog("   scale logo: %d ms", tick6 - tick5);
    #endif

    imgCache.InsertImage(img, *File, width, height);
    return img;
}

cImage* cImageLoader::LoadIcon(const char *cIcon, int width, int height, bool preserveAspect) {
    if ((width == 0)||(height==0))
        return NULL;

    cString File = cString::sprintf("%s%s/%s.%s", *Config.iconPath, Setup.OSDTheme, cIcon, *logoExtension);

    #ifdef DEBUGIMAGELOADTIME
        dsyslog("imageloader load icon %s", *File);
    #endif

    cImage *img;

    #ifdef DEBUGIMAGELOADTIME
        uint32_t tick1 = GetMsTicks();
    #endif

    img = imgCache.GetImage( *File, width, height );

    #ifdef DEBUGIMAGELOADTIME
        uint32_t tick2 = GetMsTicks();
        dsyslog("   search in cache: %d ms", tick2 - tick1);
    #endif
    if( img != NULL )
        return img;

    #ifdef DEBUGIMAGELOADTIME
        uint32_t tick3 = GetMsTicks();
    #endif

    bool success = LoadImage(File);

    #ifdef DEBUGIMAGELOADTIME
        uint32_t tick4 = GetMsTicks();
        dsyslog("   load file from disk: %d ms", tick4 - tick3);
    #endif

    if( !success ) {
        File = cString::sprintf("%s%s/%s.%s", *Config.iconPath, "default", cIcon, *logoExtension);
        #ifdef DEBUGIMAGELOADTIME
            dsyslog("imageloader load icon %s", *File);
            uint32_t tick5 = GetMsTicks();
        #endif

        img = imgCache.GetImage( *File, width, height );

        #ifdef DEBUGIMAGELOADTIME
            uint32_t tick6 = GetMsTicks();
            dsyslog("   search in cache: %d ms", tick6 - tick5);
        #endif
        if( img != NULL )
            return img;

        #ifdef DEBUGIMAGELOADTIME
            uint32_t tick7 = GetMsTicks();
        #endif

        success = LoadImage(File);

        #ifdef DEBUGIMAGELOADTIME
            uint32_t tick8 = GetMsTicks();
            dsyslog("   load file from disk: %d ms", tick8 - tick7);
        #endif

        if( !success ) {
            dsyslog("imageloader LoadIcon: %s could not be loaded", *File);
            return NULL;
        }
    }
    #ifdef DEBUGIMAGELOADTIME
        uint32_t tick9 = GetMsTicks();
    #endif

    img = CreateImage(width, height);

    #ifdef DEBUGIMAGELOADTIME
        uint32_t tick10 = GetMsTicks();
        dsyslog("   scale logo: %d ms", tick10 - tick9);
    #endif
    if( img == NULL )
        return NULL;
    imgCache.InsertImage(img, *File, width, height);
    return img;
}

cImage* cImageLoader::LoadFile(const char *cFile, int width, int height, bool preserveAspect) {
    if( (width == 0) || (height==0) )
        return NULL;
    cString File = cFile;
    #ifdef DEBUGIMAGELOADTIME
        dsyslog("imageloader load file %s", *File);
    #endif

    cImage *img;
    #ifdef DEBUGIMAGELOADTIME
        uint32_t tick1 = GetMsTicks();
    #endif

    img = imgCache.GetImage( *File, width, height );

    #ifdef DEBUGIMAGELOADTIME
        uint32_t tick2 = GetMsTicks();
        dsyslog("   search in cache: %d ms", tick2 - tick1);
    #endif
    if( img != NULL )
        return img;

    #ifdef DEBUGIMAGELOADTIME
        uint32_t tick3 = GetMsTicks();
    #endif

    bool success = LoadImage(File);

    if( !success ) {
        dsyslog("imageloader LoadFile: %s could not be loaded", *File);
        return NULL;
    }
    #ifdef DEBUGIMAGELOADTIME
        uint32_t tick4 = GetMsTicks();
        dsyslog("   load file from disk: %d ms", tick4 - tick3);
    #endif

    #ifdef DEBUGIMAGELOADTIME
        uint32_t tick5 = GetMsTicks();
    #endif

    img = CreateImage(width, height);

    if( img == NULL )
        return NULL;

    #ifdef DEBUGIMAGELOADTIME
        uint32_t tick6 = GetMsTicks();
        dsyslog("   scale logo: %d ms", tick6 - tick5);
    #endif

    imgCache.InsertImage(img, *File, width, height);
    return img;
}

void cImageLoader::toLowerCase(std::string &str) {
    const int length = str.length();
    for(int i=0; i < length; ++i) {
        str[i] = std::tolower(str[i]);
    }
}

bool cImageLoader::FileExits(const std::string& name) {
    struct stat buffer;
    return (stat (name.c_str(), &buffer) == 0);
}

bool cImageLoader::SearchRecordingPoster(cString recPath, cString &found) {
    cString manualPoster = cString::sprintf("%s/cover_vdr.jpg", *recPath);
    if (FileSize(*manualPoster) != -1) {
        dsyslog("Poster found in %s/cover_vdr.jpg", *recPath);
        found = manualPoster;
        return true;
    }
    manualPoster = cString::sprintf("%s/../../../cover_vdr.jpg", *recPath);
    if (FileSize(*manualPoster) != -1) {
        dsyslog("Poster found in %s/../../../cover_vdr.jpg", *recPath);
        found = manualPoster;
        return true;
    }
    manualPoster = cString::sprintf("%s/../../cover_vdr.jpg", *recPath);
    if (FileSize(*manualPoster) != -1) {
        dsyslog("Poster found in %s/../../cover_vdr.jpg", *recPath);
        found = manualPoster;
        return true;
    }
    return false;
}