function mrcat(directory)
% mrcat:  A program for segmenting MRI images.  Starts an MRI segmentation 
% editing window, editing the image series based in <directory>.  
%
% USAGE:  mrcat <directory>
%
% mrcat will look for images in every subdirectory of <directory>, and
% will load an image from the first image subdirectory it finds.
% It will then make reasonable guesses about the locations of locator images
% and output 'otl' files, creating the 'otl' directory if necessary.
% If you don't like mrcat's guesses, you can change them in the GUI window.
%
% Example:
%
% mrcat s3/iy/a
%   mrcat loads main editable images from s3/iy/a/<img-fmt>.
%   Locator images are loaded from s3/iy/c/<img-fmt> and s3/iy/s/<img-fmt>.
%   Edited outline files are stored in s3/iy/a/otl.
%
% mrcat s3
%   mrcat searches subdirectories of s3 to find loadable images.
%   For example, if it finds images in s3/aa/a/otl, it might load a main image from here,
%   then load locator images from s3/aa/c/otl and s3/aa/s/otl
%   Locator images are loaded from subject2c/<img-fmt> and subject2s/<img-fmt>.
%   Edited outline files are stored in subject2a/otl.
%
% Written by Mark Hasegawa-Johnson and Jul Setsu Cha (mhj@icsl.ucla.edu).
% Revision 1.3, July 27, 1999
%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Define the available outline editing tools
% All changes here should be reflected in the options list in EditOtl
EDIT_OPTIONS = {'Zoom_On';'Original_Size';'Add_Points';'Add_Line';...
    'Delete_Points';'Change_Color';'White_Pixel';'Magic_Wand'};
    
% Define the available colors for outlining
OTLCOLOR = { 'blue'; 'cyan'; 'green'; 'yellow'; 'red'; 'magenta' };

% Define the available upsampling factors for in-plane interpolation
ROI_UPSAMPLE_POSSIBILITIES = [1 2 3 4];
INTERPOLATION_STYLES = { 'Linear', 'Spline', 'Tubular', 'Filled' };
INTERPOLATION_STYLE_LABELS = { 'Surface (Linear)', 'Surface (Spline)', 'Tubular (Shape-Based)', 'Filled Solid (Shape-Based)' };

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% If no argument specified, assume that it's the current directory
if nargin<1, directory=pwd; end
L = length(directory);

% If the directory starts with filesep, or with a DOS drive specifier, it must be absolute
if (directory(1)~=filesep) & (directory(1)~='/') & (directory(2:3) ~= [ ':' filesep ]) & ...
      (directory(2:3) ~= ':/'),
  % If the directory is relative, convert it to an absolute directory
  testdir = fullfile(pwd,directory);
  if length(dir(testdir)) > 0, directory=testdir, end;
end

% Make sure that the newly-parsed directory is readable
if length(dir(directory))==0,
  error(sprintf('mrcat: Unable to read from directory %s',directory))
end;

% Now we look for the first image we can find
[ PATH, IMAGEFILE ] = findimg(directory);
LP = length(PATH);

% Parse the path into a global IMAGEBASE, and local IMAGESERIES, IMAGEPLANE, and IMAGETYPE
global ROOTDIR;
if LP>4,
  ROOTDIR = fullfile(PATH{1:(LP-3)});
elseif LP==4,
  ROOTDIR = PATH{1};
else,
  ROOTDIR = '';
end;
IMAGETYPE = PATH{LP-2};
IMAGESERIES = PATH{LP-1};
IMAGEPLANE = PATH{LP};

% Define the OTLDIR, and create as much of the path as is necessary
OTLDIR = fullfile(ROOTDIR, 'otl');
if ~isdir(OTLDIR), eval(['! mkdir ' OTLDIR ]); end
OTLDIR = fullfile(ROOTDIR, 'otl', IMAGESERIES);
if ~isdir(OTLDIR), eval(['! mkdir ' OTLDIR ]); end
OTLDIR = fullfile(ROOTDIR, 'otl', IMAGESERIES, IMAGEPLANE);
if ~isdir(OTLDIR), eval(['! mkdir ' OTLDIR ]); end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create the figure
%
a = figure('Units','normalized', ...
    'Color',[0.2 0.45 0.6], ...
    'Colormap',repmat([0:255]'/255, [1 3]), ...
    'MenuBar','none', ...
    'Name','mrcat', ...
    'Pointer','crosshair', ...
    'Position',[0.3625 0.318359 0.592187 0.576172], ...
    'Renderer','zbuffer', ...
    'RendererMode','manual', ...
    'Tag','Fig1', ...
    'WindowButtonDownFcn','LodImage zoomdown Image ;');

    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create the menus
%
% File Menu -- Image and Outline Load & Save in different formats
b = uimenu('Parent',a, ...
    'Label','&File', ...
    'Tag','Filemenu');
c = uimenu('Parent',b, ...
    'Callback','LodImage save Image File bmp ;', ...
    'Label','Save Image as BMP', ...
    'Tag','SaveImageBMPMenu');
c = uimenu('Parent',b, ...
    'Callback','LodImage save Image File tif ;', ...
    'Label','Save Image as TIF', ...
    'Tag','SaveImageTIFMenu');
c = uimenu('Parent',b, ...
    'Callback','LodImage save Image File jpg ;', ...
    'Label','Save Image as JPG', ...
    'Tag','SaveImageJPGMenu');
c = uimenu('Parent',b, ...
    'Callback','EditOtl load otl', ...
    'Label','Load Outline from OTL', ...
    'Tag','LoadOutlineOTLMenu');
c = uimenu('Parent',b, ...
    'Callback','EditOtl save otl', ...
    'Label','Save Outline as OTL', ...
    'Tag','SaveOutlineOTLMenu');
c = uimenu('Parent',b, ...
    'Callback','EditOtl save txt', ...
    'Label','Save Outline as TXT', ...
    'Tag','SaveOutlineTXTMenu');
c = uimenu('Parent',b, ...
    'Callback','EditOtl save csv', ...
    'Label','Save Outline as CSV', ...
    'Tag','SaveOutlineCSVMenu');
c = uimenu('Parent',b, ...
    'Callback','print gcbf ', ...
    'Label','Print Window', ...
    'Tag','PrintMenu');
c = uimenu('Parent',b, ...
    'Callback','close mrcat ', ...
    'Label','Close Window', ...
    'Tag','CloseMenu');

% EditMenu contains various things that are useful for editing an outline
b = uimenu('Parent', a, ...
    'Label','&Edit', ...
    'Tag','EditMenu');
c = uimenu('Parent',b, ...
    'Callback','EditOtl Undo EditUndoMenu', ...
    'Label','&Undo!', ...
    'Tag','EditUndoMenu',...
    'UserData',{});
c = uimenu('Parent',b, ...
    'Callback','EditOtl Undo EditRedoMenu', ...
    'Label','Redo', ...
    'Tag','EditRedoMenu',...
    'UserData',{});
%
%  ToolsMenu contains an entry for each of the strings in EDIT_OPTIONS. 
%  Loop through the strings in EDIT_OPTIONS, and create a new menu entry for each.
c = uimenu('Parent',b, ...
    'Label','&Tools', ...
    'Tag','ToolsMenu');
for i=1:length(EDIT_OPTIONS),
  d = uimenu('Parent',c, ...
    'Accelerator',EDIT_OPTIONS{i}(1), ...
    'Callback',['EditOtl choose ', EDIT_OPTIONS{i}], ...
    'checked','off', ...
    'Label',EDIT_OPTIONS{i}, ...
    'Tag',['ToolsMenu_' EDIT_OPTIONS{i}]);
  if i==1, set(d,'Checked','on'); end
end
c = uimenu('Parent',b, ...
    'Accelerator','t', ...
    'Callback','EditOtl Adjust_Threshold 0', ...
    'Label','&Threshold Lines', ...
    'Tag','show threshold menu');
c = uimenu('Parent',b, ...
    'Accelerator','c', ...
    'Callback','EditOtl Clear_Data ThreshPoints', ...
    'Label','&Clear Threshold Lines', ...
    'Tag','clear threshold menu');
c = uimenu('Parent',b, ...
    'Callback','EditOtl Clear_Data OtlPoints', ...
    'Label','Clea&r Outline', ...
    'Tag','clear outline menu');

% Image MENU -- commands for getting next or last image, or brightening or darkening image
%
b = uimenu('Parent',a, ...
    'Label','&Image', ...
    'Tag','Imagemenu');
c = uimenu('Parent',b, ...
    'Accelerator','n', ...
    'Callback','LodImage load Image File 1 ;', ...
    'Label','&Next Image', ...
    'Tag','next image menu');
c = uimenu('Parent',b, ...
    'Accelerator','l', ...
    'Callback','LodImage load Image File -1 ;', ...
    'Label','&Last Image', ...
    'Tag','last image menu');
c = uimenu('Parent',b, ...
    'Accelerator','b', ...
    'Callback','LodImage brighten Image ;', ...
    'Label','&Brighten', ...
    'Tag','Img_Intsity_Brighten');
c = uimenu('Parent',b, ...
    'Accelerator','r', ...
    'Callback','LodImage darken Image ;', ...
    'Label','Da&rken', ...
    'Tag','Img_Intsity_Brighten');

% 3D Menu marks parameters having to do with the 3D plot function
b = uimenu('Parent',a, ...
    'Label','&3D Plotting', ...
    'Tag','Plot3DOptionsMenu');
c = uimenu('Parent',b, ...
    'callback','radiotgl Plot3DSelectRadio', ...
    'checked','off', ...
    'Label','Plot Selected Color Only', ...
    'Tag','Plot3DSelectRadio');
c = uimenu('Parent',b, ...
    'callback','radiotgl Plot3DInterpolateRadio', ...
    'checked','off', ...
    'Label','Interpolate Before Plotting', ...
    'Tag','Plot3DInterpolateRadio');
c = uimenu('Parent',b, ...
    'Label','Interpolation Style', ...
    'Tag','INTERPOLATION_STYLEMenu');
% List the possible values of INTERPOLATION_STYLES, and check the initially selected one
for r=1:length(INTERPOLATION_STYLES),
  d = uimenu('Parent',c, ...
      'callback',[ sprintf('radiotgl Radio%s', INTERPOLATION_STYLES{r}), ...
	sprintf(' Radio%s', INTERPOLATION_STYLES{:})], ...
      'checked','off', ...
      'Label',sprintf('%s',INTERPOLATION_STYLE_LABELS{r}), ...
      'Tag',sprintf('Radio%s',INTERPOLATION_STYLES{r}));
  if r==1, set(d,'checked','on'); end
end
c = uimenu('Parent',b, ...
    'Label','Interpolation Points per mm',...
    'Tag','ROI_UPSAMPLEMenu');
% List the possible values of ROI_UPSAMPLE, and check the initially selected one
for r=ROI_UPSAMPLE_POSSIBILITIES,
  d = uimenu('Parent',c, ...
      'callback',[ sprintf('radiotgl ROI_UPSAMPLE=%d_Radio', r), ...
	sprintf(' ROI_UPSAMPLE=%d_Radio', ROI_UPSAMPLE_POSSIBILITIES)], ...
      'checked','off', ...
      'Label',sprintf('%d',r), ...
      'Tag',sprintf('ROI_UPSAMPLE=%d_Radio',r));
  if r==1, set(d,'checked','on'); end
end
c = uimenu('Parent',b, ...
    'callback','EditOtl Plot3D', ...
    'Label','3-D Plot', ...
    'Tag','Plot3DPlot3DMenu');

% Help Menu gives help on using mrcat
b = uimenu('Parent',a, ...
    'Label','&Help', ...
    'Tag','HelpMenu');
c = uimenu('Parent',b, ...
    'Callback','helpdesk', ...
    'Label','Matlab HelpDesk',...
    'Tag','Matlab Help menu');
c = uimenu('Parent',b, ...
    'Callback','EditOtl help starting.txt', ...
    'Label','Help on &Starting mrcat', ...
    'Tag','Start Help menu');
c = uimenu('Parent',b, ...
    'Callback','EditOtl help image.txt', ...
    'Label','Help on &Image Loading', ...
    'Tag','Image Help menu');
c = uimenu('Parent',b, ...
    'Callback','EditOtl help buttons.txt', ...
    'Label','Help on &Button Bar Tools', ...
    'Tag','ButtonBar Help menu');
c = uimenu('Parent',b, ...
    'Callback','edit whichdone.log', ...
    'Label','&Workgroup Log File', ...
    'Tag','WhichDone Help menu');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Right-hand button bar, and controls found on it
% Controls are listed from the bottom to the top
%
b = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.1 0.5 0.75], ...
    'Position',[0.83 0.00 0.17 1.0], ...
    'Style','frame', ...
    'Tag','Frame1');
b = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.8 0.8 0.83], ...
    'Callback','close mrcat', ...
    'Position',[0.837331 0.0 0.150396 0.05], ...
    'String','Close', ...
    'Tag','Pushbutton1');
b = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.8 0.8 0.83], ...
    'Callback','EditOtl Plot3D', ...
    'Position',[0.837731 0.06 0.150396 0.05], ...
    'String','3-D Plot', ...
    'Tag','StaticText2');
Show_Region = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.8 0.8 0.83], ...
    'ForegroundColor',[0 0 1], ...
    'Callback','EditOtl ROI show', ...
    'Position',[0.837731 0.11 0.150396 0.05], ...
    'String','Show Region', ...
    'Tag','ShowRegion');
 % OtlDir: Modified 3/31/2002 to look in same directory with image files
OtlDir = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.8 0.8 0.83], ...
    'Max',57, ...
    'Min',1, ...
    'Position',[0.837731 0.16 0.05 0.05], ...
    'String',IMAGETYPE, ...
    'Style','edit', ...
    'Tag','OtlDir', ...
    'Value',1);
b = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.8 0.8 0.83], ...
    'Max',40, ...
    'Min',1, ...
    'Position',[0.887731 0.16 0.100396 0.05], ...
    'String','28', ...
    'Style','edit', ...
    'Tag','SvEdOtl', ...
    'Value',1);
b = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.8 0.8 0.83], ...
    'Callback','EditOtl load otl', ...
    'Position',[0.837731 0.21 0.150396 0.05], ...
    'String','Load Otl', ...
    'Tag','StaticText1');
b = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.8 0.8 0.83], ...
    'Callback','EditOtl save otl', ...
    'Position',[0.837731 0.26 0.150396 0.05], ...
    'String','Save Otl', ...
    'Tag','StaticText1');
b = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.8 0.8 0.85], ...
    'Position',[0.837731 0.315 0.150396 0.1], ...
    'Style','frame', ...
    'Tag','Frame2');
b = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[1 1 1], ...
    'Callback','LodImage brighten Image ;', ...
    'Position',[0.837731 0.32 0.0752 0.045], ...
    'String','Bright', ...
    'Tag','LBright');
b = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.3 0.3 0.3], ...
    'ForegroundColor',[1 1 1], ...
    'Callback','LodImage darken Image ;', ...
    'Position',[0.912931 0.32 0.0752 0.045], ...
    'String','Dark', ...
    'Tag','LDark');
b = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.8 0.8 0.85], ...
    'Position',[0.847731 0.365 0.130396 0.045], ...
    'String','Intensity Change', ...
    'Style','text', ...
    'Tag','StaticText3');
b = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.8 0.8 0.85], ...
    'Position',[0.837731 0.42 0.150396 0.10], ...
    'Style','frame', ...
    'Tag','Frame: Delete');
b = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[1 0 0], ...
    'Callback','EditOtl Clear_Data ThreshPoints', ...
    'Position',[0.837731 0.425 0.0752 0.045], ...
    'String','Thresh', ...
    'Tag','ClearBorders');
b = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0 1 0], ...
    'Callback','EditOtl Clear_Data OtlPoints', ...
    'Position',[0.912931 0.425 0.0752 0.045], ...
    'String','Outline', ...
    'Tag','ClearOtl');
b = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.8 0.8 0.85], ...
    'Position',[0.847731 0.47 0.130396 0.045], ...
    'String','Delete All', ...
    'Style','text', ...
    'Tag','Text: Delete');
b = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.701961 0.701961 0.701961], ...
    'Callback','EditOtl Adjust_Threshold 0.0078125', ...
    'FontSize',18, ...
    'Position',[0.937931 0.525 0.0501 0.04], ...
    'String','>', ...
    'Tag','Pushbutton3');
b = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.701961 0.701961 0.701961], ...
    'Callback','EditOtl Adjust_Threshold -0.0078125', ...
    'FontSize',18, ...
    'Position',[0.837731 0.525 0.0501 0.04], ...
    'String','<', ...
    'Tag','Pushbutton4');
b = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.701961 0.701961 0.701961], ...
    'FontSize',12, ...
    'Position',[0.887831 0.525 0.0501 0.04], ...
    'String','0.25', ...
    'Style','text', ...
    'Tag','ThreshText');
b = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.701961 0.701961 0.701961], ...
    'Callback','EditOtl Adjust_Threshold', ...
    'Position',[0.837731 0.565 0.150396 0.04], ...
    'Style','slider', ...
    'Tag','Thresh', ...
    'Value',0.25);
b = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.8 0.8 0.83], ...
    'Position',[0.837731 0.605 0.150396 0.04], ...
    'String','Threshold', ...
    'Style','text', ...
    'Tag','Text: Threshold');
b = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.8 0.8 0.83], ...
    'Max',57, ...
    'Min',1, ...
    'Position',[0.837731 0.65 0.150396 0.05], ...
    'String', OTLCOLOR,...
    'Style','popupmenu', ...
    'Tag','OtlColor', ...
    'Value',3);
b = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.8 0.8 0.83], ...
    'Position',[0.837731 0.7 0.150396 0.04], ...
    'String','Outline Color', ...
    'Style','text', ...
    'Tag','Text: Threshold');
b = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.8 0.8 0.83], ...
    'Callback','EditOtl choose', ...
    'Clipping','off', ...
    'Max',2, ...
    'Min',1, ...
    'Position',[0.837731 0.75 0.150396 0.25], ...
    'SelectionHighlight','off', ...
    'String',char(EDIT_OPTIONS), ...
    'Style','listbox', ...
    'Tag','ZoomOrEdit', ...
    'Value',1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Image Axes
%
b = axes('Parent',a, ...
    'Box','on', ...
    'CameraUpVector',[0 1 0], ...
    'Color',[1 1 1], ...
    'Layer','top', ...
    'Position',[0.275 0.13 0.54 0.8], ...
    'Tag','ImageAxes', ...
    'XColor',[0 0 0], ...
    'XLim',[ 0.5 65536.5 ], ...
    'XLimMode','manual', ...
    'YColor',[0 0 0], ...
    'YDir','reverse', ...
    'YLim',[ 0.5 65536.5 ], ...
    'YLimMode','manual', ...
    'ZColor',[0 0 0]);
c = text('Parent',b, ...
    'Color',[0 0 0], ...
    'HandleVisibility','callback', ...
    'HorizontalAlignment','right', ...
    'Position',[-130.637 -21.241 0], ...
    'Tag','ImageAxesText4', ...
    'Visible','off');
set(get(c,'Parent'),'ZLabel',c);
c = text('Parent',b, ...
    'Color',[0 0 0], ...
    'HandleVisibility','callback', ...
    'HorizontalAlignment','center', ...
    'Position',[127.873 268.458 0], ...
    'Tag','ImageAxesText3', ...
    'VerticalAlignment','cap');
set(get(c,'Parent'),'XLabel',c);
c = text('Parent',b, ...
    'Color',[0 0 0], ...
    'HandleVisibility','callback', ...
    'HorizontalAlignment','center', ...
    'Position',[-18.951 129.315 0], ...
    'Rotation',90, ...
    'Tag','ImageAxesText2', ...
    'VerticalAlignment','baseline');
set(get(c,'Parent'),'YLabel',c);
c = text('Parent',b, ...
    'Color',[0 0 0], ...
    'HandleVisibility','callback', ...
    'HorizontalAlignment','center', ...
    'Position',[127.873 -2.76115 0], ...
    'Tag','ImageAxesText1', ...
    'VerticalAlignment','bottom');
set(get(c,'Parent'),'Title',c);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Load Image controls
%
ImageFile = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.8 0.8 0.83], ...
    'Callback','LodImage load Image File 0 ;', ...
    'Max',57, ...
    'Min',1, ...
    'Position',[0.625 0.02 0.19 0.05], ...
    'String',{ IMAGEFILE },...
    'Style','popupmenu', ...
    'Tag','ImageFile', ...
    'Value',1);
ImagePlane = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.8 0.8 0.83], ...
    'Callback','LodImage create Image File ;', ...
    'Max',57, ...
    'Min',1, ...
    'Position',[0.525 0.02 0.1 0.05], ...
    'String',{ IMAGEPLANE },...
    'Style','popupmenu', ...
    'Tag','ImagePlane', ...
    'Value',1);
