function displaymeshco(inp_stain, p_scale, t, adjust, printer_scheme)
% displaymeshco(stain, p_scale, t, adjust, printer_scheme)
% adjust is optional
% adjust:    0 -> no changes
%           +1 -> adjust scale of stain in positive correlation
%           -1 -> adjust scale of stain in negative correlation
% printer_scheme is optional
% displays image in CMYK scheme which is better for printouts
% inp_stain must be limited to 2 stains
% printer_scheme: 'cy' or 'yc' Cyan/Yellow or Yellow/Cyan (1st/2nd stain)
%                 'mc' or 'cm' Mangenta/Cyan or Cyan/Mangenta

    [s_dim, s_pos] = min(size(inp_stain));
    if s_dim == 1,
        stain = inp_stain;
    elseif s_dim <= 3,
        if s_pos == 1,
            stain = inp_stain';
        else
            stain = inp_stain;
        end
    else
        error('Stain should have 1 to 3 dimensions, found %d dimensions', s_dim);
    end


    if exist('adjust', 'var'),
        for i = 1:s_dim, 
            if adjust > 0,
                stain(:,i) = adj_mesh(stain(:,i), 0, 255);
            elseif adjust < 0,
                stain(:,i) = adj_mesh(stain(:,i), 255, 0);
            end
        end
    end

    
    if s_dim == 1,
        cmesh = repmat(stain ./ 255, [1, 3]);
    else
        cmesh = zeros(size(stain,1), 3);
        
        if exist('printer_scheme', 'var'),
            if s_dim ~= 2,
                error('Printer schemes only works with 2 dimensions, found %d dimensions', s_dim);
            end
          
            cy = 0;
            if strcmp(printer_scheme, 'cy'),
                s_A = 1;
                s_B = 2;
                cy=1;
            elseif strcmp(printer_scheme, 'yc'),
                s_A = 2;
                s_B = 1;
                cy=1;
            elseif strcmp(printer_scheme, 'mc'),
                s_A = 1;
                s_B = 2;
            elseif strcmp(printer_scheme, 'cm'),
                s_A = 2;
                s_B = 1;
            else 
                error('Printer scheme ''%s'' not implemented', printer_scheme);
            end
            
            if cy == 1,
                m_A = zeros(size(stain,1), 3);
                m_A(:,3) = 255;
                m_A(:,1:2) = repmat(stain(:,s_A), [1,2]);

                m_B = zeros(size(stain,1), 3);
                m_B(:,1:2) = 255;
                m_B(:,3) = stain(:,s_B);                
                for i=1:3,
                    cmesh(:,i) = min(m_A(:,i), m_B(:,i)) ./ 255;
                end
            else
                m_A = zeros(size(stain,1), 3);
                m_A(:,3) = 255;
                m_A(:,1:2) = repmat(stain(:,s_A), [1,2]);

                m_B = zeros(size(stain,1), 3);
                m_B(:,2:3) = 0;
                m_B(:,1) = stain(:,s_B);                
                for i=1:3,
                    cmesh(:,i) = max(m_A(:,i), m_B(:,i)) ./ 255;
                end
            end
            
            
        else
        
            for i = 1:s_dim,
                cmesh(:,i) = (255 - stain(:,i)) ./ 255;
            end
        end
    end
    
    
    % colormap(gray); caxis([1 255])
    axis ij; axis off;
    hold on;
    for i = 1:size(t,1)
        roic = fix(p_scale(t(i,:),1)');
        roir = fix(p_scale(t(i,:),2)');
        fill(roic,roir,cmesh(i,:),'EdgeColor', cmesh(i,:));
    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