function rotangle=searchrot(imrot,code,crosssize,range,update);

% rotangle = SEARCHROT(imrot,code,crosssize,range,update);
%
%     Searches for the unknown rotation angle of the rotated
%     watermarked image IMROT by maximizing the difference
%     between the first two bits of the signature.
%     This function uses the functions ROT and RETRIEVE for
%     signature retrieval.
%
%     range: range of angles used for search, e.g. range=[0:0.5:90]
%     update: if set to 1 the screen will constantly be updated
%             if set to 0 the screen will not be updated
%
%
% University of Rhode Island
% Dept. of Electrical Engineering
% Roger Meier, May '99

diff=zeros(1,length(range));
tic
for k = 1:length(range)
   angle = range(k);
   im = rot(imrot,-angle);
   [signature,delta] = retrieve(im,code,crosssize);
   diff(k) = delta(2)-delta(1);
   if update == 1
		plot(range(1:k),diff(1:k))
   		hold on
		plot(range(1:k),diff(1:k),'x')
		hold off
		v=axis;
		axis([range(1) range(length(range)) v(3) v(4)])
		grid
		drawnow
		disp(['Angle: ' num2str(angle) ',  Signature: ' signature])
	end
end
toc
plot(range,diff)
grid
rotangle=range(find(diff == max(diff)));
disp(' ')
disp(['Rotation angle: ' num2str(rotangle)])
xlabel('Search Angle')
ylabel('Difference \delta^1-\delta^0')