Turi Create  4.0
image_type.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 #ifndef TURI_IMAGE_IMAGE_TYPE_HPP
7 #define TURI_IMAGE_IMAGE_TYPE_HPP
8 
9 #include <string>
10 #include <core/storage/serialization/serialization_includes.hpp>
11 #include <boost/shared_ptr.hpp>
12 #include <boost/gil/typedefs.hpp>
13 
14 const char IMAGE_TYPE_CURRENT_VERSION = 0;
15 
16 namespace turi {
17 
18 /**
19  * \ingroup group_gl_flexible_type
20  * Possible image formats stored in the image type
21  */
22 enum class Format: size_t {
23  JPG = 0, ///< JPEG Compressed
24  PNG = 1, ///< PNG Compressed
25  RAW_ARRAY = 2, ///< Not Compressed
26  UNDEFINED = 3 ///< Unknown
27 };
28 
29 /**
30  * \ingroup group_gl_flexible_type
31  * Image type, which is typedef'd to flex_image, part of the flexible_type union.
32  * Holds image data and meta-data pertaining to image size and file type, but
33  * does not hold meta-data like path or category.
34  */
35 class image_type {
36 
37 public:
38  /// The image data, stored in the format indicated by m_format in a char array
39  boost::shared_ptr<char[]> m_image_data;
40  /// The height of the image
41  size_t m_height = 0;
42  /// The width of the image
43  size_t m_width = 0;
44  /// The number of channels in the image: Grayscale = 1, RGB = 3, RGBA = 4.
45  size_t m_channels = 0;
46  /// Length of m_image_data char array
47  size_t m_image_data_size = 0;
48  /// Version of image_type object
49  char m_version = IMAGE_TYPE_CURRENT_VERSION;
50  /// Format of data, intitialized as UNDEFINED
52  /// Constructor
53  image_type() = default;
54  /// Construct from existing data
55  image_type(const char* image_data, size_t height, size_t width,
56  size_t channels, size_t image_data_size, int version, int format);
57  /// Construct from a Boost GIL rgb8 Image
58  explicit image_type(const boost::gil::rgb8_image_t& gil_image);
59  /// Construct from a Boost GIL rgba8 Image
60  explicit image_type(const boost::gil::rgba8_image_t& gil_image);
61  /// Check whether image is decoded
62  inline bool is_decoded() const { return m_format == Format::RAW_ARRAY; }
63  /// Serialization
64  void save(oarchive& oarc) const;
65  /// Deserialization
66  void load(iarchive& iarc);
67  /// Returns a char* pointer to the raw image data
68  const unsigned char* get_image_data() const;
69  /// Equality comparator, two images are equal when every property is identical
70  bool operator==(const image_type& other) const;
71 };
72 
73 
74 
75 
76 } // end of namespace turi
77 
78 #endif
Not Compressed.
The serialization input archive object which, provided with a reference to an istream, will read from the istream, providing deserialization capabilities.
Definition: iarchive.hpp:60
boost::shared_ptr< char[]> m_image_data
The image data, stored in the format indicated by m_format in a char array.
Definition: image_type.hpp:39
bool is_decoded() const
Check whether image is decoded.
Definition: image_type.hpp:62
PNG Compressed.
The serialization output archive object which, provided with a reference to an ostream, will write to the ostream, providing serialization capabilities.
Definition: oarchive.hpp:80
JPEG Compressed.