function [img, size1, imtype] = readp2(fname)
% [img, size]: image matrix and size vector(width,height, max_gray)
fid = fopen(fname, 'r');
imtype = fread(fid,3,'char'); %Read P2
if imtype(1) ~= 'P' | imtype(2) ~= '2',
  disp([sprintf('Image <%s> is not type P2. It is <%s>.\n', fname, imtype(1:2))]);
  return;
end;
while 1>0,
  com = fscanf(fid,'%s',1);
  if (com(1) == '#'),
    while 1>0,
     ch = fread(fid,1,'char');
     if ch == 10,
       break;
     end;
    end;
  else 
    break;
  end;
end;

height = sscanf(com,'%d',1);  
size2 = fscanf(fid,'%d',2);
fread(fid,1,'char');
%height = size(1);
width = size2(1);
size1 = [height; size2];
%img = zeros(width,height);
%for i=1:height
%img(i,:) = fread(fid,width,'uchar');
%end
img = fscanf(fid,'%d', [height, width]);
img = img';
fclose(fid);
%for i=1:width
%for j=1:height
%      img(i,j) = img1(j,i);
%   end
%end
return









