function varargout = ImMaths1(varargin)
% Opérations mathématiques pour 1 image
% 
% INPUT:
% - image PxQ
% - cell array des paramètres :
%   + valeur texte opération : '+' | '-' | '*' | '/' | '^' | '='
%   + matrice numérique opérande (1,P) ou (1,1)
%
% OUTPUT:
% - image PxQ
%
% INFOS:
% - saturation des valeurs < 0 et > ValMax
% - opérande (1,1) dupliqué (1,P) si P > 1
%
% Plugin for OpenIsaac
% version 1
% © 2011-2018 Alain Clément - Université d'Angers

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

% Copyright 2007 - 2018 Université d'Angers - Author: Alain Clément
%
% This program is free software; you can redistribute it and/or modify it under the terms of the GNU
% General Public License as published by the Free Software Foundation; either version 3 of the
% License, or any later version. This program is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
% PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received
% a copy of the GNU General Public License along with this program; if not, write to the Free
% Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

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

% PLUGIN PARAMETERS
if (nargin == 1) && strcmp(varargin{1},'-f')
    % input
	varargout{1} = {'IMG','image', ...
					'DATA','cell array des paramètres'};
    % output
	varargout{2} = {'IMG','image résultat'};
	return
end	

% INPUT
ObjIMG = varargin{1};
DataVal = IsaacDATA_get(varargin{2},'Val');
oper = DataVal{1};
value = DataVal{2};

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

% nombre de plans
nbp = IsaacIMG_get(ObjIMG,'DimZ');
nbv = numel(value);
if (nbv ~= nbp)
	if (nbv == 1)
		value = repmat(value,1,nbp);
	else	
		error('image et opérande incompatibles')
	end	
end
% mat en double avec des valeurs non nécessairement comprises entre 0 et 1 (pas une image)
mat = double(IsaacIMG_img2mat(ObjIMG));
switch oper
case '+'
	for k = 1:nbp
		mat(:,:,k) = mat(:,:,k) + value(k);
	end
case '-'
	for k = 1:nbp
		mat(:,:,k) = mat(:,:,k) - value(k);
	end
case '*'
	for k = 1:nbp
		mat(:,:,k) = mat(:,:,k) * value(k);
	end
case '/'
	for k = 1:nbp
		mat(:,:,k) = mat(:,:,k) / value(k);
	end
case '^'
	for k = 1:nbp
		mat(:,:,k) = mat(:,:,k) .^ value(k);
	end
case '='
	for k = 1:nbp
		mat(:,:,k) = value(k);
	end
otherwise
	error('opération invalide')
end
% gestion de la saturation
valmax = IsaacIMG_get(ObjIMG,'ValMax');
mat(mat < 0) = 0;
mat(mat > valmax) = valmax;
% rétablissement du type Matlab
switch IsaacIMG_get(ObjIMG,'BitClass');
case 1
	img = logical(mat);
case 8
	img = uint8(mat);
case 16
	img = uint16(mat);
case 32
	img = single(mat);
case 64
	img = double(mat);
end
ObjIMG = IsaacIMG_mat2img(ObjIMG,img);

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

% OUTPUT
varargout{1} = ObjIMG;
