GED 2006 (C++)
File detail
Source code
/*
* File: framebuffer.h - Bitmap memory representation
* Project: GED - bitmap editor (ICP)
* Author: Kamil Dudka, xdudka00
* Team: xdudka00, xfilak01, xhefka00, xhradi08
* Created: 2006-03-10
*/
#ifndef FRAMEBUFFER_H
#define FRAMEBUFFER_H
/**
* RGB Pixel structure
*/
struct Pixel {
unsigned char red;
unsigned char green;
unsigned char blue;
unsigned char alpha; ///< Something as visibility. 0x00 means transparent, 0xFF means visible.
};
/**
* 2D ortogonal object size
*/
struct BoxSize {
int width; ///< Object width.
int height; ///< Object height.
/**
* Construct BoxSize with zero size.
*/
BoxSize (): width(0), height(0) { }
/**
* Construct BoxSize with given size.
* \param x Object width.
* \param y Object height.
*/
BoxSize (int x, int y): width(x), height(y) { }
/**
* \return Return object pixels count.
*/
int nPixels () const { return width*height; }
/// BoxSize objects comparation. Same meaning of operator as for built-in types.
bool operator== (const BoxSize &b) const
{
return
width == b.width &&
height == b.height;
}
/// Negated BoxSize objects comparation. Same meaning of operator as for built-in types.
bool operator!= (const BoxSize &b) const { return !operator==(b); }
};
/**
* 2D point position
*/
struct Point {
int x; ///< point position
int y; ///< point position
/**
* Create Point (0, 0).
*/
Point (): x(0), y(0) { }
/**
* Create Point object with given position.
* \param ix Object position.
* \param iy Object position.
*/
Point (int ix, int iy): x(ix), y(iy) { }
/// Point objects comparation. Same meaning of operator as for built-in types.
bool operator== (const Point &p) const { return x==p.x && y==p.y; }
/// Negated Point objects comparation. Same meaning of operator as for built-in types.
bool operator!= (const Point &p) const { return x!=p.x || y!=p.y; }
/// Add an another Point object. Same meaning of operator as for built-in types.
Point &operator+= (const Point &p)
{
x += p.x;
y += p.y;
return *this;
}
/// Subtract an another Point object. Same meaning of operator as for built-in types.
Point &operator-= (const Point &p)
{
x -= p.x;
y -= p.y;
return *this;
}
};
/// Add two Point objects together. Same meaning of operator as for built-in types.
inline Point operator+ (const Point &a, const Point &b) {
Point tmp (a);
return tmp+=b;
}
/// Substract two Point objects. Same meaning of operator as for built-in types.
inline Point operator- (const Point &a, const Point &b) {
Point tmp (a);
return tmp-=b;
}
/**
* 2D box object description
*/
struct Rect {
Point at; ///< Object position.
BoxSize size; ///< Object size.
/**
* Create Rect object with position (0, 0) and zero size.
*/
Rect () { }
/**
* Create Rect object with given size and position.
* \param p Object position.
* \param s Object size.
*/
Rect (Point p, BoxSize s): at(p), size(s) { }
/// Rect objects comparation. Same meaning of operator as for built-in types.
bool operator== (const Rect &r) const
{
return
at == r.at &&
size == r.size;
}
/// Negated Rect objects comparation. Same meaning of operator as for built-in types.
bool operator!= (const Rect &r) const { return !operator==(r); }
};
/**
* Frame buffer. This is base class for image data representation.
* This class holds information about image size and selected area.
* It also allocates array of Pixel objects for specified image size.
*/
class FrameBuffer {
public:
/**
* Create frame buffer with no size. This is used in derived classes.
* You can set frame buffer size later using FrameBuffer::size() or FrameBuffer::resize() method.
*/
FrameBuffer (): _pBuff(0) { }
/**
* Create frame buffer of desired size. Whole image is set as selected area.
* \param fbSize Desired size of frame buffer.
*/
FrameBuffer (const BoxSize &fbSize): _pBuff(0) { size (fbSize); }
/**
* Frame buffer copy constructor.
*/
FrameBuffer (const FrameBuffer &fb);
/**
* Frame buffer assignment.
*/
FrameBuffer &operator= (const FrameBuffer &fb);
/**
* Free memory allocated for array of Pixel objects.
*/
~FrameBuffer () { delete[] _pBuff; }
/**
* Access point to array of Pixel objects.
* This can throw FrameBuffer::ErrUnalloc exception if there is no array.
* It happens usualy if frame buffer has zero size.
* \return Return pointer to one-dimensional array of Pixel objects.
*/
Pixel *pBuff ();
/**
* This method sets pixel "pos" to color "color". It has no effect for pixels outside selected area.
* This can throw FrameBuffer::ErrUnalloc exception if there is no array.
* It happens usualy if frame buffer has zero size.
* \param pos Pixel position.
* \param color Desired color.
*/
void setPixel (Point pos, Pixel color);
/**
* Access point to row of array of Pixel objects.
* This can throw FrameBuffer::ErrUnalloc exception if there is no array.
* It happens usualy if frame buffer has zero size.
* \return Return Pointer to array of Pixel objects representing image row.
* \param y Desired row. This must be in interval from 0 to FrameBuffer::size().width-1.
*/
Pixel *operator[] (int y)
{
return pBuff() + y*_size.width;
}
/**
* \return Return current frame buffer size.
*/
const BoxSize &size () const { return _size; }
/**
* Change size of frame buffer. Current data are simply coppied.
* \param newSize New size of frame buffer.
*/
void resize (const BoxSize &newSize);
/**
* Change size of frame buffer.
* Current data are dropped and array stays uninitialized.
* \param fbSize New size of frame buffer.
*/
void size (const BoxSize &fbSize);
/**
* \return Return selected area.
*/
const Rect &select () const { return _select; }
/**
* Set select area. If desired size is greater, it is limited to frame buffer size.
* \param selectRect New select area.
*/
void select (const Rect &selectRect);
/**
* Select whole image. This means selection will have no effect for operations with frame buffer.
*/
void unSelect ();
/**
* This is an exception thrown while manipulating with zero sized frame buffer.
*/
class ErrUnalloc { };
friend class SharedObject; ///< Needed for IPC.
friend class SharedFrameBuffer; ///< Needed for IPC.
protected:
Pixel *_pBuff; ///< Pointer to array of Pixel objects.
private:
BoxSize _size;
Rect _select;
};
#endif