% Matlab function
% Transformation between two different linear Gray Level Systems
% Jose A. Rodriguez
% Parameters A : matrix to be converted to the other system
           % W : window
           % L: level
% We assume the second system range of intensities is from 0 to 1. So
% one could convert it afterwards to all systems only by multiplying by
% a factor. 
           
function y=WindowLevel(A,L,W);
dims=size(A);
width=dims(1);
height=dims(2);
clear dims;

y=zeros(width, height);

% Apply transformation
x1=L-W/2;
x2=L+W/2;
m = 1/(x2-x1);
b=1-m*x2;
for i=1:width,
  for j=1:height,
    if (A(i,j)x2)
      y(i,j)=1;
    else 
      y(i,j)=m*A(i,j)+b;  
    end  
  end  
end  



    Source: geocities.com/pretabbed/code_matlab

               ( geocities.com/pretabbed)