//ImageBuffer.cpp
#include "ImageBuffer.h"
#ifdef _IMAGEBUFFER_H
ImageBuffer::ImageBuffer(unsigned __int32 w,unsigned __int32 h,unsigned __int32 d, bool a)/*: buffer(0), size (-1), width (w), height(h), depth(d),maxColor(1 << d), alpha(a)*/{
buffer = 0;
size = -1;
width = w;
height = h;
depth = d;
maxColor = (1 << d);
alpha = a;
buffer = new (nothrow) RGBA [width * height]; //0 -- (w*h)-1
if (buffer == 0){
cout << "Could not allocate memory" << endl;
cin.ignore();
return;
}
size = width * height;
RGBA *tempAdd = buffer;
for (int i = 0; i < size; i++){
tempAdd->Set(0,0,0,0);
tempAdd++;
}
return;
}
ImageBuffer::~ImageBuffer(){
if (buffer != 0){
delete [] buffer;
buffer = 0;
}
return;
}
RGBA& ImageBuffer::operator[] (unsigned __int32 index){
return buffer [index];
}
RGBA& ImageBuffer::operator[] (Point2D p2D){
return buffer [p2D.ArrGet(width)];
}
void ImageBuffer::SetSize (unsigned __int32 w ,unsigned __int32 h){
if ((w * h != size) || width != w || height != h){
RGBA * tempBuffer = new (nothrow) RGBA [w * h];
if (tempBuffer == 0){
cout << "could not allocate memory" << endl;
cin.ignore();
return;
}
RGBA * tempBufferAdd = tempBuffer;
for (int i = 0; i < (w * h); i++){
tempBufferAdd->Set(0,0,0,0);
tempBufferAdd++;
}
RGBA * bufferAdd = buffer;
for (__int32 x = 1; x <= width; x++){
for (__int32 y = 1; y <= height; y++){
if (x <= w && x > 0 && y <= h && y > 0){
Point2D temp = Point2D((float)x,(float)y);
tempBuffer[temp.ArrGet(w)] = *bufferAdd;
}
bufferAdd++;
}
}
if (buffer != 0){
delete [] buffer;
}
buffer = tempBuffer;
}
width = w;
height = h;
size = width * height;
return;
}
void ImageBuffer::SetColorDepth(unsigned __int32 d){
depth = d;
unsigned __int32 tempMax = maxColor;
maxColor = (1 << d);
if (maxColor != tempMax){
RGBA * tempAdd = buffer;
for (int i = 0; i < size; i++){
tempAdd ->clr[0] = (unsigned __int16)((float)tempAdd->clr[0] * ((float)maxColor / (float)tempMax));
tempAdd ->clr[1] = (unsigned __int16)((float)tempAdd->clr[1] * ((float)maxColor / (float)tempMax));
tempAdd ->clr[2] = (unsigned __int16)((float)tempAdd->clr[2] * ((float)maxColor / (float)tempMax));
tempAdd ->alpha = (unsigned __int16) ((float)tempAdd->alpha * ((float)maxColor / (float)tempMax));
tempAdd++;
}
}
return;
}
void ImageBuffer::SetAlpha (bool a){
alpha = a;
if (!a){
RGBA *tempAdd = buffer;
for (int i = 0; i < size; i++){
tempAdd ->alpha = 0;
tempAdd++;
}
}
return;
}
void ImageBuffer::Clear(RGBA clr){
RGBA * tempAdd = buffer;
for (int i = 0; i < size; i ++){
*tempAdd = clr;
tempAdd++;
}
return;
}
void ImageBuffer::Invert (){
RGBA * tempAdd = buffer;
for (int i = 0; i < size; i++){
tempAdd->Set ((maxColor-1) - tempAdd->clr[0],(maxColor-1) - tempAdd->clr[1],(maxColor-1) - tempAdd->clr[2],tempAdd->alpha);
tempAdd++;
}
return;
}
void ImageBuffer::DrawDot (Point2D p2D,RGBA clr){
if (p2D.xPos < width && p2D.xPos > 0 && p2D.yPos < height && p2D.yPos > 0){
buffer[p2D.ArrGet(width)] = clr;
}
return;
}
void ImageBuffer::DrawOval (Point2D p2D,float radiusX,float radiusY,RGBA clr){
float perimeter = PI * (float) (radiusX * radiusY);
float angleInc = 360 / perimeter;
for (int i = 0; i < perimeter; i++){
DrawDot (Point2D(p2D.xPos + (radiusX * cos (((angleInc * (float) i)* PI) / 180) ), p2D.yPos + (radiusY * sin (((angleInc * (float) i)* PI) / 180) )),clr);
}
return;
}
void ImageBuffer::DrawArc (Point2D p2D,float radiusX,float radiusY,float initAngle,float finalAngle,RGBA clr){
float perimeter =((finalAngle - initAngle) / 360) * PI * (float) (radiusX * radiusY);
float angleInc = (finalAngle - initAngle) / perimeter;
for (int i = 0; i < perimeter; i++){
DrawDot (Point2D(p2D.xPos + (radiusX * cos (((initAngle + angleInc * (float) i) * PI) / 180) ), p2D.yPos + (radiusY * sin (((initAngle + angleInc * (float) i)*PI)/180) )),clr);
}
return;
}
unsigned __int32 ImageBuffer::Width (){
return width;
}
unsigned __int32 ImageBuffer::Height (){
return height;
}
#endif
|