function writebin(image,filename,minimum,maximum)
%WRITEBIN write out binary image file.
%	WRITEBIN(IMAGE,FILENAME) writes out the image in IMAGE to a 
%	binary file called FILENAME after normalizing so that the minimum
%	value maps to zero and the maximum to 255.
%	WRITEBIN(IMAGE,FILENAME,MIN,MAX) scales the image so that 
%	MIN maps to 0 and MAX to 255.


if (nargin < 4)
    image = fix((simage(image)-0.5)*255/64);
else
    scale = 255/(maximum - minimum);
    image = fix(scale * (image - minimum));
    ind = find(image > 255);
    image(ind) = 255 * ones(size(ind));
    ind = find(image < 0 );
    image(ind) = zeros(size(ind));
end

fid = fopen(filename,'w');
fprintf(fid,'%d %d \n',size(image));
fwrite(fid,image','uchar');
fclose(fid);
