%
%  Example for image rotation
% 
%  R. Unger 06/2017
%

clear
close all

% read the image and make it double valued in [0,1]
p=imread('map.png');
p=double(p) / 255.0;

% show the image
figure(1)
image(p)

[pic_height pic_width  c]=size(p)

% initialize new picture
p2=zeros(pic_height,pic_width,3);

% create the rotation matrix
phi=45; 

phi=phi*pi/180;
A=zeros(2,2);
A(1,1)= cos(phi);
A(1,2)= sin(phi);
A(2,1)=-sin(phi);
A(2,2)= cos(phi);

x=zeros(2,1);
y=zeros(2,1);


for i=1:pic_height
 for j=1:pic_width

 % make world coordinates from pixel coordinates
 x(1)= j-0.5*pic_width;
 x(2)=-i+0.5*pic_height;

 % apply the transformation
 y=A*x;

 % and back to pixel coordinates from world coordinates 
 k=0.5*pic_height-y(2);
 l=0.5*pic_width +y(1);

 k=round(k);
 l=round(l);

 % use the original pixel if coordinates are in range
 % if not, the pixel becomes black from the initialization
 if (k>0) & (l>0) & (k<=pic_height) & (l<=pic_width)
   p2(i,j,:) = p(k,l,:);
 end
  
 end
end

% show the rotated image
figure(2)
image(p2)




