Gazebo Common

API Reference

7.4.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 <optional>
24#include <string>
25#include <vector>
26#include <gz/math/Color.hh>
27
28#include <gz/common/graphics/Export.hh>
29
30#include <gz/utils/ImplPtr.hh>
31
32namespace gz
33{
34 namespace common
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 "RGBA_INT16",
48 "RGB_INT32",
49 "BGR_INT8",
50 "BGR_INT16",
51 "BGR_INT32",
52 "R_FLOAT16",
53 "RGB_FLOAT16",
54 "R_FLOAT32",
55 "RGB_FLOAT32",
56 "BAYER_RGGB8",
57 "BAYER_BGGR8",
58 "BAYER_GBRG8",
59 "BAYER_GRBG8",
60 "COMPRESSED_PNG"
61 };
62
65 class GZ_COMMON_GRAPHICS_VISIBLE Image
66 {
68 public: enum class Channel
69 {
71 RED = 0,
73 GREEN = 1,
75 BLUE = 2,
77 ALPHA = 3
78 };
79
81 public: enum PixelFormatType
82 {
83 UNKNOWN_PIXEL_FORMAT = 0,
105 // \todo(iche033) COMPRESSED_JPEG is added at the end to
106 // preserve ABI compatibility. Move this enum up when merging
107 // forward to main
108 COMPRESSED_JPEG
109 };
110
111
116 const std::string &_format);
117
120 public: explicit Image(const std::string &_filename = "");
121
123 public: virtual ~Image();
124
128 public: int Load(const std::string &_filename);
129
144 public: int Load(const std::string &_filename,
145 std::optional<PixelFormatType> _outputFormat);
146
149 public: void SavePNG(const std::string &_filename);
150
154
160 public: void SetFromData(const unsigned char *_data,
161 unsigned int _width,
162 unsigned int _height,
163 Image::PixelFormatType _format);
164
169 public: void SetFromCompressedData(unsigned char *_data,
170 unsigned int _size,
171 Image::PixelFormatType _format);
172
183 public: void SetFromCompressedData(const unsigned char *_data,
184 unsigned int _size,
185 Image::PixelFormatType _inputFormat,
186 std::optional<PixelFormatType>
187 _outputFormat);
188
192
197
202
205 public: unsigned int Width() const;
206
209 public: unsigned int Height() const;
210
213 public: unsigned int BPP() const;
214
215 // \brief Get the size of a row of pixel
217 public: int Pitch() const;
218
221 public: std::string Filename() const;
222
226
231 public: math::Color Pixel(const unsigned int _x,
232 const unsigned int _y) const;
233
236 public: math::Color AvgColor() const;
237
240 public: math::Color MaxColor() const;
241
245 public: void Rescale(const int _width, const int _height);
246
249 public: bool Valid() const;
250
256
273 public: template<typename T>
274 static void ConvertToRGBImage(const void *_data,
275 unsigned int _width, unsigned int _height, Image &_output,
277 T _max = std::numeric_limits<T>::lowest(), bool _flip = false)
278 {
279 unsigned int samples = _width * _height;
280 unsigned int bufferSize = samples * sizeof(T);
281
282 auto buffer = std::vector<T>(samples);
283 memcpy(buffer.data(), _data, bufferSize);
284
285 auto outputRgbBuffer = std::vector<uint8_t>(samples * 3);
286
287 // use min and max values found in the data if not specified
290 if (_min > max)
291 {
292 for (unsigned int i = 0; i < samples; ++i)
293 {
294 auto v = buffer[i];
295 // ignore inf values when computing min/max
296 // cast to float when calling isinf to avoid compile error on
297 // windows
298 if (v > max && !std::isinf(static_cast<float>(v)))
299 max = v;
300 if (v < min && !std::isinf(static_cast<float>(v)))
301 min = v;
302 }
303 }
304 min = math::equal(_min, std::numeric_limits<T>::max()) ? min : _min;
305 max = math::equal(_max, std::numeric_limits<T>::lowest()) ? max : _max;
306
307 // convert to rgb image
308 // color is grayscale, i.e. r == b == g
309 double range = static_cast<double>(max - min);
310 if (gz::math::equal(range, 0.0))
311 range = 1.0;
312 unsigned int idx = 0;
313 for (unsigned int j = 0; j < _height; ++j)
314 {
315 for (unsigned int i = 0; i < _width; ++i)
316 {
317 auto v = buffer[idx++];
318 double t = static_cast<double>(v - min) / range;
319 if (_flip)
320 t = 1.0 - t;
321 uint8_t r = static_cast<uint8_t>(255*t);
322 unsigned int outIdx = j * _width * 3 + i * 3;
323 outputRgbBuffer[outIdx] = r;
324 outputRgbBuffer[outIdx + 1] = r;
325 outputRgbBuffer[outIdx + 2] = r;
326 }
327 }
328 _output.SetFromData(outputRgbBuffer.data(), _width, _height, RGB_INT8);
329 }
330
332 GZ_UTILS_IMPL_PTR(dataPtr)
333 };
334 }
335}
336#endif