Line data Source code
1 : /**
2 : * @file Common.h
3 : * @author Damien Balima (www.dams-labs.net)
4 : * @brief Common objects
5 : * @date 2024-03-14
6 : *
7 : * @copyright Damien Balima (c) CC-BY-NC-SA-4.0 2024
8 : *
9 : */
10 :
11 : #pragma once
12 : #include "Image.h"
13 : #include <fstream>
14 : #include <map>
15 : #include <memory>
16 : #include <opencv2/opencv.hpp>
17 : #include <regex> // for std::regex and std::regex_replace
18 : #include <string>
19 : #include <unordered_set>
20 : #include <vector>
21 :
22 : namespace sipai {
23 : consteval unsigned long long operator"" _K(unsigned long long x) {
24 : return x * 1024;
25 : }
26 :
27 : consteval unsigned long long operator"" _M(unsigned long long x) {
28 : return x * 1024_K;
29 : }
30 :
31 : consteval unsigned long long operator"" _G(unsigned long long x) {
32 : return x * 1024_M;
33 : }
34 :
35 : enum class TrainingPhase { Training, Validation };
36 :
37 : enum class ERunMode { Enhancer, Testing, Training, Upscaler };
38 :
39 : const std::map<std::string, ERunMode, std::less<>> mode_map{
40 : {"Enhancer", ERunMode::Enhancer},
41 : {"Testing", ERunMode::Testing},
42 : {"Training", ERunMode::Training},
43 : {"Upscaler", ERunMode::Upscaler}};
44 :
45 : inline std::unordered_set<std::string> valid_extensions = {
46 : ".bmp", ".dib", ".jpeg", ".jpg", ".jpe", ".jp2", ".png",
47 : ".webp", ".pbm", ".pgm", ".ppm", ".pxm", ".pnm", ".pfm",
48 : ".sr", ".ras", ".tiff", ".tif", ".exr", ".hdr", ".pic"};
49 :
50 : class Common {
51 : public:
52 20 : static std::string getTrainingPhaseStr(TrainingPhase phase) {
53 20 : switch (phase) {
54 14 : case TrainingPhase::Training:
55 14 : return "Training";
56 6 : case TrainingPhase::Validation:
57 6 : return "Validation";
58 0 : default:
59 0 : return "";
60 : }
61 : }
62 :
63 0 : static std::string getRunModeStr(ERunMode mode) {
64 0 : for (const auto &[key, value] : mode_map) {
65 0 : if (value == mode) {
66 0 : return key;
67 : }
68 : }
69 0 : return "";
70 : }
71 :
72 : /**
73 : * @brief get total lines of a file if lines are ended by a newline
74 : * warning: lines *must* be ended by a newline
75 : * @param filename
76 : * @return int
77 : */
78 :
79 2 : static size_t countLines(const std::string &filename) {
80 2 : std::ifstream file(filename);
81 2 : if (!file.is_open()) {
82 1 : throw std::runtime_error("Error opening file: " + filename);
83 : }
84 :
85 1 : size_t lineCount = 0;
86 1 : std::string line;
87 12 : while (std::getline(file, line)) {
88 11 : if (!line.empty()) {
89 10 : ++lineCount;
90 : }
91 : }
92 :
93 1 : file.close();
94 1 : return lineCount;
95 2 : }
96 :
97 : /**
98 : * @brief Get the Filename .csv from a Filename .json
99 : *
100 : * @param filenameJson
101 : * @return std::string
102 : */
103 10 : static std::string getFilenameCsv(const std::string &filenameJson) {
104 : return std::regex_replace(filenameJson,
105 20 : std::regex(".json$", std::regex::icase), ".csv");
106 : }
107 :
108 5 : static std::array<size_t, 3> getHMSfromS(const size_t seconds) {
109 5 : size_t s = seconds;
110 5 : size_t h = s / 3600;
111 5 : s %= 3600;
112 5 : size_t m = s / 60;
113 5 : s %= 60;
114 5 : return {h, m, s};
115 : }
116 :
117 : /**
118 : * @brief Return the clamp of a cv::Mat value
119 : *
120 : * @param value
121 : * @param value_min
122 : * @param value_max
123 : */
124 2 : static cv::Mat mat_clamp4f(const cv::Mat &value,
125 : const cv::Vec4f &value_min = cv::Vec4f::all(0.0),
126 : const cv::Vec4f &value_max = cv::Vec4f::all(1.0)) {
127 2 : cv::Mat result;
128 2 : cv::min(cv::max(value, value_min), value_max, result);
129 2 : return result;
130 0 : }
131 :
132 : /**
133 : * @brief Return the clamp of a cv::Vec4f value
134 : *
135 : * @param value
136 : * @param value_min
137 : * @param value_max
138 : */
139 1816 : static cv::Vec4f clamp4f(const cv::Vec4f &value,
140 : const cv::Vec4f &value_min = cv::Vec4f::all(0.0),
141 : const cv::Vec4f &value_max = cv::Vec4f::all(1.0)) {
142 1816 : cv::Vec4f result;
143 9080 : for (int i = 0; i < 4; i++) {
144 7264 : result[i] = std::clamp(value[i], value_min[i], value_max[i]);
145 : }
146 1816 : return result;
147 : }
148 :
149 : static void copyMatToVector(const cv::Mat &mat,
150 : std::vector<std::vector<cv::Vec4f>> &array) {
151 : for (int y = 0; y < mat.rows; ++y) {
152 : for (int x = 0; x < mat.cols; ++x) {
153 : array[y][x] = mat.at<cv::Vec4f>(y, x);
154 : }
155 : }
156 : };
157 : };
158 :
159 : } // namespace sipai
|