Gazebo Common

API Reference

7.0.0~pre1
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 PixelFormatType
68 {
69 UNKNOWN_PIXEL_FORMAT = 0,
91 // \todo(iche033) COMPRESSED_JPEG is added at the end to
92 // preserve ABI compatibility. Move this enum up when merging
93 // forward to main
94 COMPRESSED_JPEG
95 };
96
97
102 const std::string &_format);
103
106 public: explicit Image(const std::string &_filename = "");
107
109 public: virtual ~Image();
110
114 public: int Load(const std::string &_filename);
115
118 public: void SavePNG(const std::string &_filename);
119
123
129 public: void SetFromData(const unsigned char *_data,
130 unsigned int _width,
131 unsigned int _height,
132 Image::PixelFormatType _format);
133
138 public: void SetFromCompressedData(unsigned char *_data,
139 unsigned int _size,
140 Image::PixelFormatType _format);
141
145
150
155
158 public: unsigned int Width() const;
159
162 public: unsigned int Height() const;
163
166 public: unsigned int BPP() const;
167
168 // \brief Get the size of a row of pixel
170 public: int Pitch() const;
171
174 public: std::string Filename() const;
175
179
184 public: math::Color Pixel(const unsigned int _x,
185 const unsigned int _y) const;
186
189 public: math::Color AvgColor() const;
190
193 public: math::Color MaxColor() const;
194
198 public: void Rescale(const int _width, const int _height);
199
202 public: bool Valid() const;
203
220 public: template<typename T>
221 static void ConvertToRGBImage(const void *_data,
222 unsigned int _width, unsigned int _height, Image &_output,
224 T _max = std::numeric_limits<T>::lowest(), bool _flip = false)
225 {
226 unsigned int samples = _width * _height;
227 unsigned int bufferSize = samples * sizeof(T);
228
229 auto buffer = std::vector<T>(samples);
230 memcpy(buffer.data(), _data, bufferSize);
231
232 auto outputRgbBuffer = std::vector<uint8_t>(samples * 3);
233
234 // use min and max values found in the data if not specified
237 if (_min > max)
238 {
239 for (unsigned int i = 0; i < samples; ++i)
240 {
241 auto v = buffer[i];
242 // ignore inf values when computing min/max
243 // cast to float when calling isinf to avoid compile error on
244 // windows
245 if (v > max && !std::isinf(static_cast<float>(v)))
246 max = v;
247 if (v < min && !std::isinf(static_cast<float>(v)))
248 min = v;
249 }
250 }
251 min = math::equal(_min, std::numeric_limits<T>::max()) ? min : _min;
252 max = math::equal(_max, std::numeric_limits<T>::lowest()) ? max : _max;
253
254 // convert to rgb image
255 // color is grayscale, i.e. r == b == g
256 double range = static_cast<double>(max - min);
257 if (gz::math::equal(range, 0.0))
258 range = 1.0;
259 unsigned int idx = 0;
260 for (unsigned int j = 0; j < _height; ++j)
261 {
262 for (unsigned int i = 0; i < _width; ++i)
263 {
264 auto v = buffer[idx++];
265 double t = static_cast<double>(v - min) / range;
266 if (_flip)
267 t = 1.0 - t;
268 uint8_t r = static_cast<uint8_t>(255*t);
269 unsigned int outIdx = j * _width * 3 + i * 3;
270 outputRgbBuffer[outIdx] = r;
271 outputRgbBuffer[outIdx + 1] = r;
272 outputRgbBuffer[outIdx + 2] = r;
273 }
274 }
275 _output.SetFromData(outputRgbBuffer.data(), _width, _height, RGB_INT8);
276 }
277
279 GZ_UTILS_IMPL_PTR(dataPtr)
280 };
281 }
282}
283#endif