function [b] = vipblkprojective(varargin)
% VIPBLKROIDISPLAY Mask dynamic dialog function for Projective 
% transformation block

% Copyright 2003-2006 The MathWorks, Inc.
% $Date: 2010/08/30 03:23:04 $ $Revision: 1.1.6.1 $

if nargin==0
    action = 'dynamic';   % mask callback
else
    action = 'icon';
end

blk = gcbh;

switch action
    case 'icon'
        current_mode = get_param(blk, 'mode');
        inLibrary = strcmp(get_param(bdroot(blk),'BlockDiagramType'),'library');
        if inLibrary
            ports.icon = 'Projective\nTransformation'; 
        else
            if (strcmp(current_mode,'Rectangle to quadrilateral'))
                ports.icon = 'Rectangle to\nQuadrilateral';
            elseif (strcmp(current_mode,'Quadrilateral to rectangle'))
                ports.icon = 'Quadrilateral \nto Rectangle';
            else
                ports.icon = 'Quadrilateral to\nQuadrilateral';
            end
        end    
        oldVerObsolete = strcmp(get_param(blk, 'inType'), 'Obsolete') == 1;
        if (oldVerObsolete)
          isSinglePort = strcmp(get_param(blk, 'imagePorts'), 'One multidimensional signal') == 1;
        else
          isSinglePort = strcmp(get_param(blk, 'inType'), 'Intensity') == 1;
        end        
        QuadToRectMode = strcmp(current_mode,'Quadrilateral to rectangle');
        RectToQuadMode = strcmp(current_mode,'Rectangle to quadrilateral');
        current_method = get_param(blk, 'method');
        isApproxMethod  = strcmp(current_method,'Quadratic approximation');
        if RectToQuadMode
            userDefRectSizeFromPort = strcmp(get_param(blk,'inRectFull'),'User-defined') && ...
                    strcmp(get_param(blk,'rectSizeSrc'),'Input port');
            if (isSinglePort)
                ports.iport1=1;
                ports.itxt1='';
                ports.oport1=1;
                ports.otxt1='';
                if (strcmp(get_param(blk,'outVerticesValid'),'on') && ...
                    strcmp(get_param(blk,'outVerticesSrc'),'Input port'))
                    ports.otxt1='Image';
                    ports.oport2=2;
                    ports.otxt2='Valid';
                    ports.oport3=2;
                    ports.otxt3='';
                    ports.oport4=2;
                    ports.otxt4='';            
                else
                    ports.oport2=1;
                    ports.otxt2='';
                    ports.oport3=1;
                    ports.otxt3='';
                    ports.oport4=1;
                    ports.otxt4='';            
                end
                if (strcmp(get_param(blk,'outVerticesSrc'),'Input port') && userDefRectSizeFromPort)
                    ports.itxt1='Image';
                    ports.iport2=2;
                    ports.itxt2='InROI';
                    ports.iport3=3;
                    ports.itxt3='OutPts';
                    ports.iport4=3;
                    ports.itxt4='';
                    ports.iport5=3;
                    ports.itxt5='';
                elseif strcmp(get_param(blk,'outVerticesSrc'),'Input port')
                    ports.itxt1='Image';
                    ports.iport2=2;
                    ports.itxt2='OutPts';
                    ports.iport3=2;
                    ports.itxt3='';
                    ports.iport4=2;
                    ports.itxt4='';
                    ports.iport5=2;
                    ports.itxt5='';
                elseif userDefRectSizeFromPort
                    ports.itxt1='Image';
                    ports.iport2=2;
                    ports.itxt2='InROI';
                    ports.iport3=2;
                    ports.itxt3='';
                    ports.iport4=2;
                    ports.itxt4='';
                    ports.iport5=2;
                    ports.itxt5='';
                else
                    ports.iport2=1;
                    ports.itxt2='';
                    ports.iport3=1;
                    ports.itxt3='';
                    ports.iport4=1;
                    ports.itxt4='';
                    ports.iport5=1;
                    ports.itxt5='';
                end
            else % input is RGB
                ports.iport1=1;
                ports.itxt1='R';
                ports.iport2=2;
                ports.itxt2='G';
                ports.iport3=3;
                ports.itxt3='B';
                ports.oport1=1;
                ports.otxt1='R';
                ports.oport2=2;
                ports.otxt2='G';
                ports.oport3=3;
                ports.otxt3='B';
                if (strcmp(get_param(blk,'outVerticesValid'),'on') && ...
                    strcmp(get_param(blk,'outVerticesSrc'),'Input port'))
                    ports.oport4=4;
                    ports.otxt4='Valid';            
                else
                    ports.oport4=3;
                    ports.otxt4='';            
                end
                if (strcmp(get_param(blk,'outVerticesSrc'),'Input port') && ...
                    userDefRectSizeFromPort)
                    ports.iport4=4;
                    ports.itxt4='InROI';
                    ports.iport5=5;
                    ports.itxt5='OutPts';
                elseif strcmp(get_param(blk,'outVerticesSrc'),'Input port')
                    ports.iport4=4;
                    ports.itxt4='OutPts';
                    ports.iport5=4;
                    ports.itxt5='';
                elseif userDefRectSizeFromPort
                    ports.iport4=4;
                    ports.itxt4='InROI';
                    ports.iport5=4;
                    ports.itxt5='';
                else
                    ports.iport4=3;
                    ports.itxt4='';
                    ports.iport5=3;
                    ports.itxt5='';
                end
            end            
        elseif QuadToRectMode
            if (isSinglePort)
                ports.iport1=1;
                ports.itxt1='';
                ports.oport1=1;
                ports.otxt1='';
                if (strcmp(get_param(blk,'inVerticesValid'),'on') && ...
                    (strcmp(get_param(blk,'inVerticesSrc'),'Input port') || ...
                         isApproxMethod))
                    ports.otxt1='Image';
                    ports.oport2=2;
                    ports.otxt2='Valid';
                    ports.oport3=2;
                    ports.otxt3='';
                    ports.oport4=2;
                    ports.otxt4='';            
                else
                    ports.oport2=1;
                    ports.otxt2='';
                    ports.oport3=1;
                    ports.otxt3='';
                    ports.oport4=1;
                    ports.otxt4='';            
                end
                if (strcmp(get_param(blk,'inVerticesSrc'),'Input port') && ...
                    strcmp(get_param(blk,'rectSizeSrc'),'Input port'))
                    ports.itxt1='Image';                
                    ports.iport2=2;
                    ports.itxt2='InPts';
                    ports.iport3=3;
                    ports.itxt3='OutSize';
                    ports.iport4=3;
                    ports.itxt4='';
                    ports.iport5=3;
                    ports.itxt5='';
                elseif strcmp(get_param(blk,'inVerticesSrc'),'Input port')
                    ports.itxt1='Image';                
                    ports.iport2=2;
                    ports.itxt2='InPts';
                    ports.iport3=2;
                    ports.itxt3='';
                    ports.iport4=2;
                    ports.itxt4='';
                    ports.iport5=2;
                    ports.itxt5='';
                elseif strcmp(get_param(blk,'rectSizeSrc'),'Input port')
                    ports.itxt1='Image';                
                    ports.iport2=2;
                    ports.itxt2='OutSize';
                    ports.iport3=2;
                    ports.itxt3='';
                    ports.iport4=2;
                    ports.itxt4='';
                    ports.iport5=2;
                    ports.itxt5='';
                else
                    ports.iport2=1;
                    ports.itxt2='';
                    ports.iport3=1;
                    ports.itxt3='';
                    ports.iport4=1;
                    ports.itxt4='';
                    ports.iport5=1;
                    ports.itxt5='';
                end
            else % input is RGB
                ports.iport1=1;
                ports.itxt1='R';
                ports.iport2=2;
                ports.itxt2='G';
                ports.iport3=3;
                ports.itxt3='B';
                ports.oport1=1;
                ports.otxt1='R';
                ports.oport2=2;
                ports.otxt2='G';
                ports.oport3=3;
                ports.otxt3='B';
                if (strcmp(get_param(blk,'inVerticesValid'),'on') && ...
                    (strcmp(get_param(blk,'inVerticesSrc'),'Input port') || ...
                    isApproxMethod))
                    ports.oport4=4;
                    ports.otxt4='Valid';            
                else
                    ports.oport4=3;
                    ports.otxt4='';            
                end
                if (strcmp(get_param(blk,'inVerticesSrc'),'Input port') && ...
                    strcmp(get_param(blk,'rectSizeSrc'),'Input port'))
                    ports.iport4=4;
                    ports.itxt4='InPts';
                    ports.iport5=5;
                    ports.itxt5='OutSize';
                elseif strcmp(get_param(blk,'inVerticesSrc'),'Input port')
                    ports.iport4=4;
                    ports.itxt4='InPts';
                    ports.iport5=4;
                    ports.itxt5='';
                elseif strcmp(get_param(blk,'rectSizeSrc'),'Input port')
                    ports.iport4=4;
                    ports.itxt4='OutSize';
                    ports.iport5=4;
                    ports.itxt5='';
                else
                    ports.iport4=3;
                    ports.itxt4='';
                    ports.iport5=3;
                    ports.itxt5='';
                end
            end
        else % quad to quad mode. 
            inPtsValidPortExists = ...
               (strcmp(get_param(blk,'inVerticesValid'),'on') && ...
                strcmp(get_param(blk,'inVerticesSrc'),'Input port'));
            outPtsValidPortExists = ...
               (strcmp(get_param(blk,'outVerticesValid'),'on') && ...
                strcmp(get_param(blk,'outVerticesSrc'),'Input port'));
            if (isSinglePort)
                ports.iport1=1;
                ports.itxt1='';
                ports.oport1=1;
                ports.otxt1='';
                if (inPtsValidPortExists && outPtsValidPortExists)
                    ports.otxt1='Image';                                    
                    ports.oport2=2;
                    ports.otxt2='InPtsValid';
                    ports.oport3=3;
                    ports.otxt3='OutPtsValid';
                    ports.oport4=3;
                    ports.otxt4='';
                    ports.oport5=3;
                    ports.otxt5='';                
                elseif (inPtsValidPortExists || outPtsValidPortExists)
                    ports.otxt1='Image';                                       
                    if inPtsValidPortExists
                        ports.oport2=2;
                        ports.otxt2='InPtsValid';
                        ports.oport3=2;
                        ports.otxt3='';
                    else
                        ports.oport2=2;
                        ports.otxt2='OutPtsValid';
                        ports.oport3=2;
                        ports.otxt3='';
                    end
                    ports.oport4=2;
                    ports.otxt4='';
                    ports.oport5=2;
                    ports.otxt5='';                
                else
                    ports.oport2=1;
                    ports.otxt2='';
                    ports.oport3=1;
                    ports.otxt3='';
                    ports.oport4=1;
                    ports.otxt4='';
                    ports.oport5=1;
                    ports.otxt5='';                
                end
                if (strcmp(get_param(blk,'inVerticesSrc'),'Input port') && ...
                    strcmp(get_param(blk,'outVerticesSrc'),'Input port') )
                    ports.itxt1='Image';                                                       
                    ports.iport2=2;
                    ports.itxt2='InPts';
                    ports.iport3=3;
                    ports.itxt3='OutPts';
                    ports.iport4=3;
                    ports.itxt4='';
                    ports.iport5=3;
                    ports.itxt5='';
                elseif strcmp(get_param(blk,'inVerticesSrc'),'Input port')
                    ports.itxt1='Image';                                                       
                    ports.iport2=2;
                    ports.itxt2='InPts';
                    ports.iport3=2;
                    ports.itxt3='';
                    ports.iport4=2;
                    ports.itxt4='';
                    ports.iport5=2;
                    ports.itxt5='';
                elseif strcmp(get_param(blk,'outVerticesSrc'),'Input port')
                    ports.itxt1='Image';                                                       
                    ports.iport2=2;
                    ports.itxt2='OutPts';
                    ports.iport3=2;
                    ports.itxt3='';
                    ports.iport4=2;
                    ports.itxt4='';
                    ports.iport5=2;
                    ports.itxt5='';
                else
                    ports.iport2=1;
                    ports.itxt2='';
                    ports.iport3=1;
                    ports.itxt3='';
                    ports.iport4=1;
                    ports.itxt4='';
                    ports.iport5=1;
                    ports.itxt5='';
                end
            else
                ports.iport1=1;
                ports.itxt1='R';
                ports.iport2=2;
                ports.itxt2='G';
                ports.iport3=3;
                ports.itxt3='B';
                ports.oport1=1;
                ports.otxt1='R';
                ports.oport2=2;
                ports.otxt2='G';
                ports.oport3=3;
                ports.otxt3='B'; 
                if (inPtsValidPortExists && outPtsValidPortExists)
                    ports.oport4=4;
                    ports.otxt4='InPtsValid';
                    ports.oport5=5;
                    ports.otxt5='OutPtsValid';
                elseif (inPtsValidPortExists || outPtsValidPortExists)
                    if inPtsValidPortExists
                        ports.oport4=4;
                        ports.otxt4='InPtsValid';
                        ports.oport5=4;
                        ports.otxt5='';
                    else
                        ports.oport4=4;
                        ports.otxt4='OutPtsValid';
                        ports.oport5=4;
                        ports.otxt5='';
                    end
                else
                    ports.oport4=3;
                    ports.otxt4='';
                    ports.oport5=3;
                    ports.otxt5='';                
                end
                if (strcmp(get_param(blk,'inVerticesSrc'),'Input port') && ... 
                    strcmp(get_param(blk,'outVerticesSrc'),'Input port'))
                    ports.iport4=4;
                    ports.itxt4 ='InPts';
                    ports.iport5=5;
                    ports.itxt5 ='OutPts';
                elseif strcmp(get_param(blk,'inVerticesSrc'),'Input port') 
                    ports.iport4=4;
                    ports.itxt4 ='InPts';
                    ports.iport5=4;
                    ports.itxt5 ='';
                elseif strcmp(get_param(blk,'outVerticesSrc'),'Input port')
                    ports.iport4=4;
                    ports.itxt4 ='OutPts';
                    ports.iport5=4;
                    ports.itxt5 ='';
                else
                    ports.iport4=3;
                    ports.itxt4='';
                    ports.iport5=3;
                    ports.itxt5='';
                end
            end
        end
        b = ports;
    otherwise
        error('vipblks:vipblkprojective:unhandledCase', 'unhandled case');
end


