function varargout = ImGray(varargin)
% Conversion en niveaux de gris
% 
% INPUT:
% - image PxQ
% - valeur texte opération : 'mean' | 'norme' | 'hotelling'
%
% OUTPUT:
% - image 1xQ
%
% INFOS:
% - mean      = (plan1 + ... + planP) / P
% - norme     = (plan1^2 + ... + planP^2)^0.5 / P^0.5
% - hotteling = 1er plan de la transformée d'hotelling  
%
% 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 source', ...
					'DATA','opération'};
    % output
	varargout{2} = {'IMG','image en niveaux de gris'};
	return
end	

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

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

switch oper
	case 'mean'
		img = mean(IsaacIMG_img2mat(ObjIMG,'double'),3);
	case 'norme'
		img = sqrt(sum(IsaacIMG_img2mat(ObjIMG,'double').^2,3))/sqrt(IsaacIMG_get(ObjIMG,'DimZ'));
	case 'hotelling'
		img2 = IsaacIMG_img2mat(IsaacModule('ImHotelling',ObjIMG));
		img = img2(:,:,1);
	otherwise
		error('opération invalide')
end
% suppression de l'espace colorimétrique si il existe
if IsaacIMG_get(ObjIMG,'IsColorSpace')
	ObjIMG = IsaacIMG_set(ObjIMG,'ColorSpace',[]);
end	
ObjIMG = IsaacIMG_mat2img(ObjIMG,img);

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

% OUTPUT
varargout{1} = ObjIMG;	
