Gazebo Common

API Reference

3.17.0
gz/common/Image.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef GZ_COMMON_IMAGE_HH_
18 #define GZ_COMMON_IMAGE_HH_
19 
20 #include <cstring>
21 #include <limits>
22 #include <memory>
23 #include <string>
24 #include <vector>
25 #include <gz/math/Color.hh>
26 #include <gz/common/graphics/Export.hh>
28 
29 namespace ignition
30 {
31  namespace common
32  {
34  class ImagePrivate;
35 
39  {
40  "UNKNOWN_PIXEL_FORMAT",
41  "L_INT8",
42  "L_INT16",
43  "RGB_INT8",
44  "RGBA_INT8",
45  "BGRA_INT8",
46  "RGB_INT16",
47  "RGB_INT32",
48  "BGR_INT8",
49  "BGR_INT16",
50  "BGR_INT32",
51  "R_FLOAT16",
52  "RGB_FLOAT16",
53  "R_FLOAT32",
54  "RGB_FLOAT32",
55  "BAYER_RGGB8",
56  "BAYER_RGGR8",
57  "BAYER_GBRG8",
58  "BAYER_GRBG8",
59  "BAYER_BGGR8"
60  };
61 
64  class IGNITION_COMMON_GRAPHICS_VISIBLE Image
65  {
67  public: enum PixelFormatType
68  {
69  UNKNOWN_PIXEL_FORMAT = 0,
89  BAYER_BGGR8
90  };
91 
92 
96  public: static Image::PixelFormatType ConvertPixelFormat(
97  const std::string &_format);
98 
101  public: explicit Image(const std::string &_filename = "");
102 
104  public: virtual ~Image();
105 
109  public: int Load(const std::string &_filename);
110 
113  public: void SavePNG(const std::string &_filename);
114 
117  public: void SavePNGToBuffer(std::vector<unsigned char> &_buffer);
118 
124  public: void SetFromData(const unsigned char *_data,
125  unsigned int _width,
126  unsigned int _height,
127  Image::PixelFormatType _format);
128 
132  public: void Data(unsigned char **_data,
133  unsigned int &_count) const;
134 
139  public: void RGBData(unsigned char **_data,
140  unsigned int &_count) const;
141 
144  public: unsigned int Width() const;
145 
148  public: unsigned int Height() const;
149 
152  public: unsigned int BPP() const;
153 
154  // \brief Get the size of a row of pixel
156  public: int Pitch() const;
157 
160  public: std::string Filename() const;
161 
164  public: PixelFormatType PixelFormat() const;
165 
170  public: math::Color Pixel(const unsigned int _x,
171  const unsigned int _y) const;
172 
175  public: math::Color AvgColor();
176 
179  public: math::Color MaxColor() const;
180 
184  public: void Rescale(const int _width, const int _height);
185 
188  public: bool Valid() const;
189 
206  public: template<typename T>
207  static void ConvertToRGBImage(const void *_data,
208  unsigned int _width, unsigned int _height, Image &_output,
209  T _min = std::numeric_limits<T>::max(),
210  T _max = std::numeric_limits<T>::lowest(), bool _flip = false)
211  {
212  unsigned int samples = _width * _height;
213  unsigned int bufferSize = samples * sizeof(T);
214 
215  auto buffer = std::vector<T>(samples);
216  memcpy(buffer.data(), _data, bufferSize);
217 
218  auto outputRgbBuffer = std::vector<uint8_t>(samples * 3);
219 
220  // use min and max values found in the data if not specified
221  T min = std::numeric_limits<T>::max();
223  if (_min > max)
224  {
225  for (unsigned int i = 0; i < samples; ++i)
226  {
227  auto v = buffer[i];
228  // ignore inf values when computing min/max
229  // cast to float when calling isinf to avoid compile error on
230  // windows
231  if (v > max && !std::isinf(static_cast<float>(v)))
232  max = v;
233  if (v < min && !std::isinf(static_cast<float>(v)))
234  min = v;
235  }
236  }
237  min = math::equal(_min, std::numeric_limits<T>::max()) ? min : _min;
238  max = math::equal(_max, std::numeric_limits<T>::lowest()) ? max : _max;
239 
240  // convert to rgb image
241  // color is grayscale, i.e. r == b == g
242  double range = static_cast<double>(max - min);
243  if (gz::math::equal(range, 0.0))
244  range = 1.0;
245  unsigned int idx = 0;
246  for (unsigned int j = 0; j < _height; ++j)
247  {
248  for (unsigned int i = 0; i < _width; ++i)
249  {
250  auto v = buffer[idx++];
251  double t = static_cast<double>(v - min) / range;
252  if (_flip)
253  t = 1.0 - t;
254  uint8_t r = static_cast<uint8_t>(255*t);
255  unsigned int outIdx = j * _width * 3 + i * 3;
256  outputRgbBuffer[outIdx] = r;
257  outputRgbBuffer[outIdx + 1] = r;
258  outputRgbBuffer[outIdx + 2] = r;
259  }
260  }
261  _output.SetFromData(outputRgbBuffer.data(), _width, _height, RGB_INT8);
262  }
263 
266  private: std::unique_ptr<ImagePrivate> dataPtr;
268  };
269  }
270 }
271 #endif
Forward declarations for the common classes.
STL class.
#define IGN_COMMON_WARN_IGNORE__DLL_INTERFACE_MISSING
Microsoft Visual Studio does not automatically export the interface information for member variables ...
Definition: gz/common/SuppressWarning.hh:65
@ BGR_INT8
Definition: gz/common/Image.hh:77
bool equal(const T &_a, const T &_b, const T &_epsilon=T(1e-6))
@ L_INT8
Definition: gz/common/Image.hh:70
@ RGBA_INT8
Definition: gz/common/Image.hh:73
STL class.
@ L_INT16
Definition: gz/common/Image.hh:71
@ BAYER_RGGB8
Definition: gz/common/Image.hh:84
@ BGR_INT16
Definition: gz/common/Image.hh:78
static std::string PixelFormatNames[]
String names for the pixel formats.
Definition: gz/common/Image.hh:38
T lowest(T... args)
@ BAYER_GBRG8
Definition: gz/common/Image.hh:86
void SetFromData(const unsigned char *_data, unsigned int _width, unsigned int _height, Image::PixelFormatType _format)
Set the image from raw data.
@ PIXEL_FORMAT_COUNT
Definition: gz/common/Image.hh:88
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:207
@ BGR_INT32
Definition: gz/common/Image.hh:79
@ RGB_INT16
Definition: gz/common/Image.hh:75
T isinf(T... args)
@ RGB_INT32
Definition: gz/common/Image.hh:76
@ RGB_FLOAT32
Definition: gz/common/Image.hh:83
@ RGB_FLOAT16
Definition: gz/common/Image.hh:81
@ RGB_INT8
Definition: gz/common/Image.hh:72
@ BAYER_GRBG8
Definition: gz/common/Image.hh:87
@ BAYER_RGGR8
Definition: gz/common/Image.hh:85
@ R_FLOAT16
Definition: gz/common/Image.hh:80
@ BGRA_INT8
Definition: gz/common/Image.hh:74
T max(T... args)
@ R_FLOAT32
Definition: gz/common/Image.hh:82
PixelFormatType
Pixel formats enumeration.
Definition: gz/common/Image.hh:67
#define IGN_COMMON_WARN_RESUME__DLL_INTERFACE_MISSING
Definition: gz/common/SuppressWarning.hh:68
Encapsulates an image.
Definition: gz/common/Image.hh:64