function [] = selectclassifycells(pathname,segmch)
% SELECTCLASSIFYCELLS Tool for making a user-defined data base of cells.
%
%  SELECTCLASSIFYCELLS(PATHNAME,SEGMCH) uses the PATHNAME of either
%  raw data or processed data (.mat files) for the channel SEGMCH. By
%  clicking on the cells they are registered as cells. All non-marked cells
%  are taken as background objects. Remember to save the data by the button
%  'save', otherwise the information is lost.
%
%   See also classifycells, trainclassifycells, combinetrainingset
% 	=======================================================================================
% 	CELLSEGM: Cell segmentation, Version 2012
% 	Copyright (c): Erlend Hodneland
%   Jonas Lies Vei 91,
%   5009 Bergen
%   Email: erlend.hodneland@biomed.uib.no
% 	=======================================================================================

global choice;

choice = 0;
nlist = numel(pathname);

% cellv = cell(0,1);
cellv = cell(nlist,1);

i = 1;
% loop over images
while 1
    
    fileload = pathname{i};
    D = load(fileload);
    data{i} = D;
    dim = size(D.wat);
    plane = round(dim(3)/2);
    
    control.l = 100;
    control.b = 200;    
    control.w = 400;
    control.h = dim(1);
    fig.l = control.l + control.w + 10;
    fig.b = control.b;    
    fig.w = dim(2)*2;
    fig.h = dim(1);
    button.h = 30;
    button.l = 10;
    button.b = dim(1)-2*button.h;
    button.w = 100;

    fig.handle = figure('Position',[fig.l fig.b fig.w fig.h]);
    set(fig.handle,'Toolbar','figure');
    control.handle = figure('Position',[control.l control.b control.w control.h]);
    set(control.handle,'Toolbar','figure');

    % control buttons
    handle.h1 = uicontrol('Parent',control.handle,'Style','PushButton','String','Up', ...
        'Position',[button.l button.b button.w button.h],'CallBack', ...
        'global choice;choice = 1;uiresume(gcbf);');    
    button.b = button.b - button.h;
    handle.h2 = uicontrol('Parent',control.handle,'Style','PushButton','String','Down', ...
        'Position',[button.l button.b button.w button.h],'CallBack', ...
        'global choice;choice = 2;uiresume(gcbf);');    
    button.b = button.b - button.h;
    handle.h3 = uicontrol('Parent',control.handle,'Style','PushButton','String','Previous', ...
        'Position',[button.l button.b button.w button.h],'CallBack', ...
        'global choice;choice = 3;uiresume(gcbf);');    
    button.b = button.b - button.h;
    handle.h3 = uicontrol('Parent',control.handle,'Style','PushButton','String','Next', ...
        'Position',[button.l button.b button.w button.h],'CallBack', ...
        'global choice;choice = 4;uiresume(gcbf);');    
    button.b = button.b - button.h;
    handle.h4 = uicontrol('Parent',control.handle,'Style','PushButton','String','Save', ...
        'Position',[button.l button.b button.w button.h],'CallBack', ...
        'global choice;choice = 5;uiresume(gcbf);');    
    button.b = button.b - button.h;
    handle.h4 = uicontrol('Parent',control.handle,'Style','PushButton','String','Quit', ...
        'Position',[button.l button.b button.w button.h],'CallBack', ...
        'global choice;choice = 6;uiresume(gcbf);');    


    D.binwat = D.wat == 0;
    D.labelled = double(D.binwat)*2;
    v = [];
    [cellv{i},D] = updateinfo(D,v,cellv{i},1);
    
    plotimages(fig,D,plane,segmch);
    while 1
        
        dcmobj = datacursormode(fig.handle);
        datacursormode on;
        while 1
            infostruct = getCursorInfo(dcmobj);
            if ~isempty(infostruct)
                pos = infostruct.Position;
                pos = fliplr(pos);
                v = D.wat(pos(1),pos(2),plane);
                [cellv{i},D] = updateinfo(D,v,cellv{i},0);
                plotimages(fig,D,plane,segmch);

            end;
            if choice > 0
                break;
            end;
            pause(0.1);
        end;
        
        % one up
        if choice == 1
            plane = min(plane+1,dim(3));
        end;
        
        % one down
        if choice == 2
            plane = max(plane-1,1);
        end;
        
        % previous stack
        if choice == 3
            % save the results  
            i = max(i-1,1);
            choice = 0;
            break;
        end;

        % next stack
        if choice == 4
            % save the results            
            i = min(i+1,nlist);
            choice = 0;
            break;
        end;
                
        % save results
        if choice == 5
            [filesave,pathsave] = uiputfile({'*.mat'});
            namesave = [pathsave '/' filesave];
            msg = ['Saving ' namesave];
            disp(msg);
            save(namesave,'cellv','data','segmch');            
        end;
        
        % quit
        if choice == 6
            close(fig.handle);
            close(control.handle);
            return;
        end;
      
        % reset
        choice = 0;
        
        % plot images
        plotimages(fig,D,plane,segmch);
        
    end;
    
    
end;

%---------------------------------------------------
function [cellv,D] = updateinfo(D,v,cellv,new)

if new 
    for j = 1 : numel(cellv)
        D.labelled(D.wat == cellv(j)) = 1;
    end;
end;
if ~isempty(v)
    if ~ismember(v,cellv)
        cellv = [cellv ; v];
        D.labelled(D.wat == v) = 1;
    else
        cellv(cellv == v) = [];
        D.labelled(D.wat == v) = 0;
    end;
end;

%-------------------------------------------------------
% 
% 
% function v = storedata(D)
% 
% v = D.wat(D.labelled == 1);
% v(v == 0) = [];
% v = unique(v);
% 
% %------------------------------------------------


function [] = plotimages(fig,D,plane,segmch)

% plot 1
figure(fig.handle);subplot('Position',[0 0 0.49 1]);
imhere = D.im(:,:,plane,segmch);
imhere = scale(imhere);
imhere = makergb(imhere,D.binwat(:,:,plane),1);
imhere = squeeze(imhere);
image(imhere);axis image;axis off;

% plot 2
figure(fig.handle);subplot('Position',[0.51 0.0 0.49 1]);
imagesc(D.labelled(:,:,plane));colormap(gray);axis image;axis off;

% --------------------------


function [imnew] = makergb(im,bw,ch)

for i = 1 : 3
    imhere = im;
    if i == ch
        imhere(bw == 1) = 1;
    else
        imhere(bw == 1) = 0;
    end;
    imnew(:,:,i) = imhere;
end;
