Gazebo Common

API Reference

5.7.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 
27 #include <gz/common/graphics/Export.hh>
28 
29 #include <gz/utils/ImplPtr.hh>
30 
31 namespace gz
32 {
33  namespace common
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_BGGR8",
56  "BAYER_GBRG8",
57  "BAYER_GRBG8",
58  "COMPRESSED_PNG"
59  };
60 
63  class GZ_COMMON_GRAPHICS_VISIBLE Image
64  {
66  public: enum PixelFormatType
67  {
68  UNKNOWN_PIXEL_FORMAT = 0,
89  // \todo(iche033) COMPRESSED_JPEG is added at the end to
90  // preserve ABI compatibility. Move this enum up when merging
91  // forward to main
92  COMPRESSED_JPEG
93  };
94 
95 
100  const std::string &_format);
101 
104  public: explicit Image(const std::string &_filename = "");
105 
107  public: virtual ~Image();
108 
112  public: int Load(const std::string &_filename);
113 
116  public: void SavePNG(const std::string &_filename);
117 
121 
127  public: void SetFromData(const unsigned char *_data,
128  unsigned int _width,
129  unsigned int _height,
130  Image::PixelFormatType _format);
131 
136  public: void SetFromCompressedData(unsigned char *_data,
137  unsigned int _size,
138  Image::PixelFormatType _format);
139 
144  public: void GZ_DEPRECATED(5) Data(unsigned char **_data,
145  unsigned int &_count) const;
146 
149  public: std::vector<unsigned char> Data() const;
150 
156  public: void GZ_DEPRECATED(5) RGBData(unsigned char **_data,
157  unsigned int &_count) const;
158 
162  public: std::vector<unsigned char> RGBData() const;
163 
169  public: void GZ_DEPRECATED(5) RGBAData(unsigned char **_data,
170  unsigned int &_count) const;
171 
175  public: std::vector<unsigned char> RGBAData() const;
176 
179  public: unsigned int Width() const;
180 
183  public: unsigned int Height() const;
184 
187  public: unsigned int BPP() const;
188 
189  // \brief Get the size of a row of pixel
191  public: int Pitch() const;
192 
195  public: std::string Filename() const;
196 
199  public: PixelFormatType PixelFormat() const;
200 
205  public: math::Color Pixel(const unsigned int _x,
206  const unsigned int _y) const;
207 
210  public: math::Color AvgColor() const;
211 
214  public: math::Color MaxColor() const;
215 
219  public: void Rescale(const int _width, const int _height);
220 
223  public: bool Valid() const;
224 
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)
246  {
247  unsigned int samples = _width * _height;
248  unsigned int bufferSize = samples * sizeof(T);
249 
250  auto buffer = std::vector<T>(samples);
251  memcpy(buffer.data(), _data, bufferSize);
252 
253  auto outputRgbBuffer = std::vector<uint8_t>(samples * 3);
254 
255  // use min and max values found in the data if not specified
256  T min = std::numeric_limits<T>::max();
258  if (_min > max)
259  {
260  for (unsigned int i = 0; i < samples; ++i)
261  {
262  auto v = buffer[i];
263  // ignore inf values when computing min/max
264  // cast to float when calling isinf to avoid compile error on
265  // windows
266  if (v > max && !std::isinf(static_cast<float>(v)))
267  max = v;
268  if (v < min && !std::isinf(static_cast<float>(v)))
269  min = v;
270  }
271  }
272  min = math::equal(_min, std::numeric_limits<T>::max()) ? min : _min;
273  max = math::equal(_max, std::numeric_limits<T>::lowest()) ? max : _max;
274 
275  // convert to rgb image
276  // color is grayscale, i.e. r == b == g
277  double range = static_cast<double>(max - min);
278  if (gz::math::equal(range, 0.0))
279  range = 1.0;
280  unsigned int idx = 0;
281  for (unsigned int j = 0; j < _height; ++j)
282  {
283  for (unsigned int i = 0; i < _width; ++i)
284  {
285  auto v = buffer[idx++];
286  double t = static_cast<double>(v - min) / range;
287  if (_flip)
288  t = 1.0 - t;
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;
294  }
295  }
296  _output.SetFromData(outputRgbBuffer.data(), _width, _height, RGB_INT8);
297  }
298 
300  GZ_UTILS_IMPL_PTR(dataPtr)
301  };
302  }
303 }
304 #endif