HighMap library (C++)
Loading...
Searching...
No Matches
texture.hpp
Go to the documentation of this file.
1/* Copyright (c) 2026 Otto Link. Distributed under the terms of the GNU General
2 Public License. The full license is in the file LICENSE, distributed with
3 this software. */
4
15#pragma once
16#include <string>
17#include <vector>
18
19#include <opencv2/core.hpp>
20
21#include "highmap/algebra.hpp"
22#include "highmap/array.hpp"
23
24namespace hmap
25{
26
37{
38public:
42 glm::ivec2 shape = glm::ivec2(0, 0);
43
47 std::vector<Array> channels;
48
52 Texture() = default;
53
60 Texture(glm::ivec2 shape, int num_channels);
61
70 Texture(glm::ivec2 shape, int num_channels, float fill_value);
71
77 explicit Texture(const Array &array);
78
82 Texture(const Array &r, const Array &g, const Array &b);
83
87 Texture(const Array &r, const Array &g, const Array &b, const Array &a);
88
92 explicit Texture(const std::vector<Array> &channels);
93
100 explicit Texture(const std::string &fname, bool flip_j = false);
101
105 int num_channels() const
106 {
107 return static_cast<int>(channels.size());
108 }
109
113 Array &operator[](size_t idx)
114 {
115 return channels[idx];
116 }
117 const Array &operator[](size_t idx) const
118 {
119 return channels[idx];
120 }
121
125 Array &channel(size_t idx)
126 {
127 return channels[idx];
128 }
129 const Array &channel(size_t idx) const
130 {
131 return channels[idx];
132 }
133
137 float &operator()(int i, int j, int c)
138 {
139 return channels[c](i, j);
140 }
141
145 const float &operator()(int i, int j, int c) const
146 {
147 return channels[c](i, j);
148 }
149
153 glm::vec3 get_pixel3(int i, int j) const;
154
158 glm::vec4 get_pixel4(int i, int j) const;
159
163 void set_pixel(int i, int j, const glm::vec3 &color);
164
168 void set_pixel(int i, int j, const glm::vec4 &color);
169
173 float max() const;
174
178 float min() const;
179
183 void remap(float vmin = 0.f, float vmax = 1.f);
184
188 Texture resample_to_shape(glm::ivec2 new_shape_xy) const;
189
193 cv::Mat to_cv_mat() const;
194
198 std::vector<uint8_t> to_img_8bit(bool flip_y = false) const;
199
203 void to_png(const std::string &fname, int depth = CV_8U) const;
204};
205
206} // namespace hmap
Header file defining basic vector and matrix manipulation classes.
Declaration of the Array class for 2D floating-point arrays with various mathematical operations and ...
Array class, helper to manipulate 2D float array with "(i, j)" indexing.
Definition array.hpp:32
A class to represent a multi-channel texture using planar hmap::Array storage.
Definition texture.hpp:37
float min() const
Find the minimum value in all channels.
Definition texture.cpp:167
cv::Mat to_cv_mat() const
Convert the texture to an OpenCV matrix.
Definition texture.cpp:219
int num_channels() const
Get the number of channels.
Definition texture.hpp:105
glm::vec3 get_pixel3(int i, int j) const
Read 3-channel color at (i, j) as glm::vec3.
Definition texture.cpp:108
Array & operator[](size_t idx)
Access channel Array by index.
Definition texture.hpp:113
Texture()=default
Construct an empty Texture object.
std::vector< uint8_t > to_img_8bit(bool flip_y=false) const
Convert the texture to an 8-bit image represented as a vector.
Definition texture.cpp:264
float & operator()(int i, int j, int c)
Access element at (i, j) in channel c.
Definition texture.hpp:137
const Array & operator[](size_t idx) const
Definition texture.hpp:117
void to_png(const std::string &fname, int depth=CV_8U) const
Saves the Texture as an image file (e.g. PNG).
Definition texture.cpp:253
float max() const
Find the maximum value in all channels.
Definition texture.cpp:156
Texture resample_to_shape(glm::ivec2 new_shape_xy) const
Resamples the texture to a new 2D shape (x, y).
Definition texture.cpp:204
const Array & channel(size_t idx) const
Definition texture.hpp:129
void remap(float vmin=0.f, float vmax=1.f)
Remap the texture values to a new range.
Definition texture.cpp:178
std::vector< Array > channels
Vector of planar Array channels.
Definition texture.hpp:47
const float & operator()(int i, int j, int c) const
Access element at (i, j) in channel c (const).
Definition texture.hpp:145
glm::vec4 get_pixel4(int i, int j) const
Read 4-channel color at (i, j) as glm::vec4.
Definition texture.cpp:118
void set_pixel(int i, int j, const glm::vec3 &color)
Write 3-channel color at (i, j) from glm::vec3.
Definition texture.cpp:131
Array & channel(size_t idx)
Access channel Array by index.
Definition texture.hpp:125
glm::ivec2 shape
Shape of the texture in 2D space (x, y).
Definition texture.hpp:42
Definition algebra.hpp:23