File: image.cpp

package info (click to toggle)
hyprgraphics 0.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 936 kB
  • sloc: cpp: 1,576; sh: 9; makefile: 6
file content (99 lines) | stat: -rw-r--r-- 3,276 bytes parent folder | download
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
#include <algorithm>
#include <print>
#include <format>
#include <filesystem>
#include <fstream>
#include <vector>
#include <hyprgraphics/image/Image.hpp>
#include "shared.hpp"

using namespace Hyprgraphics;

static bool tryLoadImageFromFile(const std::string& path) {
    auto image = path.ends_with("svg") ? CImage(path, {512, 512}) : CImage(path);

    if (!image.success()) {
        std::println("Failed to load {}: {}", path, image.getError());
        return false;
    }

    std::println("Loaded {} successfully: Image is {}x{} of type {}", path, image.cairoSurface()->size().x, image.cairoSurface()->size().y, image.getMime());

    const auto TEST_DIR = std::filesystem::current_path().string() + "/test_output";

    // try to write it for inspection
    if (!std::filesystem::exists(TEST_DIR))
        std::filesystem::create_directory(TEST_DIR);

    std::string name = image.getMime();
    std::ranges::replace(name, '/', '_');

    //NOLINTNEXTLINE
    return cairo_surface_write_to_png(image.cairoSurface()->cairo(), (TEST_DIR + "/" + name + ".png").c_str()) == CAIRO_STATUS_SUCCESS;
}

static bool tryLoadImageFromBuffer(const std::span<uint8_t>& data, eImageFormat format) {
    auto image = CImage(data, format);

    if (!image.success()) {
        std::println("Failed to load embedded image: {}", image.getError());
        return false;
    }

    std::println("Loaded embedded Image successfully: Image is {}x{} of type {}", image.cairoSurface()->size().x, image.cairoSurface()->size().y, image.getMime());

    const auto TEST_DIR = std::filesystem::current_path().string() + "/test_output";

    // try to write it for inspection
    if (!std::filesystem::exists(TEST_DIR))
        std::filesystem::create_directory(TEST_DIR);

    std::string name = image.getMime() + "_embedded";
    std::ranges::replace(name, '/', '_');

    //NOLINTNEXTLINE
    return cairo_surface_write_to_png(image.cairoSurface()->cairo(), (TEST_DIR + "/" + name + ".png").c_str()) == CAIRO_STATUS_SUCCESS;
}

static std::vector<uint8_t> getImageBuffer(const std::string& path) {
    std::vector<uint8_t> buffer;

    std::ifstream        file(path, std::ios::binary | std::ios::ate);
    std::streamsize      size = file.tellg();
    file.seekg(0, std::ios::beg);

    buffer.resize(size);

    file.read(reinterpret_cast<char*>(buffer.data()), size);

    return buffer;
}

int main(int argc, char** argv, char** envp) {
    int ret = 0;

    for (auto& file : std::filesystem::directory_iterator("./resource/images/")) {
        if (!file.is_regular_file())
            continue;
        auto expectation = true;
#ifndef JXL_FOUND
        if (file.path().filename() == "hyprland.jxl")
            expectation = false;
#endif
#ifndef HEIF_FOUND
        if (file.path().filename() == "hyprland.avif")
            expectation = false;
#endif
        EXPECT(tryLoadImageFromFile(file.path()), expectation);
    }

    auto pngBuffer = getImageBuffer("./resource/images/hyprland.png");
    EXPECT(tryLoadImageFromBuffer(pngBuffer, Hyprgraphics::IMAGE_FORMAT_PNG), true);

#ifdef HEIF_FOUND
    auto avifBuffer = getImageBuffer("./resource/images/hyprland.avif");
    EXPECT(tryLoadImageFromBuffer(avifBuffer, Hyprgraphics::IMAGE_FORMAT_AVIF), true);
#endif

    return ret;
}