/****************************** DoSwap *************************** * This is where the work will be done *************************************************************************************/ OSErr DoSwap( FILE *inFile, FILE *outFile ) { OSErr error = noErr; long theTimePt; void *inData, *outData; long bufSize; int elemSize; void *buf; int count; elemSize = sizeof( float ); bufSize = u.id.xs * u.id.ys * elemSize; buf = ck_malloc( bufSize, "swap buffer" ); for( count=0; count<u.id.zs; count++ ) { if( u.Verbose ) { printf( "." ); } error = ck_fread( buf, sizeof( char ), bufSize, inFile ); ILError( error, "DoSwap" ); error = vbyteswap( buf, u.id.xs * u.id.ys, T_FLOAT ); ILError( error, "DoSwap" ); error = ck_fwrite( buf, sizeof( char ), bufSize, outFile ); ILError( error, "DoSwap" ); } return error; }
/* Fill an exhausted line_filter buffer from its INFILE */ static size_t lf_refill (struct line_filter *lf) { size_t s = ck_fread (lf->buffer, SDIFF_BUFSIZE, lf->infile); lf->bufpos = lf->buffer; lf->buflim = lf->buffer + s; lf->buflim[0] = '\n'; checksigs (); return s; }
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 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; }