casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
ImageInterface.h
Go to the documentation of this file.
00001 //# ImageInterface.h: a base class for astronomical images
00002 //# Copyright (C) 1996,1997,1998,1999,2000,2001
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: ImageInterface.h 20615 2009-06-09 02:16:01Z Malte.Marquarding $
00027 
00028 #ifndef IMAGES_IMAGEINTERFACE_H
00029 #define IMAGES_IMAGEINTERFACE_H
00030 
00031 
00032 //# Includes
00033 #include <casa/aips.h>
00034 #include <images/Regions/RegionHandler.h>
00035 #include <images/Images/MaskSpecifier.h>
00036 #include <images/Images/ImageInfo.h>
00037 #include <lattices/Lattices/MaskedLattice.h>
00038 #include <coordinates/Coordinates/CoordinateSystem.h>
00039 #include <tables/LogTables/LoggerHolder.h>
00040 #include <tables/Tables/TableRecord.h>
00041 #include <casa/Quanta/Unit.h>
00042 
00043 namespace casa { //# NAMESPACE CASA - BEGIN
00044 
00045 //# Forward Declarations
00046 template <class T> class LatticeIterInterface;
00047 template <class T> class Vector;
00048 template <class T> class COWPtr;
00049 class ImageRegion;
00050 class IPosition;
00051 class TiledShape;
00052 
00053 
00054 // <summary>
00055 // A base class for astronomical images.
00056 // </summary>
00057 
00058 // <use visibility=export>
00059 
00060 // <reviewed reviewer="" date="" tests="" demos="">
00061 // </reviewed>
00062 
00063 // <prerequisite>
00064 //   <li> <linkto class=Lattice>Lattices</linkto>
00065 //   <li> <linkto class=CoordinateSystem>CoordinateSystem</linkto>
00066 // </prerequisite>
00067 
00068 // <etymology>
00069 // The ImageInterface class name is derived from its role as the cookie cutter
00070 // Interface base class for Images.  
00071 // </etymology>
00072 
00073 // <synopsis> 
00074 // The ImageInterface class is an abstract base class. All Image classes
00075 // should derive from this class to ensure functions which operate on Images
00076 // will work for all Image derivations.
00077 // 
00078 // An Image is currently defined as an Array of pixels, a Boolean mask,
00079 // defining which pixels are valid and coordinates to define the reference
00080 // frame. The only concrete class currently derived from this Interface is
00081 // PagedImage, which allows the image to be stored on disk, and only reads
00082 // specified portions of the image into memory.  
00083 // </synopsis>
00084 
00085 // <example>
00086 // As this is an abstract base class it is not possible to construct an
00087 // instance of this object.  It can however be used as a function argument.<br>
00088 // eg 1. (used in dImageInterface.cc)
00089 // <srcblock>
00090 // Float sumPixels(const ImageInterface<Float>& image){
00091 //   uInt rowLength = image.shape()(0);
00092 //   IPosition rowShape(image.ndim());
00093 //   rowShape = 1; rowShape(0) = rowLength;
00094 //   Float sumPix = 0;
00095 //   RO_LatticeIterator<Float> iter(image, rowShape);
00096 //   while(!iter.atEnd()){
00097 //     sumPix += sum(iter.vectorCursor());
00098 //     iter++;
00099 //   }
00100 //   return sumPix;
00101 // }
00102 // </srcblock>
00103 //
00104 // The main purpose of this class is for programming objects, the following
00105 // example is of how one would derive from ImageInterface: <br>
00106 // eg 2.
00107 // <srcblock>
00108 // template <class T> class myNewImage : public ImageInterface<T>
00109 // {
00110 // public:
00111 //   // default constructor
00112 //   myNewImage();
00113 //
00114 //   // argumented constructor
00115 //   myNewImage(...);
00116 //
00117 //   // destructor
00118 //   ~myNewImage
00119 //   
00120 //   // the shape function is forced upon us by the Lattice base class
00121 //   IPosition shape() const;
00122 //   
00123 //   // doGetSlice is another function required of all Lattice objects.
00124 //   Bool doGetSlice(<Array<T>& buffer, const Slicer& section);
00125 //
00126 //  // etc...
00127 // private:
00128 //  // put the actual map data down here.
00129 //  // etc...
00130 // };
00131 // </srcblock>
00132 // </example>
00133 
00134 // <motivation> 
00135 // The use of abstract base classes to guide inheritance seemed appropriate
00136 // for Images to ensure that CoordinateSystems and masking get handled
00137 // uniformly.
00138 // </motivation>
00139 
00140 // <todo asof="1995/04/25">
00141 //   <li> replace ImageCoordinates
00142 // </todo>
00143 
00144 
00145 template <class T> class ImageInterface: public MaskedLattice<T>
00146 {
00147   //# Make members of parent class known.
00148 public:
00149   using MaskedLattice<T>::shape;
00150 
00151 public: 
00152   ImageInterface();
00153 
00154   // Construct for a specific region handler object.
00155   ImageInterface (const RegionHandler& regionHandler);
00156 
00157   // Copy constructor (copy semantics).
00158   ImageInterface (const ImageInterface& other);
00159 
00160   virtual ~ImageInterface();
00161 
00162   // Make a copy of the derived object (reference semantics).
00163   // <group>
00164   virtual MaskedLattice<T>* cloneML() const;
00165   virtual ImageInterface<T>* cloneII() const = 0;
00166   // </group>
00167 
00168   // Get the image type (returns name of derived class).
00169   virtual String imageType() const = 0;
00170 
00171   // Function which changes the shape of the image (N.B. the data is thrown 
00172   // away - the Image will be filled with nonsense afterwards)
00173   virtual void resize (const TiledShape& newShape) = 0;
00174   
00175   // Function which get and set the units associated with the image
00176   // pixels (i.e. the "brightness" unit). <src>setUnits()</src> returns
00177   // False if it cannot set the unit for some reason (e.g. the underlying
00178   // file is not writable).
00179   // <group>
00180   virtual Bool setUnits (const Unit& newUnits);
00181   virtual const Unit& units() const
00182     { return unit_p; }
00183   // </group>
00184 
00185   // Return the name of the current ImageInterface object. This will generally 
00186   // be a file name for images that have a persistent form.  Any path
00187   // before the actual file name can be optionally stripped off.
00188   virtual String name (Bool stripPath=False) const = 0;
00189 
00190   // Functions to set or replace the coordinate information in the Image
00191   // Returns False on failure, e.g. if the number of axes do not match.
00192   //# NOTE. setCoordinateInfo should be pure virtual with a partial 
00193   //# implementation however SGI ntv will not generate it with -no_prelink.
00194   // <group>
00195   virtual Bool setCoordinateInfo (const CoordinateSystem& coords);
00196   const CoordinateSystem& coordinates() const
00197     { return coords_p; }
00198   // </group>
00199 
00200   // Function to get a LELCoordinate object containing the coordinates.
00201   virtual LELCoordinates lelCoordinates() const;
00202 
00203   // Get access to the LoggerHolder.
00204   // <group>
00205   LoggerHolder& logger()
00206     { return log_p; }
00207   const LoggerHolder& logger() const
00208     { return log_p; }
00209   // </group>
00210 
00211   // Allow messages to be logged to this ImageInterface.
00212   // <group>
00213   LogIO& logSink()
00214     { return logger().logio(); }
00215   const LogIO& logSink() const
00216     { return const_cast<ImageInterface<T>*>(this)->logSink(); }
00217   // </group>
00218   
00219   // Add the messages from the other image logger to this one.
00220   void appendLog (const LoggerHolder& other)
00221     { log_p.append (other); }
00222 
00223   // Often we have miscellaneous information we want to attach to an image.
00224   // This is where it goes.  
00225   // <br>
00226   // Note that setMiscInfo REPLACES the information with the new information.
00227   // It can fail if, e.g., the underlying table is not writable.
00228   // <group>
00229   const TableRecord& miscInfo() const
00230     { return miscInfo_p; }
00231   virtual Bool setMiscInfo (const RecordInterface& newInfo);
00232   // </group>
00233 
00234   // The ImageInfo object contains some miscellaneous information about the image
00235   // which unlike that stored in MiscInfo, has a standard list of things,
00236   // such as the restoring beam.
00237   //
00238   // Note that setImageInfo REPLACES the information with the new information.
00239   // It is up to the derived class to make the ImageInfo permanent.
00240   // <group>
00241   const ImageInfo& imageInfo() const
00242     { return imageInfo_p; }
00243   virtual Bool setImageInfo (const ImageInfo& info);
00244   // </group>
00245 
00246   // Can the image handle region definition?
00247   Bool canDefineRegion() const
00248     { return regHandPtr_p->canDefineRegion(); }
00249 
00250   // Make a mask which is suitable for the type of image.
00251   // Optionally the mask can be initialized with the given value
00252   // (by default it will not).
00253   // <br>Optionally the mask can be defined as an image region/mask
00254   // and turned in the default mask for the image. By default it will.
00255   virtual ImageRegion makeMask (const String& name,
00256                                 Bool defineAsRegion = True,
00257                                 Bool setAsDefaultMask = True,
00258                                 Bool initialize = False,
00259                                 Bool value = True);
00260 
00261   // Define a region/mask belonging to the image.
00262   // The group type determines if it stored as a region or mask.
00263   // If overwrite=False, an exception will be thrown if the region
00264   // already exists.
00265   // <br>An exception is thrown if canDefineRegion is False.
00266   virtual void defineRegion (const String& name, const ImageRegion& region,
00267                              RegionHandler::GroupType,
00268                              Bool overwrite = False);
00269 
00270   // Does the image have a region with the given name?
00271   virtual Bool hasRegion (const String& regionName,
00272                           RegionHandler::GroupType = RegionHandler::Any) const;
00273 
00274   // Get a region/mask belonging to the image from the given group
00275   // (which can be Any).
00276   // <br>Optionally an exception is thrown if the region does not exist.
00277   // A zero pointer is returned if the region does not exist.
00278   // The caller has to delete the <src>ImageRegion</src> object created.
00279   virtual ImageRegion* getImageRegionPtr
00280                             (const String& name,
00281                              RegionHandler::GroupType = RegionHandler::Any,
00282                              Bool throwIfUnknown = True) const;
00283 
00284   // Rename a region.
00285   // If a region with the new name already exists, it is deleted or
00286   // an exception is thrown (depending on <src>overwrite</src>).
00287   // The region name is looked up in the given group(s).
00288   // <br>An exception is thrown if the old region name does not exist.
00289   virtual void renameRegion (const String& newName,
00290                              const String& oldName,
00291                              RegionHandler::GroupType = RegionHandler::Any,
00292                              Bool overwrite = False);
00293 
00294   // Remove a region/mask belonging to the image from the given group
00295   // (which can be Any).
00296   // <br>Optionally an exception is thrown if the region does not exist.
00297   virtual void removeRegion (const String& name,
00298                              RegionHandler::GroupType = RegionHandler::Any,
00299                              Bool throwIfUnknown = True);
00300 
00301   // Get the names of all regions/masks.
00302   virtual Vector<String> regionNames
00303                    (RegionHandler::GroupType = RegionHandler::Any) const;
00304 
00305   // Use the mask as specified.
00306   // If a mask was already in use, it is replaced by the new one.
00307   virtual void useMask (MaskSpecifier = MaskSpecifier());
00308 
00309   // Set the default pixelmask to the mask with the given name
00310   // (which has to exist in the "masks" group).
00311   // If the image table is writable, the setting is persistent by writing
00312   // the name as a keyword.
00313   // If the given regionName is the empty string,
00314   // the default pixelmask is unset.
00315   virtual void setDefaultMask (const String& regionName);
00316 
00317   // Get the name of the default pixelmask.
00318   // An empty string is returned if no default pixelmask.
00319   virtual String getDefaultMask() const;
00320 
00321   // Get a region belonging to the image.
00322   // An exception is thrown if the region does not exist.
00323   ImageRegion getRegion (const String& regionName,
00324                          RegionHandler::GroupType = RegionHandler::Any) const;
00325 
00326   // Make a unique region name from the given root name, thus make it such
00327   // that the name is not already in use for a region or mask.
00328   // The root name is returned if it is already unique.
00329   // Otherwise a number is appended to the root name to make it unique.
00330   // The number starts at the given number and is incremented until the name
00331   // is unique.
00332   String makeUniqueRegionName (const String& rootName,
00333                                uInt startNumber = 1) const;
00334 
00335   // Check class invariants. 
00336   virtual Bool ok() const = 0;
00337 
00338   // Save and restore an ImageInterface object to or from a state Record
00339   Bool toRecord (String& error, RecordInterface& outRec);
00340   Bool fromRecord (String& error, const RecordInterface& inRec);
00341  
00342 protected:
00343   // Assignment (copy semantics) is only useful for derived classes.
00344   ImageInterface& operator= (const ImageInterface& other);
00345 
00346   // Restore the image info from the record.
00347   Bool restoreImageInfo (const RecordInterface& rec);
00348 
00349   // Set the image logger variable.
00350   void setLogMember (const LoggerHolder& logger)
00351     { log_p = logger; }
00352 
00353   /*
00354   // Set the image info variable.
00355   void setImageInfoMember (const ImageInfo& imageInfo)
00356     { imageInfo_p = imageInfo; }
00357     */
00358 
00359   // Set the coordinate system variable.
00360   void setCoordsMember (const CoordinateSystem& coords)
00361     { coords_p = coords; }
00362 
00363   // Set the unit variable.
00364   void setUnitMember (const Unit& unit)
00365     { unit_p = unit; }
00366 
00367   // Set the miscinfo variable.
00368   void setMiscInfoMember (const RecordInterface& rec)
00369     { miscInfo_p.assign (rec); }
00370 
00371   // Get access to the region handler.
00372   RegionHandler* getRegionHandler()
00373     { return regHandPtr_p; }
00374 
00375 private:
00376   // It is the job of the derived class to make these variables valid.
00377   CoordinateSystem coords_p;
00378   LoggerHolder     log_p;
00379   ImageInfo        imageInfo_p;
00380   Unit             unit_p;
00381   TableRecord      miscInfo_p;
00382 
00383   // The region handling object.
00384   RegionHandler* regHandPtr_p;
00385 
00386 };
00387 
00388 
00389 
00390 } //# NAMESPACE CASA - END
00391 
00392 #ifndef CASACORE_NO_AUTO_TEMPLATES
00393 #include <images/Images/ImageInterface.tcc>
00394 #endif //# CASACORE_NO_AUTO_TEMPLATES
00395 #endif