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 <cassert>
00029
00030 namespace mapnik
00031 {
00032 template <class T> class ImageData
00033 {
00034 public:
00035 typedef T pixel_type;
00036
00037 ImageData(unsigned width,unsigned height)
00038 : width_(width),
00039 height_(height),
00040 pData_((width!=0 && height!=0)? static_cast<T*>(::operator new(sizeof(T)*width*height)):0)
00041 {
00042 if (pData_) memset(pData_,0,sizeof(T)*width_*height_);
00043 }
00044
00045 ImageData(const ImageData<T>& rhs)
00046 :width_(rhs.width_),
00047 height_(rhs.height_),
00048 pData_((rhs.width_!=0 && rhs.height_!=0)? new T[rhs.width_*rhs.height_]:0)
00049 {
00050 if (pData_) memcpy(pData_,rhs.pData_,sizeof(T)*rhs.width_* rhs.height_);
00051 }
00052 inline T& operator() (unsigned i,unsigned j)
00053 {
00054 assert(i<width_ && j<height_);
00055 return pData_[j*width_+i];
00056 }
00057 inline const T& operator() (unsigned i,unsigned j) const
00058 {
00059 assert(i<width_ && j<height_);
00060 return pData_[j*width_+i];
00061 }
00062 inline unsigned width() const
00063 {
00064 return width_;
00065 }
00066 inline unsigned height() const
00067 {
00068 return height_;
00069 }
00070 inline void set(const T& t)
00071 {
00072 for (unsigned i=0;i<width_;++i)
00073 {
00074 for (unsigned j=0;j<height_;++j)
00075 {
00076 (*this)(i,j)=t;
00077 }
00078 }
00079 }
00080 inline const T* getData() const
00081 {
00082 return pData_;
00083 }
00084
00085 inline T* getData()
00086 {
00087 return pData_;
00088 }
00089
00090 inline const unsigned char* getBytes() const
00091 {
00092 return (unsigned char*)pData_;
00093 }
00094
00095 inline unsigned char* getBytes()
00096 {
00097 return (unsigned char*)pData_;
00098 }
00099
00100 inline const T* getRow(unsigned row) const
00101 {
00102 return pData_+row*width_;
00103 }
00104 inline void setRow(unsigned row,const T* buf,unsigned size)
00105 {
00106 assert(row<height_);
00107 assert(size<=(width_*sizeof(T)));
00108 memcpy(pData_+row*width_,buf,size*sizeof(T));
00109 }
00110 inline void setRow(unsigned row,unsigned x0,unsigned x1,const T* buf)
00111 {
00112 memcpy(pData_+row*width_+x0,buf,(x1-x0)*sizeof(T));
00113 }
00114
00115 inline ~ImageData()
00116 {
00117 ::operator delete(pData_),pData_=0;
00118 }
00119
00120 private:
00121 const unsigned width_;
00122 const unsigned height_;
00123 T *pData_;
00124 ImageData& operator=(const ImageData&);
00125
00126 };
00127
00128 typedef ImageData<unsigned> ImageData32;
00129 }
00130
00131 #endif //IMAGE_DATA_HPP