core/vil1/vil1_image.h

Go to the documentation of this file.
00001 // This is core/vil1/vil1_image.h
00002 #ifndef vil1_image_h_
00003 #define vil1_image_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief A reference-counted image object.
00010 // \author fsm
00011 
00012 #include <vcl_iosfwd.h>
00013 #include <vil1/vil1_image_impl.h>
00014 
00015 //: A smart pointer to an actual image.
00016 // All operations are delegated to the vil1_image_impl object,
00017 // which uses class inheritance to make various file images etc.
00018 // For fuller documentation on any method, see vil1_image_impl
00019 //
00020 // Imagine that vil1_image is a class derived from
00021 // vbl_smart_ptr<vil1_image_impl>, but with some extra convenience
00022 // methods.  These methods might traditionally be attached to the abstract
00023 // base class vil1_image_impl, but this avoids cluttering that interface.
00024 //
00025 // You should not derive from vil1_image to make a new image type.
00026 // Derive from vil1_image_impl instead.
00027 
00028 class vil1_image
00029 {
00030   VCL_SAFE_BOOL_DEFINE;
00031  public:
00032 // use this delegation macro for consistency, not convenience.
00033 #define vil1_image_delegate(m, args, default) { return ptr ? ptr->m args : default; }
00034 
00035   //: Dimensions:  Planes x W x H x Components
00036   int planes() const { vil1_image_delegate(planes, (), 0); }
00037 
00038   //: Dimensions:  Planes x W x H x Components
00039   int width() const { vil1_image_delegate(width, (), 0); }
00040 
00041   //: Dimensions:  Planes x W x H x Components
00042   int height() const { vil1_image_delegate(height, (), 0); }
00043 
00044   //: Dimensions:  Planes x W x H x Components
00045   int components() const { vil1_image_delegate(components, (), 0); }
00046 
00047   //: Format.
00048   int bits_per_component() const { vil1_image_delegate(bits_per_component, (), 0); }
00049 
00050   //: Format.
00051   enum vil1_component_format component_format() const
00052   { vil1_image_delegate(component_format, (), VIL1_COMPONENT_FORMAT_UNKNOWN);  }
00053 
00054   //: return the ith plane.
00055   vil1_image get_plane(unsigned int p) const { vil1_image_delegate(get_plane, (p), vil1_image()); }
00056 
00057   //: Copy from image to buf
00058   bool get_section(void *buf, int x0, int y0, int wd, int ht) const
00059   { vil1_image_delegate(get_section, (buf, x0, y0, wd, ht), false); }
00060 
00061   //: Copy from buf to image
00062   bool put_section(void const *buf, int x0, int y0, int wd, int ht)
00063   { vil1_image_delegate(put_section, (buf, x0, y0, wd, ht), false); }
00064 
00065   //: Getting property information
00066   bool get_property(char const *tag, void *property_value = 0) const
00067   { vil1_image_delegate(get_property, (tag, property_value), false); }
00068 
00069   //: Setting property information
00070   bool set_property(char const *tag, void const *property_value = 0)
00071   { vil1_image_delegate(set_property, (tag, property_value), false); }
00072 
00073   //: Return a string describing the file format.
00074   // Only file images have a format, others return 0
00075   char const *file_format() const { vil1_image_delegate(file_format, (), "(null)"); }
00076 
00077 #undef vil1_image_delegate
00078   // -------------------- convenience --------------------
00079 
00080   //: Number of rows
00081   int rows() const { return height(); }
00082   //: Number of columns
00083   int cols() const { return width(); }
00084 
00085   //: return size in bytes.
00086   int get_size_bytes() const;
00087 
00088   //: Print a 1-line summary of contents
00089   vcl_ostream& print(vcl_ostream&) const;
00090 
00091   //------------ smart-pointer logic --------
00092 
00093   vil1_image(vil1_image_impl *p = 0) : ptr(p)
00094   {
00095     if (ptr)
00096       ptr->up_ref();
00097   }
00098 
00099   vil1_image(vil1_image const& that) : ptr(that.ptr) {
00100     if (ptr)
00101       ptr->up_ref();
00102   }
00103 
00104   //: Destructor
00105   ~vil1_image() {
00106     if (ptr)
00107       ptr->down_ref();
00108     ptr = 0; // don't dangle
00109   }
00110 
00111   vil1_image& operator=(vil1_image const &that) {
00112     if (ptr != that.ptr) {
00113       if (that.ptr)
00114         that.ptr->up_ref();
00115       if (ptr)
00116         ptr->down_ref();
00117       ptr = that.ptr;
00118     }
00119     return *this;
00120   }
00121 
00122   vil1_image& operator=(vil1_image_impl *p) {
00123     if (ptr)
00124       ptr->down_ref();
00125     ptr = p;
00126     if (ptr)
00127       ptr->up_ref();
00128     return *this;
00129   }
00130 
00131   //: equality means equality of implementation, not pixels.
00132   bool operator==(vil1_image const &that) const {
00133     return ptr == that.ptr;
00134   }
00135 
00136   //: needed for sorted containers of images.
00137   bool operator< (vil1_image const &that) const {
00138     return ptr <  that.ptr;
00139   }
00140 
00141   //: conversion to bool
00142   operator safe_bool () const
00143     { return (ptr != 0)? VCL_SAFE_BOOL_TRUE : 0; }
00144 
00145   //: inverse conversion to bool
00146   bool operator!() const
00147     { return (ptr != 0)? false : true; }
00148 
00149   //: use "sptr.impl()" to get a pointer to the impl object.
00150   vil1_image_impl *impl() const {
00151     return ptr;
00152   }
00153 
00154  protected:
00155   vil1_image_impl *ptr;
00156 };
00157 
00158 //: Print a 1-line summary of contents
00159 inline vcl_ostream& operator<<(vcl_ostream& s, vil1_image const& i)
00160 {
00161   return i.print(s);
00162 }
00163 
00164 #endif // vil1_image_h_

Generated on Sun Sep 7 05:08:27 2008 for core/vil1 by  doxygen 1.5.1