function [retPortLabels, displayFilename, infoString] = vipblkreadavifile(action,varargin) %#ok
% VIPBLKREADAVIFILE Video Processing Blockset Read AVI File block helper
% function

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

blk = gcbh;
if nargin==0, action='dynamic'; end

infoString = '';

switch action
case 'init'
    filename = strtrim(get_param(blk, 'filename'));
    theFile = which(filename);
    if ~isempty(theFile),
        filename = theFile;
    end

    [path, file_no_path, file_ext] = fileparts(filename);
    displayFilename = [file_no_path file_ext];
    
    portLabels = struct([]);
    
    try
        info = aviinfo(filename);
        if strcmp(info.ImageType, 'grayscale') == 1 || ...
            (strcmp(info.ImageType, 'indexed') == 1 && ...
            info.NumColormapEntries == 256),
            portLabels(1).port = 1;
            portLabels(1).txt  = 'I';
            nextPortLabelIndex = 2;
        else
            portLabels(1).port = 1;
            portLabels(1).txt  = 'R';        
            portLabels(2).port = 2;
            portLabels(2).txt  = 'G'; 
            portLabels(3).port = 3;
            portLabels(3).txt  = 'B';
            nextPortLabelIndex = 4;
        end
        
        if strcmp(get_param(blk, 'outputEOF'), 'on'),
            portLabels(nextPortLabelIndex).port = nextPortLabelIndex;
            portLabels(nextPortLabelIndex).txt = 'EOF';
            nextPortLabelIndex = nextPortLabelIndex + 1;
        end
        
        for i=nextPortLabelIndex:4,
            portLabels(i).port = portLabels(nextPortLabelIndex-1).port;
            portLabels(i).txt = portLabels(nextPortLabelIndex-1).txt;
        end
        
        infoString = sprintf('%dx%d, %d fps', ...
            info.Height, info.Width, info.FramesPerSecond);
    catch aviinfoException
        for i=1:4,
            portLabels(i).port = i;
            portLabels(i).txt = '';
        end
        infoString = ['Error: ' aviinfoException.message];
    end
    
    retPortLabels = portLabels;
    
case 'dynamic'
   
otherwise,
   error('vipblks:vipblkreadavifile:unknownAction', 'Unknown action specified');
end

% [EOF] vipblkreadavifile.m
