1 /*****************************************************************************
2 Major portions of this software are copyrighted by the Medical College
3 of Wisconsin, 1994-2000, and are released under the Gnu General Public
4 License, Version 2. See the file README.Copyright for details.
5 ******************************************************************************/
6
7 #include "mrilib.h"
8 #include <string.h>
9
10 static byte map[26] =
11 { 30, 50, 70, 90, 106, 118, 130, 140, 146, 152, 158, 164, 170,
12 176, 182, 190, 198, 206, 212, 218, 224, 230, 236, 242, 248, 254 } ;
13
14 static byte bk[26] ;
15
16 static char * alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
17 static char * num = "0123456789" ;
18
19 #define NOUT 61
20
main(int argc,char * argv[])21 int main( int argc , char * argv[] )
22 {
23 MRI_IMAGE * im ;
24 byte * bp , bb ;
25 int ii , jj , nn , rr , kk , nlin=0 ;
26 char out[NOUT+1] , zout[NOUT+1] , cc,nc , *nam ;
27
28 if( argc < 2 ){ fprintf(stderr,"Usage: to26 NAME input.pgm > output.26\n"); exit(0); }
29
30 nam = argv[1] ;
31 im = mri_read( argv[2] ) ; if( im == NULL ) exit(1) ;
32 bp = MRI_BYTE_PTR(im) ;
33
34 for( ii=0 ; ii < 25 ; ii++ ) bk[ii] = (map[ii]+map[ii+1])/2 ;
35 bk[25] = 255 ;
36
37 fprintf(stderr,"image dimensions = %d %d\n",im->nx,im->ny) ;
38
39 nn = 0 ; rr = 0 ;
40 printf( "#undef NX_%s\n",nam) ;
41 printf( "#undef NY_%s\n",nam) ;
42 printf( "#define NX_%s %d\n",nam,im->nx) ;
43 printf( "#define NY_%s %d\n",nam,im->ny) ;
44 printf( "static char * BAR_%s[] = {\n" , nam ) ;
45 for( ii=0 ; ii < im->nvox ; ii++ ){
46 bb = bp[ii] ;
47 for( jj=0 ; jj < 26 ; jj++ ) if( bb <= bk[jj] ) break ;
48
49 out[nn++] = (jj<26) ? alpha[jj] : 'Z' ;
50
51 if( nn == NOUT && ii < im->nvox-1 ){
52 out[nn] = '\0' ;
53 cc = out[0] ; rr = 1 ; kk = 0 ;
54 for( jj=1 ; jj <= nn ; jj++ ){
55 nc = out[jj] ;
56 if( nc == cc ){ /* same character */
57 if( rr == 9 ){ zout[kk++] = num[rr] ; zout[kk++] = cc ; rr = 0 ; }
58 rr++ ;
59 } else { /* new character */
60 if( rr == 1 ){
61 zout[kk++] = cc ; rr = 1 ;
62 } else if( rr == 2 ){
63 zout[kk++] = cc ; zout[kk++] = cc ; rr = 1 ;
64 } else {
65 zout[kk++] = num[rr] ; zout[kk++] = cc ; rr = 1 ;
66 }
67 }
68 cc = nc ;
69 }
70 zout[kk] = '\0' ;
71 printf(" \"%s\",\n",zout) ; nlin++ ; nn = 0 ;
72 }
73 }
74
75 out[nn] = '\0' ;
76 printf(" \"%s\"\n};\n",out) ; nlin++ ;
77 printf("#undef NLINE_%s\n",nam) ;
78 printf("#define NLINE_%s %d\n",nam,nlin) ;
79 exit(0) ;
80 }
81