2016-09-05 12:00:04 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-08-25 17:48:06 -07:00
|
|
|
/* Copyright (c) 2016-2017 FIRST. All Rights Reserved. */
|
2016-09-05 12:00:04 -07:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2017-08-25 17:48:06 -07:00
|
|
|
#ifndef CSCORE_FRAME_H_
|
|
|
|
|
#define CSCORE_FRAME_H_
|
2016-09-05 12:00:04 -07:00
|
|
|
|
|
|
|
|
#include <atomic>
|
2016-10-13 00:16:24 -07:00
|
|
|
#include <memory>
|
2016-12-20 20:48:31 -08:00
|
|
|
#include <mutex>
|
2017-08-25 17:48:06 -07:00
|
|
|
#include <string>
|
|
|
|
|
#include <utility>
|
|
|
|
|
#include <vector>
|
2016-09-05 12:00:04 -07:00
|
|
|
|
2017-08-25 17:48:06 -07:00
|
|
|
#include <llvm/SmallVector.h>
|
2016-09-05 12:00:04 -07:00
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
#include "Image.h"
|
2017-08-25 17:48:06 -07:00
|
|
|
#include "cscore_cpp.h"
|
2016-09-05 12:00:04 -07:00
|
|
|
|
|
|
|
|
namespace cs {
|
|
|
|
|
|
|
|
|
|
class SourceImpl;
|
|
|
|
|
|
|
|
|
|
class Frame {
|
|
|
|
|
friend class SourceImpl;
|
|
|
|
|
|
2016-10-13 00:16:24 -07:00
|
|
|
public:
|
2016-10-22 22:09:47 -07:00
|
|
|
typedef uint64_t Time;
|
2016-10-13 00:16:24 -07:00
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
private:
|
|
|
|
|
struct Impl {
|
2017-08-25 17:48:06 -07:00
|
|
|
explicit Impl(SourceImpl& source_) : source(source_) {}
|
2016-10-13 00:16:24 -07:00
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
std::recursive_mutex mutex;
|
2016-09-05 12:00:04 -07:00
|
|
|
std::atomic_int refcount{0};
|
2016-12-20 20:48:31 -08:00
|
|
|
Time time{0};
|
|
|
|
|
SourceImpl& source;
|
|
|
|
|
std::string error;
|
|
|
|
|
llvm::SmallVector<Image*, 4> images;
|
|
|
|
|
std::vector<int> compressionParams;
|
2016-09-05 12:00:04 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public:
|
2016-12-20 20:48:31 -08:00
|
|
|
Frame() noexcept : m_impl{nullptr} {}
|
2016-09-16 18:46:36 -07:00
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
Frame(SourceImpl& source, llvm::StringRef error, Time time);
|
|
|
|
|
|
|
|
|
|
Frame(SourceImpl& source, std::unique_ptr<Image> image, Time time);
|
2016-09-05 12:00:04 -07:00
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
Frame(const Frame& frame) noexcept : m_impl{frame.m_impl} {
|
|
|
|
|
if (m_impl) ++m_impl->refcount;
|
2016-09-05 12:00:04 -07:00
|
|
|
}
|
|
|
|
|
|
2016-09-16 18:46:36 -07:00
|
|
|
Frame(Frame&& other) noexcept : Frame() { swap(*this, other); }
|
2016-09-05 12:00:04 -07:00
|
|
|
|
|
|
|
|
~Frame() { DecRef(); }
|
|
|
|
|
|
2016-09-16 18:46:36 -07:00
|
|
|
Frame& operator=(Frame other) noexcept {
|
|
|
|
|
swap(*this, other);
|
2016-09-05 12:00:04 -07:00
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
explicit operator bool() const { return m_impl && m_impl->error.empty(); }
|
2016-09-05 12:00:04 -07:00
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
friend void swap(Frame& first, Frame& second) noexcept {
|
|
|
|
|
using std::swap;
|
|
|
|
|
swap(first.m_impl, second.m_impl);
|
2016-10-13 00:16:24 -07:00
|
|
|
}
|
|
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
Time GetTime() const { return m_impl ? m_impl->time : 0; }
|
|
|
|
|
|
|
|
|
|
llvm::StringRef GetError() const {
|
|
|
|
|
if (!m_impl) return llvm::StringRef{};
|
|
|
|
|
return m_impl->error;
|
2016-09-05 12:00:04 -07:00
|
|
|
}
|
|
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
int GetOriginalWidth() const {
|
|
|
|
|
if (!m_impl) return 0;
|
|
|
|
|
std::lock_guard<std::recursive_mutex> lock(m_impl->mutex);
|
|
|
|
|
if (m_impl->images.empty()) return 0;
|
|
|
|
|
return m_impl->images[0]->width;
|
2016-10-13 00:16:24 -07:00
|
|
|
}
|
|
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
int GetOriginalHeight() const {
|
|
|
|
|
if (!m_impl) return 0;
|
|
|
|
|
std::lock_guard<std::recursive_mutex> lock(m_impl->mutex);
|
|
|
|
|
if (m_impl->images.empty()) return 0;
|
|
|
|
|
return m_impl->images[0]->height;
|
2016-11-10 00:00:20 -08:00
|
|
|
}
|
|
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
int GetOriginalPixelFormat() const {
|
|
|
|
|
if (!m_impl) return 0;
|
|
|
|
|
std::lock_guard<std::recursive_mutex> lock(m_impl->mutex);
|
|
|
|
|
if (m_impl->images.empty()) return 0;
|
|
|
|
|
return m_impl->images[0]->pixelFormat;
|
2016-09-05 12:00:04 -07:00
|
|
|
}
|
|
|
|
|
|
2017-08-25 17:48:06 -07:00
|
|
|
Image* GetExistingImage(size_t i = 0) const {
|
2016-12-20 20:48:31 -08:00
|
|
|
if (!m_impl) return nullptr;
|
|
|
|
|
std::lock_guard<std::recursive_mutex> lock(m_impl->mutex);
|
|
|
|
|
if (i >= m_impl->images.size()) return nullptr;
|
|
|
|
|
return m_impl->images[i];
|
2016-11-10 00:00:20 -08:00
|
|
|
}
|
|
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
Image* GetExistingImage(int width, int height) const {
|
|
|
|
|
if (!m_impl) return nullptr;
|
|
|
|
|
std::lock_guard<std::recursive_mutex> lock(m_impl->mutex);
|
|
|
|
|
for (auto i : m_impl->images) {
|
|
|
|
|
if (i->Is(width, height)) return i;
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
2016-11-10 00:00:20 -08:00
|
|
|
}
|
|
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
Image* GetExistingImage(int width, int height,
|
|
|
|
|
VideoMode::PixelFormat pixelFormat) const {
|
|
|
|
|
if (!m_impl) return nullptr;
|
|
|
|
|
std::lock_guard<std::recursive_mutex> lock(m_impl->mutex);
|
|
|
|
|
for (auto i : m_impl->images) {
|
|
|
|
|
if (i->Is(width, height, pixelFormat)) return i;
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
2016-09-05 12:00:04 -07:00
|
|
|
}
|
|
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
Image* GetNearestImage(int width, int height) const;
|
|
|
|
|
Image* GetNearestImage(int width, int height,
|
|
|
|
|
VideoMode::PixelFormat pixelFormat) const;
|
|
|
|
|
|
|
|
|
|
Image* Convert(Image* image, VideoMode::PixelFormat pixelFormat,
|
|
|
|
|
int jpegQuality = 80);
|
|
|
|
|
Image* ConvertMJPEGToBGR(Image* image);
|
2016-12-23 21:01:21 -08:00
|
|
|
Image* ConvertMJPEGToGray(Image* image);
|
2016-12-20 20:48:31 -08:00
|
|
|
Image* ConvertYUYVToBGR(Image* image);
|
|
|
|
|
Image* ConvertBGRToRGB565(Image* image);
|
|
|
|
|
Image* ConvertRGB565ToBGR(Image* image);
|
2016-12-23 21:01:21 -08:00
|
|
|
Image* ConvertBGRToGray(Image* image);
|
|
|
|
|
Image* ConvertGrayToBGR(Image* image);
|
2016-12-20 20:48:31 -08:00
|
|
|
Image* ConvertBGRToMJPEG(Image* image, int quality);
|
2016-12-23 21:01:21 -08:00
|
|
|
Image* ConvertGrayToMJPEG(Image* image, int quality);
|
2016-12-20 20:48:31 -08:00
|
|
|
|
|
|
|
|
Image* GetImage(int width, int height, VideoMode::PixelFormat pixelFormat,
|
|
|
|
|
int jpegQuality = 80);
|
|
|
|
|
|
|
|
|
|
bool GetCv(cv::Mat& image) {
|
|
|
|
|
return GetCv(image, GetOriginalWidth(), GetOriginalHeight());
|
2016-09-16 18:46:36 -07:00
|
|
|
}
|
2016-12-20 20:48:31 -08:00
|
|
|
bool GetCv(cv::Mat& image, int width, int height);
|
2016-09-16 18:46:36 -07:00
|
|
|
|
2016-09-05 12:00:04 -07:00
|
|
|
private:
|
|
|
|
|
void DecRef() {
|
2016-12-20 20:48:31 -08:00
|
|
|
if (m_impl && --(m_impl->refcount) == 0) ReleaseFrame();
|
2016-09-05 12:00:04 -07:00
|
|
|
}
|
|
|
|
|
void ReleaseFrame();
|
|
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
Impl* m_impl;
|
2016-09-05 12:00:04 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace cs
|
|
|
|
|
|
2017-08-25 17:48:06 -07:00
|
|
|
#endif // CSCORE_FRAME_H_
|