function xpimage(action);
%XPIMAGE Demonstrates basic commands in the Image Processing Toolbox.

%	Copyright (c) 1984-94 by The MathWorks, Inc.

% Possible actions:
% initialize

if nargin<1,
    action='initialize';
end;

if strcmp(action,'initialize'),
    oldFigNumber=watchon;

    figNumber=figure( ...
	'Name','Examples of Image Processing Commands', ...
        'NumberTitle','off', ...
	'ShareColors','no', ...
	'Visible','off');

    figDefaultAxesPos=[0.10 0.45 0.60 0.45];
    set(figNumber,'DefaultAxesPosition',figDefaultAxesPos);

    axes( ...
	'Units','normalized', ...
        'Position',figDefaultAxesPos);
    
    load gatlin2
    I=ind2gray(X,map);
    imshow(I,64);

    %===================================
    % Set up the MiniCommand Window
    top=0.35;
    left=0.05;
    right=0.70;
    bottom=0.05;
    labelHt=0.05;
    spacing=0.005;
    promptStr=str2mat(' ',' % Press the buttons at the right to see', ...
	' % examples of basic image processing commands');
    % First, the MiniCommand Window frame
    frmBorder=0.02;
    frmPos=[left-frmBorder bottom-frmBorder ...
	(right-left)+2*frmBorder (top-bottom)+2*frmBorder];
    uicontrol( ...
        'Style','frame', ...
        'Units','normalized', ...
        'Position',frmPos, ...
	'BackgroundColor',[0.50 0.50 0.50]);
    % Then the text label
    labelPos=[left top-labelHt (right-left) labelHt];
    uicontrol( ...
	'Style','text', ...
        'Units','normalized', ...
        'Position',labelPos, ...
        'BackgroundColor',[0.50 0.50 0.50], ...
	'ForegroundColor',[1 1 1], ...
        'String','MiniCommand Window');
    % Then the editable text field
    mcwPos=[left bottom (right-left) top-bottom-labelHt-spacing];
    mcwHndl=uicontrol( ...
	'Style','edit', ...
        'Units','normalized', ...
        'Max',10, ...
        'BackgroundColor',[1 1 1], ...
        'Position',mcwPos, ...
	'Callback','xpimage(''eval'')', ...
        'String',promptStr);
    % Save this handle for future use
    set(gcf,'UserData',mcwHndl);

    %====================================
    % Information for all buttons
    labelColor=[0.8 0.8 0.8];
    top=0.95;
    btnWid=0.20;
    btnHt=0.07;
    right=0.95;
    left=right-btnWid;
    % Spacing between the button and the next command's label
    spacing=0.03;

    %====================================
    % The CONSOLE frame
    frmBorder=0.02;
    yPos=0.05-frmBorder;
    frmPos=[left-frmBorder yPos btnWid+2*frmBorder 0.9+2*frmBorder];
    uicontrol( ...
        'Style','frame', ...
        'Units','normalized', ...
        'Position',frmPos, ...
	'BackgroundColor',[0.50 0.50 0.50]);
    
    %====================================
    % The RELOAD IMAGE button
    btnNumber=1;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='Reload image';
    callbackStr='xpimage(''demobutton'')';
    cmdStr=str2mat( ...
        ' % Reload image', ...
        ' load gatlin2', ...
        ' I=ind2gray(X,map);', ...
	' imshow(I,64)' ...
    );        
    
    % Generic popup button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    uicontrol( ...
	'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Callback',callbackStr, ...
	'UserData',cmdStr);

    %====================================
    % The BRIGHTEN button
    btnNumber=2;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='Brighten';
    callbackStr='xpimage(''demobutton'')';
    cmdStr=str2mat( ...
        ' % Brighten the image', ...
        ' brighten(0.2);' ...
    );        
    
    % Generic popup button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    uicontrol( ...
	'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Callback',callbackStr, ...
	'UserData',cmdStr);

    %====================================
    % The DARKEN button
    btnNumber=3;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='Darken';
    callbackStr='xpimage(''demobutton'')';
    cmdStr=str2mat( ...
        ' % Darken the image', ...
        ' brighten(-0.2);' ...
    );
    
    % Generic popup button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    uicontrol( ...
	'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Callback',callbackStr, ...
	'UserData',cmdStr);

    %====================================
    % The HISTOGRAM EQUALIZATION button
    btnNumber=4;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='Equalization';
    callbackStr='xpimage(''demobutton'')';
    cmdStr=str2mat( ...
        ' % Histogram equalization', ...
        ' I=getimage;', ...
        ' J=histeq(I,64);', ...
	' imshow(J,64)' ...
    );        
    
    % Generic popup button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    uicontrol( ...
	'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Callback',callbackStr, ...
	'UserData',cmdStr);

    %====================================
    % The EMBOSS button
    btnNumber=5;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='Emboss';
    callbackStr='xpimage(''demobutton'')';
    cmdStr=str2mat( ...
        ' % Emboss the image', ...
        ' I=getimage;', ...
        ' J=filter2(fspecial(''sobel''),I);', ...
	' J2=mat2gray(J);', ...
	' imshow(J2,64)' ...
    );        
    
    % Generic popup button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    uicontrol( ...
	'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Callback',callbackStr, ...
	'UserData',cmdStr);

    %====================================
    % The EDGE DETECTION button
    btnNumber=6;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='Edge detection';
    callbackStr='xpimage(''demobutton'')';
    cmdStr=str2mat( ...
        ' % Edge detection', ...
        ' I=getimage;', ...
        ' BW=edge(I);', ...
	' imshow(BW,2)' ...
    );        
    
    % Generic popup button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    uicontrol( ...
	'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Callback',callbackStr, ...
	'UserData',cmdStr);

    %====================================
    % The NOISE button
    btnNumber=7;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='Noise';
    callbackStr='xpimage(''demobutton'')';
    cmdStr=str2mat( ...
        ' % Add noise', ...
        ' I=getimage;', ...
        ' J=imnoise(I,''speckle'',0.1);', ...
	' imshow(J,64)' ...
    );        
    
    % Generic popup button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    uicontrol( ...
	'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Callback',callbackStr, ...
	'UserData',cmdStr);

    %====================================
    % The INFO button
    uicontrol( ...
	'Style','push', ...
        'Units','normalized', ...
        'Position',[left bottom+btnHt+spacing btnWid btnHt], ...
        'String','Info', ...
        'Callback','xpimage(''info'')');

    %====================================
    % The CLOSE button
    uicontrol( ...
	'Style','push', ...
        'Units','normalized', ...
        'Position',[left bottom btnWid btnHt], ...
        'String','Close', ...
        'Callback','close(gcf)');

    % Now uncover the figure
    set(figNumber,'Visible','on');
    watchoff(oldFigNumber);
    figure(figNumber);

elseif strcmp(action,'demobutton'),
    cmdStr=get(gco,'UserData');
    mcwHndl=get(gcf,'UserData');
    set(mcwHndl,'String',cmdStr);
    evalmcw(mcwHndl);

elseif strcmp(action,'eval'),
    mcwHndl=get(gcf,'UserData');
    cmdStr=get(mcwHndl,'String');
    evalmcw(mcwHndl);

elseif strcmp(action,'info'),
    ttlStr=get(gcf,'Name');
    hlpStr= ...                                             
        ['                                               '  
         ' This demo lets you explore a number of the    '  
         ' basic image processing commands that are      '  
         ' available to you in the Image Processing      '  
         ' Toolbox. Pressing the various buttons allows  '  
         ' you to, among other things, brighten, darken, '  
         ' and equalize a gray scale image. The Image    '  
         ' Processing Toolbox also works with color      '  
         ' images.                                       '  
         '                                               '  
         ' If you like, you can even directly edit the   '  
         ' commands in the MiniCommand Window.           '  
         ' Type control-return to execute code in the    '  
         ' MiniCommand Window.                           '  
         '                                               '  
         ' File name: xpimage.m                          '];
    helpfun(ttlStr,hlpStr);                                 

end;    % if strcmp(action, ...
