Home Hierarchy Members Alphabetical Related Pages

sfimage.h

Go to the documentation of this file.
00001 #ifndef XDKWRL_SFIMAGE_H
00002 #define XDKWRL_SFIMAGE_H
00003 
00004 #include <xdkwrl/fieldtypes.h>
00005 #include <iostream>
00006 #include <iomanip>
00007 
00008 namespace wrl
00009 {
00010   //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00011   // Interface of  SFImage
00012   //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00013   /*! \ingroup fieldtypes
00014    *
00015    * Represents an image. Below is included the documentation for this field
00016    * type from the ISO standard.   
00017    * \htmlinclude sfimage.html
00018    */
00019   class SFImage 
00020   {
00021   public:
00022     inline SFImage();
00023     inline SFImage(const unsigned int w,
00024                    const unsigned int h,
00025                    const unsigned int n);
00026     inline SFImage(const SFImage& i);
00027     inline SFImage& operator=(const SFImage&);
00028     inline ~SFImage();
00029     inline void setDimensions(const unsigned int w,
00030                               const unsigned int h,
00031                               const unsigned int n);
00032     inline unsigned int width() const;
00033     inline unsigned int height() const;
00034     inline unsigned int numComponents() const;
00035     inline const unsigned char* pixels() const;
00036     inline unsigned char* pixels();
00037     static inline const char* typeName();
00038     static inline FieldTypeId typeId();
00039     inline bool operator==(const SFImage& i) const;
00040     friend std::ostream& operator<<(std::ostream& s,const SFImage& f);
00041   protected:
00042   public:
00043     unsigned int   width_;
00044     unsigned int   height_;
00045     unsigned int   numComponents_;
00046     unsigned char* pixels_;
00047   };
00048   //************************************************************
00049   // Implementation of 
00050   //************************************************************
00051   // TODO: implement reference counting
00052   inline
00053   SFImage::SFImage()
00054     : width_(0),height_(0),numComponents_(0),pixels_(NULL)
00055   {
00056   }
00057   inline
00058   SFImage::SFImage(const SFImage& i)
00059   {
00060     width_ = i.width_;
00061     height_ = i.height_;
00062     numComponents_ = i.numComponents_;
00063     pixels_ = new unsigned char[width_*height_*numComponents_];
00064     std::copy(i.pixels_,i.pixels_+width_*height_*numComponents_,
00065               pixels_);
00066   }
00067   inline SFImage&
00068   SFImage::operator=(const SFImage& i)
00069   {
00070     // We must check for the tricky case a=a (to avoid subtle leaks)
00071     if (this != &i)
00072     {
00073       // Deallocate previously used pixels
00074       if (pixels_ != NULL)
00075       {
00076         delete [] pixels_;
00077       }
00078       // Copy the new
00079       width_ = i.width_;
00080       height_ = i.height_;
00081       numComponents_ = i.numComponents_;
00082       pixels_ = new unsigned char[width_*height_*numComponents_];
00083       std::copy(i.pixels_,i.pixels_+width_*height_*numComponents_,
00084                 pixels_);
00085     }
00086     return *this;
00087   }  
00088   inline void
00089   SFImage::setDimensions(const unsigned int w,
00090                          const unsigned int h,
00091                          const unsigned int n)
00092   {
00093     if (pixels_ != NULL)
00094     {
00095       delete [] pixels_;
00096     }    
00097     width_ = w;
00098     height_ = h;
00099     numComponents_ = n;
00100     pixels_ = new unsigned char[w*h*n];
00101   }
00102   inline
00103   SFImage::SFImage(const unsigned int w,
00104                    const unsigned int h,
00105                    const unsigned int n)
00106     : width_(w),height_(h),numComponents_(n)
00107   {
00108     pixels_ = new unsigned char[w*h*n];
00109   }
00110   inline
00111   SFImage::~SFImage()
00112   {
00113     if (pixels_ != NULL)
00114     {
00115       delete [] pixels_;
00116     }
00117   }
00118   inline unsigned int
00119   SFImage::width() const
00120   {
00121     return width_;
00122   }
00123   inline unsigned int
00124   SFImage::height() const
00125   {
00126     return height_;
00127   }
00128   inline unsigned int
00129   SFImage::numComponents() const
00130   {
00131     return numComponents_;
00132   }
00133   inline const unsigned char*
00134   SFImage::pixels() const
00135   {
00136     return pixels_;
00137   }
00138   inline unsigned char*
00139   SFImage::pixels() 
00140   {
00141     return pixels_;
00142   }
00143   inline const char*
00144   SFImage::typeName()
00145   {
00146     return "SFImage";
00147   }
00148   inline FieldTypeId
00149   SFImage::typeId()
00150   {
00151     return sfImage;
00152   }  
00153   inline bool
00154   SFImage::operator==(const SFImage&) const
00155   {
00156     return false; // TODO:
00157   }
00158   inline std::ostream& operator<<(std::ostream& s,const SFImage& f)
00159   {
00160     s<<f.width_<<' '<<f.height_<<' '<<f.numComponents_<<' ';
00161     for (unsigned char* p = f.pixels_,
00162            *pstop = f.pixels_+f.width_*f.height_*f.numComponents_;
00163          p != pstop;)
00164     {
00165       int i = 0;
00166       for(unsigned int k=0;k<f.numComponents_;++k)
00167       {
00168         i <<= 8;
00169         i += *p++;
00170       }
00171       if (i != 0)
00172       {
00173         s<<"0x";
00174       }
00175       s<<std::hex<<i<<' ';
00176     }
00177     return s<<std::dec;    
00178   }
00179 }
00180 
00181 #endif // XDKWRL_SFIMAGE_H

Generated on 24 Feb 2005 with doxygen version 1.3.9.1. Valid HTML 4.0! Valid CSS!