iSpike  2.1
Spike conversion library for robotics
D:/Home/Programs/iSpike/src/Bitmap.cpp
Go to the documentation of this file.
00001 #include <iSpike/Bitmap.hpp>
00002 #include <iSpike/ISpikeException.hpp>
00003 
00004 using namespace ispike;
00005 
00007 Bitmap::Bitmap() :      width(0), height(0), depth(0), contents(NULL){
00008         ;
00009 }
00010 
00011 
00013 Bitmap::Bitmap(unsigned width, unsigned height, unsigned depth) : width(width), height(height), depth(depth){
00014         contents = new unsigned char[width * height * depth];
00015 }
00016 
00017 
00019 Bitmap::Bitmap(unsigned width, unsigned height, unsigned depth, unsigned char initVal) : width(width), height(height), depth(depth){
00020         contents = new unsigned char[width * height * depth];
00021         int tmpSize = size();//Avoid repeated calls to size
00022         for(int i=0; i<tmpSize; ++i)
00023                 contents[i] = initVal;
00024 }
00025 
00026 
00028 Bitmap::Bitmap (const Bitmap& bmp){
00029         //Clean up old bitmap
00030         if(!isEmpty()){
00031                 delete [] contents;
00032         }
00033 
00034         //Store new values
00035         width = bmp.width;
00036         height = bmp.height;
00037         depth = bmp.depth;
00038 
00039         //Create new bitmap
00040         int tmpSize = bmp.size();//Avoid repeated calls to size
00041         contents = new unsigned char[tmpSize];
00042         unsigned char* bmpContents = bmp.getContents();
00043 
00044         //Copy contents across into this bitmap
00045         for(int i=0; i<tmpSize; ++i) {
00046                 contents[i] = bmpContents[i];
00047         }
00048 }
00049 
00050 
00052 Bitmap::~Bitmap(){
00053         delete contents;
00054 }
00055 
00056 
00057 /*--------------------------------------------------------------------*/
00058 /*---------                 PUBLIC METHODS                     -------*/
00059 /*--------------------------------------------------------------------*/
00060 
00062 Bitmap& Bitmap::operator=(const Bitmap& rhs){
00063         if(this == &rhs) {
00064                 return *this;
00065         }
00066 
00067         //Clean up old bitmap
00068         if(!isEmpty()){
00069                 delete [] contents;
00070         }
00071 
00072         //Store new values
00073         width = rhs.width;
00074         height = rhs.height;
00075         depth = rhs.depth;
00076 
00077         //Create new bitmap
00078         int tmpSize = rhs.size();
00079         contents = new unsigned char[tmpSize];
00080         unsigned char* rhsContents = rhs.getContents();
00081 
00082         //Copy contents across into this bitmap
00083         for(int i=0; i<tmpSize; ++i) {
00084                 contents[i] = rhsContents[i];
00085         }
00086 
00087         return *this;
00088 }
00089 
00090 
00092 unsigned char Bitmap::getPixel(unsigned x, unsigned y, unsigned d) {
00093         if( (x >= 0 && x < width) && (y >= 0 && y < height) && (d >=0 && d < depth)){
00094                 if(depth == 0) {
00095                         return *(contents + (width * y) + x);
00096                 }
00097                 return *(contents + (width * depth * y) + x * depth + d);
00098         } else {
00099                 throw ISpikeException("Invalid pixel address");
00100                 return 0U;
00101         }
00102 }
00103 
00104 
00109 void Bitmap::reset(unsigned width, unsigned height, unsigned depth){
00110         size_t newSize = width * height * depth;
00111         if(size() != newSize) {
00112                 delete [] contents;
00113                 this->width = width;
00114                 this->height = height;
00115                 this->depth = depth;
00116                 contents = new unsigned char[newSize];
00117         }
00118 }
00119 
00120 
00121 unsigned Bitmap::size() const {
00122         return width*height*depth;
00123 }
00124 
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines