%
%  macm 202 -- 11 jan 03 -- djm
%
%  w2convert.m:  converts jpeg or gif image file to 
%                matlab array of monocolor [0,1] data
%    - to run, edit in filename & type "w1convert" at the matlab prompt
%

%  clear workspace & graphics ("help clear", etc)

clear;  figure(1);  clf

%  image filename

%file = 'circles.jpg';
%file = 'escher.gif';
%file = 'lines.jpg';
%file = 'moire.jpg';
%file = 'sfu.jpg';
%file = 'spiral.gif';
%file = 'star.gif';
file = 'triangle.jpeg';

%  read imagefile & convert to matlab array

imdata = imread(file);  imsize  = size(imdata);

switch ndims(imdata)
    case{2} % monocolor image
        imdata  = double(imdata)/255;
    case{3} % RGB 3-color image - convert to monocolor 
        rgb     = reshape(imdata(:),imsize(1)*imsize(2),3);
        
            %  RGB-to-monocolor conversion constants
        T       = inv([1.0 0.956 0.621; 1.0 -0.272 -0.647; 1.0 -1.106 1.703]);

        imdata  = reshape(double(rgb)*T(1,:)'/255,imsize(1),imsize(2));
    otherwise
        disp('something odd about your file, try another');  return
end

imdata = imdata/max(imdata(:));
imdata = min(max(imdata,0),1);  % restrict values to [0,1]

%  optional cropping (must be even for "w2fft")
%  NOTE: reverse order of array indices!  imdata(down,across)

%  this sample line works really well with triangle.jpeg
%imdata = imdata([1:192]+0,[1:192]+18);  
imsize = size(imdata);

%  plot image ("copper" for sepiatone, "gray" for b/w printing)

imagesc(imdata);  
caxis([0 1]);  colormap(copper);  colorbar;  
axis equal;  axis image
title(['\bf monocolor image of ' file])
xlabel(['\bf # of pixels = ' num2str(imsize(2))])
ylabel(['\bf # of pixels = ' num2str(imsize(1))])