mexConverts.H

Go to the documentation of this file.
00001 /*!@file Matlab/mexConverts.H conversions from ans to MEX arrays
00002  */
00003 
00004 // //////////////////////////////////////////////////////////////////// //
00005 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00006 // University of Southern California (USC) and the iLab at USC.         //
00007 // See http://iLab.usc.edu for information about this project.          //
00008 // //////////////////////////////////////////////////////////////////// //
00009 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00010 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00011 // in Visual Environments, and Applications'' by Christof Koch and      //
00012 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00013 // pending; application number 09/912,225 filed July 23, 2001; see      //
00014 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00015 // //////////////////////////////////////////////////////////////////// //
00016 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00017 //                                                                      //
00018 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00019 // redistribute it and/or modify it under the terms of the GNU General  //
00020 // Public License as published by the Free Software Foundation; either  //
00021 // version 2 of the License, or (at your option) any later version.     //
00022 //                                                                      //
00023 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00024 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00025 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00026 // PURPOSE.  See the GNU General Public License for more details.       //
00027 //                                                                      //
00028 // You should have received a copy of the GNU General Public License    //
00029 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00030 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00031 // Boston, MA 02111-1307 USA.                                           //
00032 // //////////////////////////////////////////////////////////////////// //
00033 //
00034 // Primary maintainer for this file: Dirk Walther <walther@caltech.edu>
00035 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Matlab/mexConverts.H $
00036 // $Id: mexConverts.H 9412 2008-03-10 23:10:15Z farhan $
00037 //
00038 
00039 #ifndef MEXCONVERTS_H_DEFINED
00040 #define MEXCONVERTS_H_DEFINED
00041 
00042 #include "Util/Types.H"
00043 #include "Image/Point2D.H"
00044 #include "Image/ImageSet.H"
00045 #include <mex.h>
00046 #include <vector>
00047 
00048 template <class T> class PixRGB;
00049 template <class T> class Image;
00050 
00051 //! convert a mxArray into a grayscale image
00052 template <class T>
00053 Image<T> mexArray2Image(const mxArray* arrptr);
00054 
00055 //! convert a mxArray into a RGB image
00056 template <class T>
00057 Image< PixRGB<T> > mexArray2RGBImage(const mxArray* arrptr);
00058 
00059 //! convert a grayscale image into a mxArray
00060 template <class T>
00061 mxArray* Image2mexArray(const Image<T>& img);
00062 
00063 //! convert a grayscale image into a mxArray of class 'uint8'
00064 /** We take a pointer rather than a reference to ensure that there are
00065     no implicit Image type conversions in calls to this function -- we
00066     want exactly an Image<byte>, and not an Image<float> or some other
00067     pixel type. */
00068 mxArray* Image2mexArrayUint8(const Image<byte>* img);
00069 
00070 //! convert a RGB image into a mxArray of class 'double'
00071 template <class T>
00072 mxArray* RGBImage2mexArray(const Image< PixRGB<T> >& img);
00073 
00074 /// convert a RGB image into a mxArray of class 'uint8'
00075 /** We take a pointer rather than a reference to ensure that there are
00076     no implicit Image type conversions in calls to this function -- we
00077     want exactly an Image<PixRGB<byte>>, and not an
00078     Image<PixRGB<float>> or some other pixel type. */
00079 mxArray* RGBImage2mexArrayUint8(const Image< PixRGB<byte> >* img);
00080 
00081 //! convert the first num_elements elements of a mxArray into a std::vector
00082 template <class T>
00083 std::vector<T> mexArr2Vector(const mxArray* vecptr, uint num_elements);
00084 
00085 //! convert a mxArray vector into a std::vector
00086 template <class T>
00087 std::vector<T> mexArr2Vector(const mxArray* vecptr);
00088 
00089 //! convert the first num_elements elements of a std::vector into a mxArray
00090 template <class T>
00091 mxArray* Vector2mexArr(const std::vector<T>& vect, uint num_elements);
00092 
00093 //! convert a std::vector into a mxArray vector
00094 template <class T>
00095 mxArray* Vector2mexArr(const std::vector<T>& vect);
00096 
00097 //! convert a 2 x N mxArray into a std::vector<Point2D<int> >
00098 std::vector<Point2D<int> > mexArr2Point2DVec(const mxArray* arrptr);
00099 
00100 //! convert a std::vector<Point2D<int> > into a 2 x N mxArray
00101 mxArray* Point2DVec2mexArr(const std::vector<Point2D<int> >& vect);
00102 
00103 //! convert a vector of pointers to images of equal size into a 3D mxArray
00104 template <class T>
00105 mxArray* ImgVec2mexArr(const ImageSet<T>& vect);
00106 
00107 //! convert a 3D mxArray into a vector of pointers to images of equal size
00108 template <class T>
00109 ImageSet<T> mexArr2ImgVec(const mxArray* arrptr);
00110 
00111 //! convert an ImageSet into a cell array of images
00112 template <class T>
00113 mxArray* ImageSet2cellArr(const ImageSet<T>& imset);
00114 
00115 //! convert a cell array of images into an ImageSet
00116 template <class T>
00117 ImageSet<T> cellArr2ImageSet(const mxArray* arrptr);
00118 
00119 //! convert a vector of ImageSet into a 2d cell array
00120 template <class T>
00121 mxArray* ImageSetVec2cellArr(const std::vector< ImageSet<T> >& vec);
00122 
00123 //! convert a 2d cell array into a vector of ImageSet
00124 template <class T>
00125 std::vector< ImageSet<T> > cellArr2ImageSetVec(const mxArray* arrptr);
00126 
00127 // ######################################################################
00128 inline double conv(const double* src, double* target)
00129 { *target = *src; return *target; }
00130 
00131 inline byte conv(const double* src, byte* target)
00132 { *target = (byte)(*src * 255.0); return *target; }
00133 
00134 inline double conv(const byte* src, double* target)
00135 { *target = (double)*src / 255.0; return *target; }
00136 
00137 inline byte conv(const byte* src, byte* target)
00138 { *target = *src; return *target; }
00139 
00140 inline float conv(const double* src, float* target)
00141 { *target = (float)*src; return *target; }
00142 
00143 inline double conv(const float* src, double* target)
00144 { *target = (double)*src; return *target; }
00145 
00146 inline float conv(const byte* src, float* target)
00147 { *target = (float)*src / 255.0; return *target; }
00148 
00149 
00150 // ######################################################################
00151 //! Class to help manage return values in mex files.
00152 class MexReturn
00153 {
00154 public:
00155   //! Constructor takes arguments from the mexFunction call
00156   MexReturn(int nlhs, mxArray *plhs[]);
00157 
00158   //! Destructor checks that all arguments are assigned
00159   virtual ~MexReturn();
00160 
00161   //! returns the number of return arguments
00162   int numArgs();
00163 
00164   //! test if 0 <= num < numArgs()
00165   bool isNumOK(int num);
00166 
00167   //! store a mxArray as return argumnt num; true if successful
00168   bool store(int num, mxArray *val);
00169 
00170   //! store  double scalar
00171   bool store(int num, double val);
00172 
00173   //! store a vector of numbers
00174   template <class T>
00175   bool store(int num, const std::vector<T>& val);
00176 
00177   //! store a grayscale image
00178   template <class T>
00179   bool store(int num, const Image<T>& val);
00180 
00181   //! store an RGB image
00182   template <class T>
00183   bool store(int num, const Image< PixRGB<T> >& val);
00184 
00185  private :
00186   int itsNlhs;
00187   mxArray **itsPlhs;
00188 };
00189 
00190 #endif
00191 
00192 // ######################################################################
00193 /* So things look consistent in everyone's emacs... */
00194 /* Local Variables: */
00195 /* indent-tabs-mode: nil */
00196 /* End: */
Generated on Sun May 8 08:40:59 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3