00001 /*!@file Matlab/mexInvtread.C */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Rob Peters <rjpeters at usc dot edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Matlab/mexInvtread.C $ 00035 // $Id: mexInvtread.C 10360 2008-10-16 21:26:38Z itti $ 00036 // 00037 00038 #ifndef MATLAB_MEXINVTREAD_C_DEFINED 00039 #define MATLAB_MEXINVTREAD_C_DEFINED 00040 00041 #include <mex.h> 00042 00043 #include "Matlab/mexConverts.H" 00044 #include "Image/Image.H" 00045 #include "Raster/GenericFrame.H" 00046 #include "Raster/Raster.H" 00047 00048 #include <exception> 00049 00050 // ########################################################################## 00051 //! Read any image into matlab 00052 void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) 00053 { 00054 MYLOGVERB = LOG_INFO; 00055 LOG_FLAGS &= (~LFATAL_PRINTS_ABORT); 00056 LOG_FLAGS &= (~LFATAL_XTRA_NOISY); 00057 00058 if (nrhs < 1) mexErrMsgTxt("Missing required filename parameter"); 00059 00060 if (nlhs > 1) mexErrMsgTxt("Too many output arguments (expected 0 or 1)"); 00061 00062 try 00063 { 00064 // get the file name: 00065 const int siz = mxGetN(prhs[0]) + 1; 00066 00067 // (no need to mxFree() this pointer; matlab does that 00068 // automatically when the mex function returns) 00069 char* fname = (char*)mxCalloc(siz, sizeof(char)); 00070 mxGetString(prhs[0], fname, siz); 00071 00072 // read the image: 00073 const GenericFrame frame = Raster::ReadFrame(fname); 00074 00075 // return the image if desired: 00076 if (nlhs > 0) 00077 { 00078 switch (frame.nativeType()) 00079 { 00080 case GenericFrame::NONE: 00081 { 00082 Image<byte> empty; 00083 plhs[0] = Image2mexArrayUint8(&empty); 00084 } 00085 break; 00086 00087 case GenericFrame::RGB_U8: 00088 { 00089 const Image<PixRGB<byte> > rgb(frame.asRgb()); 00090 plhs[0] = RGBImage2mexArrayUint8(&rgb); 00091 } 00092 break; 00093 00094 case GenericFrame::RGB_F32: 00095 { 00096 plhs[0] = RGBImage2mexArray(frame.asRgbF32()); 00097 } 00098 break; 00099 00100 case GenericFrame::GRAY_U8: 00101 { 00102 const Image<byte> gray(frame.asGray()); 00103 plhs[0] = Image2mexArrayUint8(&gray); 00104 } 00105 break; 00106 00107 case GenericFrame::GRAY_F32: 00108 plhs[0] = Image2mexArray(frame.asFloat()); 00109 break; 00110 00111 case GenericFrame::VIDEO: 00112 { 00113 // FIXME need a way to specify YUV dims if needed here: 00114 const Image<PixRGB<byte> > rgb(frame.asRgb()); 00115 plhs[0] = RGBImage2mexArrayUint8(&rgb); 00116 } 00117 break; 00118 00119 default: 00120 LFATAL("Cannot read frames of type %d", int(frame.nativeType())); 00121 } 00122 } 00123 } 00124 catch (std::exception& e) 00125 { 00126 mexErrMsgTxt(e.what()); 00127 } 00128 } 00129 00130 // ###################################################################### 00131 /* So things look consistent in everyone's emacs... */ 00132 /* Local Variables: */ 00133 /* mode: c++ */ 00134 /* indent-tabs-mode: nil */ 00135 /* End: */ 00136 00137 #endif // MATLAB_MEXINVTREAD_C_DEFINED