Gazebo Common

API Reference

3.14.1
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 IGNITION_COMMON_IMAGE_HH_
18 #define IGNITION_COMMON_IMAGE_HH_
19 
20 #include <limits>
21 #include <memory>
22 #include <string>
23 #include <vector>
24 #include <ignition/math/Color.hh>
25 #include <ignition/common/graphics/Export.hh>
27 
28 namespace ignition
29 {
30  namespace common
31  {
33  class ImagePrivate;
34 
38  {
39  "UNKNOWN_PIXEL_FORMAT",
40  "L_INT8",
41  "L_INT16",
42  "RGB_INT8",
43  "RGBA_INT8",
44  "BGRA_INT8",
45  "RGB_INT16",
46  "RGB_INT32",
47  "BGR_INT8",
48  "BGR_INT16",
49  "BGR_INT32",
50  "R_FLOAT16",
51  "RGB_FLOAT16",
52  "R_FLOAT32",
53  "RGB_FLOAT32",
54  "BAYER_RGGB8",
55  "BAYER_RGGR8",
56  "BAYER_GBRG8",
57  "BAYER_GRBG8",
58  "BAYER_BGGR8"
59  };
60 
63  class IGNITION_COMMON_GRAPHICS_VISIBLE Image
64  {
66  public: enum PixelFormatType
67  {
68  UNKNOWN_PIXEL_FORMAT = 0,
88  BAYER_BGGR8
89  };
90 
91 
95  public: static Image::PixelFormatType ConvertPixelFormat(
96  const std::string &_format);
97 
100  public: explicit Image(const std::string &_filename = "");
101 
103  public: virtual ~Image();
104 
108  public: int Load(const std::string &_filename);
109 
112  public: void SavePNG(const std::string &_filename);
113 
116  public: void SavePNGToBuffer(std::vector<unsigned char> &_buffer);
117 
123  public: void SetFromData(const unsigned char *_data,
124  unsigned int _width,
125  unsigned int _height,
126  Image::PixelFormatType _format);
127 
131  public: void Data(unsigned char **_data,
132  unsigned int &_count) const;
133 
138  public: void RGBData(unsigned char **_data,
139  unsigned int &_count) const;
140 
143  public: unsigned int Width() const;
144 
147  public: unsigned int Height() const;
148 
151  public: unsigned int BPP() const;
152 
153  // \brief Get the size of a row of pixel
155  public: int Pitch() const;
156 
159  public: std::string Filename() const;
160 
163  public: PixelFormatType PixelFormat() const;
164 
169  public: math::Color Pixel(const unsigned int _x,
170  const unsigned int _y) const;
171 
174  public: math::Color AvgColor();
175 
178  public: math::Color MaxColor() const;
179 
183  public: void Rescale(const int _width, const int _height);
184 
187  public: bool Valid() const;
188 
205  public: template<typename T>
206  static void ConvertToRGBImage(const void *_data,
207  unsigned int _width, unsigned int _height, Image &_output,
208  T _min = std::numeric_limits<T>::max(),
209  T _max = std::numeric_limits<T>::lowest(), bool _flip = false)
210  {
211  unsigned int samples = _width * _height;
212  unsigned int bufferSize = samples * sizeof(T);
213 
214  auto buffer = std::vector<T>(samples);
215  memcpy(buffer.data(), _data, bufferSize);
216 
217  auto outputRgbBuffer = std::vector<uint8_t>(samples * 3);
218 
219  // use min and max values found in the data if not specified
220  T min = std::numeric_limits<T>::max();
222  if (_min > max)
223  {
224  for (unsigned int i = 0; i < samples; ++i)
225  {
226  auto v = buffer[i];
227  // ignore inf values when computing min/max
228  // cast to float when calling isinf to avoid compile error on
229  // windows
230  if (v > max && !std::isinf(static_cast<float>(v)))
231  max = v;
232  if (v < min && !std::isinf(static_cast<float>(v)))
233  min = v;
234  }
235  }
236  min = math::equal(_min, std::numeric_limits<T>::max()) ? min : _min;
237  max = math::equal(_max, std::numeric_limits<T>::lowest()) ? max : _max;
238 
239  // convert to rgb image
240  // color is grayscale, i.e. r == b == g
241  double range = static_cast<double>(max - min);
242  if (ignition::math::equal(range, 0.0))
243  range = 1.0;
244  unsigned int idx = 0;
245  for (unsigned int j = 0; j < _height; ++j)
246  {
247  for (unsigned int i = 0; i < _width; ++i)
248  {
249  auto v = buffer[idx++];
250  double t = static_cast<double>(v - min) / range;
251  if (_flip)
252  t = 1.0 - t;
253  uint8_t r = static_cast<uint8_t>(255*t);
254  unsigned int outIdx = j * _width * 3 + i * 3;
255  outputRgbBuffer[outIdx] = r;
256  outputRgbBuffer[outIdx + 1] = r;
257  outputRgbBuffer[outIdx + 2] = r;
258  }
259  }
260  _output.SetFromData(outputRgbBuffer.data(), _width, _height, RGB_INT8);
261  }
262 
265  private: std::unique_ptr<ImagePrivate> dataPtr;
267  };
268  }
269 }
270 #endif
Forward declarations for the common classes.
STL class.
@ BGR_INT8
Definition: Image.hh:76
bool equal(const T &_a, const T &_b, const T &_epsilon=T(1e-6))
@ L_INT8
Definition: Image.hh:69
@ RGBA_INT8
Definition: Image.hh:72
STL class.
@ L_INT16
Definition: Image.hh:70
@ BAYER_RGGB8
Definition: Image.hh:83
@ BGR_INT16
Definition: Image.hh:77
#define IGN_COMMON_WARN_IGNORE__DLL_INTERFACE_MISSING
Microsoft Visual Studio does not automatically export the interface information for member variables ...
Definition: SuppressWarning.hh:64
static std::string PixelFormatNames[]
String names for the pixel formats.
Definition: Image.hh:37
T lowest(T... args)
@ BAYER_GBRG8
Definition: Image.hh:85
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: Image.hh:87
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: Image.hh:206
@ BGR_INT32
Definition: Image.hh:78
@ RGB_INT16
Definition: Image.hh:74
T isinf(T... args)
@ RGB_INT32
Definition: Image.hh:75
#define IGN_COMMON_WARN_RESUME__DLL_INTERFACE_MISSING
Definition: SuppressWarning.hh:67
@ RGB_FLOAT32
Definition: Image.hh:82
@ RGB_FLOAT16
Definition: Image.hh:80
@ RGB_INT8
Definition: Image.hh:71
@ BAYER_GRBG8
Definition: Image.hh:86
@ BAYER_RGGR8
Definition: Image.hh:84
@ R_FLOAT16
Definition: Image.hh:79
@ BGRA_INT8
Definition: Image.hh:73
T max(T... args)
@ R_FLOAT32
Definition: Image.hh:81
PixelFormatType
Pixel formats enumeration.
Definition: Image.hh:66
Encapsulates an image.
Definition: Image.hh:63