2016-12-20 20:48:31 -08:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-08-25 17:48:06 -07:00
|
|
|
/* Copyright (c) 2016-2017 FIRST. All Rights Reserved. */
|
2016-12-20 20:48:31 -08: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_IMAGE_H_
|
|
|
|
|
#define CSCORE_IMAGE_H_
|
2016-12-20 20:48:31 -08:00
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2017-08-25 17:48:06 -07:00
|
|
|
#include <llvm/StringRef.h>
|
|
|
|
|
#include <opencv2/core/core.hpp>
|
2016-12-20 20:48:31 -08:00
|
|
|
|
|
|
|
|
#include "cscore_cpp.h"
|
|
|
|
|
#include "default_init_allocator.h"
|
|
|
|
|
|
|
|
|
|
namespace cs {
|
|
|
|
|
|
|
|
|
|
class Frame;
|
|
|
|
|
|
|
|
|
|
class Image {
|
|
|
|
|
friend class Frame;
|
|
|
|
|
|
|
|
|
|
public:
|
2017-08-14 22:27:07 -07:00
|
|
|
#ifndef __linux__
|
2017-08-25 17:48:06 -07:00
|
|
|
explicit Image(size_t capacity) { m_data.reserve(capacity); }
|
2017-01-02 23:16:35 -08:00
|
|
|
#else
|
2017-08-25 17:48:06 -07:00
|
|
|
explicit Image(size_t capacity)
|
2017-01-02 23:16:35 -08:00
|
|
|
: m_data{capacity, default_init_allocator<uchar>{}} {
|
2016-12-20 20:48:31 -08:00
|
|
|
m_data.resize(0);
|
|
|
|
|
}
|
2017-01-02 23:16:35 -08:00
|
|
|
#endif
|
|
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
Image(const Image&) = delete;
|
|
|
|
|
Image& operator=(const Image&) = delete;
|
|
|
|
|
|
|
|
|
|
// Getters
|
|
|
|
|
operator llvm::StringRef() const { return str(); }
|
|
|
|
|
llvm::StringRef str() const { return llvm::StringRef(data(), size()); }
|
2017-08-25 17:48:06 -07:00
|
|
|
size_t capacity() const { return m_data.capacity(); }
|
2016-12-20 20:48:31 -08:00
|
|
|
const char* data() const {
|
|
|
|
|
return reinterpret_cast<const char*>(m_data.data());
|
|
|
|
|
}
|
|
|
|
|
char* data() { return reinterpret_cast<char*>(m_data.data()); }
|
2017-08-25 17:48:06 -07:00
|
|
|
size_t size() const { return m_data.size(); }
|
2016-12-20 20:48:31 -08:00
|
|
|
|
|
|
|
|
const std::vector<uchar>& vec() const { return m_data; }
|
|
|
|
|
std::vector<uchar>& vec() { return m_data; }
|
|
|
|
|
|
2017-08-25 17:48:06 -07:00
|
|
|
void resize(size_t size) { m_data.resize(size); }
|
|
|
|
|
void SetSize(size_t size) { m_data.resize(size); }
|
2016-12-20 20:48:31 -08:00
|
|
|
|
|
|
|
|
cv::Mat AsMat() {
|
|
|
|
|
int type;
|
|
|
|
|
switch (pixelFormat) {
|
|
|
|
|
case VideoMode::kYUYV:
|
|
|
|
|
case VideoMode::kRGB565:
|
|
|
|
|
type = CV_8UC2;
|
|
|
|
|
break;
|
|
|
|
|
case VideoMode::kBGR:
|
|
|
|
|
type = CV_8UC3;
|
|
|
|
|
break;
|
2016-12-23 21:01:21 -08:00
|
|
|
case VideoMode::kGray:
|
2016-12-20 20:48:31 -08:00
|
|
|
case VideoMode::kMJPEG:
|
|
|
|
|
default:
|
|
|
|
|
type = CV_8UC1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return cv::Mat{height, width, type, m_data.data()};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cv::_InputArray AsInputArray() { return cv::_InputArray{m_data}; }
|
|
|
|
|
|
|
|
|
|
bool Is(int width_, int height_) {
|
|
|
|
|
return width == width_ && height == height_;
|
|
|
|
|
}
|
|
|
|
|
bool Is(int width_, int height_, VideoMode::PixelFormat pixelFormat_) {
|
|
|
|
|
return width == width_ && height == height_ && pixelFormat == pixelFormat_;
|
|
|
|
|
}
|
|
|
|
|
bool IsLarger(int width_, int height_) {
|
|
|
|
|
return width >= width_ && height >= height_;
|
|
|
|
|
}
|
|
|
|
|
bool IsLarger(const Image& oth) {
|
|
|
|
|
return width >= oth.width && height >= oth.height;
|
|
|
|
|
}
|
|
|
|
|
bool IsSmaller(int width_, int height_) { return !IsLarger(width_, height_); }
|
|
|
|
|
bool IsSmaller(const Image& oth) { return !IsLarger(oth); }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::vector<uchar> m_data;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
VideoMode::PixelFormat pixelFormat{VideoMode::kUnknown};
|
|
|
|
|
int width{0};
|
|
|
|
|
int height{0};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace cs
|
|
|
|
|
|
2017-08-25 17:48:06 -07:00
|
|
|
#endif // CSCORE_IMAGE_H_
|