Files
allwpilib/cscore/src/main/native/cpp/Frame.h

257 lines
6.7 KiB
C
Raw Normal View History

// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
2016-09-05 12:00:04 -07:00
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>
#include <memory>
2017-08-25 17:48:06 -07:00
#include <string>
#include <string_view>
2017-08-25 17:48:06 -07:00
#include <utility>
#include <vector>
2016-09-05 12:00:04 -07:00
#include <wpi/SmallVector.h>
#include <wpi/mutex.h>
2016-09-05 12:00:04 -07: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;
2024-02-12 23:42:17 -08:00
std::unique_ptr<Image> CreateImageFromBGRA(cs::SourceImpl* source, size_t width,
size_t height, size_t stride,
const uint8_t* data);
2016-09-05 12:00:04 -07:00
class Frame {
friend class SourceImpl;
public:
using Time = uint64_t;
private:
struct Impl {
2017-08-25 17:48:06 -07:00
explicit Impl(SourceImpl& source_) : source(source_) {}
wpi::recursive_mutex mutex;
2016-09-05 12:00:04 -07:00
std::atomic_int refcount{0};
Time time{0};
WPI_TimestampSource timeSource{WPI_TIMESRC_UNKNOWN};
SourceImpl& source;
std::string error;
wpi::SmallVector<Image*, 4> images;
std::vector<int> compressionParams;
2016-09-05 12:00:04 -07:00
};
public:
Frame() noexcept = default;
Frame(SourceImpl& source, std::string_view error, Time time,
WPI_TimestampSource timeSrc);
Frame(SourceImpl& source, std::unique_ptr<Image> image, Time time,
WPI_TimestampSource timeSrc);
2016-09-05 12:00:04 -07: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
}
Frame(Frame&& other) noexcept : Frame() { swap(*this, other); }
2016-09-05 12:00:04 -07:00
~Frame() { DecRef(); }
Frame& operator=(Frame other) noexcept {
swap(*this, other);
2016-09-05 12:00:04 -07:00
return *this;
}
explicit operator bool() const { return m_impl && m_impl->error.empty(); }
2016-09-05 12:00:04 -07:00
friend void swap(Frame& first, Frame& second) noexcept {
using std::swap;
swap(first.m_impl, second.m_impl);
}
Time GetTime() const { return m_impl ? m_impl->time : 0; }
WPI_TimestampSource GetTimeSource() const {
return m_impl ? m_impl->timeSource : WPI_TIMESRC_UNKNOWN;
}
std::string_view GetError() const {
if (!m_impl) {
return {};
}
return m_impl->error;
2016-09-05 12:00:04 -07:00
}
int GetOriginalWidth() const {
if (!m_impl) {
return 0;
}
std::scoped_lock lock(m_impl->mutex);
if (m_impl->images.empty()) {
return 0;
}
return m_impl->images[0]->width;
}
int GetOriginalHeight() const {
if (!m_impl) {
return 0;
}
std::scoped_lock lock(m_impl->mutex);
if (m_impl->images.empty()) {
return 0;
}
return m_impl->images[0]->height;
}
int GetOriginalPixelFormat() const {
if (!m_impl) {
return 0;
}
std::scoped_lock 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
}
int GetOriginalJpegQuality() const {
if (!m_impl) {
return 0;
}
std::scoped_lock lock(m_impl->mutex);
if (m_impl->images.empty()) {
return 0;
}
return m_impl->images[0]->jpegQuality;
}
2017-08-25 17:48:06 -07:00
Image* GetExistingImage(size_t i = 0) const {
if (!m_impl) {
return nullptr;
}
std::scoped_lock lock(m_impl->mutex);
if (i >= m_impl->images.size()) {
return nullptr;
}
return m_impl->images[i];
}
Image* GetExistingImage(int width, int height) const {
if (!m_impl) {
return nullptr;
}
std::scoped_lock lock(m_impl->mutex);
for (auto i : m_impl->images) {
if (i->Is(width, height)) {
return i;
}
}
return nullptr;
}
Image* GetExistingImage(int width, int height,
VideoMode::PixelFormat pixelFormat) const {
if (!m_impl) {
return nullptr;
}
std::scoped_lock 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
}
Image* GetExistingImage(int width, int height,
VideoMode::PixelFormat pixelFormat,
int jpegQuality) const {
if (!m_impl) {
return nullptr;
}
std::scoped_lock lock(m_impl->mutex);
for (auto i : m_impl->images) {
if (i->Is(width, height, pixelFormat, jpegQuality)) {
return i;
}
}
return nullptr;
}
Image* GetNearestImage(int width, int height) const;
Image* GetNearestImage(int width, int height,
VideoMode::PixelFormat pixelFormat,
int jpegQuality = -1) const;
Image* Convert(Image* image, VideoMode::PixelFormat pixelFormat) {
if (pixelFormat == VideoMode::kMJPEG) {
return nullptr;
}
return ConvertImpl(image, pixelFormat, -1, 80);
}
Image* ConvertToMJPEG(Image* image, int requiredQuality,
int defaultQuality = 80) {
return ConvertImpl(image, VideoMode::kMJPEG, requiredQuality,
defaultQuality);
}
Image* ConvertMJPEGToBGR(Image* image);
Image* ConvertMJPEGToGray(Image* image);
Image* ConvertYUYVToBGR(Image* image);
Image* ConvertYUYVToGray(Image* image);
Image* ConvertUYVYToBGR(Image* image);
Image* ConvertUYVYToGray(Image* image);
Image* ConvertBGRToRGB565(Image* image);
Image* ConvertRGB565ToBGR(Image* image);
Image* ConvertBGRToGray(Image* image);
Image* ConvertGrayToBGR(Image* image);
Image* ConvertBGRToMJPEG(Image* image, int quality);
Image* ConvertGrayToMJPEG(Image* image, int quality);
2022-11-24 09:06:06 -08:00
Image* ConvertGrayToY16(Image* image);
Image* ConvertY16ToGray(Image* image);
2024-02-12 23:42:17 -08:00
Image* ConvertBGRToBGRA(Image* image);
Image* GetImage(int width, int height, VideoMode::PixelFormat pixelFormat) {
if (pixelFormat == VideoMode::kMJPEG) {
return nullptr;
}
return GetImageImpl(width, height, pixelFormat, -1, 80);
}
Image* GetImageMJPEG(int width, int height, int requiredQuality,
int defaultQuality = 80) {
return GetImageImpl(width, height, VideoMode::kMJPEG, requiredQuality,
defaultQuality);
}
bool GetCv(cv::Mat& image, VideoMode::PixelFormat pixelFormat) {
return GetCv(image, GetOriginalWidth(), GetOriginalHeight(), pixelFormat);
}
bool GetCv(cv::Mat& image, int width, int height,
VideoMode::PixelFormat pixelFormat);
2016-09-05 12:00:04 -07:00
private:
Image* ConvertImpl(Image* image, VideoMode::PixelFormat pixelFormat,
int requiredJpegQuality, int defaultJpegQuality);
Image* GetImageImpl(int width, int height, VideoMode::PixelFormat pixelFormat,
int requiredJpegQuality, int defaultJpegQuality);
2016-09-05 12:00:04 -07:00
void DecRef() {
if (m_impl && --(m_impl->refcount) == 0) {
ReleaseFrame();
}
2016-09-05 12:00:04 -07:00
}
void ReleaseFrame();
Impl* m_impl{nullptr};
2016-09-05 12:00:04 -07:00
};
} // namespace cs
2017-08-25 17:48:06 -07:00
#endif // CSCORE_FRAME_H_