int main( int argc, char **argv ) { IMAGE im; dsr myDSR; short tmp; FILE *fp; char inName[256], outName[256]; OSErr error = noErr; printf( "%s - Treat times points in an analyze header as slices\n", argv[0] ); printf( "WARNING -- this tool will overwrite the original header\n" ); if( argc == 1 ) { printf( "Enter the name of an analyze image file ( xxx.img ): " ); scanf( "%s", inName ); } else { strcpy( inName, argv[1] ); } error = UC_Readheader( inName, &im ); ILError( error, "reading input header" ); tmp = im.dim.n_slices; im.dim.n_slices = im.dim.timePts; im.dim.timePts = tmp; error = dsrFromIMAGE( &im, &myDSR ); ILError( error, "Creating analyze header" ); strcpy( outName, inName ); sprintf( strrchr( outName, '.' ), ".hdr" ); printf( "Header will be overwritten to %s\n", outName ); error = ck_fopen( &fp, outName, "wb" ); ILError( error, "opening output header" ); error = ck_fwrite( &myDSR, sizeof( dsr ), 1, fp ); ILError( error, "writing analyze header" ); error = ck_fclose( fp ); ILError( error, "closing header file" ); return 0; }
static struct output * get_openfile (struct output **file_ptrs, const char *mode, int fail) { struct buffer *b; char *file_name; struct output *p; b = read_filename(); file_name = get_buffer(b); for (p=*file_ptrs; p; p=p->link) if (strcmp(p->name, file_name) == 0) break; if (posixicity == POSIXLY_EXTENDED) { /* Check whether it is a special file (stdin, stdout or stderr) */ struct special_files *special = special_files; /* std* sometimes are not constants, so they cannot be used in the initializer for special_files */ my_stdin = stdin; my_stdout = stdout; my_stderr = stderr; for (special = special_files; special->outf.name; special++) if (strcmp(special->outf.name, file_name) == 0) { special->outf.fp = *special->pfp; free_buffer (b); return &special->outf; } } if (!p) { p = OB_MALLOC(&obs, 1, struct output); p->name = ck_strdup(file_name); p->fp = ck_fopen(p->name, mode, fail); p->missing_newline = false; p->link = *file_ptrs; *file_ptrs = p; }
OSErr MoreAnalyzeHeader( char *name ) { OSErr error = noErr; FILE *hdrFile; dsr theDSR; error = ck_fopen( &hdrFile, name, "rb" ); RETURNONERROR; error = ck_fread( &theDSR, sizeof( dsr ), 1, hdrFile ); RETURNONERROR; if( theDSR.dime.dim[0] != 4 || // check byte order consistency theDSR.hk.sizeof_hdr != sizeof( dsr ) ) { swapHdrBytes( &theDSR ); } ShowAnalyzeHeader( &theDSR, stdout, name ); error = ck_fclose( hdrFile ); return error; }
/* interpret an edit command */ static bool edit (struct line_filter *left, char const *lname, lin lline, lin llen, struct line_filter *right, char const *rname, lin rline, lin rlen, FILE *outfile) { for (;;) { int cmd0, cmd1; bool gotcmd = false; cmd1 = 0; /* Pacify `gcc -W'. */ while (! gotcmd) { if (putchar ('%') != '%') perror_fatal (_("write failed")); ck_fflush (stdout); cmd0 = skip_white (); switch (cmd0) { case '1': case '2': case 'l': case 'r': case 's': case 'v': case 'q': if (skip_white () != '\n') { give_help (); flush_line (); continue; } gotcmd = true; break; case 'e': cmd1 = skip_white (); switch (cmd1) { case '1': case '2': case 'b': case 'd': case 'l': case 'r': if (skip_white () != '\n') { give_help (); flush_line (); continue; } gotcmd = true; break; case '\n': gotcmd = true; break; default: give_help (); flush_line (); continue; } break; case EOF: if (feof (stdin)) { gotcmd = true; cmd0 = 'q'; break; } /* Fall through. */ default: flush_line (); /* Fall through. */ case '\n': give_help (); continue; } } switch (cmd0) { case '1': case 'l': lf_copy (left, llen, outfile); lf_skip (right, rlen); return true; case '2': case 'r': lf_copy (right, rlen, outfile); lf_skip (left, llen); return true; case 's': suppress_common_lines = true; break; case 'v': suppress_common_lines = false; break; case 'q': return false; case 'e': { int fd; if (tmpname) tmp = fopen (tmpname, "w"); else { if ((fd = temporary_file ()) < 0) perror_fatal ("mkstemp"); tmp = fdopen (fd, "w"); } if (! tmp) perror_fatal (tmpname); switch (cmd1) { case 'd': if (llen) { if (llen == 1) fprintf (tmp, "--- %s %ld\n", lname, (long int) lline); else fprintf (tmp, "--- %s %ld,%ld\n", lname, (long int) lline, (long int) (lline + llen - 1)); } /* Fall through. */ case '1': case 'b': case 'l': lf_copy (left, llen, tmp); break; default: lf_skip (left, llen); break; } switch (cmd1) { case 'd': if (rlen) { if (rlen == 1) fprintf (tmp, "+++ %s %ld\n", rname, (long int) rline); else fprintf (tmp, "+++ %s %ld,%ld\n", rname, (long int) rline, (long int) (rline + rlen - 1)); } /* Fall through. */ case '2': case 'b': case 'r': lf_copy (right, rlen, tmp); break; default: lf_skip (right, rlen); break; } ck_fclose (tmp); { int wstatus; int werrno = 0; ignore_SIGINT = true; checksigs (); { #if ! (HAVE_WORKING_FORK || HAVE_WORKING_VFORK) char *command = xmalloc (quote_system_arg (0, editor_program) + 1 + strlen (tmpname) + 1); sprintf (command + quote_system_arg (command, editor_program), " %s", tmpname); wstatus = system (command); if (wstatus == -1) werrno = errno; free (command); #else pid_t pid; pid = vfork (); if (pid == 0) { char const *argv[3]; int i = 0; argv[i++] = editor_program; argv[i++] = tmpname; argv[i] = 0; execvp (editor_program, (char **) argv); _exit (errno == ENOENT ? 127 : 126); } if (pid < 0) perror_fatal ("fork"); while (waitpid (pid, &wstatus, 0) < 0) if (errno == EINTR) checksigs (); else perror_fatal ("waitpid"); #endif } ignore_SIGINT = false; check_child_status (werrno, wstatus, EXIT_SUCCESS, editor_program); } { char buf[SDIFF_BUFSIZE]; size_t size; tmp = ck_fopen (tmpname, "r"); while ((size = ck_fread (buf, SDIFF_BUFSIZE, tmp)) != 0) { checksigs (); ck_fwrite (buf, size, outfile); } ck_fclose (tmp); } return true; } default: give_help (); break; } } }
int main (int argc, char *argv[]) { int opt; char const *prog; exit_failure = EXIT_TROUBLE; initialize_main (&argc, &argv); program_name = argv[0]; setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); c_stack_action (cleanup); prog = getenv ("EDITOR"); if (prog) editor_program = prog; diffarg (DEFAULT_DIFF_PROGRAM); /* parse command line args */ while ((opt = getopt_long (argc, argv, "abBdEHiI:lo:stvw:W", longopts, 0)) != -1) { switch (opt) { case 'a': diffarg ("-a"); break; case 'b': diffarg ("-b"); break; case 'B': diffarg ("-B"); break; case 'd': diffarg ("-d"); break; case 'E': diffarg ("-E"); break; case 'H': diffarg ("-H"); break; case 'i': diffarg ("-i"); break; case 'I': diffarg ("-I"); diffarg (optarg); break; case 'l': diffarg ("--left-column"); break; case 'o': output = optarg; break; case 's': suppress_common_lines = true; break; case 't': diffarg ("-t"); break; case 'v': version_etc (stdout, "sdiff", PACKAGE_NAME, PACKAGE_VERSION, "Thomas Lord", (char *) 0); check_stdout (); return EXIT_SUCCESS; case 'w': diffarg ("-W"); diffarg (optarg); break; case 'W': diffarg ("-w"); break; case DIFF_PROGRAM_OPTION: diffargv[0] = optarg; break; case HELP_OPTION: usage (); check_stdout (); return EXIT_SUCCESS; case STRIP_TRAILING_CR_OPTION: diffarg ("--strip-trailing-cr"); break; case TABSIZE_OPTION: diffarg ("--tabsize"); diffarg (optarg); break; default: try_help (0, 0); } } if (argc - optind != 2) { if (argc - optind < 2) try_help ("missing operand after `%s'", argv[argc - 1]); else try_help ("extra operand `%s'", argv[optind + 2]); } if (! output) { /* easy case: diff does everything for us */ if (suppress_common_lines) diffarg ("--suppress-common-lines"); diffarg ("-y"); diffarg ("--"); diffarg (argv[optind]); diffarg (argv[optind + 1]); diffarg (0); execvp (diffargv[0], (char **) diffargv); perror_fatal (diffargv[0]); } else { char const *lname, *rname; FILE *left, *right, *out, *diffout; bool interact_ok; struct line_filter lfilt; struct line_filter rfilt; struct line_filter diff_filt; bool leftdir = diraccess (argv[optind]); bool rightdir = diraccess (argv[optind + 1]); if (leftdir & rightdir) fatal ("both files to be compared are directories"); lname = expand_name (argv[optind], leftdir, argv[optind + 1]); left = ck_fopen (lname, "r"); rname = expand_name (argv[optind + 1], rightdir, argv[optind]); right = ck_fopen (rname, "r"); out = ck_fopen (output, "w"); diffarg ("--sdiff-merge-assist"); diffarg ("--"); diffarg (argv[optind]); diffarg (argv[optind + 1]); diffarg (0); trapsigs (); #if ! (HAVE_WORKING_FORK || HAVE_WORKING_VFORK) { size_t cmdsize = 1; char *p, *command; int i; for (i = 0; diffargv[i]; i++) cmdsize += quote_system_arg (0, diffargv[i]) + 1; command = p = xmalloc (cmdsize); for (i = 0; diffargv[i]; i++) { p += quote_system_arg (p, diffargv[i]); *p++ = ' '; } p[-1] = 0; errno = 0; diffout = popen (command, "r"); if (! diffout) perror_fatal (command); free (command); } #else { int diff_fds[2]; # if HAVE_WORKING_VFORK sigset_t procmask; sigset_t blocked; # endif if (pipe (diff_fds) != 0) perror_fatal ("pipe"); # if HAVE_WORKING_VFORK /* Block SIGINT and SIGPIPE. */ sigemptyset (&blocked); sigaddset (&blocked, SIGINT); sigaddset (&blocked, SIGPIPE); sigprocmask (SIG_BLOCK, &blocked, &procmask); # endif diffpid = vfork (); if (diffpid < 0) perror_fatal ("fork"); if (! diffpid) { /* Alter the child's SIGINT and SIGPIPE handlers; this may munge the parent. The child ignores SIGINT in case the user interrupts the editor. The child does not ignore SIGPIPE, even if the parent does. */ if (initial_handler (handler_index_of_SIGINT) != SIG_IGN) signal_handler (SIGINT, SIG_IGN); signal_handler (SIGPIPE, SIG_DFL); # if HAVE_WORKING_VFORK /* Stop blocking SIGINT and SIGPIPE in the child. */ sigprocmask (SIG_SETMASK, &procmask, 0); # endif close (diff_fds[0]); if (diff_fds[1] != STDOUT_FILENO) { dup2 (diff_fds[1], STDOUT_FILENO); close (diff_fds[1]); } execvp (diffargv[0], (char **) diffargv); _exit (errno == ENOENT ? 127 : 126); } # if HAVE_WORKING_VFORK /* Restore the parent's SIGINT and SIGPIPE behavior. */ if (initial_handler (handler_index_of_SIGINT) != SIG_IGN) signal_handler (SIGINT, catchsig); if (initial_handler (handler_index_of_SIGPIPE) != SIG_IGN) signal_handler (SIGPIPE, catchsig); else signal_handler (SIGPIPE, SIG_IGN); /* Stop blocking SIGINT and SIGPIPE in the parent. */ sigprocmask (SIG_SETMASK, &procmask, 0); # endif close (diff_fds[1]); diffout = fdopen (diff_fds[0], "r"); if (! diffout) perror_fatal ("fdopen"); } #endif lf_init (&diff_filt, diffout); lf_init (&lfilt, left); lf_init (&rfilt, right); interact_ok = interact (&diff_filt, &lfilt, lname, &rfilt, rname, out); ck_fclose (left); ck_fclose (right); ck_fclose (out); { int wstatus; int werrno = 0; #if ! (HAVE_WORKING_FORK || HAVE_WORKING_VFORK) wstatus = pclose (diffout); if (wstatus == -1) werrno = errno; #else ck_fclose (diffout); while (waitpid (diffpid, &wstatus, 0) < 0) if (errno == EINTR) checksigs (); else perror_fatal ("waitpid"); diffpid = 0; #endif if (tmpname) { unlink (tmpname); tmpname = 0; } if (! interact_ok) exiterr (); check_child_status (werrno, wstatus, EXIT_FAILURE, diffargv[0]); untrapsig (0); checksigs (); exit (WEXITSTATUS (wstatus)); } } return EXIT_SUCCESS; /* Fool `-Wall'. */ }
int G2A (int argc, char *argv[] ) { char origname[128]; // original name char newname [128]; // renumbered name char command [255]; // command buffer char *TempName; char *numstring; char ImageName[128]; int counter = 0; int number; char *p; char nameFileName[255]; char analyzeFileName[255]; char headerFileName[255]; char baseName[255]; short verbose = true; FILE *nameFile; FILE *TempFile; FILE *dataFile; FILE *procFile; FILE *headerFile; FILE *inFile; int i; IMAGE im; short count; short *dataBuff; Boolean DebugMode = false; static char id[] = "$Revision: 1.21 $$Date: 2006/06/09 18:40:34 $"; OSErr error = noErr; if( argc < 2 ) { print_usage( argv[0] ); } // setup an output file name, based on the last input file name sprintf( baseName, "%s", argv[argc-1] ); // output file name p = strrchr( baseName, 'I' ); *p = '\0'; // null terminate error = OpenProcFile( argc, argv, baseName, id, &procFile ); ILError( error, "Opening Proc file" ); sprintf( nameFileName, "%s.NAME_FILE", baseName ); fprintf( procFile, "Associated name file ... %s\n", nameFileName ); sprintf( headerFileName, "%s.GenesisHdrs", baseName ); fprintf( procFile, "Associated headers file ... %s\n", headerFileName ); // corrected names will be stored temporarily in nameFile error = CreateTempFile( &TempFile, &TempName ); ILError( error, TempName ); // use UC_ReadHeader to determine image dimensions, etc... error = UC_Readheader( argv[argc-1], &im ); if( error ) ILError( error, "Read Image" ); // rename the files and create name list into the file called TempName for( i=argc-1; i>0; i-- ) { strcpy( origname, argv[i] ); strcpy( newname, argv[i] ); numstring = strrchr( newname, 'I' ) + 1; number = atoi( numstring ); sprintf( numstring, "%0.3d.MR", number ); fprintf( TempFile, "%s\n", newname ); if( strcmp( origname, newname ) || DebugMode ) { if( verbose ) { printf( "i:%d\t%s --> %s\n", i, origname, newname ); } sprintf( command, "mv %s %s\n", origname, newname ); fprintf( procFile, "\tRenamed %s to %s\n", origname, newname ); system( command ); } } error = ck_fclose( TempFile ); ILError( error, TempName ); // Sort the contents of the temporary nameFile and place it into the permanent version sprintf( command, "sort %s > %s", TempName, nameFileName ); system( command ); if( false ) { DBG( command ); printf( "\nContents of name file\n" ); sprintf( command, "cat %s", nameFileName ); DBG( command ); system( command ); } // now, place the name of the header file at the beginning of a file */ error = CreateTempFile( &TempFile, &TempName ); ILError ( error, TempName ); fprintf( TempFile, "%s\n", headerFileName ); error = ck_fclose( TempFile ); ILError( error, TempName ); // add the file names to the end sprintf( command, "cat %s >> %s\n", nameFileName, TempName ); system( command ); // and rename this file sprintf( command, "mv %s %s\n", TempName, nameFileName ); system( command ); if( false ) { printf( "new Contents of name file" ); sprintf( command, "cat %s", nameFileName ); DBG( command ); system( command ); } im.dim.n_slices = argc - 1; error = CreateHeader( ANALYZE, &im, baseName ); if( error ) ILError( error, "Writing Header" ); sprintf( analyzeFileName, "%s.img", baseName ); error = ck_fopen( &dataFile, analyzeFileName, "wb" ); ILError( error, "data file" ); error = ck_fopen( &nameFile, nameFileName, "rb" ); ILError( error, "name file" ); error = ck_fopen( &headerFile, headerFileName, "wb" ); ILError( error, "header file" ); if( im.dim.x * im.dim.y * sizeof(short) < SIGHDRSIZE ) { dataBuff = (short *)ck_malloc( SIGHDRSIZE, "data buffer" ); } else { dataBuff = (short *)ck_malloc( im.dim.x * im.dim.y * sizeof(short), "data buffer" ); } count = fscanf( nameFile, "%s", headerFileName ); // just read past this. count = fscanf( nameFile, "%s", ImageName ); if( false ) { DBG( ImageName ); } printf( "\n\tPatience for a moment\n" ); while( count>0 ) { printf( "%d of %d complete\n", counter++, argc - 1 ); error = ck_fopen( &inFile, ImageName, "rb" ); ILError( error, "Opening image" ); error = ck_fread ( dataBuff, sizeof( char ), SIGHDRSIZE, inFile ); ILError( error, "Reading header" ); error = ck_fwrite( dataBuff, sizeof( char ), SIGHDRSIZE, headerFile ); ILError( error, "Writing image header" ); error = ck_fread ( dataBuff, sizeof( short ), im.dim.x * im.dim.y, inFile ); ILError( error, "Reading image data" ); error = ck_fwrite( dataBuff, sizeof( short ), im.dim.x * im.dim.y, dataFile ); ILError( error, "Writing image data" ); ck_fclose( inFile ); count = fscanf( nameFile, "%s", ImageName ); } printf( "\n\nCreated Analyze (4D) file: %s\n", analyzeFileName ); printf( "\tSave these files!: %s and %s\n\n", nameFileName, headerFileName ); printf( "To create corresponding genesis images from an analyze file named XXX.img, use:\n" ); printf( "\tAnalyze2Genesis -n %s -i XXX.img\n", nameFileName ); free( dataBuff ); error = ck_fclose( headerFile ); if( error ) ILError( error, headerFileName ); error = ck_fclose( nameFile ); if( error ) ILError( error, nameFileName ); error = ck_fclose( dataFile ); if( error ) ILError( error, analyzeFileName ); error = ck_fclose( procFile ); if( error ) ILError( error, "proc file" ); return error; }
OSErr RunMain( int argc, char **argv, char *id ) { OSErr error = noErr; error = InitializeUserVariables(); RETURNONERROR; error = ProcessCommandLine( argc, argv ); RETURNONERROR; error = OpenProcFile( argc, argv, u.OutFileName, id, &u.ProcFile ); RETURNONERROR; /* read in the input image header */ error = UC_Readheader( u.InFileName, &u.inIm ); RETURNONERROR; memcpy( &u.outIm, &u.inIm, sizeof( IMAGE )); // copy the input header to the output header u.inIm.data_type = T_FLOAT; // all operations on float error = OutBaseNameFromOutName( u.OutFileName, u.OutBaseName ); RETURNONERROR; error = ck_fopen( &u.inFile, u.InFileName, "rb" ); RETURNONERROR; error = ck_fopen( &u.TEFile, u.TEFileName, "r" ); RETURNONERROR; error = FindOutputType( u.OutFileName, &u.outIm.file_type, &u.outIm.data_type ); RETURNONERROR; // Only floating point output types can be supported at this time. if( u.outIm.file_type == ANALYZE ) { u.outIm.data_type = T_FLOAT; } if( u.outIm.data_type != T_FLOAT ) { printf( "Only floating point output types are accepted\n" ); return 0; } error = ck_fopen( &u.outFile, u.OutFileName, "wb" ); RETURNONERROR; /* do something */ error = T2Fit( u.inFile, u.outFile ); RETURNONERROR; error = ck_fclose( u.TEFile ); RETURNONERROR; error = ck_fclose( u.inFile ); RETURNONERROR; error = ck_fclose( u.outFile ); RETURNONERROR; /* Change elements of output IMAGE (e.g., number of images/slices/timePts ) */ error = AdjustOutputIMAGE( &u.outIm ); RETURNONERROR; error = CreateHeader( u.outIm.file_type, &u.outIm, u.OutBaseName ); RETURNONERROR; /* dump processing file contents */ ck_fclose( u.ProcFile ); return error; }