Ignition Common

API Reference

4.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 IGNITION_COMMON_IMAGE_HH_
18 #define IGNITION_COMMON_IMAGE_HH_
19 
20 #include <limits>
21 #include <memory>
22 #include <string>
23 #include <vector>
24 #include <ignition/math/Color.hh>
25 #include <ignition/common/graphics/Export.hh>
26 
27 #include <ignition/utils/ImplPtr.hh>
28 
29 namespace ignition
30 {
31  namespace common
32  {
35  static std::string PixelFormatNames[] =
36  {
37  "UNKNOWN_PIXEL_FORMAT",
38  "L_INT8",
39  "L_INT16",
40  "RGB_INT8",
41  "RGBA_INT8",
42  "BGRA_INT8",
43  "RGB_INT16",
44  "RGB_INT32",
45  "BGR_INT8",
46  "BGR_INT16",
47  "BGR_INT32",
48  "R_FLOAT16",
49  "RGB_FLOAT16",
50  "R_FLOAT32",
51  "RGB_FLOAT32",
52  "BAYER_RGGB8",
53  "BAYER_BGGR8",
54  "BAYER_GBRG8",
55  "BAYER_GRBG8"
56  };
57 
60  class IGNITION_COMMON_GRAPHICS_VISIBLE Image
61  {
63  public: enum PixelFormatType
64  {
65  UNKNOWN_PIXEL_FORMAT = 0,
84  PIXEL_FORMAT_COUNT
85  };
86 
87 
91  public: static Image::PixelFormatType ConvertPixelFormat(
92  const std::string &_format);
93 
96  public: explicit Image(const std::string &_filename = "");
97 
99  public: virtual ~Image();
100 
104  public: int Load(const std::string &_filename);
105 
108  public: void SavePNG(const std::string &_filename);
109 
112  public: void SavePNGToBuffer(std::vector<unsigned char> &_buffer);
113 
119  public: void SetFromData(const unsigned char *_data,
120  unsigned int _width,
121  unsigned int _height,
122  Image::PixelFormatType _format);
123 
127  public: void Data(unsigned char **_data, unsigned int &_count);
128 
133  public: void RGBData(unsigned char **_data, unsigned int &_count);
134 
137  public: unsigned int Width() const;
138 
141  public: unsigned int Height() const;
142 
145  public: unsigned int BPP() const;
146 
147  // \brief Get the size of a row of pixel
149  public: int Pitch() const;
150 
153  public: std::string Filename() const;
154 
157  public: PixelFormatType PixelFormat() const;
158 
163  public: math::Color Pixel(const unsigned int _x,
164  const unsigned int _y) const;
165 
168  public: math::Color AvgColor();
169 
172  public: math::Color MaxColor() const;
173 
177  public: void Rescale(const int _width, const int _height);
178 
181  public: bool Valid() const;
182 
199  public: template<typename T>
200  static void ConvertToRGBImage(const void *_data,
201  unsigned int _width, unsigned int _height, Image &_output,
202  T _min = std::numeric_limits<T>::max(),
203  T _max = std::numeric_limits<T>::lowest(), bool _flip = false)
204  {
205  unsigned int samples = _width * _height;
206  unsigned int bufferSize = samples * sizeof(T);
207 
208  auto buffer = std::vector<T>(samples);
209  memcpy(buffer.data(), _data, bufferSize);
210 
211  auto outputRgbBuffer = std::vector<uint8_t>(samples * 3);
212 
213  // use min and max values found in the data if not specified
214  T min = std::numeric_limits<T>::max();
216  if (_min > max)
217  {
218  for (unsigned int i = 0; i < samples; ++i)
219  {
220  auto v = buffer[i];
221  // ignore inf values when computing min/max
222  // cast to float when calling isinf to avoid compile error on
223  // windows
224  if (v > max && !std::isinf(static_cast<float>(v)))
225  max = v;
226  if (v < min && !std::isinf(static_cast<float>(v)))
227  min = v;
228  }
229  }
230  min = math::equal(_min, std::numeric_limits<T>::max()) ? min : _min;
231  max = math::equal(_max, std::numeric_limits<T>::lowest()) ? max : _max;
232 
233  // convert to rgb image
234  // color is grayscale, i.e. r == b == g
235  double range = static_cast<double>(max - min);
236  if (ignition::math::equal(range, 0.0))
237  range = 1.0;
238  unsigned int idx = 0;
239  for (unsigned int j = 0; j < _height; ++j)
240  {
241  for (unsigned int i = 0; i < _width; ++i)
242  {
243  auto v = buffer[idx++];
244  double t = static_cast<double>(v - min) / range;
245  if (_flip)
246  t = 1.0 - t;
247  uint8_t r = static_cast<uint8_t>(255*t);
248  unsigned int outIdx = j * _width * 3 + i * 3;
249  outputRgbBuffer[outIdx] = r;
250  outputRgbBuffer[outIdx + 1] = r;
251  outputRgbBuffer[outIdx + 2] = r;
252  }
253  }
254  _output.SetFromData(outputRgbBuffer.data(), _width, _height, RGB_INT8);
255  }
256 
258  IGN_UTILS_IMPL_PTR(dataPtr)
259  };
260  }
261 }
262 #endif
Definition: Image.hh:72
Definition: Image.hh:71
bool equal(const T &_a, const T &_b, const T &_epsilon=T(1e-6))
Definition: Image.hh:67
Definition: Image.hh:66
STL class.
PixelFormatType
Pixel formats enumeration.
Definition: Image.hh:63
T data(T... args)
T lowest(T... args)
Definition: Image.hh:78
Definition: Image.hh:76
Definition: Image.hh:68
Definition: Image.hh:69
T max(T... args)
STL class.
Definition: Image.hh:74
static void ConvertToRGBImage(const void *_data, unsigned int _width, unsigned int _height, Image &_output, T _min=std::numeric_limits< T >::max(), T _max=std::numeric_limits< T >::lowest(), bool _flip=false)
Convert a single channel image data buffer into an RGB image. During the conversion, the input image data are normalized to 8 bit values i.e. [0, 255]. Optionally, specify min and max values to use when normalizing the input image data. For example, if min and max are set to 1 and 10, a data value 2 will be normalized to: (2 - 1) / (10 - 1) * 255.
Definition: Image.hh:200
void SetFromData(const unsigned char *_data, unsigned int _width, unsigned int _height, Image::PixelFormatType _format)
Set the image from raw data.
T isinf(T... args)
Forward declarations for the common classes.
Definition: Image.hh:73
Definition: Image.hh:75
Encapsulates an image.
Definition: Image.hh:60
Definition: Image.hh:70