SOMHunter Core
image-processor.h
Go to the documentation of this file.
1 /* This file is part of SOMHunter.
2  *
3  * Copyright (C) 2021 Frantisek Mejzlik <frankmejzlik@protonmail.com>
4  * Mirek Kratochvil <exa.exa@gmail.com>
5  * Patrik Vesely <prtrikvesely@gmail.com>
6  *
7  * SOMHunter is free software: you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License as published by the Free
9  * Software Foundation, either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * SOMHunter is distributed in the hope that it will be useful, but WITHOUT ANY
13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * SOMHunter. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef IMAGE_MANIPULATOR_H_
22 #define IMAGE_MANIPULATOR_H_
23 
24 #include <algorithm>
25 #include <iterator>
26 #include <stdexcept>
27 
28 #include <stb_image.h>
29 #include <stb_image_write.h>
30 #include <opencv2/imgcodecs.hpp>
31 #include <opencv2/imgproc.hpp>
32 #include <opencv2/opencv.hpp>
33 
34 #include "common.h"
35 
36 #include "os-utils.hpp"
37 #include "utils.hpp"
38 
39 namespace sh {
41 template <typename DType_>
42 class BitmapImage {
43 public:
44  size_t w;
45  size_t h;
46  size_t num_channels;
47  std::vector<DType_> _data;
48 };
49 
52 public:
53  template <typename OutType_>
54  static OutType_ load_image(const std::string& filepath);
55 
56  static void show_image(const std::string& filepath, size_t delay = 0) {
57  if (!osutils::file_exists(filepath)) {
58  std::string msg{ "Unable to open file '" + filepath + "'." };
59  SHLOG_E(msg);
60  throw std::runtime_error{ msg };
61  }
62 
63  cv::Mat image = cv::imread(filepath, cv::IMREAD_UNCHANGED);
64  cv::imshow(filepath, image);
65  cv::waitKey(delay);
66  }
67 
68  static void show_image(const cv::Mat image, size_t delay = 0) {
69  cv::imshow("IMAGE", image);
70  cv::waitKey(delay);
71  }
72 
73  static std::vector<uint8_t> resize(const std::vector<uint8_t>& in, size_t orig_w, size_t orig_h, size_t new_w,
74  size_t new_h, size_t num_channels = 3);
75 
76  static void store_JPEG(const std::string& filepath, const std::vector<std::uint8_t>& in, size_t w, size_t h,
77  size_t quality, size_t num_channels);
78 
80  static std::vector<float> to_float32(const std::vector<std::uint8_t>& in);
81 
83  static std::vector<std::uint8_t> to_uint8(const std::vector<float>& in);
84 
85  // NEW
86  // -------------------------------------------------
87  // OLD
88 
100  static void store_PNG(const std::string& filepath, const std::vector<uint8_t>& in, size_t w, size_t h,
101  size_t num_channels) {
102  auto res{ stbi_write_png(filepath.c_str(), w, h, num_channels, (uint8_t*)in.data(), w * num_channels) };
103 
104  if (res == 0) {
105  std::string msg{ "Writing the '" + filepath + "' image failed!" };
106  SHLOG_E(msg);
107  throw std::runtime_error(msg);
108  }
109  }
110 
111  static void store_PNG(const std::string& filepath, const std::vector<float>& in, size_t w, size_t h,
112  size_t num_channels) {
113  auto res{ stbi_write_png(filepath.c_str(), w, h, num_channels, (float*)in.data(), w * num_channels) };
114 
115  if (res == 0) {
116  std::string msg{ "Writing the '" + filepath + "' image failed!" };
117  SHLOG_E(msg);
118  throw std::runtime_error(msg);
119  }
120  }
121 
122  static void store_JPEG(const std::string& filepath, const std::vector<float>& in, size_t w, size_t h,
123  size_t quality = 100, size_t num_channels = 3, bool are_ints = false);
124 #ifdef TESTING
125 private:
126 #else
127 public:
128 #endif
129 };
130 
131 }; // namespace sh
132 
133 #endif // IMAGE_MANIPULATOR_
Basic abstraction for images.
Definition: image-processor.h:42
std::vector< DType_ > _data
Definition: image-processor.h:47
size_t num_channels
Definition: image-processor.h:46
size_t w
Definition: image-processor.h:44
size_t h
Definition: image-processor.h:45
Provides utilities for image manipulation and processing.
Definition: image-processor.h:51
static void store_PNG(const std::string &filepath, const std::vector< float > &in, size_t w, size_t h, size_t num_channels)
Definition: image-processor.h:111
static std::vector< uint8_t > resize(const std::vector< uint8_t > &in, size_t orig_w, size_t orig_h, size_t new_w, size_t new_h, size_t num_channels=3)
Definition: image-processor.cpp:31
static void show_image(const cv::Mat image, size_t delay=0)
Definition: image-processor.h:68
static void store_PNG(const std::string &filepath, const std::vector< uint8_t > &in, size_t w, size_t h, size_t num_channels)
Loads the image from the provided filepath.
Definition: image-processor.h:100
static void store_JPEG(const std::string &filepath, const std::vector< float > &in, size_t w, size_t h, size_t quality=100, size_t num_channels=3, bool are_ints=false)
static std::vector< std::uint8_t > to_uint8(const std::vector< float > &in)
Returns a copy of float buffer from the uint8 one.
Definition: image-processor.cpp:61
static OutType_ load_image(const std::string &filepath)
static void show_image(const std::string &filepath, size_t delay=0)
Definition: image-processor.h:56
static std::vector< float > to_float32(const std::vector< std::uint8_t > &in)
Returns a copy of uint8 buffer from the float one.
Definition: image-processor.cpp:52
static void store_JPEG(const std::string &filepath, const std::vector< std::uint8_t > &in, size_t w, size_t h, size_t quality, size_t num_channels)
Definition: image-processor.cpp:116
bool file_exists(const std::string &filepath)
Definition: os-utils.hpp:148
Definition: common-types.h:33
#define SHLOG_E(x)
Definition: static-logger.hpp:157