~aleteoryx/muditaos

ref: 2695cc830472190ce1401869bb67f5506a3e17e6 muditaos/module-gui/gui/core/ImageManager.cpp -rw-r--r-- 5.8 KiB
2695cc83 — piotrleniec-mudita [DW-31] Add commit message format checking on CI (#1201) 5 years ago
                                                                                
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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

/*
 * ImageManager.cpp
 *
 *  Created on: 18 maj 2019
 *      Author: robert
 */

#include <set>
#include <string>

#include <cstring>

#include "ImageManager.hpp"
#include "utf8/UTF8.hpp"
#include "log/log.hpp"
// module-gui
#include "ImageMap.hpp"
#include "VecMap.hpp"
#include "PixMap.hpp"
#include "vfs.hpp"

namespace gui
{

    ImageManager::ImageManager()
    {}

    ImageManager::~ImageManager()
    {
        clear();
    }

    void ImageManager::loadImageMaps(std::string baseDirectory)
    {
        mapFolder                            = baseDirectory + "/images";
        std::vector<std::string> pixMapFiles = getImageMapList(".mpi");
        std::vector<std::string> vecMapFiles = getImageMapList(".vpi");

        for (std::string mapName : pixMapFiles) {
            loadPixMap(mapName);
        }
        for (std::string mapName : vecMapFiles) {
            loadVecMap(mapName);
        }
    }

    void ImageManager::clear()
    {
        for (ImageMap *imageMap : imageMaps) {
            LOG_INFO("deleting image: %s", imageMap->getName().c_str());
            delete imageMap;
        }
        imageMaps.clear();
    }

    std::vector<std::string> splitpath(const std::string &str, const std::set<char> delimiters)
    {
        std::vector<std::string> result;

        char const *pch   = str.c_str();
        char const *start = pch;
        for (; *pch; ++pch) {
            if (delimiters.find(*pch) != delimiters.end()) {
                if (start != pch) {
                    std::string str(start, pch);
                    result.push_back(str);
                }
                else {
                    result.push_back("");
                }
                start = pch + 1;
            }
        }
        result.push_back(start);

        return result;
    }

    ImageMap *ImageManager::loadPixMap(std::string filename)
    {

        auto file = vfs.fopen(filename.c_str(), "rb");

        auto fileSize = vfs.filelength(file);

        char *data = new char[fileSize];
        if (data == nullptr) {
            vfs.fclose(file);
            LOG_ERROR(" Failed to allocate temporary font buffer");
            return nullptr;
        }

        // read data to buffer
        vfs.fread(data, 1, fileSize, file);

        // close file
        vfs.fclose(file);

        // allocate memory for new font
        PixMap *pixMap = new PixMap();
        if (pixMap->load(reinterpret_cast<uint8_t *>(data), fileSize) != gui::Status::GUI_SUCCESS) {
            delete pixMap;
            delete[] data;
            return nullptr;
        }
        else {
            // set id and push it to vector
            pixMap->setID(imageMaps.size());
            std::set<char> delims{'/'};
            std::vector<std::string> path = splitpath(filename, delims);
            std::string filename          = path[path.size() - 1];
            filename                      = filename.substr(0, filename.length() - 4);

            pixMap->setName(filename);
            // TODO remove commented code
            LOG_INFO("%s", filename.c_str());
            imageMaps.push_back(pixMap);
        }
        delete[] data;
        return pixMap;
    }

    ImageMap *ImageManager::loadVecMap(std::string filename)
    {

        auto file = vfs.fopen(filename.c_str(), "rb");

        auto fileSize = vfs.filelength(file);

        char *data = new char[fileSize];
        if (data == nullptr) {
            vfs.fclose(file);
            LOG_ERROR(" Failed to allocate temporary font buffer");
            return nullptr;
        }

        // read data to buffer
        vfs.fread(data, 1, fileSize, file);

        // close file
        vfs.fclose(file);

        VecMap *vecMap = new VecMap();
        if (vecMap->load(reinterpret_cast<uint8_t *>(data), fileSize) != gui::Status::GUI_SUCCESS) {
            delete vecMap;
            delete[] data;
            return nullptr;
        }
        else {
            // set id and push it to vector
            vecMap->setID(imageMaps.size());
            std::set<char> delims{'/'};
            std::vector<std::string> path = splitpath(filename, delims);
            std::string filename          = path[path.size() - 1];
            filename                      = filename.substr(0, filename.length() - 4);
            vecMap->setName(filename);
            // TODO remove commented code
            //		LOG_INFO("%s",filename.c_str());
            imageMaps.push_back(vecMap);
        }
        delete[] data;
        return vecMap;
    }

    std::vector<std::string> ImageManager::getImageMapList(std::string ext)
    {

        std::vector<std::string> mapFiles;

        LOG_INFO("Scanning %s images folder: %s", ext.c_str(), mapFolder.c_str());
        auto dirList = vfs.listdir(mapFolder.c_str(), ext);

        for (vfs::DirectoryEntry ent : dirList) {
            if (ent.attributes != vfs::FileAttributes::Directory)
                mapFiles.push_back(mapFolder + "/" + ent.fileName);
        }

        LOG_INFO("Total number of images: %u", static_cast<unsigned int>(mapFiles.size()));

        return mapFiles;
    }

    bool ImageManager::init(std::string baseDirectory)
    {
        // load fonts from specified folder
        loadImageMaps(baseDirectory);

        return true;
    }

    ImageManager &ImageManager::getInstance()
    {

        static ImageManager instance;

        return instance;
    }

    ImageMap *ImageManager::getImageMap(uint32_t id)
    {
        if (id >= imageMaps.size())
            return nullptr;
        return imageMaps[id];
    }
    uint32_t ImageManager::getImageMapID(const std::string &name)
    {
        for (uint32_t i = 0; i < imageMaps.size(); i++) {
            if (name.compare(imageMaps[i]->getName()) == 0)
                return i;
        }
        return 0;
    }

} /* namespace gui */