00001 function [err, ErrMessage, Info, V] = ResliceBrick (BrickName, NewCode, Opt)
00002 %
00003 % [err, ErrMessage, Info, M] = ResliceBrick (BrickName, NewCode, [Opt])
00004 %
00005 %Purpose:
00006 % Reslices an AFNI brick kinda a la 3daxialize but in any orientation desired
00007 % No interpolation or shananigans are done
00008 %
00009 %Input Parameters:
00010 % BrickName the name of the afni brick
00011 % NewCode The orientation code to write the new brick as 'RPS' or 'RAI'
00012 % or the name of a brick to reslice the data as. The new code is extracted from
00013 % that brick's header.
00014 % Opt is an optional options structure with the following fields
00015 % .Prefix : (optional) prefix of the output brick. Default is ''
00016 % in which case, the output prefix is the input prefix_<NewCode>
00017 % .WriteBrick : 0/[1] optional flag for writing the brick to disk
00018 % you may choose not to if you are passing it back to the
00019 % calling function.
00020 %
00021 %Output Parameters:
00022 % err : 0 No Problem
00023 % : 1 Mucho Problems
00024 % ErrMessage: a string containing warnings or error messages
00025 % Info : The header of the resliced brick
00026 % M : the resliced brick
00027 %
00028 %Key Terms:
00029 %
00030 %More Info :
00031 %
00032 % example: The images making up data set AMzsspgrco+orig were acquired coronally.
00033 % In certain instances (especially those involving 3dvolreg), you need to have
00034 % the images making up the different volumes to be in aqcuired in the same plane
00035 % and orientation. In this example, we reslice AMzsspgrco+orig to look like its images
00036 % were acquired just like those making up AHzsspgrax+orig (axial, LAI)
00037 %
00038 % [err, ErrMessage] = ResliceBrick ('AMzsspgrco+orig.BRIK', 'AHzsspgrax+orig');
00039 %
00040 % Author : Ziad Saad
00041 % Date : Fri May 11 14:10:50 PDT 2001 / latest update Oct 11 01
00042 % LBC/NIMH/ National Institutes of Health, Bethesda Maryland
00043
00044
00045 %Define the function name for easy referencing
00046 FuncName = 'ResliceBrick';
00047
00048 %Debug Flag
00049 DBG = 1;
00050
00051 %initailize return variables
00052 err = 1;
00053 ErrMessage = '';
00054
00055 %make sure Code is OK
00056 tmp = WordNumber(AllOrient , NewCode, '|');
00057 if (isempty(tmp)),
00058 %maybe it's a Brick
00059 CodeBrick = RemoveExtension(NewCode, '.HEAD|.BRIK');
00060 CodeBrick = sprintf('%s.HEAD', CodeBrick);
00061 if (exist(CodeBrick) == 2),
00062 [err,InfoCode] = BrikInfo (CodeBrick);
00063 if (err), ErrMessage = sprintf('Error %s subfunction: %s has a bad header', FuncName, CodeBrick ); return; end
00064 NewCode = InfoCode.ORIENT_SPECIFIC;
00065 %change it to string
00066 [err, NewCode] = AFNI_OrientCode (NewCode);
00067 else
00068 ErrMessage = sprintf('Error %s subfunction: %s . %s not a valid orientation code nore a brick.' ,FuncName, ErrMessage, NewCode); return;
00069 end
00070 end
00071
00072 %Clean up the input Brick's header
00073 BrickName = RemoveExtension(BrickName, '.HEAD|.BRIK');
00074 [BrickName_Prefix, BrickName_View] = RemoveExtension(BrickName, '+orig|+acpc|+tlrc');
00075
00076 %make sure Opt is OK
00077 if (nargin == 2),
00078 Opt.Prefix = '';
00079 Opt.verbose = 1;
00080 Opt.WriteBrick = 1;
00081 else
00082 if (~isfield(Opt, 'Prefix') | isempty(Opt.Prefix)), Opt.Prefix = ''; end
00083 if (~isfield(Opt, 'verbose') | isempty(Opt.verbose)), Opt.verbose = 1; end
00084 if (~isfield(Opt, 'WriteBrick') | isempty(Opt.WriteBrick)), Opt.WriteBrick = 1; end
00085 end
00086
00087 if ((~isfield(Opt,'WriteBrick') | ~Opt.WriteBrick) & nargout < 4),
00088 warndlg(sprintf('Warning %s: From the number of output arguments and the Opt.WriteBrick, it does not look like you are planning on doing much.', FuncName));
00089 drawnow;
00090 end
00091
00092 if (isempty(Opt.Prefix)),
00093 Opt.Prefix = sprintf('%s_%s', BrickName_Prefix, NewCode);
00094 end
00095 Info.RootName = sprintf('%s%s', Opt.Prefix, BrickName_View);
00096
00097
00098 if ( (exist(sprintf('%s.HEAD', Info.RootName)) == 2 ) | (exist(sprintf('%s.BRIK', Info.RootName)) == 2 ) )
00099 ErrMessage = sprintf ('Error %s: Brick %s exists. Will not overwrite', FuncName, Info.RootName);
00100 end
00101
00102 %load the brick
00103 if (Opt.verbose),
00104 fprintf(1,'Loading brick %s from disk ...', BrickName);
00105 end
00106 [err, V, Info , ErrMessage] = BrikLoad(sprintf('%s', BrickName));
00107 if (err), ErrMessage = sprintf('Error %s subfunction: %s ', FuncName, ErrMessage); return; end
00108 if (Opt.verbose),
00109 fprintf(1,'\tDone.\n');
00110 end
00111
00112 %determine the required transformation
00113 if (Opt.verbose),
00114 [err, OldCode] = AFNI_OrientCode (Info.ORIENT_SPECIFIC);
00115 fprintf(1,'Reslicing %s to %s ...\n', OldCode, NewCode);
00116 end
00117
00118 [err, maplocation, mapsign, Mtrans] = AFNI_CoordChange (Info.ORIENT_SPECIFIC, NewCode);
00119
00120 V = permute(V, maplocation);
00121 Info_orig = Info;
00122
00123 Info.DELTA = Info_orig.DELTA(maplocation).* mapsign;
00124
00125 if (mapsign(1) < 0),
00126 N = size(V,1);
00127 V(:,:,:) = V(N:-1:1, :, :);
00128 Info.ORIGIN(1) = Info_orig.ORIGIN(maplocation(1)) + (Info_orig.DATASET_DIMENSIONS(maplocation(1)) -1).* Info_orig.DELTA(maplocation(1));
00129 else
00130 Info.ORIGIN(1) = Info_orig.ORIGIN(maplocation(1));
00131 end
00132
00133 if (mapsign(2) < 0),
00134 N = size(V,2);
00135 V(:,:,:) = V(:, N:-1:1, :);
00136 Info.ORIGIN(2) = Info_orig.ORIGIN(maplocation(2)) + (Info_orig.DATASET_DIMENSIONS(maplocation(2)) -1).* Info_orig.DELTA(maplocation(2));
00137 else
00138 Info.ORIGIN(2) = Info_orig.ORIGIN(maplocation(2));
00139 end
00140
00141 if (mapsign(3) < 0),
00142 N = size(V,3);
00143 V(:,:,:) = V(:, :, N:-1:1);
00144 Info.ORIGIN(3) = Info_orig.ORIGIN(maplocation(3)) + (Info_orig.DATASET_DIMENSIONS(maplocation(3)) -1) .* Info_orig.DELTA(maplocation(3));
00145 else
00146 Info.ORIGIN(3) = Info_orig.ORIGIN(maplocation(3));
00147 end
00148
00149 %setup the new header and write out the files
00150 %FIXED: Oct. 11 01 . THIS HERE NEEDS FIXING. IT ONLY WORKS WHEN THE VOLUME IS CENTERED IN ALL DIMENSIONS
00151 %warndlg(sprintf('%s: This function may not yet properly correct for the origin.\nCheck results in AFNI', FuncName));
00152
00153 Info.DATASET_DIMENSIONS = Info_orig.DATASET_DIMENSIONS(maplocation);
00154 [err, Info.ORIENT_SPECIFIC] = AFNI_OrientCode(NewCode);
00155
00156 if (Opt.verbose),
00157 fprintf(1,'Writing resliced brick %s to disk ...', Info.RootName);
00158 end
00159
00160 if (Opt.WriteBrick),
00161 [err, ErrMessage, Info] = WriteBrik (V, Info, Opt);
00162 end
00163
00164 if (Opt.verbose),
00165 fprintf(1,'\tDone.\n');
00166 end
00167
00168 err = 0;
00169 return;
00170