"Fossies" - the Fresh Open Source Software Archive

Member "pfstools-2.2.0/src/octave/pfsget.cpp" (12 Aug 2021, 5248 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 "pfsget.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 Read frame from pfs stream in GNU Octave
    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: pfsget.cpp,v 1.1 2005/06/15 13:36:54 rafm Exp $
   26  */
   27 
   28 #include <octave/oct.h>
   29 #include <octave/oct-stream.h>
   30 #include <octave/ov-struct.h>
   31 #include <octave/Cell.h>
   32 
   33 //#include "../../config.h"             // conflicts with config.h from octave distribution
   34 
   35 #include <string>
   36 #include <pfs.h>
   37 
   38 #if OCTAVE_MAJOR_VERSION < 4 || (OCTAVE_MAJOR_VERSION == 4 && OCTAVE_MINOR_VERSION < 4)
   39   #define isstruct is_map
   40 #endif
   41 
   42 #define SCRIPT_NAME "pfsget"
   43 
   44 static const char *helpString =
   45 #include "pfsget_help.h"
   46 
   47 DEFUN_DLD( pfsget, args, , helpString)
   48 {
   49   octave_value_list retval;
   50     
   51   int nargin = args.length();
   52 
   53   if( nargin != 1 || !args(0).isstruct() )
   54   {
   55     error( SCRIPT_NAME ": Improper usage!");
   56     return retval;
   57   }
   58 
   59   octave_map pfsStream = args(0).map_value();
   60 
   61   octave_map::const_iterator itFH = pfsStream.seek( "FH" );
   62   if( itFH == pfsStream.end() ||
   63     !pfsStream.contents( itFH )(0).is_real_scalar() )
   64   {
   65     error( SCRIPT_NAME ": FH field missing in the structure or it has wrong type");
   66     return retval;
   67   }  
   68   FILE *fh = (FILE*)((long)(pfsStream.contents( itFH )(0).double_value()));
   69 
   70   octave_map::const_iterator itMode = pfsStream.seek( "MODE" );
   71   if( itMode == pfsStream.end() || !pfsStream.contents( itMode )(0).is_string() )
   72   {
   73     error( SCRIPT_NAME ": MODE field missing in the structure or it has wrong type");
   74     return retval;
   75   }
   76   if( pfsStream.contents( itMode )(0).string_value() != "R" ) {
   77     error( SCRIPT_NAME ": Can not read from stream open for writing." );
   78     return retval;    
   79   }
   80 
   81   
   82   pfsStream.del( "channels" );
   83   pfsStream.del( "tags" );
   84   pfsStream.del( "channelTags" );
   85 
   86   
   87   try {
   88     pfs::DOMIO ctx;
   89     pfs::Frame *frame = ctx.readFrame( fh );
   90     if( frame == NULL ) {         // No more frames
   91                                   
   92       pfsStream.assign( "EOF", octave_value(true) );
   93       
   94     } else {                    // Not EOF
   95 
   96       // Set width and height
   97       {
   98         pfsStream.assign( "columns", octave_value(frame->getWidth()) );
   99         pfsStream.assign( "rows", octave_value(frame->getHeight()) ); 
  100       }
  101     
  102       // Add channels as matrices to pfs stream struct
  103       {
  104         octave_scalar_map channels;
  105         
  106         pfs::ChannelIteratorPtr cit( frame->getChannelIterator() );
  107         while( cit->hasNext() ) {
  108           pfs::Channel *ch = cit->getNext();
  109 
  110           Matrix mat( ch->getRows(), ch->getCols() );
  111           int index = 0;
  112           for( int r = 0; r < ch->getRows(); r++ ) // Copy channel data to Octave matrix
  113             for( int c = 0; c < ch->getCols(); c++ ) {
  114               mat(r,c) = (*ch)(index++);
  115             }      
  116 
  117           channels.assign( ch->getName(), mat );
  118           
  119         }
  120         pfsStream.assign( "channels", octave_value(channels) );
  121         
  122       }
  123 
  124       //Add tags
  125       {
  126         octave_scalar_map tags;
  127         
  128         pfs::TagIteratorPtr it( frame->getTags()->getIterator() );        
  129         while( it->hasNext() ) {
  130           const char *tagName = it->getNext();         
  131           tags.assign( tagName, octave_value( frame->getTags()->getString( tagName )) );
  132         }
  133         
  134         pfsStream.assign( "tags", octave_value(tags) );
  135         
  136         octave_scalar_map channelTagList;
  137 
  138         //Copy all channel tags
  139         pfs::ChannelIteratorPtr cit( frame->getChannelIterator() );
  140         while( cit->hasNext() ) {
  141           pfs::Channel *ch = cit->getNext();
  142 
  143           octave_scalar_map channelTags;
  144           
  145           pfs::TagIteratorPtr tit( ch->getTags()->getIterator() );        
  146           while( tit->hasNext() ) {
  147             const char *tagName = tit->getNext();
  148             channelTags.assign( tagName, octave_value(ch->getTags()->getString( tagName )) );
  149           }
  150           channelTagList.assign( ch->getName(), octave_value(channelTags) );
  151         }        
  152         pfsStream.assign( "channelTags", octave_value(channelTagList) );
  153         
  154       }
  155       
  156     
  157     }
  158     ctx.freeFrame( frame );    
  159   }
  160   catch( pfs::Exception ex )
  161   {
  162     error( "%s: %s", SCRIPT_NAME, ex.getMessage() );
  163   }
  164 //    if( fh != stdin ) fclose( fh );    
  165     
  166   retval.append(pfsStream);  
  167   return retval;
  168 }