core/vil1/vil1_memory_image_of.h

Go to the documentation of this file.
00001 // This is core/vil1/vil1_memory_image_of.h
00002 #ifndef vil1_memory_image_of_h_
00003 #define vil1_memory_image_of_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \author awf@robots.ox.ac.uk
00010 // \date 16 Feb 00
00011 //
00012 //\verbatim
00013 // Modifications
00014 //     960916 AWF Added save_pgm and more comments.
00015 //     961209 Peter Vanroose added operator=() and copy constructor
00016 //     980710 FSM Changed constructor to take a const Image *, not Image *
00017 //     981105 AWF Made bilinear/bicubic return double.
00018 //     990211 Peter Vanroose moved save_pgm() to Templates/ (EGCS complained)
00019 //     990421 FSM Added constructor from a const fast_array<T> &
00020 //     010126 BJM (mccane@cs.otago.ac.nz) added constructor from
00021 //            previously allocated memory. This memory is not deallocated on
00022 //            destruction.
00023 //   Feb.2002 - Peter Vanroose - brief doxygen comment placed on single line
00024 //\endverbatim
00025 
00026 #include <vil1/vil1_image.h>
00027 #include <vil1/vil1_memory_image.h>
00028 
00029 //: Image stored entirely in RAM
00030 //
00031 //    vil1_memory_image_of<Type> provides a templated interface to a
00032 //    vil1_memory_image.  It is assumed that the user has queried the pixel size
00033 //    of the image and is instantiating an ImageBuffer of the appropriate type.
00034 //
00035 //    This allows C-efficiency access with C++ notational convenience after the
00036 //    type has been ascertained.  Note that this should not be used for images
00037 //    too large to fit in memory.
00038 //
00039 //  CAVEAT PROGRAMMER:
00040 //    Each raster (row) is stored in a contiguous chunk of memory and the
00041 //    operator [] method gives a pointer to the beginning of a raster.
00042 //    Thus image[i][j] is the element in the i-th row and j-th column.
00043 //    However, image(x, y) is the element in the x-th column and y-th row.
00044 
00045 template <class T>
00046 class vil1_memory_image_of : public vil1_memory_image
00047 {
00048  public:
00049   // The pixel type of this image
00050   typedef T pixel_type;
00051 
00052   // iterators
00053   typedef T *iterator;
00054   inline iterator begin() { return get_buffer(); }
00055   inline iterator end  () { return get_buffer() + rows()*cols(); }
00056 
00057   typedef T const *const_iterator;
00058   inline const_iterator begin() const { return get_buffer(); }
00059   inline const_iterator end  () const { return get_buffer() + rows()*cols(); }
00060 
00061   inline unsigned size() const { return rows() * cols(); }
00062 
00063   //: Empty image.
00064   vil1_memory_image_of();
00065 
00066   //: This is a copy constructor, but it doesn't make a new buffer.
00067   vil1_memory_image_of(vil1_memory_image_of<T> const &);
00068 
00069   //: Copy given image into a memory buffer.
00070   // If it's already a memory image, do as the copy constructor (above) does.
00071   explicit
00072   vil1_memory_image_of(vil1_image const& image);
00073 
00074   //: Construct a w x h image, pixel format is determined from T
00075   vil1_memory_image_of(int sizex, int sizey);
00076 
00077   //: Construct a w x h image, pixel format is determined from T from memory previously created and pointed to by buf
00078   vil1_memory_image_of(T *buf, int sizex, int sizey);
00079 #if 0
00080   //: Make memory imagebuffer, and fill with "value"
00081   vil1_memory_image_of(int sizex, int sizey, T const& value);
00082 #endif
00083   //: Clearly, this will deallocate the memory buffer
00084   inline ~vil1_memory_image_of() {}
00085 
00086   //: This method hides the operator= in the base class.
00087   vil1_memory_image_of<T>& operator=(vil1_memory_image_of<T> const &);
00088 
00089   //: Copy a vil1_image, only if it's in an appropriate format.
00090   // This routine does not try to guess how to convert images which are
00091   // not compatible with T.
00092   vil1_memory_image_of<T>& operator=(vil1_image const &);
00093 
00094   //: Load image.
00095   void set(vil1_image const& image);
00096 
00097   //: These override the methods in the base class.
00098   void resize(int width, int height);
00099  private:
00100   // don't try to use this.
00101   void resize(int planes, int width, int height);
00102  public:
00103 
00104   // Data Access---------------------------------------------------------------
00105 
00106   //: Return read/write reference to pixel at (x,y)
00107   inline T&       operator () (int x, int y) { return ((T**)rows0_)[y][x]; }
00108   inline T const& operator () (int x, int y) const { return ((T const* const*)rows0_)[y][x]; }
00109 
00110   //: Return pointer to raster y.
00111   inline T*       operator [] (int y) { return ((T**)rows0_)[y]; }
00112   inline T const* operator [] (int y) const { return ((T const* const*)rows0_)[y]; }
00113 
00114   //: Return pointer to array of rasters. aka known as data_array() for matrices.
00115   inline T*        const* row_array() { return (T**)rows0_; }
00116   inline T const*  const* row_array() const { return (T**)rows0_; }
00117 
00118   //: Return pointer to the memory buffer.
00119   inline T*       get_buffer() { return (T*)rows0_[0]; }
00120   inline T const* get_buffer() const { return (T*)rows0_[0]; }
00121 
00122   //: Return true if (x,y) is a valid index into this buffer
00123   inline bool in_range(int x, int y) const { return (0 <= x) && (0 <= y) && (x < width_) && (y < height_); }
00124 
00125   //: Return true if (x+/-w,y+/-h) are valid indices into this buffer
00126   inline bool in_range_window(int x, int y, int w) const {
00127     return (w <= x) && (w <= y) && (x + w < width_) && (y + w < height_);
00128   }
00129 
00130   //: Return true if the region of size w,h starting at x,y is valid in this buffer.
00131   inline bool in_range(int x, int y, unsigned w, unsigned h) const {
00132     return (0<=x && x+int(w)<=width_) && (0<=y && y+int(h)<=height_);
00133   }
00134 
00135   //: Fill with given value
00136   void fill(T const& );
00137 };
00138 
00139 #endif // vil1_memory_image_of_h_

Generated on Mon Mar 8 05:09:33 2010 for core/vil1 by  doxygen 1.5.1