~aleteoryx/muditaos

muditaos/module-gui/gui/core/VecMap.cpp -rw-r--r-- 1.6 KiB
a405cad6Aleteoryx trim readme 6 days 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
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include <cstring>
#include "VecMap.hpp"

namespace gui
{
    VecMap::VecMap()
    {
        width  = 0;
        height = 0;
        name   = {};
        type   = Type::Vecmap;
    }

    VecMap::VecMap(std::uint16_t w, std::uint16_t h, std::uint8_t *vecMapData) : ImageMap(w, h, vecMapData)
    {
        // Add empty vectors for all rows
        data = new std::uint8_t[height];
        type = Type::Vecmap;

        // No data provided - allocate buffer and clear it with white color
        if (vecMapData == nullptr) {
            std::memset(data, 0x00, height);
        }
    }

    auto VecMap::load(std::uint8_t *vecMapData, std::uint32_t size) -> gui::Status
    {
        std::uint32_t offset = 0;

        // Read width and height of the image
        std::memcpy(&width, &vecMapData[offset], sizeof(std::uint16_t));
        offset += sizeof(std::uint16_t);
        std::memcpy(&height, &vecMapData[offset], sizeof(std::uint16_t));
        offset += sizeof(std::uint16_t);
        std::memcpy(&alphaColor, &vecMapData[offset], sizeof(std::uint8_t));
        offset += sizeof(std::uint8_t);

        constexpr auto metadataSize = 2 * sizeof(std::uint16_t) + sizeof(std::uint8_t);

        data = new (std::nothrow) std::uint8_t[size - metadataSize];
        if (data == nullptr) {
            return gui::Status::GUI_FAILURE;
        }

        std::memcpy(data, &vecMapData[offset], size - metadataSize);
        return gui::Status::GUI_SUCCESS;
    }
} /* namespace gui */