Commit 25bac91f authored by ahoms's avatar ahoms
Browse files

* added Roi utility class


git-svn-id: https://scm.blissgarden.org/svn/lima/trunk@14 45c4679d-1946-429d-baad-37912b19538b
parent 6a1f8377
......@@ -106,10 +106,10 @@ inline bool operator !=(const Point& p1, const Point& p2)
class Size
{
public:
Size() : m_xy() {}
Size(int w, int h) : m_xy(w, h) {}
Size(const Point& p) : m_xy(p) {}
Size(const Size& s) : m_xy(s.m_xy) {}
Size() : m_xy() {}
Size(int w, int h) : m_xy(checkValid(Point(w, h))) {}
Size(const Point& p) : m_xy(checkValid(p)) {}
Size(const Size& s) : m_xy(s.m_xy) {}
int getWidth() const
{ return m_xy.x; }
......@@ -124,40 +124,54 @@ class Size
{ return m_xy; }
Size& operator =(const Point& p)
{ m_xy = p; return *this; }
{ m_xy = checkValid(p); return *this; }
Size& operator +=(const Point& p)
{ m_xy += p; return *this; }
{ m_xy = checkValid(m_xy + p); return *this; }
Size& operator -=(const Point& p)
{ m_xy -= p; return *this; }
{ m_xy = checkValid(m_xy - p); return *this; }
Size& operator *=(const Point& p)
{ m_xy *= p; return *this; }
{ m_xy = checkValid(m_xy * p); return *this; }
Size& operator /=(const Point& p)
{ m_xy /= p; return *this; }
{ m_xy = checkValid(m_xy / p); return *this; }
private:
static bool isValid(int i);
static Point checkValid(const Point& p);
Point m_xy;
};
inline bool Size::isValid(int i)
{
return (i >= 0);
}
inline Point Size::checkValid(const Point& p)
{
if (!(isValid(p.x) && isValid(p.y)))
throw LIMA_COM_EXC(InvalidValue, "Invalid size");
return p;
}
/*******************************************************************
* \class Size
* \brief Basic rectangle size class
* \class Bin
* \brief Class holding a horizontal (X) and vertical (Y) binning
*
* This class helps managing the size of rectangular objects
* Basic binning handling. The values must be power of 2
*******************************************************************/
class Bin
{
public:
Bin() : m_xy(1) {}
Bin(int x, int y) : m_xy(checkValid(Point(x, y))) {}
Bin(const Point& p) : m_xy(checkValid(p)) {}
Bin(const Bin& b) : m_xy(b.m_xy) {}
Bin() : m_xy(1) {}
Bin(int x, int y) : m_xy(checkValid(Point(x, y))) {}
Bin(const Point& p) : m_xy(checkValid(p)) {}
Bin(const Bin& b) : m_xy(b.m_xy) {}
int getX() const
{ return m_xy.x; }
......@@ -197,6 +211,147 @@ inline Point Bin::checkValid(const Point& p)
}
/*******************************************************************
* \class Roi
* \brief Class specifying a rectangular region-of-interest
*
* The roi is represented in terms of origin (top_left) + size.
* The bottom_right corner \b belongs to the roi: top_left + size - 1
*******************************************************************/
class Roi
{
public:
Roi();
Roi(const Point& top_left, const Size& size)
: m_top_left(checkCorner(top_left)),
m_size(size) {}
Roi(const Point& top_left, const Point& bottom_right)
: m_top_left(checkCorner(top_left)),
m_size(bottom_right + 1 - top_left) {}
Roi(const Roi& r)
: m_top_left(r.m_top_left),
m_size(r.m_size) {}
Roi& operator =(const Roi& r);
const Point& getTopLeft() const;
const Size& getSize() const;
Point getTopRight() const;
Point getBottomLeft() const;
Point getBottomRight() const;
void setTopLeft(const Point& top_left);
void setSize(const Size& size);
void setCorners(const Point& p1, const Point& p2);
bool containsPoint(const Point& p) const;
bool containsRoi(const Roi& r) const;
Roi getBinned(const Bin& b) const;
Roi getUnbinned(const Bin& b) const;
private:
static bool isValidCoord(int i);
static Point checkCorner(const Point& TopLeft);
Point m_top_left;
Size m_size;
};
inline Roi& Roi::operator =(const Roi& r)
{
m_top_left = r.m_top_left;
m_size = r.m_size;
return *this;
}
inline const Point& Roi::getTopLeft() const
{
return m_top_left;
}
inline Point Roi::getTopRight() const
{
return m_top_left + Point(m_size.getWidth() - 1, 0);
}
inline Point Roi::getBottomLeft() const
{
return m_top_left + Point(0, m_size.getHeight() - 1);
}
inline Point Roi::getBottomRight() const
{
return m_top_left + m_size - 1;
}
inline const Size& Roi::getSize() const
{
return m_size;
}
inline void Roi::setTopLeft(const Point& top_left)
{
m_top_left = checkCorner(top_left);
}
inline void Roi::setSize(const Size& size)
{
m_size = size;
}
inline bool Roi::isValidCoord(int i)
{
return (i >= 0);
}
inline Point Roi::checkCorner(const Point& p)
{
if (!(isValidCoord(p.x) && isValidCoord(p.y)))
throw LIMA_COM_EXC(InvalidValue, "Invalid corner coords");
return p;
}
inline bool Roi::containsPoint(const Point& p) const
{
return p.contains(getTopLeft()) && getBottomRight().contains(p);
}
inline bool Roi::containsRoi(const Roi& r) const
{
return (containsPoint(r.getTopLeft()) &&
containsPoint(r.getBottomRight()));
}
inline Roi Roi::getBinned(const Bin& b) const
{
return Roi(m_top_left / b, Size(m_size / b));
}
inline Roi Roi::getUnbinned(const Bin& b) const
{
return Roi(m_top_left * b, Size(m_size * b));
}
inline bool operator ==(const Roi& r1, const Roi& r2)
{
return ((r1.getTopLeft() == r2.getTopLeft()) &&
(r1.getSize() == r2.getSize()));
}
inline bool operator !=(const Roi& r1, const Roi& r2)
{
return !(r1 == r2);
}
/*******************************************************************
* \class FrameDim
* \brief Class holding the Size and ImageType of a frame
......@@ -227,6 +382,20 @@ class FrameDim
int m_depth;
};
inline FrameDim::FrameDim(const Size& size, ImageType type)
{
m_size = size;
m_type = type;
m_depth = getImageTypeDepth(type);
}
inline FrameDim::FrameDim(int width, int height, ImageType type)
{
m_size = Size(width, height);
m_type = type;
m_depth = getImageTypeDepth(type);
}
inline const Size& FrameDim::getSize() const
{
return m_size;
......
......@@ -2,6 +2,10 @@
using namespace lima;
using std::min;
using std::max;
/*******************************************************************
* \brief Aligns the Point to the nearest boundary of another Point
*
......@@ -20,19 +24,21 @@ void Point::alignTo(const Point& p, AlignDir align_dir)
}
FrameDim::FrameDim(const Size& size, ImageType type)
{
m_size = size;
m_type = type;
m_depth = getImageTypeDepth(type);
}
/*******************************************************************
* \brief Set the roi by giving to arbitrary (diagonal) corners
*
* This method works by finding the top-left and bottom-right corners
*******************************************************************/
FrameDim::FrameDim(int width, int height, ImageType type)
void Roi::setCorners(const Point& p1, const Point& p2)
{
m_size = Size(width, height);
m_type = type;
m_depth = getImageTypeDepth(type);
int x1 = min(p1.x, p2.x);
int x2 = max(p1.x, p2.x);
int y1 = min(p1.y, p2.y);
int y2 = max(p1.y, p2.y);
m_top_left = checkCorner(Point(x1, y1));
Point bottom_right = Point(x2, y2);
m_size = bottom_right + 1 - m_top_left;
}
int FrameDim::getImageTypeBpp(ImageType type)
......@@ -44,7 +50,8 @@ int FrameDim::getImageTypeBpp(ImageType type)
case Bpp14: return 14;
case Bpp16: return 16;
case Bpp32: return 32;
default: return 0;
default:
throw LIMA_COM_EXC(InvalidValue, "Invalid image type");
}
}
......@@ -61,6 +68,6 @@ int FrameDim::getImageTypeDepth(ImageType type)
case Bpp32:
return 4;
default:
return 0;
throw LIMA_COM_EXC(InvalidValue, "Invalid image type");
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment