function [img] = writep2(xx,fname)
% [img, size]: image matrix and size vector(width,height, max_gray)
fid = fopen(fname, 'wb');
comment = ['Writing ' fname ' Please wait ...'];
disp(comment);
magic = [sprintf('P2\n')];
fprintf(fid,'%c',magic); %Write P5
%img = xx';
img = round(xx)';
siz = size(img);
height = siz(2);
width = siz(1);
maxgray = max(max(img));
header1 = [sprintf('%d %d\n%d\n',width, height,maxgray)];
fprintf(fid,'%c',header1);
fprintf(fid, '%d ', img);
fclose(fid);
comment = ['Writep5 is Done.'];
disp(comment);
return
