function imagearray = imageconvert(varargin);

%Function imageconvert
%Purpose - Converts crystal image into 1024x1024 gray scale array
%
%Inputs -   
%           filename - Name of crystal image file (or complete path) 
%           (e.g. 'i_0_01.jpg' or '0a\i_0_01.jpg' (need single quotations))
%           dirname  - Name of crystal directory
%           (e.g. '0' or '5' (defaults to .))
%
%Outputs - imagearray - 1024x1024 grayscale array containing image info
%
%Example Use: im = imageconvert('0a','i_0_0.jpg');
%OR           im = imageconvert('0a\i_0_0.jpg');

if size(varargin,2) == 2
    realfilename = [varargin{1},'\',varargin{2}];
elseif size(varargin,1) == 1
    realfilename = varargin{1};
else
    'Error in imageconvert : Need to pass filename as arguments'
    exit;
end

%Read in file
r = imread(realfilename);

%Change to grayscale
rgb = reshape(r(:),size(r,1)*size(r,2),3);
a = zeros([size(r,1), size(r,2)]);
T = inv([1.0 0.956 0.621; 1.0 -0.272 -0.647; 1.0 -1.106 1.703]);
a = uint8(reshape(double(rgb)*T(1,:)', size(a)));
imgray = [a,a,a];

%Crop dimensions
[l,w] = size(imgray);
imagearray = imgray(:,1:w/3);




