00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef IMAGE_DATA_HPP
00026 #define IMAGE_DATA_HPP
00027
00028 #include <mapnik/octree.hpp>
00029 #include <cassert>
00030 #include <cstring>
00031
00032
00033 #ifdef __SUNPRO_CC
00034 using std::memcpy;
00035 using std::memset;
00036 #endif
00037
00038 namespace mapnik
00039 {
00040 template <class T> class ImageData
00041 {
00042 public:
00043 typedef T pixel_type;
00044
00045 ImageData(unsigned width,unsigned height)
00046 : width_(width),
00047 height_(height),
00048 pData_((width!=0 && height!=0)? static_cast<T*>(::operator new(sizeof(T)*width*height)):0)
00049 {
00050 if (pData_) memset(pData_,0,sizeof(T)*width_*height_);
00051 }
00052
00053 ImageData(const ImageData<T>& rhs)
00054 :width_(rhs.width_),
00055 height_(rhs.height_),
00056 pData_((rhs.width_!=0 && rhs.height_!=0)?
00057 static_cast<T*>(::operator new(sizeof(T)*rhs.width_*rhs.height_)) :0)
00058 {
00059 if (pData_) memcpy(pData_,rhs.pData_,sizeof(T)*rhs.width_* rhs.height_);
00060 }
00061 inline T& operator() (unsigned i,unsigned j)
00062 {
00063 assert(i<width_ && j<height_);
00064 return pData_[j*width_+i];
00065 }
00066 inline const T& operator() (unsigned i,unsigned j) const
00067 {
00068 assert(i<width_ && j<height_);
00069 return pData_[j*width_+i];
00070 }
00071 inline unsigned width() const
00072 {
00073 return width_;
00074 }
00075 inline unsigned height() const
00076 {
00077 return height_;
00078 }
00079 inline void set(const T& t)
00080 {
00081 for (unsigned y = 0; y < height_; ++y)
00082 {
00083 T * row = getRow(y);
00084 for (unsigned x = 0; x < width_; ++x)
00085 {
00086 row[x] = t;
00087 }
00088 }
00089 }
00090
00091 inline const T* getData() const
00092 {
00093 return pData_;
00094 }
00095
00096 inline T* getData()
00097 {
00098 return pData_;
00099 }
00100
00101 inline const unsigned char* getBytes() const
00102 {
00103 return (unsigned char*)pData_;
00104 }
00105
00106 inline unsigned char* getBytes()
00107 {
00108 return (unsigned char*)pData_;
00109 }
00110
00111 inline const T* getRow(unsigned row) const
00112 {
00113 return pData_+row*width_;
00114 }
00115
00116 inline T* getRow(unsigned row)
00117 {
00118 return pData_+row*width_;
00119 }
00120
00121 inline void setRow(unsigned row,const T* buf,unsigned size)
00122 {
00123 assert(row<height_);
00124 assert(size<=(width_*sizeof(T)));
00125 memcpy(pData_+row*width_,buf,size*sizeof(T));
00126 }
00127 inline void setRow(unsigned row,unsigned x0,unsigned x1,const T* buf)
00128 {
00129 memcpy(pData_+row*width_+x0,buf,(x1-x0)*sizeof(T));
00130 }
00131
00132 inline ~ImageData()
00133 {
00134 ::operator delete(pData_),pData_=0;
00135 }
00136
00137 private:
00138 const unsigned width_;
00139 const unsigned height_;
00140 T *pData_;
00141 ImageData& operator=(const ImageData&);
00142 };
00143
00144 typedef ImageData<unsigned> ImageData32;
00145 typedef ImageData<uint8_t> ImageData8;
00146 }
00147
00148 #endif //IMAGE_DATA_HPP