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
68 UNKNOWN_PIXEL_FORMAT = 0,
129 unsigned int _height,
144 public:
void GZ_DEPRECATED(5) Data(
unsigned char **_data,
145 unsigned int &_count) const;
149 public:
std::vector<
unsigned char> Data() const;
156 public:
void GZ_DEPRECATED(5) RGBData(
unsigned char **_data,
157 unsigned int &_count) const;
162 public:
std::vector<
unsigned char> RGBData() const;
169 public:
void GZ_DEPRECATED(5) RGBAData(
unsigned char **_data,
170 unsigned int &_count) const;
175 public:
std::vector<
unsigned char> RGBAData() const;
179 public:
unsigned int Width() const;
183 public:
unsigned int Height() const;
187 public:
unsigned int BPP() const;
191 public:
int Pitch() const;
195 public:
std::
string Filename() const;
205 public: math::Color Pixel(const
unsigned int _x,
206 const
unsigned int _y) const;
210 public: math::Color AvgColor() const;
214 public: math::Color MaxColor() const;
219 public:
void Rescale(const
int _width, const
int _height);
223 public:
bool Valid() const;
241 public: template<typename T>
242 static
void ConvertToRGBImage(const
void *_data,
243 unsigned int _width,
unsigned int _height,
Image &_output,
244 T _min =
std::numeric_limits<T>::max(),
245 T _max =
std::numeric_limits<T>::lowest(),
bool _flip = false)
247 unsigned int samples = _width * _height;
248 unsigned int bufferSize = samples *
sizeof(T);
251 memcpy(buffer.data(), _data, bufferSize);
260 for (
unsigned int i = 0; i < samples; ++i)
266 if (v > max && !
std::isinf(
static_cast<float>(v)))
268 if (v < min && !
std::isinf(
static_cast<float>(v)))
277 double range =
static_cast<double>(max - min);
280 unsigned int idx = 0;
281 for (
unsigned int j = 0; j < _height; ++j)
283 for (
unsigned int i = 0; i < _width; ++i)
285 auto v = buffer[idx++];
286 double t =
static_cast<double>(v - min) / range;
289 uint8_t r =
static_cast<uint8_t
>(255*t);
290 unsigned int outIdx = j * _width * 3 + i * 3;
291 outputRgbBuffer[outIdx] = r;
292 outputRgbBuffer[outIdx + 1] = r;
293 outputRgbBuffer[outIdx + 2] = r;
296 _output.SetFromData(outputRgbBuffer.data(), _width, _height, RGB_INT8);
300 GZ_UTILS_IMPL_PTR(dataPtr)