function varargout = NivGrisCooccurrence (varargin)
% Calcul de la matrice de cooccurrence d'une image en niveaux de gris
%
% INPUT: 
% - image 1xQ, Q>=8
% - nombre de bits de requantification (1 à 8) pour 2 à 256 niveaux de gris
% - matrice numérique :
%   [0 D]   : angle 0°,   distance D
%   [-D D]  : angle 45°,  distance D
%   [-D 0]  : angle 90°,  distance D
%   [-D -D] : angle 135°, distance D
%
% OUTPUT: 
% - matrice numérique : matrice de cooccurrence
%
% Plugin for OpenIsaac
% version 1
% © 2017-2018 Arthur Berger / 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','nombre de bits de quantification', ...
                  'DATA','matrice offset'};
    % output            
	varargout{2} = {'DATA','matrice de cooccurrence'};
	return
end	

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

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

if (IsaacIMG_get(ObjIMG,'BitClass') < 8) || (IsaacIMG_get(ObjIMG,'DimZ') ~= 1)
	error('image incompatible')
end
% requantification image sur nbb bits (valeurs entre 0 et (2^nbb)-1)
if (nbb ~= IsaacIMG_get(ObjIMG,'NbBits'))
	ObjIMG = IsaacIMG_changebits(ObjIMG,nbb);
end	
img = IsaacIMG_img2mat(ObjIMG);
% en 2^nbb niveaux de gris du min au max
glcm = graycomatrix(img,'NumLevels',2^nbb,'GrayLimits',[],'Offset',DataVal);

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

% OUTPUT
varargout{1} = IsaacDATA('TABN',glcm,'matrice de cooccurrence');