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
14
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 bool is_empty() const
106 {
107 return channels.empty() || shape.x <= 0 || shape.y <= 0;
108 }
109
113 int num_channels() const
114 {
115 return static_cast<int>(channels.size());
116 }
117
121 Array &operator[](size_t idx)
122 {
123 return channels[idx];
124 }
125 const Array &operator[](size_t idx) const
126 {
127 return channels[idx];
128 }
129
133 Array &channel(size_t idx)
134 {
135 return channels[idx];
136 }
137 const Array &channel(size_t idx) const
138 {
139 return channels[idx];
140 }
141
145 float &operator()(int i, int j, int c)
146 {
147 return channels[c](i, j);
148 }
149
153 const float &operator()(int i, int j, int c) const
154 {
155 return channels[c](i, j);
156 }
157
161 glm::vec3 get_pixel3(int i, int j) const;
162
166 glm::vec4 get_pixel4(int i, int j) const;
167
171 void set_pixel(int i, int j, const glm::vec3 &color);
172
176 void set_pixel(int i, int j, const glm::vec4 &color);
177
181 float max() const;
182
186 float min() const;
187
191 void remap(float vmin = 0.f, float vmax = 1.f);
192
196 Texture resample_to_shape(glm::ivec2 new_shape_xy) const;
197
201 cv::Mat to_cv_mat() const;
202
206 std::vector<uint8_t> to_img_8bit(bool flip_y = false) const;
207
211 void to_png(const std::string &fname, int depth = CV_8U) const;
212};
213
214} // 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:31
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:170
cv::Mat to_cv_mat() const
Convert the texture to an OpenCV matrix.
Definition texture.cpp:222
int num_channels() const
Get the number of channels.
Definition texture.hpp:113
glm::vec3 get_pixel3(int i, int j) const
Read 3-channel color at (i, j) as glm::vec3.
Definition texture.cpp:111
Array & operator[](size_t idx)
Access channel Array by index.
Definition texture.hpp:121
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:267
float & operator()(int i, int j, int c)
Access element at (i, j) in channel c.
Definition texture.hpp:145
const Array & operator[](size_t idx) const
Definition texture.hpp:125
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:256
float max() const
Find the maximum value in all channels.
Definition texture.cpp:159
Texture resample_to_shape(glm::ivec2 new_shape_xy) const
Resamples the texture to a new 2D shape (x, y).
Definition texture.cpp:207
const Array & channel(size_t idx) const
Definition texture.hpp:137
void remap(float vmin=0.f, float vmax=1.f)
Remap the texture values to a new range.
Definition texture.cpp:181
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:153
glm::vec4 get_pixel4(int i, int j) const
Read 4-channel color at (i, j) as glm::vec4.
Definition texture.cpp:121
void set_pixel(int i, int j, const glm::vec3 &color)
Write 3-channel color at (i, j) from glm::vec3.
Definition texture.cpp:134
Array & channel(size_t idx)
Access channel Array by index.
Definition texture.hpp:133
glm::ivec2 shape
Shape of the texture in 2D space (x, y).
Definition texture.hpp:42
bool is_empty() const
Check whether the texture is empty (no channels or zero area).
Definition texture.hpp:105
Definition opencv.hpp:20