function A = myimadjust(A, met, fac)
% myimadjust      Trim or extend image, height and width have given factor. 
%
% If image already match the factors, the image size is not changed.
% The implemetation use only matlab functions.
% 
%  A = myimadjust(A, method, fac)
%-------------------------------------------------------------------------
% arguments:
%   A           an image, (returned as double) and possible larger 
%               output than input
%   method      the method to use: 'none' to cut off 
%               'mirror', 'zeros', 'periodic', 'extend' repeat last 
%   fac         the factor that height and width may be divided by 
%               it may be a single number or two elements, the first is
%               used on the height and the second on the width
%-------------------------------------------------------------------------
% ex:
% x0 = reshape(1:70,5,7,2); 
% x = myimadjust(x0, 'extend', [4,3]); % size(x): 8x9x2

%----------------------------------------------------------------------
% Copyright (c) 2009.  Karl Skretting.  All rights reserved.
% University of Stavanger (Stavanger University), Signal Processing Group
% Mail:  karl.skretting@uis.no   Homepage:  http://www.ux.his.no/~karlsk/
% 
% HISTORY:  dd.mm.yyyy
% Ver. 1.0  25.11.2009  KS: Made the function 
%----------------------------------------------------------------------

if (nargin < 3)
    error('myimadjust: wrong number of input arguments, see help.');
end

if ~(strcmpi(met,'none') || strcmpi(met,'mirror') || ...
     strcmpi(met,'extend') || ...   
     strcmpi(met,'zeros') || strcmpi(met,'periodic') )
    disp(['Illegal method given, ',met,' changed to none']);
    met = 'none';
end

if (numel(fac) == 1); fac = [fac, fac]; end;

[M,N,L] = size(A);
A = double(A);
addRows = mod(fac(1)-mod(M,fac(1)),fac(1));
addCols = mod(fac(2)-mod(N,fac(2)),fac(2));

if ((addRows > 0) || (addCols > 0)) % adjust is needed
    if (L == 1)  % simpler operations can be used
        if strcmpi(met,'none')
            A = A(1:(M-mod(M,fac(1))),1:(N-mod(N,fac(2))));
        elseif strcmpi(met,'extend')
            if (addRows > 0)
                A = [A; ones(addRows,1)*A(end,:)];
            end
            if (addCols > 0)
                A = [A,A(:,end)*ones(1,addCols)];
            end
        elseif strcmpi(met,'periodic')
            if (addRows > 0)
                A = [A; A(1:addRows,:)];
            end
            if (addCols > 0)
                A = [A, A(:,1:addCols)];
            end
        elseif strcmpi(met,'mirror')
            if (addRows > 0)
                A = [A; A(M:(-1):(M-addRows+1),:)];
            end
            if (addCols > 0)
                A = [A, A(:,N:(-1):(N-addCols+1))];
            end
        elseif strcmpi(met,'zeros')
            if (addRows > 0)
                A = [A; zeros(addRows, N)];
            end
            if (addCols > 0)
                A = [A, zeros(M+addRows,addCols)];
            end
        end
    else
        if strcmpi(met,'none')
            A = A(1:(M-mod(M,fac(1))),1:(N-mod(N,fac(2))),:);
        elseif strcmpi(met,'extend')
            if (addRows > 0)
                A = [reshape(permute(A,[2,3,1]),N*L,M), ...
                    kron(ones(1,addRows),reshape(A(end,:,:),N*L,1))];
                M = M + addRows;
                A = permute(reshape(A,N,L,M),[3,1,2]);
            end
            if (addCols > 0)
                A = [reshape(permute(A,[1,3,2]),M*L,N), ...
                    kron(ones(1,addCols),reshape(A(:,end,:),M*L,1))];
                N = N + addCols;
                A = permute(reshape(A,M,L,N),[1,3,2]);
            end
        elseif strcmpi(met,'periodic')
            if (addRows > 0)
                A = reshape(permute(A,[3,2,1]),L*N,M);
                A = [A,A(:,1:addRows)];
                M = M + addRows;
                A = permute(reshape(A,L,N,M),[3,2,1]);
            end
            if (addCols > 0)
                A = reshape(permute(A,[3,1,2]),L*M,N);
                A = [A,A(:,1:addCols)];
                N = N + addCols;
                A = permute(reshape(A,L,M,N),[2,3,1]);
            end
        elseif strcmpi(met,'mirror')
            if (addRows > 0)
                A = reshape(permute(A,[3,2,1]),L*N,M);
                A = [A,A(:,M:(-1):(M-addRows+1))];
                M = M + addRows;
                A = permute(reshape(A,L,N,M),[3,2,1]);
            end
            if (addCols > 0)
                A = reshape(permute(A,[3,1,2]),L*M,N);
                A = [A,A(:,N:(-1):(N-addCols+1))];
                N = N + addCols;
                A = permute(reshape(A,L,M,N),[2,3,1]);
            end
        elseif strcmpi(met,'zeros')
            if (addRows > 0)
                A = reshape(permute(A,[3,2,1]),L*N,M);
                A = [A,zeros(L*N,addRows)];
                M = M + addRows;
                A = permute(reshape(A,L,N,M),[3,2,1]);
            end
            if (addCols > 0)
                A = reshape(permute(A,[3,1,2]),L*M,N);
                A = [A,zeros(L*M,addCols)];
                N = N + addCols;
                A = permute(reshape(A,L,M,N),[2,3,1]);
            end
        end
    end
end

return