%ZOOM = DIPFIG_ISNORMALASPECT(AXHANDLE)
%    Finds out if the aspect ratio is 1:1 for the image in AXHANDLE, and
%    returns the pixel size. If not, returns [].

% (C) Copyright 1999-2001               Pattern Recognition Group
%     All rights reserved               Faculty of Applied Physics
%                                       Delft University of Technology
%                                       Lorentzweg 1
%                                       2628 CJ Delft
%                                       The Netherlands
%
% Cris Luengo, November 2000.
% 16-26 August 2001: Changed DIPSHOW. This function changes accordingly.
% 22 August 2001: Changed interface: now only examines the axes properties.
%                 Now we don't assume the axes have ('Units','normalized','Position',[0,0,1,1])
%                 Now supporting 1D images.

function zoom = dipfig_isnormalaspect(axHandle)
zoom = [];
if ~isempty(findobj(axHandle,'type','image'))
   % 2D image
   imageWidth = diff(get(axHandle,'Xlim'));
   imageHeight = diff(get(axHandle,'Ylim'));
   axunits = get(axHandle,'Units');
   set(axHandle,'Units','pixels');
   pos = get(axHandle,'Position');
   set(axHandle,'Units',axunits);
   figureWidth = pos(3);
   figureHeight = pos(4);
   %if ((imageWidth*figureHeight)/(imageHeight*figureWidth)-1) < 0.001 % Allowed error
   if abs((imageWidth*figureHeight/imageHeight)-(figureWidth)) < 1 % Allowed error
      zoom = figureWidth/imageWidth;
   end
else
   % 1D image
   imageWidth = diff(get(axHandle,'Xlim'));
   axunits = get(axHandle,'Units');
   set(axHandle,'Units','pixels');
   pos = get(axHandle,'Position');
   set(axHandle,'Units',axunits);
   figureWidth = pos(3);
   zoom = figureWidth/imageWidth;
end
