17 #ifndef GZ_COMMON_IMAGE_HH_
18 #define GZ_COMMON_IMAGE_HH_
25 #include <gz/math/Color.hh>
27 #include <gz/common/graphics/Export.hh>
29 #include <gz/utils/ImplPtr.hh>
39 "UNKNOWN_PIXEL_FORMAT",
63 class GZ_COMMON_GRAPHICS_VISIBLE
Image
81 UNKNOWN_PIXEL_FORMAT = 0,
142 unsigned int _height,
157 public:
void GZ_DEPRECATED(5) Data(
unsigned char **_data,
158 unsigned int &_count) const;
162 public:
std::vector<
unsigned char> Data() const;
169 public:
void GZ_DEPRECATED(5) RGBData(
unsigned char **_data,
170 unsigned int &_count) const;
175 public:
std::vector<
unsigned char> RGBData() const;
182 public:
void GZ_DEPRECATED(5) RGBAData(
unsigned char **_data,
183 unsigned int &_count) const;
188 public:
std::vector<
unsigned char> RGBAData() const;
192 public:
unsigned int Width() const;
196 public:
unsigned int Height() const;
200 public:
unsigned int BPP() const;
204 public:
int Pitch() const;
208 public:
std::
string Filename() const;
218 public: math::Color Pixel(const
unsigned int _x,
219 const
unsigned int _y) const;
223 public: math::Color AvgColor() const;
227 public: math::Color MaxColor() const;
232 public:
void Rescale(const
int _width, const
int _height);
236 public:
bool Valid() const;
242 public:
std::vector<
unsigned char> ChannelData(
Channel _channel) const;
260 public: template<typename T>
261 static
void ConvertToRGBImage(const
void *_data,
262 unsigned int _width,
unsigned int _height,
Image &_output,
263 T _min =
std::numeric_limits<T>::max(),
264 T _max =
std::numeric_limits<T>::lowest(),
bool _flip = false)
266 unsigned int samples = _width * _height;
267 unsigned int bufferSize = samples *
sizeof(T);
270 memcpy(buffer.data(), _data, bufferSize);
279 for (
unsigned int i = 0; i < samples; ++i)
285 if (v > max && !
std::isinf(
static_cast<float>(v)))
287 if (v < min && !
std::isinf(
static_cast<float>(v)))
296 double range =
static_cast<double>(max - min);
299 unsigned int idx = 0;
300 for (
unsigned int j = 0; j < _height; ++j)
302 for (
unsigned int i = 0; i < _width; ++i)
304 auto v = buffer[idx++];
305 double t =
static_cast<double>(v - min) / range;
308 uint8_t r =
static_cast<uint8_t
>(255*t);
309 unsigned int outIdx = j * _width * 3 + i * 3;
310 outputRgbBuffer[outIdx] = r;
311 outputRgbBuffer[outIdx + 1] = r;
312 outputRgbBuffer[outIdx + 2] = r;
315 _output.SetFromData(outputRgbBuffer.data(), _width, _height, RGB_INT8);
319 GZ_UTILS_IMPL_PTR(dataPtr)