GED 2006 (C++)
File detail
Source code
/*
* File: framebuffer.cc - Bitmap memory representation
* Project: GED - bitmap editor (ICP)
* Author: Kamil Dudka, xdudka00
* Team: xdudka00, xfilak01, xhefka00, xhradi08
* Created: 2006-03-10
*/
#include "global.h"
#include "framebuffer.h"
using GlobalH::min;
using GlobalH::max;
/*
* set fb size to "fbSize" without initialization
*/
void FrameBuffer::size (const BoxSize &fbSize) {
// Delete old fb, create a new one
delete[] _pBuff;
_pBuff = new Pixel [fbSize.nPixels ()];
_size = fbSize;
this->unSelect ();
}
/*
* copy constructor for fb
*/
FrameBuffer::FrameBuffer (const FrameBuffer &fb):
_size (fb._size), _select (fb._select)
{
// Allocate new fb
const int nPixels = _size.nPixels ();
_pBuff = new Pixel [nPixels];
// Copy bitmap
for (int i=0; i<nPixels; i++)
_pBuff [i] = fb._pBuff [i];
}
/*
* fb assignment
*/
FrameBuffer &FrameBuffer::operator= (const FrameBuffer &fb)
{
this->size (fb.size ());
this->select (fb.select ());
// Allocate new fb
const int nPixels = _size.nPixels ();
_pBuff = new Pixel [nPixels];
// Copy bitmap
for (int i=0; i<nPixels; i++)
_pBuff [i] = fb._pBuff [i];
return *this;
}
/*
* give pointer to fb
*/
Pixel *FrameBuffer::pBuff () {
if (0== _pBuff)
// fb not allocated yet
throw ErrUnalloc ();
return _pBuff;
}
/*
* Set pixel "pos" to color "color"
* if pixel is in selected area
*/
void FrameBuffer::setPixel (Point pos, Pixel color)
{
if (0== _pBuff)
// fb not allocated
throw ErrUnalloc ();
if (
pos.x < _select.at.x ||
pos.y < _select.at.y ||
pos.x >= _select.at.x + _select.size.width ||
pos.y >= _select.at.y + _select.size.height)
return;
_pBuff [pos.y*_size.width + pos.x] = color;
}
/*
* resize fb to size "newSsize"
*/
void FrameBuffer::resize (const BoxSize &newSize) {
const Pixel blankPixel = { 0xFF, 0xFF, 0xFF, 0xFF };
// Allocate new fb and copy the old one
Pixel *newBuff = new Pixel [newSize.nPixels ()];
for (int y=0; y<newSize.height; y++)
for (int x=0; x<newSize.width; x++) {
Pixel &oldPixel = _pBuff [y*_size.width + x];
Pixel &newPixel = newBuff [y*newSize.width + x];
newPixel = (y<_size.height && x<_size.width) ? oldPixel : blankPixel;
}
// Set image size to new size
_size = newSize;
this->select (_select);
// Replace old fb
delete[] _pBuff;
_pBuff = newBuff;
}
/*
* set select area to "newSelect"
*/
void FrameBuffer::select (const Rect &newSelect) {
// Check "at" range
_select.at.x = max (0, min (_size.width-1, newSelect.at.x));
_select.at.y = max (0, min (_size.height-1, newSelect.at.y));
// Check "size" range
_select.size.width = max (0, min (_size.width - _select.at.x, newSelect.size.width));
_select.size.height = max (0, min (_size.height - _select.at.y, newSelect.size.height));
}
/*
* select whole fb
*/
void FrameBuffer::unSelect () {
_select.at = Point (0, 0);
_select.size = _size;
}