-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathImage.h
More file actions
340 lines (285 loc) · 12.5 KB
/
Copy pathImage.h
File metadata and controls
340 lines (285 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**
* @copyright Copyright (c) 2017 B-com http://www.b-com.com/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef SOLAR_IMAGE_H
#define SOLAR_IMAGE_H
#include <xpcf/core/refs.h>
#include <core/SolARFrameworkDefinitions.h>
#include <core/Messages.h>
#include <datastructure/GeometryDefinitions.h>
#include <memory>
#include <core/SerializationDefinitions.h>
#include <boost/serialization/export.hpp>
namespace SolAR {
namespace datastructure {
/**
* @class ImageInternal
* @brief <B>A 2D image buffer.</B>.
*
*/
class SOLARFRAMEWORK_API ImageInternal {
public:
ImageInternal() = default;
explicit ImageInternal(uint32_t size);
explicit ImageInternal(void* data, uint32_t size);
virtual ~ImageInternal();
void setBufferSize(uint32_t size);
inline uint32_t getBufferSize() { return m_bufferSize; }
void setData(void * data, uint32_t size);
inline void* data() { return m_storageData.data(); }
inline const void* data() const { return m_storageData.data(); }
private:
friend class boost::serialization::access;
template<typename Archive>
void serialize(Archive &ar, const unsigned int version);
private:
std::vector<uint8_t> m_storageData;
uint32_t m_bufferSize = 0;
};
DECLARESERIALIZE(ImageInternal);
//Add stride notion
// Hypothese : pas de bits per component : only full format image YUV444, RGB888, RGB 555 but not YUV420, RGB565 and so on or YUV422 with splatting
/**
* @class Image
* @brief <B>A 2D image.</B>.
*
* This class provides an image abstraction for SolAR
*/
class SOLARFRAMEWORK_API Image {
public:
//static_assert(std::is_same<T, double>::value || std::is_same<T, float>::value || std::is_same<T, uint8_t>::value,"type not allowed for Image");
enum DataType {
TYPE_8U, /**< each pixel part information is stored in one unsigned integer byte. For instance, an RGBA layer image is stored in 4 bytes, one byte per channel */
TYPE_16U, /**< each pixel part information is stored in two unsigned integer bytes. For instance, an RGBA layer image is stored in 8 bytes, two bytes per channel */
TYPE_32U, /**< each pixel part information is stored in four unsigned integer bytes. For instance, an RGBA layer image is stored in 16 bytes, four bytes per channel */
TYPE_64U /**< each pixel part information is stored in height unsigned integer bytes. For instance, an RGBA layer image is stored in 32 bytes, eight bytes per channel */
};//NOTE :: openvx handles differently "descriptors" or "pyramids" and images : they do not represent the same thing in openvx
// openvx image handles only integer types !!!
enum ImageLayout {
LAYOUT_RGB=0, /**< means 3 channels per pixel : Red, Green, Blue */
LAYOUT_BGR, /**< means 3 channels per pixel : Green,Red, Blue (this is opencv model)*/
LAYOUT_GREY, /**< means 1 channel per pixel : grey color*/
LAYOUT_RGBA, /**< means 4 channels per pixel : Red, Green, Blue and Alpha channel*/
LAYOUT_RGBX, /**< means 4 channels per pixel : Red, Green, Blue and transparency */
LAYOUT_UNDEFINED
};
enum ColorSpace {
SPACE_RGB_709=0, /**< means ColorSpace is RGB 709 */
SPACE_GREY, /**< means ColorSpace is GREY */
SPACE_UNDEFINED
};
// channel info ?
// black and white
// BitsPerComponent<dimension f(nb channels)>
enum PixelOrder {
INTERLEAVED=0, /**< means channels are interleaved. For instance for LAYOUT RGBA, pixels are stored RGBARGBARGBA and so on... */
PER_CHANNEL /**< means data buffer holds separately each image channel. For instance for an RGBA layout image, pixels are stored gathered by layer : RRRR....GGGG....BBBB....AAAA.... */
};
// Encoding types available for image serialization
enum ImageEncoding {
ENCODING_NONE=0,
ENCODING_JPEG,
ENCODING_PNG
};
Image() = default;
/** @brief Image
* @param pixLayout: defined by ImageLayout
* @param pixOrder: defined if the data are stored interleaved RGB,RGB or as a planar representation RRR,GGG,BBB
* @param type: defined by DataType
*/
Image(enum ImageLayout pixLayout, enum PixelOrder pixOrder, DataType type);
/** @brief Image
* @param width: width of the image
* @param height: height of the image
* @param pixLayout: defined by ImageLayout
* @param pixOrder: defined if the data are stored interleaved RGB,RGB or as a planar representation RRR,GGG,BBB
* @param type: defined by DataType
*/
Image(uint32_t width, uint32_t height, enum ImageLayout pixLayout, enum PixelOrder pixOrder, DataType type);
/** @brief Image built from a raw data pointer
* @param imageData: pointer to the raw data
* @param width: width of the image
* @param height: height of the image
* @param pixLayout: defined by ImageLayout
* @param pixOrder: defined if the data are stored interleaved RGB,RGB or as a planar representation RRR,GGG,BBB
* @param type: defined by DataType
* @param encoding: image encoding (PNG, JPG or NONE by default)
*/
Image(void* imageData, uint32_t width, uint32_t height, enum ImageLayout pixLayout, enum PixelOrder pixOrder,
DataType type, ImageEncoding encoding = ENCODING_NONE);
/** @brief ~Image
*/
~Image() = default;
/** @brief copy the current Image
* @retval a shared_ptr to the copy of the current Image
*/
SRef<Image> copy() const;
/** @brief reserves new space depending on the image layers and bitspercomponent infos
* @param width: width of the image
* @param height: height of the image
*/
void setSize(uint32_t width, uint32_t height);
/** @brief reserves new space depending on the image layers and bitspercomponent infos
* @param Size: size of the image
*/
void setSize(Sizei size);
/** @brief get bytes size of underlying storage
*/
uint32_t getBufferSize();
//never use this accessor to delete the underlying data !
// use to wrap internal buffer data in other framework image wrapper for instance cv::Mat
/** @brief never use this accessor to delete the underlying data !
*/
void* data();
/** @brief never use this accessor to delete the underlying data !
*/
const void* data() const;
/** @brief extracts a subregion for tiling for interleaved data representation only
* @param region: defines the region to extract as a rectangle
* @param channel: assumes planar representation of the image
*/
SRef<Image> extractRegion(Rectanglei region); // to handle in a splitter/extractor component, must not be handled in the image itself
/** @brief extracts a subregion for tiling for a single plane inside a multiplanar image
* @param region: defines the regoion to extract as a rectangle
* @param channel: assumes planar representation of the image
*/
SRef<Image> extractRegion(Rectanglei region, uint32_t channel);
/** @brief returns the image layout
*/
inline enum ImageLayout getImageLayout() const { return m_layout; }
/** @brief returns the pixel order
*/
inline enum PixelOrder getPixelOrder() const { return m_pixOrder; }
/** @brief returns the dataType
*/
inline enum DataType getDataType() const { return m_type; }
/** @brief returns the number of channels
*/
inline uint32_t getNbChannels() const { return m_nbChannels; }
/** @brief returns the amount of bit per component
*/
inline uint32_t getNbBitsPerComponent() const { return m_nbBitsPerComponent; }
/** @brief returns the size of the image
*/
inline Sizei getSize() const { return m_size; };
/** @brief returns height of the image
*/
inline uint32_t getWidth() const { return m_size.width; }
/** @brief returns height of the image
*/
inline uint32_t getHeight() const { return m_size.height; }
inline uint32_t getStep() const { return m_size.width * m_nbChannels * (m_nbBitsPerComponent/8); }
/** @brief set encoding for the image
*/
void setImageEncoding(enum ImageEncoding encoding);
/** @brief returns encoding of the image
*/
inline enum ImageEncoding getImageEncoding() const { return m_imageEncoding; }
/** @brief set encoding quality for the image
* Must be set between 100 and 0: 100 for loseless compression, 0 to low quality and high compression rate
*/
void setImageEncodingQuality(uint8_t encodingQuality);
/** @brief returns encoding quality of the image
*/
inline uint8_t getImageEncodingQuality() const { return m_imageEncodingQuality; }
/// @brief Get pixel value.
/// @param[in] row row index.
/// @param[in] col column index.
/// @return the pixel value
template<typename T> T& getPixel(int row, int col);
/// @brief Get pixel value.
/// @param[in] row row index.
/// @param[in] col column index.
/// @return the pixel value
template<typename T> const T& getPixel(int row, int col) const;
/// @brief Save the image in a file.
/// @param[in] imagePath path to the file with suffix .jpg, .jpeg or .png.
/// @return _SUCCESS if the image is saved, _ERROR_ otherwise.
FrameworkReturnCode save(std::string imagePath) const;
/// @brief Load an image from a file.
/// @param[in] imagePath path to the file with suffix .jpg, .jpeg or .png.
/// @return _SUCCESS if the image is saved, _ERROR_ otherwise.
FrameworkReturnCode load(std::string imagePath);
/// @brief Rotate image 90 degrees
/// @return _SUCCESS if the image is rotated, _ERROR_ otherwise.
FrameworkReturnCode rotate90();
/// @brief Rotate image 180 degrees
/// @return _SUCCESS if the image is rotated, _ERROR_ otherwise.
FrameworkReturnCode rotate180();
/// @brief Rotate image 270 degrees
/// @return _SUCCESS if the image is rotated, _ERROR_ otherwise.
FrameworkReturnCode rotate270();
private:
friend class boost::serialization::access;
protected:
/*
template<typename Archive>
void serialize(Archive &ar, const unsigned int version);
*/
#ifndef SWIG
template<class Archive>
void save(Archive & ar, const unsigned int version) const;
template<class Archive>
void load(Archive & ar, const unsigned int version) ;
BOOST_SERIALIZATION_SPLIT_MEMBER()
#endif
private:
SRef<ImageInternal> m_internalImpl;
uint32_t computeImageBufferSize();
// Rotate degrees
enum class RotateQuantity {
DEGREE_90,
DEGREE_180,
DEGREE_270
};
FrameworkReturnCode rotate(RotateQuantity);
Sizei m_size;
enum ImageLayout m_layout;
enum PixelOrder m_pixOrder;
enum DataType m_type;
uint32_t m_nbChannels;
uint32_t m_nbPlanes;
uint32_t m_nbBitsPerComponent;
enum ImageEncoding m_imageEncoding = ENCODING_NONE;
uint8_t m_imageEncodingQuality = 70;
};
DECLARESERIALIZE(Image);
template<typename T>
inline T & Image::getPixel(int row, int col)
{
assert((sizeof(T) == m_nbChannels * (m_nbBitsPerComponent / 8)) && "type not allowed to get pixel value");
return ((T*)((uint8_t*)m_internalImpl->data() + (row * m_size.width + col) * m_nbChannels * (m_nbBitsPerComponent / 8)))[0];
}
template<typename T>
inline const T & Image::getPixel(int row, int col) const
{
assert((sizeof(T) == m_nbChannels * (m_nbBitsPerComponent / 8)) && "type not allowed to get pixel value");
return ((const T*)((uint8_t*)m_internalImpl->data() + (row * m_size.width + col) * m_nbChannels * (m_nbBitsPerComponent / 8)))[0];
}
//image creation from opencv conversion ... : howto handle memory allocation locality : factory ?
// conversion from/to opencv for instance : how to handle the T* type while bound to void* ?
}
}
BOOST_CLASS_TYPE_INFO(
SolAR::datastructure::ImageInternal,
boost::serialization::extended_type_info_typeid<SolAR::datastructure::ImageInternal>
)
BOOST_CLASS_TYPE_INFO(
SolAR::datastructure::Image,
boost::serialization::extended_type_info_typeid<SolAR::datastructure::Image>
)
BOOST_CLASS_EXPORT_KEY(SolAR::datastructure::Image);
BOOST_CLASS_EXPORT_KEY(SolAR::datastructure::ImageInternal);
#endif