Main Page | Namespace List | Class List | File List | Namespace Members | Class Members | File Members

gilImageBase.h

Go to the documentation of this file.
00001 //
00002 // Generic Image Library
00003 // Yung-Yu Chaung, 2003
00004 //
00005 
00006 #ifndef __GIL_IMAGE_BASE_H__
00007 #define __GIL_IMAGE_BASE_H__
00008 
00009 #include <map>
00010 #include <string>
00011 
00012 using std::map;
00013 using std::string;
00014 
00015 namespace Gil{
00016 
00022 template <typename Type> class PixelPtr {
00023     private:
00024         Type* pixel;
00025         int channel;
00026     public:
00027         inline PixelPtr(void){
00028                 pixel = NULL;
00029                 channel = c;
00030         }
00031         inline PixelPtr(Type* p, int c){
00032             pixel = p;
00033             channel = c;
00034         }
00035         inline PixelPtr(const PixelPtr<Type>& ptr){
00036             pixel = ptr.pixel;
00037             channel = ptr.channel;
00038         }
00039 
00040         inline bool operator ==(const PixelPtr<Type>& ptr){
00041             return pixel == ptr.pixel;
00042         }
00043 
00044         inline bool operator !=(const PixelPtr<Type>& ptr){
00045             return pixel != ptr.pixel;
00046         }
00047 
00052         inline PixelPtr<Type> operator =(const PixelPtr<Type>& ptr){
00053             pixel = ptr.pixel;
00054             channel = ptr.channel;
00055             return *this;
00056         }
00057 
00061         inline Type& operator [](int i){
00062             return pixel[i];
00063         }
00064 
00065         inline PixelPtr<Type>& operator ++(void){ // ++ptr
00066             pixel += channel;
00067             return *this;
00068         }
00069 
00070         inline PixelPtr<Type> operator ++(int){ // ptr++
00071             PixelPtr<Type> tmp(pixel, channel);
00072             pixel += channel;
00073             return tmp;
00074         }
00075         
00076         inline PixelPtr<Type>& operator --(void){ // --ptr
00077             pixel -= channel;
00078             return *this;
00079         }
00080 
00081         inline PixelPtr<Type> operator --(int){ // ptr--
00082             PixelPtr<Type> tmp(pixel, channel);
00083             pixel -= channel;
00084             return tmp;
00085         }
00086 
00090         inline void assign(const PixelPtr<Type>& ptr){
00091             for(int i=0; i<channel; i++)
00092                 pixel[i] = ptr.pixel[i];
00093         }
00094 
00095 };
00096     
00097 template <typename Type> class ImageBase {
00098     private:
00099         // member variables
00100         int  m_nWidth;
00101         int  m_nHeight;
00102         int  m_nChannels;
00103         Type *m_pBuffer;
00104         Type **m_pRow;
00105         int  m_nPitch;
00106         int  m_nSaveQuality;
00107 
00108         map<string, string> m_comments;
00109 
00110         // member functions
00111         void init_rowp();
00112 
00113         void read_comments();
00114         void write_comments();
00115 
00116     public:
00117         ImageBase(void);
00118         
00122         ImageBase(int w, int h, int c);
00123         
00127         ImageBase(const ImageBase<Type> &image);
00128         ~ImageBase(void);
00129         
00133         inline int width(void) const{ return m_nWidth;}
00134 
00138         inline int height(void) const { return m_nHeight; }
00139 
00143         inline int channels(void) const { return m_nChannels; }
00144 
00149         inline int elements(void) const { return m_nWidth*m_nHeight*m_nChannels; }
00150 
00155         inline int pitch(void) const { return m_nPitch; }
00156 
00160         inline Type* data(void) { return m_pBuffer; }
00161 
00165         inline Type* rowp(int r){ return m_pRow[r]; }
00166 
00171         void allocate(int w, int h, int c);
00172 
00177         void allocate(const ImageBase<Type> &image){ 
00178             allocate(image.width(), image.height(), image.channels());
00179         }
00180         
00184         void fillPixels(Type value);
00185 
00189         void copyPixelsTo(ImageBase<Type> &dst);
00190 
00194         inline PixelPtr<Type> getPixelPtr(int x, int y){
00195             return PixelPtr<Type>(m_pRow[y]+m_nChannels*x, m_nChannels);
00196         }
00197         
00202         inline PixelPtr<Type> begin(void){
00203             return PixelPtr<Type>(m_pBuffer, m_nChannels);
00204         }
00205         
00215         inline PixelPtr<Type> end(void){
00216             return PixelPtr<Type>(m_pRow[m_nHeight-1]+m_nPitch, m_nChannels);
00217         }
00218 
00219         inline Type& pixel(int x, int y, int channel=0){
00220             return m_pRow[y][m_nChannels*x+channel];
00221         }
00222 
00226         inline bool inBounds(int x, int y){
00227             return x>=0 && x<m_nWidth && y>=0 && y<m_nHeight;
00228         }
00229 
00230         void reverseRows();
00231 
00238         bool readFile(const char* filename);
00239 
00244         bool writeFile(const char* filename);
00245 
00246         ImageBase<Type>* subImage(int x, int y, int w, int h);
00247         void cropImage(int x, int y, int w, int h);
00248 
00249         inline void setSaveQuality(int q) { m_nSaveQuality=q; }
00250 
00251         inline int getNumComments() { return (int)(m_comments.size()); }
00252         void printComments();
00253         void addComment(string key, string text);
00254         string getComment(string key);
00255         void getOriginalSize(int &w, int &h);
00256         void getOriginalPos(int &x, int &y);
00257 };
00258 
00259 // instantiation
00260 template class ImageBase<unsigned char>;
00261 template class ImageBase<float>;
00262 // alias
00263 typedef PixelPtr<unsigned char> BytePixelPtr;
00264 typedef PixelPtr<float> FloatPixelPtr;
00265 typedef ImageBase<unsigned char> ByteImage;
00266 typedef ImageBase<float> FloatImage;
00267 
00268 
00269 } // namespace Gil
00270 
00271 #endif // __IMAGE_BASE_H__

Generated on Tue Mar 8 17:46:18 2005 for libgil by  doxygen 1.4.1