Wildmeshing Toolkit
Image.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stb_image.h>
4 #include <stb_image_write.h>
6 #include <Eigen/Core>
7 #include <array>
8 #include <cmath>
9 #include <filesystem>
10 #include <type_traits>
13 #include "load_image_exr.hpp"
14 #include "save_image_exr.hpp"
15 
16 
18 class Image
19 {
20  using DScalar = DScalar2<double, Eigen::Matrix<double, -1, 1>, Eigen::Matrix<double, -1, -1>>;
21  using ImageMatrixf =
22  Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor | Eigen::AutoAlign>;
23 
24 protected:
25  ImageMatrixf m_image; // saving scanline images
28 
29 public:
30  Image() = default;
31  Image(int height_, int width_) { m_image.resize(height_, width_); };
32 
34  const ImageMatrixf& get_raw_image() const { return m_image; }
35 
36 public:
37  // point coordinates between [0, 1]
38  int width() const { return static_cast<int>(m_image.cols()); };
39  int height() const { return static_cast<int>(m_image.rows()); };
40 
41  template <class T>
42  std::decay_t<T> get(const T& u, const T& v) const;
43  float get_pixel(const int i, const int j) const { return m_image(i, j); };
44  std::pair<int, int> get_pixel_index(const double& u, const double& v) const;
45  int get_coordinate(const int x, const WrappingMode mode) const;
48  bool set(
49  const std::function<float(const double&, const double&)>& f,
52  bool set(const int r, const int c, const float v)
53  {
54  m_image(r, c) = v;
55  return true;
56  };
57  bool save(const std::filesystem::path& path) const;
58  void load(const std::filesystem::path& path, WrappingMode mode_x, WrappingMode mode_y);
59 
61  {
62  m_mode_x = mode_x;
63  m_mode_y = mode_y;
64  };
65  Image down_sample() const;
66 };
67 
71 template <class T>
72 std::decay_t<T> Image::get(const T& u, const T& v) const
73 {
74  int w = width();
75  int h = height();
76  auto size = std::max(w, h);
77  // x, y are between 0 and 1
78  auto x = u * static_cast<std::decay_t<T>>(size);
79  auto y = v * static_cast<std::decay_t<T>>(size);
80  // use bicubic interpolation
81 
82  BicubicVector<float> sample_vector = extract_samples(
83  static_cast<size_t>(w),
84  static_cast<size_t>(h),
85  m_image.data(),
86  get_value(x),
87  get_value(y),
88  m_mode_x,
89  m_mode_y);
90  BicubicVector<float> bicubic_coeff = get_bicubic_matrix() * sample_vector;
91  return eval_bicubic_coeffs(bicubic_coeff, x, y);
92 };
93 
94 void split_and_save_3channels(const std::filesystem::path& path);
95 
96 Image buffer_to_image(const std::vector<float>& buffer, int w, int h);
97 
98 std::array<Image, 3> load_rgb_image(const std::filesystem::path& path);
99 
100 std::array<Image, 3> combine_position_normal_texture(
101  double normalization_scale,
102  const Eigen::Matrix<double, 1, 3>& offset,
103  const std::filesystem::path& position_path,
104  const std::filesystem::path& normal_path,
105  const std::filesystem::path& texture_path,
106  float min_height = 0.f,
107  float max_height = 1.f);
108 } // namespace wmtk::components::adaptive_tessellation::image
int get_coordinate(const int x, const WrappingMode mode) const
Definition: Image.cpp:26
Eigen::Matrix< float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor|Eigen::AutoAlign > ImageMatrixf
Definition: Image.hpp:22
const ImageMatrixf & get_raw_image() const
Definition: Image.hpp:34
std::decay_t< T > get(const T &u, const T &v) const
Definition: Image.hpp:72
void load(const std::filesystem::path &path, WrappingMode mode_x, WrappingMode mode_y)
Definition: Image.cpp:113
void set_wrapping_mode(WrappingMode mode_x, WrappingMode mode_y)
Definition: Image.hpp:60
std::pair< int, int > get_pixel_index(const double &u, const double &v) const
Definition: Image.cpp:46
bool save(const std::filesystem::path &path) const
Definition: Image.cpp:84
bool set(const int r, const int c, const float v)
Definition: Image.hpp:52
float get_pixel(const int i, const int j) const
Definition: Image.hpp:43
bool set(const std::function< float(const double &, const double &)> &f, const WrappingMode mode_x=WrappingMode::CLAMP_TO_EDGE, const WrappingMode mode_y=WrappingMode::CLAMP_TO_EDGE)
Definition: Image.cpp:61
std::array< Image, 3 > load_rgb_image(const std::filesystem::path &path)
Definition: Image.cpp:273
std::array< Image, 3 > combine_position_normal_texture(double normalization_scale, const Eigen::Matrix< double, 1, 3 > &offset, const std::filesystem::path &position_path, const std::filesystem::path &normal_path, const std::filesystem::path &height_path, float min_height, float max_height)
Definition: Image.cpp:163
std::decay_t< T > eval_bicubic_coeffs(const BicubicVector< float > &coeffs, const T &sx, const T &sy)
Image buffer_to_image(const std::vector< float > &buffer, int w, int h)
Definition: Image.cpp:262
BicubicVector< float > extract_samples(const size_t width, const size_t height, const float *buffer, const double sx_, const double sy_, const WrappingMode mode_x, const WrappingMode mode_y)
void split_and_save_3channels(const std::filesystem::path &path)
Definition: Image.cpp:233
Automatic differentiation scalar with first- and second-order derivatives.
Definition: autodiff.h:501