WPILibC++  unspecified
Image.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2016-2017 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #ifndef CSCORE_IMAGE_H_
9 #define CSCORE_IMAGE_H_
10 
11 #include <vector>
12 
13 #include <llvm/StringRef.h>
14 #include <opencv2/core/core.hpp>
15 
16 #include "cscore_cpp.h"
17 #include "default_init_allocator.h"
18 
19 namespace cs {
20 
21 class Frame;
22 
23 class Image {
24  friend class Frame;
25 
26  public:
27 #ifndef __linux__
28  explicit Image(size_t capacity) { m_data.reserve(capacity); }
29 #else
30  explicit Image(size_t capacity)
31  : m_data{capacity, default_init_allocator<uchar>{}} {
32  m_data.resize(0);
33  }
34 #endif
35 
36  Image(const Image&) = delete;
37  Image& operator=(const Image&) = delete;
38 
39  // Getters
40  operator llvm::StringRef() const { return str(); }
41  llvm::StringRef str() const { return llvm::StringRef(data(), size()); }
42  size_t capacity() const { return m_data.capacity(); }
43  const char* data() const {
44  return reinterpret_cast<const char*>(m_data.data());
45  }
46  char* data() { return reinterpret_cast<char*>(m_data.data()); }
47  size_t size() const { return m_data.size(); }
48 
49  const std::vector<uchar>& vec() const { return m_data; }
50  std::vector<uchar>& vec() { return m_data; }
51 
52  void resize(size_t size) { m_data.resize(size); }
53  void SetSize(size_t size) { m_data.resize(size); }
54 
55  cv::Mat AsMat() {
56  int type;
57  switch (pixelFormat) {
58  case VideoMode::kYUYV:
59  case VideoMode::kRGB565:
60  type = CV_8UC2;
61  break;
62  case VideoMode::kBGR:
63  type = CV_8UC3;
64  break;
65  case VideoMode::kGray:
66  case VideoMode::kMJPEG:
67  default:
68  type = CV_8UC1;
69  break;
70  }
71  return cv::Mat{height, width, type, m_data.data()};
72  }
73 
74  cv::_InputArray AsInputArray() { return cv::_InputArray{m_data}; }
75 
76  bool Is(int width_, int height_) {
77  return width == width_ && height == height_;
78  }
79  bool Is(int width_, int height_, VideoMode::PixelFormat pixelFormat_) {
80  return width == width_ && height == height_ && pixelFormat == pixelFormat_;
81  }
82  bool IsLarger(int width_, int height_) {
83  return width >= width_ && height >= height_;
84  }
85  bool IsLarger(const Image& oth) {
86  return width >= oth.width && height >= oth.height;
87  }
88  bool IsSmaller(int width_, int height_) { return !IsLarger(width_, height_); }
89  bool IsSmaller(const Image& oth) { return !IsLarger(oth); }
90 
91  private:
92  std::vector<uchar> m_data;
93 
94  public:
95  VideoMode::PixelFormat pixelFormat{VideoMode::kUnknown};
96  int width{0};
97  int height{0};
98 };
99 
100 } // namespace cs
101 
102 #endif // CSCORE_IMAGE_H_
Definition: SinkImpl.h:19
Definition: Image.h:23
Definition: default_init_allocator.h:16
Definition: Frame.h:27
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:42