ImageSeries = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.8 0.8 0.83], ...
    'Callback','LodImage create Image Plane ;', ...
    'Max',57, ...
    'Min',1, ...
    'Position',[0.375 0.02 0.15 0.05], ...
    'String',{ IMAGESERIES },...
    'Style','popupmenu', ...
    'Tag','ImageSeries', ...
    'Value',1);
ImageType = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.8 0.8 0.83], ...
    'CreateFcn','LodImage create Image Type ;', ...
    'Callback', 'LodImage create Image Series ; LodImage create Sag Series ; LodImage create CorA Series ;', ...
    'Max',57, ...
    'Min',1, ...
    'Position',[0.275 0.02 0.1 0.05], ...
    'String',{ IMAGETYPE },...
    'Style','popupmenu', ...
    'Tag','ImageType', ...
    'Value',1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Sagittal Locator Image
%

b = axes('Parent',a, ...
    'Box','on', ...
    'CameraUpVector',[0 1 0], ...
    'Color',[1 1 1], ...
    'Layer','top', ...
    'NextPlot','add', ...
    'Position',[0 0.58 0.235 0.349153], ...
    'Tag','SagAxes', ...
    'UserData','new',...
    'XColor',[0 0 0], ...
    'XLim',[ 0.5 65536.5 ], ...
    'XLimMode','manual', ...
    'YColor',[0 0 0], ...
    'YDir','reverse', ...
    'YLim',[ 0.5 65536.5 ], ...
    'YLimMode','manual', ...
    'ZColor',[0 0 0]);
b = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[1 1 0], ...
    'Callback','LodImage zoomvisible Sag ;', ...
    'Position',[0.08 0.53 0.08 0.05], ...
    'String','off', ...
    'Tag','SagLines');
b = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[1 1 1], ...
    'Callback','LodImage brighten Sag ;', ...
    'Position',[0 0.53 0.08 0.05], ...
    'String','Bright', ...
    'Tag','SagBright');
b = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'ForegroundColor',[1 1 1], ...
    'BackgroundColor',[0.3 0.3 0.3], ...
    'Callback','LodImage darken Sag ;', ...
    'Position',[0.16 0.53 0.08 0.05], ...
    'String','Dark', ...
    'Tag','SagDark');
SagFile = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.8 0.8 0.83], ...
    'Callback','LodImage load Sag File 0 ;', ...
    'Max',8, ...
    'Min',1, ...
    'Position',[0.14 0.48 0.1 0.05], ...
    'String',{ 'not initialized' },...
    'Style','popupmenu', ...
    'Tag','SagFile', ...
    'Value',1);
SagPlane = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.8 0.8 0.83], ...
    'Callback','LodImage create Sag File ;', ...
    'Max',8, ...
    'Min',1, ...
    'Position',[0.07 0.48 0.07 0.05], ...
    'String',{ 's' },...
    'Style','popupmenu', ...
    'Tag','SagPlane', ...
    'Value',1);
SagSeries = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.8 0.8 0.83], ...
    'CreateFcn','LodImage create Sag Series ;', ...
    'Callback','LodImage create Sag Plane ;', ...
    'Max',8, ...
    'Min',1, ...
    'Position',[0.0 0.48 0.07 0.05], ...
    'String', { IMAGESERIES },...
    'Style','popupmenu', ...
    'Tag','SagSeries', ...
    'Value',1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Coronal or Axial Locator Image (whichever is not the main image)
%
if IMAGEPLANE=='c', 
  CORAPLANE='a'; 
else,
  CORAPLANE='c';
end

b = axes('Parent',a, ...
    'Box','on', ...
    'CameraUpVector',[0 1 0], ...
    'Color',[1 1 1], ...
    'Layer','top', ...
    'NextPlot','add', ...
    'Position',[0 0.133898 0.235 0.338983], ...
    'Tag','CorAAxes', ...
    'UserData','new',...
    'XColor',[0 0 0], ...
    'XLim',[ 0.5 65536.5 ], ...
    'XLimMode','manual', ...
    'YColor',[0 0 0], ...
    'YDir','reverse', ...
    'YLim',[ 0.5 65536.5 ], ...
    'YLimMode','manual', ...
    'ZColor',[0 0 0]);
b = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[1 1 0], ...
    'Callback','LodImage zoomvisible CorA ;', ...
    'Position',[0.08 0.08 0.08 0.05], ...
    'String','off', ...
    'Tag','CorALines');
b = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[1 1 1], ...
    'Callback','LodImage brighten CorA ;', ...
    'Position',[0 0.08 0.08 0.05], ...
    'String','Bright', ...
    'Tag','CorABright');
b = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'ForegroundColor',[1 1 1], ...
    'BackgroundColor',[0.3 0.3 0.3], ...
    'Callback','LodImage darken CorA ;', ...
    'Position',[0.16 0.08 0.08 0.05], ...
    'String','Dark', ...
    'Tag','CorADark');
CorAFile = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.8 0.8 0.83], ...
    'Callback','LodImage load CorA File 0 ;', ...
    'Max',37, ...
    'Min',1, ...
    'Position',[0.14 0.03 0.1 0.05], ...
    'String',{ 'not initialized' },...
    'Style','popupmenu', ...
    'Tag','CorAFile', ...
    'Value',1);
CorAPlane = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.8 0.8 0.83], ...
    'Callback','LodImage create CorA File ;', ...
    'Max',37, ...
    'Min',1, ...
    'Position',[0.07 0.03 0.07 0.05], ...
    'String',{ CORAPLANE },...
    'Style','popupmenu', ...
    'Tag','CorAPlane', ...
    'Value',1);
CorASeries = uicontrol('Parent',a, ...
    'Units','normalized', ...
    'BackgroundColor',[0.8 0.8 0.83], ...
    'CreateFcn','LodImage create CorA Series ;', ...
    'Callback','LodImage create CorA Plane ;', ...
    'Max',37, ...
    'Min',1, ...
    'Position',[0 0.03 0.07 0.05], ...
    'String',{ IMAGESERIES },...
    'Style','popupmenu', ...
    'Tag','CorASeries', ...
    'Value',1);



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ PATH, IMAGE ] = findimg( DIRECTORY, recursiondepth )
% [ PATH, IMAGE ] = findimg( DIRECTORY )
%
% Look in subdirectories of the current directory, until we find an image that
% can be loaded.
% 
% Once the images have been found, we return the path in multiple components.
% The full pathname of the first loadable image file (on unix and DOS systems) is
%    fullfile(PATH{:}, IMAGE)
%

% First, set recursiondepth to zero if not specified
if nargin<2, recursiondepth=0; end

% Set default PATH and IMAGE
PATH = {};
IMAGE = '';

% Run filemenu to look for image files that can be loaded
[ filesanddirs, files, dirs ] = filemenu(DIRECTORY);

% If image files were found, parse the path name, and return
if length(files) > 0,
   IMAGE = files{round(length(files)/2)};
  npath = 0;
  
  % If DIRECTORY starts with a filesep, save that as PATH{1}
  if (DIRECTORY(1)=='/') | (DIRECTORY(1)==filesep),
    PATH{1} = filesep;  
    npath=1;
    DIRECTORY = DIRECTORY(2:length(DIRECTORY));
  end;
    
  while length(DIRECTORY) > 0,
    npath = npath+1;
    [ PATH{npath} DIRECTORY ] = strtok(DIRECTORY, [ '/' filesep ]);
  end;
  return;
end;  

% If image files were not found, call findimg recursively for all subdirectories
for i=2:length(dirs), if strcmp(dirs{i}, '..')==0,
    [ PATH, IMAGE ] = findimg( fullfile(DIRECTORY, dirs{i}), recursiondepth+1 );
    if length(PATH) > 0, 
      return; 
    end;
end; end;

% If image files not found, and recursiondepth==0, try going just one directory up
if recursiondepth==0,
  [t r] = strtok(fliplr(DIRECTORY), [ '/' filesep ]);
  [ PATH, IMAGE ] = findimg( fliplr(r), recursiondepth+1 );
  if length(PATH) > 0,
    return;
  end;
end;

% If no image files found anywhere, tell the user we failed
if recursiondepth==0,
   disp(sprintf('Unable to find any image files in directory %s', DIRECTORY)),
	return;   
end;

