clc                                       % we study all basics in our previous tutorial i.e. of zooming
clear all
a=imread('C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Sunset.jpg');
a=rgb2gray(a);

figure(1);
imshow(a);


[m n]=size(a);

r2=max(max(a)) ;      %this function is used for finding the max value of a pixel in an image
r1=min(min(a)) ;         %this function is used for finding the min value of a pixel in an image
s1=0;                             % this is the min and max of an image i.e. 0-255 gray levels
s2=255;
for i=1:m            %here we use 2 for loops for traversing the image in x & y coordinate
for j=1:n
if(a(i,j) <=r1)%we nave the basic formula (x-x1)/(x2-x1) = (y-y1)/(y2-y1)
s(i,j)=(r1/s1)*(a(i,j));      % we apply this formula on th epoint as shown in figure
else  %and then find the new formula
    if(a(i,j) >=r2)              % the logic is that we shift white pixels on brighter side and
s(i,j)=(((255-s2)/(255-r2))*(a(i,j)-r2))+s2; % black pixels on darker side and stretched the
    else % middle pixels having medium gray levels
s(i,j)=(((s2-s1)/(r2-r1)*(a(i,j)-r1))+(s1));
    end
end
end
end
figure(2);
imshow(s);

