forked from learning-process/parallel_programming_course
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutil.cpp
More file actions
51 lines (45 loc) · 1.37 KB
/
util.cpp
File metadata and controls
51 lines (45 loc) · 1.37 KB
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
#include "core/util/include/util.hpp"
#include <cstdlib>
#ifdef _WIN32
#include <cstdint>
#include <iostream>
#include <memory>
#endif
#define STB_IMAGE_IMPLEMENTATION
#include <stb/stb_image.h>
#include <filesystem>
#include <iostream>
#include <string>
std::string ppc::util::GetAbsolutePath(const std::string &relative_path) {
const std::filesystem::path path = std::string(PPC_PATH_TO_PROJECT) + "/tasks/" + relative_path;
return path.string();
}
int ppc::util::GetPPCNumThreads() {
#ifdef _WIN32
size_t len;
char omp_env[100];
errno_t err = getenv_s(&len, omp_env, sizeof(omp_env), "OMP_NUM_THREADS");
if (err != 0 || len == 0) {
omp_env[0] = '\0';
}
int num_threads = std::atoi(omp_env);
#else
const char *omp_env = std::getenv("OMP_NUM_THREADS");
int num_threads = (omp_env != nullptr) ? std::atoi(omp_env) : 1;
#endif
return num_threads;
}
bool ppc::util::GetImageData(const std::string &abs_path, std::vector<uint8_t> &image, int &width, int &height,
int &channels) {
width = -1;
height = -1;
channels = -1;
unsigned char *data = stbi_load(abs_path.c_str(), &width, &height, &channels, 0);
if (data == nullptr) {
std::cerr << "Failed to load image: " << stbi_failure_reason() << '\n';
return false;
}
image = std::vector<uint8_t>(data, data + (width * height * channels));
stbi_image_free(data);
return true;
}