Skip to content

Instantly share code, notes, and snippets.

@prodeveloper0
Created February 2, 2017 05:31
Show Gist options
  • Select an option

  • Save prodeveloper0/45a893ef46bb1d1846397f5a8c72d90b to your computer and use it in GitHub Desktop.

Select an option

Save prodeveloper0/45a893ef46bb1d1846397f5a8c72d90b to your computer and use it in GitHub Desktop.
Image Container for C++
#include <iostream>
#include <vector>
#include <assert.h>
using namespace std;
typedef unsigned short uattr;
typedef unsigned char uchar;
typedef uchar Vec3b[3];
typedef uchar Vec4b[4];
typedef float Vec3f[3];
typedef float Vec4f[4];
#define UNITATTR(id, size) ((uattr)((id << 8) | size))
#define UNITATTR_ID(v) ((uattr)(v >> 8))
#define UNITATTR_SIZE(v) ((uattr)(v & 0x00ff))
enum MATRIX_UNIT
{
MATRIX_8UC1 = UNITATTR(0, 1),
MATRIX_8UC2 = UNITATTR(1, 2),
MATRIX_8UC3 = UNITATTR(2, 3),
MATRIX_8UC4 = UNITATTR(3, 4),
MATRIX_32FC1 = UNITATTR(4, 4),
MATRIX_32FC2 = UNITATTR(5, 8),
MATRIX_32FC3 = UNITATTR(6, 12),
MATRIX_32FC4 = UNITATTR(7, 16)
};
class Point
{
public:
int x, y;
public:
Point(int x = 0, int y = 0)
: x(x), y(y) {}
};
class Size
{
public:
int width, height;
public:
Size(int width = 0, int height = 0)
: width(width), height(height) {}
};
class Rect
{
public:
int x, y, width, height;
public:
Rect(int x = 0, int y = 0, int width = 0, int height = 0)
: x(x), y(y), width(width), height(height) {}
public:
Size inline size()
{
return Size(std::abs(width - x), std::abs(height - y));
}
};
class Matrix
{
private:
uchar *_Ptr;
Rect _OriginRect;
Rect _MyRect;
uattr _UnitType;
bool _Owner;
public:
int rows;
int cols;
public:
Matrix()
{
_Ptr = NULL;
_Owner = false;
_UnitType = 0;
_OriginRect = Rect();
_MyRect = Rect();
rows = 0;
cols = 0;
}
Matrix(Matrix &other)
{
_Ptr = other._Ptr;
_Owner = true;
other._Owner = false;
_UnitType = other._UnitType;
_OriginRect = other._OriginRect;
_MyRect = other._MyRect;
rows = other.rows;
cols = other.cols;
}
Matrix& operator=(Matrix &other)
{
_release();
_Ptr = other._Ptr;
_OriginRect = other._OriginRect;
_MyRect = other._MyRect;
_UnitType = other._UnitType;
_Owner = true;
other._Owner = false;
rows = other.rows;
cols = other.cols;
return *this;
}
Matrix operator()(const Rect &rect)
{
Matrix ret = *this;
ret._Owner = false;
ret._MyRect = rect;
return ret;
}
virtual ~Matrix()
{
_release();
}
private:
void _release()
{
if (_Owner && _Ptr)
free(_Ptr);
_Ptr = NULL;
_Owner = true;
_UnitType = 0;
_OriginRect = Rect();
_MyRect = Rect();
rows = 0;
cols = 0;
}
public:
void create(const Size &size, uattr type)
{
_release();
_Ptr = (uchar*)malloc(size.width * size.height * UNITATTR_SIZE(type));
assert(_Ptr != NULL);
_MyRect = _OriginRect = Rect(0, 0, size.width, size.height);
_UnitType = type;
rows = size.height;
cols = size.width;
}
void copyTo(Matrix &other)
{
other.create(_MyRect.size(), _UnitType);
if (_MyRect.x == 0 && _MyRect.y == 0)
{
memcpy(other._Ptr, _Ptr, _MyRect.height * _MyRect.width * UNITATTR_SIZE(_UnitType));
return;
}
for (int row = 0; row < other._MyRect.height; ++row)
memcpy(other._Ptr + (UNITATTR_SIZE(_UnitType) * other._MyRect.width * row), _Ptr + (((_OriginRect.width * (row + _MyRect.y)) + _MyRect.x) * UNITATTR_SIZE(_UnitType)), (UNITATTR_SIZE(_UnitType) * other._MyRect.width));
}
public:
template<typename E>
E& at(int x, int y)
{
return *((E*)(&(_Ptr[(_OriginRect.width * (y + _MyRect.y)) + (x + _MyRect.x)])));
}
template<typename E>
E& at(const Point &point)
{
return at(point.x, point.y);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment