1 #include "mrilib.h"
2 
3 #define VERSION_STRING "<center><small>galler v0.1 - RWCox</small></center>\n"
4 
5 typedef struct { char name[64] ; int *vpt , retab,bot,top ; } cvar ;
6 
7 static int thumbsize = 120 ;
8 static int imagesize = 900 ;
9 static int rowmax    = 7 ;
10 
11 static cvar clist[] = {
12  { "thumbsize" , &thumbsize , 1 , 50,500 } ,
13  { "imagesize" , &imagesize , 0 , 50,2000} ,
14  { "rowmax"    , &rowmax    , 1 ,  2,10  } ,
15  { "\0"        , NULL       , 0 ,  0,0   }
16 } ;
17 
18 static int tabled = 0 ;
19 static int tr     = 0 ;
20 static int tc     = 0 ;
21 
22 static float gam  = 1.0f ;
23 
24 static char imagename[1024] ;
25 static char thumbname[1024] ;
26 
27 static int  nx_im , ny_im , nx_th , ny_th ;
28 
29 static void GAL_imageize( char *iname, char *prefix , int lab ) ;
30 
31 #define CLOSEROW                        \
32  do{ if( tabled && tr ){                 \
33        fprintf(ofp,"</tr>\n\n"); tr=tc=0; \
34      }} while(0)
35 
36 #define OPENTABLE                                   \
37  do{ fprintf(ofp,"\n<center>\n"                      \
38        "<table cellspacing='2' cellpadding='2'>\n") ; \
39      tabled = 1 ; tr = tc = 0 ;                        \
40  } while(0)
41 
42 #define CLOSETABLE                                           \
43  do{ if(tabled){ CLOSEROW ;                                   \
44        fprintf(ofp,"</table>\n</center>\n<hr>\n"); tabled = 0; \
45      }} while(0)
46 
47 #define HEADERIZE                                                    \
48  do{ if( !head_done ){                                               \
49        int hh ;                                                      \
50        fprintf(ofp, "<html>\n") ;                                    \
51        if( num_tst > 0 ){                                            \
52          fprintf(ofp,"<head><title>") ;                              \
53          for( hh=0; hh < num_tst; hh++ ) fprintf(ofp," %s",tst[hh]); \
54          fprintf(ofp,"</title></head>\n") ;                          \
55        }                                                             \
56        fprintf(ofp, "<body") ;                                       \
57        for( hh=0 ; hh < num_bst ; hh++ ) fprintf(ofp," %s",bst[hh]); \
58        fprintf(ofp," >\n") ;                                         \
59        head_done = 1 ;                                               \
60      }} while(0)
61 
62 /*-----------------------------------------------------------------------*/
63 
main(int argc,char * argv[])64 int main( int argc , char *argv[] )
65 {
66    NI_str_array *sar=NULL ;
67    FILE *ifp=stdin , *ofp=stdout ;
68    int iarg=1 , ii , nim=0 ;
69    char linbuf[65536] , *cpt , *prefix , *inam,*tnam ;
70    int head_done=0 , tdperc ;
71    int num_bst=0 ; char *bst[666] ;
72    int num_tst=0 ; char *tst[666] ;
73 
74    /* - - - - - - - - - - */
75 
76    if( argc < 3 || strcasecmp(argv[1],"-help") == 0 ){
77      printf("Usage: galler infile outdirectory\n") ;
78      printf(
79        "Creates an HTML thumbnail image gallery in 'outdirectory',\n"
80        "using the specification in 'infile', which consists of\n"
81        "a series of 1 line commands and file specifications.\n"
82      ) ;
83      exit(0) ;
84    }
85 
86    /* - - - - - - - - - - */
87 
88    while( iarg < argc && argv[iarg][0] == '-' && argv[iarg][1] != '\0' ){
89      ERROR_exit("This program has no options") ;
90    }
91 
92    /* - - - - - - - - - - */
93 
94    if( strcmp(argv[iarg],"-") != 0 ){
95      ifp = fopen( argv[iarg] , "r" ) ;
96      if( ifp == NULL ) ERROR_exit("Can't open input file '%s'",argv[iarg]);
97      INFO_message("Opened input file '%s'",argv[iarg]) ;
98    }
99    iarg++ ;
100 
101    /* - - - - - - - - - - */
102 
103    prefix = argv[iarg] ;
104    if( THD_is_ondisk(prefix) ){
105      if( !THD_is_directory(prefix) )
106        ERROR_exit("Prefix '%s' already exists, but not a directory!",prefix);
107    } else {
108      ii = THD_mkdir(prefix) ;
109      if( ii == 0 ) ERROR_exit("Can't create directory '%s'",prefix);
110      INFO_message("Created directory '%s'",prefix) ;
111    }
112 
113    /* - - - - - - - - - - */
114 
115    sprintf(imagename,"%-1.999s/index.html",prefix) ;
116    ofp = fopen( imagename , "w" ) ;
117    if( ofp == NULL ) ERROR_exit("Can't open file '%s' for output",imagename);
118 
119    /* - - - - - - - - - - */
120 
121 #if 0
122    fprintf(ofp,"<html>\n"
123                "<body bgcolor='#ffeebb'>\n"
124           ) ;
125    head_done = 1 ;
126 #endif
127 
128    /* - - - - - - - - - - */
129 
130  GetLine:
131    cpt = afni_fgets( linbuf , 65536 , ifp ) ;
132    if( cpt == NULL ) goto CleanUp ;
133  GotLine:
134    if( sar != NULL ) NI_delete_str_array(sar) ;
135    sar = NI_decode_string_list( linbuf , "~" ) ;
136    if( sar == NULL || sar->num < 1 ){ CLOSEROW ; goto GetLine ; }
137    if( sar->str[0][0] == '#' ){
138      char *dpt ; int ndpt ;
139      CLOSEROW ;
140      if( sar->str[0][1] == '\0' ){
141        if( sar->num < 2 ) goto GetLine ;
142        cpt = sar->str[1] ;
143        dpt = (sar->num > 2) ? sar->str[2] : NULL ;
144        ndpt = 2 ;
145      } else {
146        cpt = sar->str[0]+1 ;
147        dpt = (sar->num > 1) ? sar->str[1] : NULL ;
148        ndpt = 1 ;
149      }
150      if( dpt != NULL ){
151        int val=(int)strtod(dpt,NULL) , didit=0 ;
152        for( ii=0 ; clist[ii].vpt != NULL ; ii++ ){
153          if( strcasecmp(cpt,clist[ii].name) == 0 ){
154                 if( val < clist[ii].bot ) val = clist[ii].bot ;
155            else if( val > clist[ii].top ) val = clist[ii].top ;
156            *(clist[ii].vpt) = val ;
157            if( clist[ii].retab ) CLOSETABLE ;
158            INFO_message("Set variable %s = %d",clist[ii].name,val) ;
159            didit = 1 ; break ;
160          }
161        }
162        if( didit ) goto GetLine ;
163      }
164      if( strncasecmp(cpt,"cop",3)==0 || strncasecmp(cpt,"lit",3)==0 ){
165        HEADERIZE ; CLOSETABLE ;
166        while(1){
167          cpt = afni_fgets( linbuf , 65536 , ifp ) ;
168          if( cpt == NULL ) goto CleanUp ;
169          for( ; isspace(*cpt) ; cpt++ ) ; /* nada */
170          if( *cpt == '#' ) goto GotLine ;
171          fprintf(ofp,"%s",cpt) ;
172        }
173      } else if( strcasecmp(cpt,"hr")==0 ){
174        HEADERIZE ;
175        if( tabled ) CLOSETABLE ;
176        else         fprintf(ofp,"<hr>\n") ;
177      } else if( strncasecmp(cpt,"newtab",6)==0 ){
178        HEADERIZE ;
179        CLOSETABLE ;
180      } else if( strncasecmp(cpt,"body",4)==0 && dpt != NULL ){
181        for( ii=ndpt ; ii < sar->num ; ii++ )
182          bst[num_bst++] = strdup(sar->str[ii]) ;
183      } else if( strncasecmp(cpt,"titl",4)==0 && dpt != NULL ){
184        for( ii=ndpt ; ii < sar->num ; ii++ )
185          tst[num_tst++] = strdup(sar->str[ii]) ;
186      } else if( strncasecmp(cpt,"gam",3)==0 && dpt != NULL ){
187        float qam=(float)strtod(dpt,NULL) ;
188        if( qam > 0.0f ){ gam = qam; INFO_message("Set gamma = %f",gam); }
189      }
190      goto GetLine ;
191    }
192 
193    HEADERIZE ;
194    if( !tabled ) OPENTABLE ;
195 
196    if( !tr ){
197      fprintf(ofp,"<tr>\n") ; tr = 1 ;
198    }
199 
200    INFO_message("Processing image '%s'",sar->str[0]) ;
201    GAL_imageize( sar->str[0] , prefix , ++nim ) ;
202    if( imagename[0] == '\0' || thumbname[0] == '\0' ){
203      WARNING_message("Can't process image '%s'",sar->str[0]) ;
204      --nim ; goto GetLine ;
205    }
206    inam = THD_trailname(imagename,0) ;
207    tnam = THD_trailname(thumbname,0) ;
208    tdperc = (int)(100.0/rowmax) ;
209    fprintf(ofp,
210            "<td align='center' width='%d%%'> "
211            "<a href='%s'><img src='%s' width='%d' height='%d' /></a>" ,
212            tdperc , inam , tnam , nx_th , ny_th ) ;
213    if( sar->num > 1 ){
214     fprintf(ofp,"<br><small>\n") ;
215     for( ii=1 ; ii < sar->num ; ii++ ) fprintf(ofp," %s",sar->str[ii]) ;
216     fprintf(ofp,"</small>\n") ;
217    }
218    fprintf(ofp,"</td>\n") ;
219    if( ++tc >= rowmax ) CLOSEROW ;
220    goto GetLine ;
221 
222    /* - - - - - - - - - - */
223 
224  CleanUp:
225    CLOSETABLE ;
226    if( head_done )
227      fprintf(ofp,
228               VERSION_STRING
229               "</body>\n"
230               "</html>\n") ;
231    else
232      WARNING_message("No Web page output?!") ;
233 
234    INFO_message("Program done") ; exit(0) ;
235 }
236 
237 /*-----------------------------------------------------------------------*/
238 
GAL_imageize(char * iname,char * prefix,int lab)239 static void GAL_imageize( char *iname, char *prefix , int lab )
240 {
241    MRI_IMAGE *inim , *qim , *bim ;
242    int nx,ny , nxnew , nynew ;
243 
244    imagename[0] = thumbname[0] = '\0' ;
245 
246    if( iname  == NULL || *iname  == '\0' ) return ;
247    if( prefix == NULL || *prefix == '\0' ) prefix = "." ;
248 
249    inim = mri_read( iname ) ; if( inim == NULL ) return ;
250    if( inim->kind != MRI_rgb ){
251      qim = mri_to_rgb(inim) ; mri_free(inim) ; inim = qim ;
252    }
253    if( gam != 1.0f ){
254      ININFO_message(" -Processing gamma") ;
255      mri_gamma_rgb_inplace( gam , inim ) ;
256    }
257 
258    nx = inim->nx ; ny = inim->ny ;
259 
260    sprintf(imagename,"%-1.999s/Im%04d.jpg",prefix,lab) ;
261    if( nx <= imagesize && ny <= imagesize ){
262      mri_write_jpg( imagename , inim ) ;
263      nx_im = inim->nx ; ny_im = inim->ny ;
264    } else {
265      float fx , fy ;
266      fx = imagesize / (float)nx ; fy = imagesize / (float)ny ;
267      fx = MIN(fx,fy) ; nxnew = (int)(nx*fx); nynew = (int)(ny*fx) ;
268      if( fx < 0.95f ){
269        float sigma = 0.3456789f/fx ;
270        ININFO_message(" -Antialias filter sigma=%.2f",sigma) ;
271        bim = mri_rgb_blur2D( sigma , inim ) ;
272      }
273      else bim = inim ;
274      ININFO_message(" -Resizing to %d x %d",nxnew,nynew) ;
275      qim = mri_resize( bim , nxnew , nynew ) ;
276      nx_im = qim->nx ; ny_im = qim->ny ;
277      mri_write_jpg( imagename , qim ) ;
278      mri_free(qim) ; if( bim != inim ) mri_free(bim) ;
279    }
280 
281    sprintf(thumbname,"%-1.999s/Th%04d.jpg",prefix,lab) ;
282    if( nx <= thumbsize && ny <= thumbsize ){
283      mri_write_jpg( thumbname , inim ) ;
284      nx_th = inim->nx ; ny_th = inim->ny ;
285    } else {
286      float fx , fy ;
287      fx = thumbsize / (float)nx ; fy = thumbsize / (float)ny ;
288      fx = MIN(fx,fy) ; nxnew = (int)(nx*fx); nynew = (int)(ny*fx) ;
289      ININFO_message(" - Making thumbnail %d x %d",nxnew,nynew) ;
290      qim = mri_resize( inim , nxnew , nynew ) ;
291      nx_th = qim->nx ; ny_th = qim->ny ;
292      mri_write_jpg( thumbname , qim ) ; mri_free(qim) ;
293    }
294 
295    mri_free(inim) ; return ;
296 }
297