RAPP Platform API
 All Classes Namespaces Files Functions Variables Typedefs
picture.hpp
Go to the documentation of this file.
1 #ifndef RAPP_OBJECT_PICTURE
2 #define RAPP_OBJECT_PICTURE
3 #include "includes.ihh"
4 namespace rapp {
5 namespace object {
13 class picture
14 {
15 public:
16 
18  picture(const std::string filepath)
19  {
20  std::ifstream bytestream(filepath,
21  std::ios::in | std::ios::binary | std::ios::ate);
22  if (!bytestream.is_open())
23  throw std::runtime_error("could not open bytestream for "+filepath);
24  else
25  opencb_(bytestream);
26  }
27 
29  picture(std::ifstream & bytestream)
30  {
31  opencb_(bytestream);
32  }
33 
35  picture(const picture &) = default;
36 
38  picture & operator=(const picture &) = default;
39 
41  bool operator==(const picture & rhs) const
42  {
43  return (this->bytearray_ == rhs.bytearray_);
44  }
45 
47  std::vector<rapp::types::byte> bytearray() const
48  {
49  return bytearray_;
50  }
51 
53  std::string type() const
54  {
55  return imgtype_;
56  }
57 
59  bool save(const std::string filepath)
60  {
61  std::ofstream os(filepath, std::ios::out | std::ofstream::binary);
62  if (os.is_open())
63  {
64  std::copy(bytearray_.begin(), bytearray_.end(),
65  std::ostreambuf_iterator<rapp::types::byte>(os));
66  os.close();
67  return true;
68  }
69  else
70  return false;
71  }
72 
73 private:
74  typedef char byte;
75 
76  // Delete empty constructor
77  picture() = delete;
78 
79  // Parse the bytestream into the bytearray
80  void opencb_(std::ifstream & bytestream)
81  {
82  // copy byte by byte
83  bytestream.seekg(0, std::ios_base::end);
84  std::streampos fileSize = bytestream.tellg();
85  bytearray_.resize(fileSize);
86  bytestream.seekg(0, std::ios_base::beg);
87  bytestream.read(&bytearray_[0], fileSize);
88  // Check Magic Number to find picture format
89  if ((unsigned int)bytearray_[0] == 0xFFFFFF89
90  && (unsigned int)bytearray_[1] == 0x00000050)
91  imgtype_ = "png";
92  if ((unsigned int)bytearray_[0] == 0xFFFFFFFF
93  && (unsigned int)bytearray_[1] == 0xFFFFFFD8)
94  imgtype_ = "jpg";
95  }
96 
97  std::vector<rapp::types::byte> bytearray_;
98  std::string imgtype_;
99 };
100 }
101 }
102 #endif
class which wraps around raw bytes of a picture
Definition: picture.hpp:13
bool operator==(const picture &rhs) const
picture equality
Definition: picture.hpp:41
picture(std::ifstream &bytestream)
Construct using an open file stream.
Definition: picture.hpp:29
std::string imgtype_
Definition: picture.hpp:98
picture & operator=(const picture &)=default
Assignment operator.
std::vector< rapp::types::byte > bytearray() const
Get picture as array of bytes.
Definition: picture.hpp:47
void opencb_(std::ifstream &bytestream)
Definition: picture.hpp:80
std::string type() const
try and get image type (JPG/PNG supported)
Definition: picture.hpp:53
picture(const std::string filepath)
Construct from a file-path.
Definition: picture.hpp:18
bool save(const std::string filepath)
Save picture to filepath.
Definition: picture.hpp:59
std::vector< rapp::types::byte > bytearray_
Definition: picture.hpp:97