Gazebo Common

API Reference

6.0.0
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
31namespace 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
143
148
153
156 public: unsigned int Width() const;
157
160 public: unsigned int Height() const;
161
164 public: unsigned int BPP() const;
165
166 // \brief Get the size of a row of pixel
168 public: int Pitch() const;
169
172 public: std::string Filename() const;
173
177
182 public: math::Color Pixel(const unsigned int _x,
183 const unsigned int _y) const;
184
187 public: math::Color AvgColor() const;
188
191 public: math::Color MaxColor() const;
192
196 public: void Rescale(const int _width, const int _height);
197
200 public: bool Valid() const;
201
218 public: template<typename T>
219 static void ConvertToRGBImage(const void *_data,
220 unsigned int _width, unsigned int _height, Image &_output,
222 T _max = std::numeric_limits<T>::lowest(), bool _flip = false)
223 {
224 unsigned int samples = _width * _height;
225 unsigned int bufferSize = samples * sizeof(T);
226
227 auto buffer = std::vector<T>(samples);
228 memcpy(buffer.data(), _data, bufferSize);
229
230 auto outputRgbBuffer = std::vector<uint8_t>(samples * 3);
231
232 // use min and max values found in the data if not specified
235 if (_min > max)
236 {
237 for (unsigned int i = 0; i < samples; ++i)
238 {
239 auto v = buffer[i];
240 // ignore inf values when computing min/max
241 // cast to float when calling isinf to avoid compile error on
242 // windows
243 if (v > max && !std::isinf(static_cast<float>(v)))
244 max = v;
245 if (v < min && !std::isinf(static_cast<float>(v)))
246 min = v;
247 }
248 }
249 min = math::equal(_min, std::numeric_limits<T>::max()) ? min : _min;
250 max = math::equal(_max, std::numeric_limits<T>::lowest()) ? max : _max;
251
252 // convert to rgb image
253 // color is grayscale, i.e. r == b == g
254 double range = static_cast<double>(max - min);
255 if (gz::math::equal(range, 0.0))
256 range = 1.0;
257 unsigned int idx = 0;
258 for (unsigned int j = 0; j < _height; ++j)
259 {
260 for (unsigned int i = 0; i < _width; ++i)
261 {
262 auto v = buffer[idx++];
263 double t = static_cast<double>(v - min) / range;
264 if (_flip)
265 t = 1.0 - t;
266 uint8_t r = static_cast<uint8_t>(255*t);
267 unsigned int outIdx = j * _width * 3 + i * 3;
268 outputRgbBuffer[outIdx] = r;
269 outputRgbBuffer[outIdx + 1] = r;
270 outputRgbBuffer[outIdx + 2] = r;
271 }
272 }
273 _output.SetFromData(outputRgbBuffer.data(), _width, _height, RGB_INT8);
274 }
275
277 GZ_UTILS_IMPL_PTR(dataPtr)
278 };
279 }
280}
281#endif