1 #include "mrilib.h"
2 
images_equal(MRI_IMARR * aar,MRI_IMARR * bar)3 static int images_equal( MRI_IMARR *aar , MRI_IMARR *bar )
4 {
5    int nima = IMARR_COUNT(aar) ;
6    int nimb = IMARR_COUNT(bar) ;
7    int ii ;
8 
9    if( nima != nimb ) return 0 ;
10 
11    for( ii=0 ; ii < nima ; ii++ ){
12      if( mri_equal( IMARR_SUBIM(aar,ii) , IMARR_SUBIM(bar,ii) ) == 0 ) return 0;
13    }
14 
15    return 1 ;
16 }
17 
18 /*----------------------------------------------------------------------------*/
19 
main(int argc,char * argv[])20 int main( int argc , char *argv[] )
21 {
22    MRI_IMARR **imar ;
23    int num_imar , iopt=1 , jj,kk , ngood ;
24 
25    if( argc < 2 || strcasecmp(argv[1],"-help") == 0 ){
26      printf(
27        "Usage: uniq_images fileA fileB ...\n"
28        "\n"
29        "* Simple program to read in a list of image filenames,\n"
30        "  determine which files have unique images inside, and\n"
31        "  echo out only a list of the filenames with unique images.\n"
32        "* This program is meant for use in scripts that deal with DICOM\n"
33        "  servers that sometimes deal out multiple copies of the same\n"
34        "  image in different filenames :-(\n"
35        "* Author: Zhark the Comparator, October 2015.\n"
36      ) ;
37      exit(0) ;
38    }
39 
40    mainENTRY("uniq_images") ; machdep() ;
41 
42    /* no options at this time, so leave iopt=1 */
43 
44    if( iopt+1 > argc )
45      ERROR_exit("uniq_images: Need at least 1 filename, but don't have even that!") ;
46 
47    num_imar = argc - iopt ;
48    imar     = (MRI_IMARR **)malloc(sizeof(MRI_IMARR *)*num_imar) ;
49    for( ngood=jj=0 ; jj < num_imar ; jj++ ){
50      imar[jj] = mri_read_file( argv[jj+iopt] ) ;
51      if( imar[jj] == NULL ){
52        ERROR_message("Can't read from file %s",argv[jj+iopt]) ;
53      } else {
54        ngood++ ;
55      }
56    }
57 
58    if( ngood < 1 )
59      ERROR_exit("Didn't get at least 1 input file :-(") ;
60 
61    for( jj=0 ; jj < num_imar-1 ; jj++ ){
62      if( imar[jj] == NULL ) continue ;
63      for( kk=jj+1 ; kk < num_imar ; kk++ ){
64        if( imar[kk] == NULL ) continue ;
65        if( images_equal(imar[jj],imar[kk]) ) DESTROY_IMARR(imar[kk]) ;
66      }
67    }
68 
69    for( jj=0 ; jj < num_imar ; jj++ ){
70      if( imar[jj] != NULL ){
71        printf("%s ",argv[jj+iopt]) ;
72        DESTROY_IMARR(imar[jj]) ;
73      }
74    }
75    printf("\n") ;
76 
77    exit(0) ;
78 }
79