summaryrefslogtreecommitdiff
path: root/include/image.hpp
blob: 3124b58c52e44aaff989bdba4f269685bf691a7c (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
// Display images inside a terminal
// Copyright (C) 2023  JustKidding
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

#ifndef IMAGE_H
#define IMAGE_H

#include <filesystem>
#include <memory>
#include <nlohmann/json.hpp>
#include <string>

#include "dimensions.hpp"
#include "terminal.hpp"

class Image
{
  public:
    static auto load(const nlohmann::json &command, const Terminal *terminal) -> std::unique_ptr<Image>;
    static auto check_cache(const Dimensions &dimensions, const std::filesystem::path &orig_path) -> std::string;
    static auto get_dimensions(const nlohmann::json &json, const Terminal *terminal) -> std::shared_ptr<Dimensions>;

    virtual ~Image() = default;

    [[nodiscard]] virtual auto dimensions() const -> const Dimensions & = 0;
    [[nodiscard]] virtual auto width() const -> int = 0;
    [[nodiscard]] virtual auto height() const -> int = 0;
    [[nodiscard]] virtual auto size() const -> size_t = 0;
    [[nodiscard]] virtual auto data() const -> const unsigned char * = 0;
    [[nodiscard]] virtual auto channels() const -> int = 0;

    [[nodiscard]] virtual auto frame_delay() const -> int { return -1; }
    [[nodiscard]] virtual auto is_animated() const -> bool { return false; }
    [[nodiscard]] virtual auto filename() const -> std::string = 0;
    virtual auto next_frame() -> void {}

  protected:
    [[nodiscard]] auto get_new_sizes(double max_width, double max_height, std::string_view scaler,
                                     int scale_factor = 0) const -> std::pair<int, int>;
};

#endif