function h = cvexShowDisparity(d, varargin)
% cvexShowDisparity Show disparity map.
%   cvexShowDisparity(D) shows a disparity map D. The disparity can be any
%   finite value and invalid disparity is represented by
%   -REALMAX('single').
% 
%   cvexShowDisparity(D,L) also specifies L, the value for invalid
%   disparity value.
%
%   H = cvexShowDisparity(...) returns the handle of the figure.
%
% Notes
% -----
%   This is a helper function in support of examples and is subject to
%   change or removal in a future release.
%
% See also disparity.

% Copyright  The MathWorks, Inc.
% $Revision: 1.1.6.1 $  $Date: 2011/02/09 21:14:47 $


if nargin == 1
  label = -realmax('single');
else
  label = varargin{1};
end
mask = (d == label);

% Create a colormap
d(mask) = NaN;
minDis = min(d(:));
maxDis = max(d(:));
scaleFactor = 16;
validRange = (maxDis - minDis) * scaleFactor;
padRange = ceil(validRange * 0.2);
range = padRange + validRange;
clrmap = jet(range);
clrmap(2:padRange,:) = kron(ones(padRange-1,1), clrmap(1,:));

% Show disparity map
v = (d - minDis) * 16 + padRange;
v(mask) = 1;
h = figure;
imshow(v, clrmap);

% Show colorbar
hbar = colorbar;
tick(1, :) = [1, label];
tick(2:6, 1) = padRange: 0.25*validRange: padRange+validRange;
tick(2:6, 2) = minDis: 0.25*(maxDis-minDis): maxDis;
set(hbar, 'YTick', tick(:,1));
set(hbar, 'YTickLabel', ...
  sprintf('Unreliable disparity|%.2f|%.2f|%.2f|%.2f|%.2f', tick(2:end,2)));
