pixelAxes.m 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. % [ZOOM] = pixelAxes(DIMS, ZOOM)
  2. %
  3. % Set the axes of the current plot to cover a multiple of DIMS pixels,
  4. % thereby eliminating screen aliasing artifacts when displaying an
  5. % image of size DIMS.
  6. %
  7. % ZOOM (optional, default='same') expresses the desired number of
  8. % samples displayed per screen pixel. It should be a scalar, which
  9. % will be rounded to the nearest integer, or 1 over an integer. It
  10. % may also be the string 'same' or 'auto', in which case the value is chosen so
  11. % as to produce an image closest in size to the currently displayed
  12. % image. It may also be the string 'full', in which case the image is
  13. % made as large as possible while still fitting in the window.
  14. % Eero Simoncelli, 2/97.
  15. function [zoom] = pixelAxes(dims, zoom)
  16. %------------------------------------------------------------
  17. %% OPTIONAL ARGS:
  18. if (exist('zoom') ~= 1)
  19. zoom = 'same';
  20. end
  21. %% Reverse dimension order, since Figure Positions reported as (x,y).
  22. dims = dims(2:-1:1);
  23. %% Use MatLab's axis function to force square pixels, etc:
  24. axis('image');
  25. ax = gca;
  26. oldunits = get(ax,'Units');
  27. if strcmp(zoom,'full');
  28. set(ax,'Units','normalized');
  29. set(ax,'Position',[0 0 1 1]);
  30. zoom = 'same';
  31. end
  32. set(ax,'Units','pixels');
  33. pos = get(ax,'Position');
  34. ctr = pos(1:2)+pos(3:4)/2;
  35. if (strcmp(zoom,'same') | strcmp(zoom,'auto'))
  36. %% HACK: enlarge slightly so that floor doesn't round down
  37. zoom = min( pos(3:4) ./ (dims - 1) );
  38. elseif isstr(zoom)
  39. error(sprintf('Bad ZOOM argument: %s',zoom));
  40. end
  41. %% Force zoom value to be an integer, or inverse integer.
  42. if (zoom < 0.75)
  43. zoom = 1/ceil(1/zoom);
  44. %% Round upward, subtracting 0.5 to avoid floating point errors.
  45. newsz = ceil(zoom*(dims-0.5));
  46. else
  47. zoom = floor(zoom + 0.001); % Avoid floating pt errors
  48. if (zoom < 1.5) % zoom=1
  49. zoom = 1;
  50. newsz = dims + 0.5;
  51. else
  52. newsz = zoom*(dims-1) + mod(zoom,2);
  53. end
  54. end
  55. set(ax,'Position', [floor(ctr-newsz/2)+0.5, newsz] )
  56. % Restore units
  57. set(ax,'Units',oldunits);