Gazebo Common

API Reference

7.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 "RGBA_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_BGGR8",
57 "BAYER_GBRG8",
58 "BAYER_GRBG8",
59 "COMPRESSED_PNG"
60 };
61
64 class GZ_COMMON_GRAPHICS_VISIBLE Image
65 {
67 public: enum class Channel
68 {
70 RED = 0,
72 GREEN = 1,
74 BLUE = 2,
76 ALPHA = 3
77 };
78
80 public: enum PixelFormatType
81 {
82 UNKNOWN_PIXEL_FORMAT = 0,
104 // \todo(iche033) COMPRESSED_JPEG is added at the end to
105 // preserve ABI compatibility. Move this enum up when merging
106 // forward to main
107 COMPRESSED_JPEG
108 };
109
110
115 const std::string &_format);
116
119 public: explicit Image(const std::string &_filename = "");
120
122 public: virtual ~Image();
123
127 public: int Load(const std::string &_filename);
128
131 public: void SavePNG(const std::string &_filename);
132
136
142 public: void SetFromData(const unsigned char *_data,
143 unsigned int _width,
144 unsigned int _height,
145 Image::PixelFormatType _format);
146
151 public: void SetFromCompressedData(unsigned char *_data,
152 unsigned int _size,
153 Image::PixelFormatType _format);
154
158
163
168
171 public: unsigned int Width() const;
172
175 public: unsigned int Height() const;
176
179 public: unsigned int BPP() const;
180
181 // \brief Get the size of a row of pixel
183 public: int Pitch() const;
184
187 public: std::string Filename() const;
188
192
197 public: math::Color Pixel(const unsigned int _x,
198 const unsigned int _y) const;
199
202 public: math::Color AvgColor() const;
203
206 public: math::Color MaxColor() const;
207
211 public: void Rescale(const int _width, const int _height);
212
215 public: bool Valid() const;
216
222
239 public: template<typename T>
240 static void ConvertToRGBImage(const void *_data,
241 unsigned int _width, unsigned int _height, Image &_output,
243 T _max = std::numeric_limits<T>::lowest(), bool _flip = false)
244 {
245 unsigned int samples = _width * _height;
246 unsigned int bufferSize = samples * sizeof(T);
247
248 auto buffer = std::vector<T>(samples);
249 memcpy(buffer.data(), _data, bufferSize);
250
251 auto outputRgbBuffer = std::vector<uint8_t>(samples * 3);
252
253 // use min and max values found in the data if not specified
256 if (_min > max)
257 {
258 for (unsigned int i = 0; i < samples; ++i)
259 {
260 auto v = buffer[i];
261 // ignore inf values when computing min/max
262 // cast to float when calling isinf to avoid compile error on
263 // windows
264 if (v > max && !std::isinf(static_cast<float>(v)))
265 max = v;
266 if (v < min && !std::isinf(static_cast<float>(v)))
267 min = v;
268 }
269 }
270 min = math::equal(_min, std::numeric_limits<T>::max()) ? min : _min;
271 max = math::equal(_max, std::numeric_limits<T>::lowest()) ? max : _max;
272
273 // convert to rgb image
274 // color is grayscale, i.e. r == b == g
275 double range = static_cast<double>(max - min);
276 if (gz::math::equal(range, 0.0))
277 range = 1.0;
278 unsigned int idx = 0;
279 for (unsigned int j = 0; j < _height; ++j)
280 {
281 for (unsigned int i = 0; i < _width; ++i)
282 {
283 auto v = buffer[idx++];
284 double t = static_cast<double>(v - min) / range;
285 if (_flip)
286 t = 1.0 - t;
287 uint8_t r = static_cast<uint8_t>(255*t);
288 unsigned int outIdx = j * _width * 3 + i * 3;
289 outputRgbBuffer[outIdx] = r;
290 outputRgbBuffer[outIdx + 1] = r;
291 outputRgbBuffer[outIdx + 2] = r;
292 }
293 }
294 _output.SetFromData(outputRgbBuffer.data(), _width, _height, RGB_INT8);
295 }
296
298 GZ_UTILS_IMPL_PTR(dataPtr)
299 };
300 }
301}
302#endif