function [I]=imtrans(img,tt,first_oper)

% ------------------------------------------------------------- %
% THIS VERSION OF IMTRANS WORKS ONLY WITH RIGID TRANSFORMATIONS %
% ------------------------------------------------------------- %
%
%img: IMAGE TO BE TRANSFORMED
%
%tt: TRANSFORMATION PARAMETERS (VECTOR !!!)
%  tt(1): ROTATION (DEGREES !!!)
%  tt(2): TRANSLATION (PIXELS !!!) - Tx
%  tt(3): TRANSLATION (PIXELS !!!) - Ty
%
%first_oper: DETERMINE WHICH OPERATION (['ROTATION'] OR 'TRANSLATION')
%            WILL BE DONE FIRST.


if nargin == 2
    first_oper='ROTATION';
end

tt(1) = tt(1) * 0.0174532925; %CONVERT ANGLE TO RADS
if length(tt) == 1
    tt = [tt(1); 0; 0];
elseif length(tt) == 2
    tt = [tt(1); tt(2); 0];
end


[ROWS COLS] = size(img);
Xm = (COLS+1)/2; Ym = (ROWS+1)/2;

%ROTATION MATRIX
C = cos(tt(1)); S = sin(tt(1));
R = [C -S 0; 
     S  C 0; 
     0  0 1];

%TRANSLATION MATRIX
if strcmp (first_oper, 'ROTATION') == 1 
    T1 = [ 1   0   0; 
           0   1   0; 
          -Xm -Ym  1];
    T2 = [    1         0      0;
              0         1      0;
          tt(2)+Xm  tt(3)+Ym   1];
elseif strcmp (first_oper, 'TRANSLATION') == 1
    T1 = [    1         0      0;
              0         1      0;
          tt(2)-Xm  tt(3)-Ym   1];    
    T2 = [ 1   0   0; 
           0   1   0; 
           Xm  Ym  1];
else
    error ('%s: Unknown transformation operation.',first_oper);
end

%GET THE TRANSFORMATION MATRIX
Q1 = maketform ('affine',T1);
Q2 = maketform ('affine',R);
Q3 = maketform ('affine',T2);
Q  = maketform ('composite',[Q3,Q2,Q1]);

%GET THE IMAGE
% P = zeros(2,4);
% P(:,1) = R(1:2,1:2)*[1-Xm   ; 1-Ym   ] + [tt(2)+Xm; tt(3)+Ym];
% P(:,2) = R(1:2,1:2)*[1-Xm   ; ROWS-Ym] + [tt(2)+Xm; tt(3)+Ym];
% P(:,3) = R(1:2,1:2)*[COLS-Xm; 1-Ym   ] + [tt(2)+Xm; tt(3)+Ym];
% P(:,4) = R(1:2,1:2)*[COLS-Xm; ROWS-Ym] + [tt(2)+Xm; tt(3)+Ym];
%
% I = imtransform (img,Q,'bicubic',...
%     'XData',[min(P(1,:)) max(P(1,:))],...
%     'YData',[min(P(2,:)) max(P(2,:))]);
I = imtransform (img,Q,'bicubic','XData',[1 COLS],'YData',[1 ROWS]);