1 #include "mrilib.h"
2 
main(int argc,char * argv[])3 int main( int argc , char *argv[] )
4 {
5    MRI_IMARR *iar ;
6    MRI_IMAGE *im ;
7    NI_stream ns=NULL ;
8    NI_element *nel ;
9    int ii , iarg=1 ;
10    int nb=0 ;
11 
12    if( argc < 2 || strcmp(argv[1],"-help") == 0 ){
13      printf("Usage: im2niml imagefile [imagefile ...]\n"
14             "Converts the input image(s) to a text-based NIML element\n"
15             "and writes the result to stdout.  Sample usage:\n"
16             " aiv -p 4444 &\n"
17             " im2niml zork.jpg | nicat tcp:localhost:4444\n"
18             "-- Author: RW Cox.\n"
19            ) ;
20      exit(0) ;
21    }
22 
23    ns = NI_stream_open( "stdout:" , "w" ) ;
24    if( ns == NULL ) ERROR_exit("Can't open stdout?!") ;
25    for( ; iarg < argc ; iarg++ ){
26      iar = mri_read_file( argv[iarg] ) ;
27      if( iar == NULL ){
28        ERROR_message("Can't read file %s",argv[iarg]); continue;
29      }
30      for( ii=0 ; ii < IMARR_COUNT(iar) ; ii++ ){
31        im = IMARR_SUBIM(iar,ii) ; nel = mri_to_niml(im) ; mri_free(im) ;
32        if( nel == NULL ){
33          ERROR_message("Can't process %s[%d]",argv[iarg],ii); continue;
34        }
35        nb += NI_write_element( ns , nel , NI_TEXT_MODE ) ;
36        NI_free_element(nel) ;
37      }
38      FREE_IMARR(iar) ;
39    }
40    NI_stream_closenow(ns) ;
41    INFO_message("Wrote %d bytes to stdout",nb) ;
42    exit(0) ;
43 }
44