"Fossies" - the Fresh Open Source Software Archive

Member "pfstools-2.2.0/src/matlab/pfsput.cpp" (12 Aug 2021, 6607 Bytes) of package /linux/privat/pfstools-2.2.0.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "pfsput.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.1.0_vs_2.2.0.

    1 /**
    2  * @brief Write frame to pfs stream in MATLAB
    3  * 
    4  * This file is a part of PFSTOOLS package.
    5  * ---------------------------------------------------------------------- 
    6  * Copyright (C) 2003,2004 Rafal Mantiuk and Grzegorz Krawczyk
    7  * 
    8  *  This program is free software; you can redistribute it and/or modify
    9  *  it under the terms of the GNU General Public License as published by
   10  *  the Free Software Foundation; either version 2 of the License, or
   11  *  (at your option) any later version.
   12  *
   13  *  This program is distributed in the hope that it will be useful,
   14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16  *  GNU General Public License for more details.
   17  *
   18  *  You should have received a copy of the GNU General Public License
   19  *  along with this program; if not, write to the Free Software
   20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   21  * ---------------------------------------------------------------------- 
   22  *
   23  * @author Rafal Mantiuk, <mantiuk@mpi-sb.mpg.de>
   24  *
   25  * $Id: pfsput.cpp,v 1.6 2008/05/06 17:11:35 rafm Exp $
   26  */
   27 
   28 #include "compatibility.h"
   29 
   30 #include <stdio.h>
   31 #include <stdlib.h>
   32 #include <fcntl.h>
   33 
   34 #include <list>
   35 
   36 #include "mex.h"
   37 #include "mex_utils.h"
   38 
   39 #define SCRIPT_NAME "pfsput"
   40 #define error mexErrMsgTxt
   41 
   42 #include <pfs.h>
   43 
   44 
   45 void mexFunction(int nlhs, mxArray *plhs[],
   46                  int nrhs, const mxArray *prhs[])
   47 {     
   48 
   49   /* Check for proper number of arguments. */
   50   if( nrhs != 1 || !mxIsStruct( prhs[0] ) ) 
   51     error(SCRIPT_NAME ": Improper usage!");
   52  
   53   const mxArray *pfs_stream = prhs[0];
   54   
   55   mxArray *f_fid = mxGetField( pfs_stream, 0, "FID" );
   56   if( f_fid == NULL || !is_mex_scalar( f_fid ) )
   57   {
   58     error( SCRIPT_NAME ": FH field missing in the structure or it has wrong type");
   59   }
   60   
   61   int fid = (int)get_mex_double( f_fid );
   62 
   63   // Check mode
   64   {                             
   65     mxArray *f_mode = mxGetField( pfs_stream, 0, "MODE" );
   66     if( f_mode == NULL ) {
   67       error( SCRIPT_NAME ": MODE field missing in the structure or it has wrong type");
   68     }
   69     if( strcmp( "W", get_mex_string( f_mode ) ) ) {
   70       error( SCRIPT_NAME ": Can not write to the stream that is open for reading." );
   71     }
   72   }
   73 
   74   // Get width & height
   75   int width, height;
   76   {                             
   77     mxArray *f_cols, *f_rows;
   78     f_cols = mxGetField( pfs_stream, 0, "columns" );
   79     f_rows = mxGetField( pfs_stream, 0, "rows" );
   80 
   81     if( !is_mex_scalar( f_cols ) || !is_mex_scalar( f_rows ) )
   82     {
   83       error( SCRIPT_NAME ": 'rows' and 'columns' fields missing in the structure or it have wrong type");
   84     }
   85     width = (int)get_mex_double( f_cols );
   86     height = (int)get_mex_double( f_rows );
   87   }
   88 
   89   // Get channels
   90   int ch_count;
   91   mxArray *f_channels;
   92   {
   93     f_channels = mxGetField( pfs_stream, 0, "channels" );
   94     if( f_channels == NULL || !mxIsStruct( f_channels ) ) 
   95       error( SCRIPT_NAME ": 'channels' field missing in the structure or it has wrong type");
   96     ch_count = mxGetNumberOfFields( f_channels );
   97   }  
   98   
   99   try {
  100     pfs::DOMIO ctx;
  101     pfs::Frame *frame = ctx.createFrame( width, height );
  102 
  103     // For each channel in the 'channels' map
  104     for( int ch = 0; ch < ch_count; ch++ ) {
  105       const char *channelName = mxGetFieldNameByNumber( f_channels, ch );
  106 
  107       mxArray *f_data = mxGetFieldByNumber( f_channels, 0, ch );
  108 
  109       if( !(mxIsDouble( f_data ) || mxIsSingle( f_data )) || mxIsComplex( f_data ) ) {
  110         throw pfs::Exception( "all channels must be given as real matrices" );
  111       }
  112       
  113       if( mxGetM( f_data ) != height || mxGetN( f_data ) != width ) {
  114         throw pfs::Exception( "size of the channel must be the same as given in pfsopen" );
  115       }
  116       
  117       pfs::Channel *pfsChannel = frame->createChannel( channelName );
  118 
  119       // Copy matrix to pfs::Channel
  120       copy_mex_to_pfschannel( f_data, pfsChannel );
  121     }
  122 
  123     // Copy frame tags
  124     {
  125       mxArray *tags, *f_rows;
  126       tags = mxGetField( pfs_stream, 0, "tags" );
  127       if( tags != NULL ) {        
  128         if( !mxIsStruct( f_channels ) ) 
  129           error( SCRIPT_NAME ": 'tags' field has wrong type");
  130         int tag_count = mxGetNumberOfFields( tags );
  131 
  132         for( int t = 0; t < tag_count; t++ ) {
  133           const char *tag_name = mxGetFieldNameByNumber( tags, t );
  134           mxArray *tag_value = mxGetFieldByNumber( tags, 0, t );
  135 
  136 //        printf( "tag: %s = %s\n", tag_name, get_mex_string( tag_value ) );
  137           frame->getTags()->setString( tag_name, get_mex_string( tag_value ) );
  138         
  139         }
  140       }
  141       
  142       
  143     }
  144     
  145     /* Not implemented - hopefuly nobody uses this
  146 
  147     // Copy channel tags
  148     {
  149       Octave_map::const_iterator itChTags = pfsStream.seek( "channelTags" );
  150       if( itChTags != pfsStream.end() ) {
  151         if( !pfsStream.contents( itChTags )(0).isstruct() )
  152         {
  153           throw pfs::Exception( "'channelTags' field must be a structure" );  
  154         }
  155         Octave_map tagChannels = pfsStream.contents( itChTags )(0).map_value();
  156         for( Octave_map::iterator itCh = tagChannels.begin(); itCh != tagChannels.end(); itCh++ ) {
  157           std::string channelName = tagChannels.key(itCh);
  158           if( !tagChannels.contents( itCh )(0).isstruct() ) {
  159             throw pfs::Exception( "each channelTags file must be a structure" );  
  160           }
  161           pfs::Channel *pfsChannel = frame->getChannel( channelName.c_str() );
  162           if( pfsChannel == NULL ) {
  163             throw pfs::Exception( "can not set channel tag if channel is missing" );
  164           }
  165           
  166           Octave_map tags = tagChannels.contents( itCh )(0).map_value();
  167           for( Octave_map::iterator itTag = tags.begin(); itTag != tags.end(); itTag++ ) {
  168             std::string tagName = tags.key(itTag);
  169             if( !tags.contents( itTag )(0).is_string() ) 
  170               throw pfs::Exception( "all channel tags must be given as strings" );
  171             std::string tagValue = tags.contents( itTag )(0).string_value();
  172             pfsChannel->getTags()->setString( tagName.c_str(), tagValue.c_str() );
  173           }
  174         }
  175       }
  176       
  177     }
  178 */
  179 
  180 
  181 //    FILE *fh = fdopen( fid, "a" );
  182     FILE *fh = fdopen( dup( fid ), "w" );
  183     if( fh == NULL ) 
  184       error( SCRIPT_NAME ": Cannot open file for writing" );
  185     
  186     ctx.writeFrame( frame, fh );
  187     ctx.freeFrame( frame );
  188     fclose( fh );
  189     
  190     //fsync( fid );
  191 
  192   }
  193   catch( pfs::Exception ex )
  194   {
  195     char error_message[100];
  196     sprintf( error_message, "%s: %s", SCRIPT_NAME, ex.getMessage() );
  197     error( error_message );      
  198   }    
  199     
  200 }
  201 
  202