WPILibC++  unspecified
Frame.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2016-2018 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_FRAME_H_
9 #define CSCORE_FRAME_H_
10 
11 #include <atomic>
12 #include <memory>
13 #include <string>
14 #include <utility>
15 #include <vector>
16 
17 #include <wpi/SmallVector.h>
18 #include <wpi/mutex.h>
19 
20 #include "Image.h"
21 #include "cscore_cpp.h"
22 
23 namespace cs {
24 
25 class SourceImpl;
26 
27 class Frame {
28  friend class SourceImpl;
29 
30  public:
31  typedef uint64_t Time;
32 
33  private:
34  struct Impl {
35  explicit Impl(SourceImpl& source_) : source(source_) {}
36 
37  wpi::recursive_mutex mutex;
38  std::atomic_int refcount{0};
39  Time time{0};
40  SourceImpl& source;
41  std::string error;
43  std::vector<int> compressionParams;
44  };
45 
46  public:
47  Frame() noexcept : m_impl{nullptr} {}
48 
49  Frame(SourceImpl& source, wpi::StringRef error, Time time);
50 
51  Frame(SourceImpl& source, std::unique_ptr<Image> image, Time time);
52 
53  Frame(const Frame& frame) noexcept : m_impl{frame.m_impl} {
54  if (m_impl) ++m_impl->refcount;
55  }
56 
57  Frame(Frame&& other) noexcept : Frame() { swap(*this, other); }
58 
59  ~Frame() { DecRef(); }
60 
61  Frame& operator=(Frame other) noexcept {
62  swap(*this, other);
63  return *this;
64  }
65 
66  explicit operator bool() const { return m_impl && m_impl->error.empty(); }
67 
68  friend void swap(Frame& first, Frame& second) noexcept {
69  using std::swap;
70  swap(first.m_impl, second.m_impl);
71  }
72 
73  Time GetTime() const { return m_impl ? m_impl->time : 0; }
74 
75  wpi::StringRef GetError() const {
76  if (!m_impl) return wpi::StringRef{};
77  return m_impl->error;
78  }
79 
80  int GetOriginalWidth() const {
81  if (!m_impl) return 0;
82  std::lock_guard<wpi::recursive_mutex> lock(m_impl->mutex);
83  if (m_impl->images.empty()) return 0;
84  return m_impl->images[0]->width;
85  }
86 
87  int GetOriginalHeight() const {
88  if (!m_impl) return 0;
89  std::lock_guard<wpi::recursive_mutex> lock(m_impl->mutex);
90  if (m_impl->images.empty()) return 0;
91  return m_impl->images[0]->height;
92  }
93 
94  int GetOriginalPixelFormat() const {
95  if (!m_impl) return 0;
96  std::lock_guard<wpi::recursive_mutex> lock(m_impl->mutex);
97  if (m_impl->images.empty()) return 0;
98  return m_impl->images[0]->pixelFormat;
99  }
100 
101  Image* GetExistingImage(size_t i = 0) const {
102  if (!m_impl) return nullptr;
103  std::lock_guard<wpi::recursive_mutex> lock(m_impl->mutex);
104  if (i >= m_impl->images.size()) return nullptr;
105  return m_impl->images[i];
106  }
107 
108  Image* GetExistingImage(int width, int height) const {
109  if (!m_impl) return nullptr;
110  std::lock_guard<wpi::recursive_mutex> lock(m_impl->mutex);
111  for (auto i : m_impl->images) {
112  if (i->Is(width, height)) return i;
113  }
114  return nullptr;
115  }
116 
117  Image* GetExistingImage(int width, int height,
118  VideoMode::PixelFormat pixelFormat) const {
119  if (!m_impl) return nullptr;
120  std::lock_guard<wpi::recursive_mutex> lock(m_impl->mutex);
121  for (auto i : m_impl->images) {
122  if (i->Is(width, height, pixelFormat)) return i;
123  }
124  return nullptr;
125  }
126 
127  Image* GetNearestImage(int width, int height) const;
128  Image* GetNearestImage(int width, int height,
129  VideoMode::PixelFormat pixelFormat) const;
130 
131  Image* Convert(Image* image, VideoMode::PixelFormat pixelFormat,
132  int jpegQuality = 80);
133  Image* ConvertMJPEGToBGR(Image* image);
134  Image* ConvertMJPEGToGray(Image* image);
135  Image* ConvertYUYVToBGR(Image* image);
136  Image* ConvertBGRToRGB565(Image* image);
137  Image* ConvertRGB565ToBGR(Image* image);
138  Image* ConvertBGRToGray(Image* image);
139  Image* ConvertGrayToBGR(Image* image);
140  Image* ConvertBGRToMJPEG(Image* image, int quality);
141  Image* ConvertGrayToMJPEG(Image* image, int quality);
142 
143  Image* GetImage(int width, int height, VideoMode::PixelFormat pixelFormat,
144  int jpegQuality = 80);
145 
146  bool GetCv(cv::Mat& image) {
147  return GetCv(image, GetOriginalWidth(), GetOriginalHeight());
148  }
149  bool GetCv(cv::Mat& image, int width, int height);
150 
151  private:
152  void DecRef() {
153  if (m_impl && --(m_impl->refcount) == 0) ReleaseFrame();
154  }
155  void ReleaseFrame();
156 
157  Impl* m_impl;
158 };
159 
160 } // namespace cs
161 
162 #endif // CSCORE_FRAME_H_
Definition: CvSourceImpl.h:19
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:868
Definition: Image.h:23
Definition: SourceImpl.h:30
Definition: Frame.h:27
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49