function displaymesh(stain, p_scale, t, adjust)
% displaymesh(stain, p_scale, t, adjust)
% adjust is optional
% adjust:    0 -> no changes
%           +1 -> adjust scale of stain in positive correlation
%           -1 -> adjust scale of stain in negative correlation
%

    if exist('adjust', 'var'),
        if adjust > 0,
            stain = adj_mesh(stain, 0, 255);
        elseif adjust < 0,
            stain = adj_mesh(stain, 255, 0);
        end
    end

    colormap(gray); caxis([1 255])
    axis ij; axis off;
    hold on;
    for i = 1:size(t,1)
        co = stain(i) / 255;
        roic = fix(p_scale(t(i,:),1)');
        roir = fix(p_scale(t(i,:),2)');
        fill(roic,roir,stain(i),'EdgeColor', [co co co]);
    end;
    hold off;
end


function [adj] = adj_mesh(toadj, rmin, rmax)

    if min(toadj) == max(toadj),
        adj = repmat(255, [length(toadj), 1]);
        return;
    end

    to_r = range(toadj);
    to_min = min(toadj);
    adj_r = abs(rmax - rmin);
    
    adj = abs(rmin - ((toadj - to_min) .* (adj_r / to_r)));
    adj(adj > 255) = 255;
end