17 #ifndef IGNITION_COMMON_IMAGE_HH_
18 #define IGNITION_COMMON_IMAGE_HH_
25 #include <gz/math/Color.hh>
27 #include <gz/common/config.hh>
28 #include <gz/common/graphics/Export.hh>
30 #include <ignition/utils/ImplPtr.hh>
40 "UNKNOWN_PIXEL_FORMAT",
63 class IGNITION_COMMON_GRAPHICS_VISIBLE
Image
68 UNKNOWN_PIXEL_FORMAT = 0,
124 unsigned int _height,
130 public:
void Data(
unsigned char **_data,
unsigned int &_count);
136 public:
void RGBData(
unsigned char **_data,
unsigned int &_count);
148 public:
unsigned int BPP()
const;
167 const unsigned int _y)
const;
180 public:
void Rescale(
const int _width,
const int _height);
202 public:
template<
typename T>
204 unsigned int _width,
unsigned int _height,
Image &_output,
208 unsigned int samples = _width * _height;
209 unsigned int bufferSize = samples *
sizeof(T);
212 memcpy(buffer.data(), _data, bufferSize);
221 for (
unsigned int i = 0; i < samples; ++i)
227 if (v > max && !
std::isinf(
static_cast<float>(v)))
229 if (v < min && !
std::isinf(
static_cast<float>(v)))
238 double range =
static_cast<double>(max - min);
241 unsigned int idx = 0;
242 for (
unsigned int j = 0; j < _height; ++j)
244 for (
unsigned int i = 0; i < _width; ++i)
246 auto v = buffer[idx++];
247 double t =
static_cast<double>(v - min) / range;
250 uint8_t r =
static_cast<uint8_t
>(255*t);
251 unsigned int outIdx = j * _width * 3 + i * 3;
252 outputRgbBuffer[outIdx] = r;
253 outputRgbBuffer[outIdx + 1] = r;
254 outputRgbBuffer[outIdx + 2] = r;
257 _output.
SetFromData(outputRgbBuffer.data(), _width, _height, RGB_INT8);
261 IGN_UTILS_IMPL_PTR(dataPtr)
Encapsulates an image.
Definition: gz/common/Image.hh:64
unsigned int BPP() const
Get the size of one pixel in bits.
math::Color AvgColor()
Get the average color.
unsigned int Width() const
Get the width.
void SavePNGToBuffer(std::vector< unsigned char > &_buffer)
Get the PNG image in a buffer.
void Data(unsigned char **_data, unsigned int &_count)
Get the image as a data array.
bool Valid() const
Returns whether this is a valid image.
static void ConvertToRGBImage(const void *_data, unsigned int _width, unsigned int _height, Image &_output, T _min=std::numeric_limits< T >::max(), T _max=std::numeric_limits< T >::lowest(), bool _flip=false)
Convert a single channel image data buffer into an RGB image. During the conversion,...
Definition: gz/common/Image.hh:203
virtual ~Image()
Destructor.
void RGBData(unsigned char **_data, unsigned int &_count)
Get only the RGB data from the image. This will drop the alpha channel if one is present.
Image(const std::string &_filename="")
Constructor.
math::Color MaxColor() const
Get the max color.
PixelFormatType PixelFormat() const
Get the pixel format.
void SetFromData(const unsigned char *_data, unsigned int _width, unsigned int _height, Image::PixelFormatType _format)
Set the image from raw data.
PixelFormatType
Pixel formats enumeration.
Definition: gz/common/Image.hh:67
@ BAYER_GRBG8
Definition: gz/common/Image.hh:86
@ RGB_FLOAT16
Definition: gz/common/Image.hh:80
@ RGB_INT8
Definition: gz/common/Image.hh:71
@ RGB_FLOAT32
Definition: gz/common/Image.hh:82
@ BAYER_GBRG8
Definition: gz/common/Image.hh:85
@ L_INT16
Definition: gz/common/Image.hh:70
@ BGR_INT8
Definition: gz/common/Image.hh:76
@ R_FLOAT32
Definition: gz/common/Image.hh:81
@ BAYER_RGGB8
Definition: gz/common/Image.hh:83
@ L_INT8
Definition: gz/common/Image.hh:69
@ BAYER_BGGR8
Definition: gz/common/Image.hh:84
@ RGB_INT32
Definition: gz/common/Image.hh:75
@ RGBA_INT8
Definition: gz/common/Image.hh:72
@ BGR_INT32
Definition: gz/common/Image.hh:78
@ BGRA_INT8
Definition: gz/common/Image.hh:73
@ RGB_INT16
Definition: gz/common/Image.hh:74
@ BGR_INT16
Definition: gz/common/Image.hh:77
@ R_FLOAT16
Definition: gz/common/Image.hh:79
void Rescale(const int _width, const int _height)
Rescale the image.
int Load(const std::string &_filename)
Load an image. Return 0 on success.
static Image::PixelFormatType ConvertPixelFormat(const std::string &_format)
Convert a string to a Image::PixelFormat.
unsigned int Height() const
Get the height.
std::string Filename() const
Get the full filename of the image.
math::Color Pixel(const unsigned int _x, const unsigned int _y) const
Get a pixel color value.
void SavePNG(const std::string &_filename)
Save the image in PNG format.
static std::string PixelFormatNames[]
String names for the pixel formats.
Definition: gz/common/Image.hh:38
bool equal(const T &_a, const T &_b, const T &_epsilon=T(1e-6))
Forward declarations for the common classes.