Skip to content

Commit

Permalink
(3.22) Fixed small potential memory leak during screen-capture; expan…
Browse files Browse the repository at this point in the history
…ded exportgraphics message for vector exports; fixed rotated tick labels on R2021a+
  • Loading branch information
altmany committed Mar 2, 2022
1 parent c7e5859 commit 9371cae
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 19 deletions.
62 changes: 44 additions & 18 deletions export_fig.m
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@
% 25/10/21: (3.19) Fixed print error when exporting a specific subplot (issue #347); avoid duplicate error messages
% 11/12/21: (3.20) Added GIF support, including animated & transparent-background; accept format options as cell-array, not just nested struct
% 20/12/21: (3.21) Speedups; fixed exporting non-current figure (hopefully fixes issue #318); fixed warning when appending to animated GIF
% 02/03/22: (3.22) Fixed small potential memory leak during screen-capture; expanded exportgraphics message for vector exports; fixed rotated tick labels on R2021a+
%}

if nargout
Expand Down Expand Up @@ -365,14 +366,14 @@
[fig, options] = parse_args(nargout, fig, argNames, varargin{:});

% Check for newer version and exportgraphics/copygraphics compatibility
currentVersion = 3.21;
currentVersion = 3.22;
if options.version % export_fig's version requested - return it and bail out
imageData = currentVersion;
return
end
if ~options.silent
% Check for newer version (not too often)
checkForNewerVersion(3.21); % ...(currentVersion) is better but breaks in version 3.05- due to regexp limitation in checkForNewerVersion()
checkForNewerVersion(3.22); % ...(currentVersion) is better but breaks in version 3.05- due to regexp limitation in checkForNewerVersion()

% Hint to users to use exportgraphics/copygraphics in certain cases
alertForExportOrCopygraphics(options);
Expand Down Expand Up @@ -482,25 +483,29 @@
Hlims = findall(fig, 'Type', 'axes');
if ~cls
% Record the old axes limit and tick modes
Xlims = make_cell(get(Hlims, 'XLimMode'));
Ylims = make_cell(get(Hlims, 'YLimMode'));
Zlims = make_cell(get(Hlims, 'ZLimMode'));
Xtick = make_cell(get(Hlims, 'XTickMode'));
Ytick = make_cell(get(Hlims, 'YTickMode'));
Ztick = make_cell(get(Hlims, 'ZTickMode'));
Xlabel = make_cell(get(Hlims, 'XTickLabelMode'));
Ylabel = make_cell(get(Hlims, 'YTickLabelMode'));
Zlabel = make_cell(get(Hlims, 'ZTickLabelMode'));
Xlims = make_cell(get(Hlims, 'XLimMode'));
Ylims = make_cell(get(Hlims, 'YLimMode'));
Zlims = make_cell(get(Hlims, 'ZLimMode'));
Xtick = make_cell(get(Hlims, 'XTickMode'));
Ytick = make_cell(get(Hlims, 'YTickMode'));
Ztick = make_cell(get(Hlims, 'ZTickMode'));
Xlabel = make_cell(get(Hlims, 'XTickLabelMode'));
Ylabel = make_cell(get(Hlims, 'YTickLabelMode'));
Zlabel = make_cell(get(Hlims, 'ZTickLabelMode'));
try % XTickLabelRotation etc. was added in R2021a
Xtkrot = make_cell(get(Hlims, 'XTickLabelRotationMode'));
Ytkrot = make_cell(get(Hlims, 'YTickLabelRotationMode'));
Ztkrot = make_cell(get(Hlims, 'ZTickLabelRotationMode'));
catch
end % only in R2021a+
end

% Set all axes limit and tick modes to manual, so the limits and ticks can't change
% Fix Matlab R2014b bug (issue #34): plot markers are not displayed when ZLimMode='manual'
set(Hlims, 'XLimMode', 'manual', 'YLimMode', 'manual');
set_tick_mode(Hlims, 'X');
set_tick_mode(Hlims, 'Y');
set_manual_axes_modes(Hlims, 'X');
set_manual_axes_modes(Hlims, 'Y');
if ~using_hg2(fig)
set(Hlims,'ZLimMode', 'manual');
set_tick_mode(Hlims, 'Z');
set_manual_axes_modes(Hlims, 'Z');
end
catch
% ignore - fix issue #4 (using HG2 on R2014a and earlier)
Expand Down Expand Up @@ -1155,7 +1160,14 @@
try
set(Hlims(a), 'XLimMode', Xlims{a}, 'YLimMode', Ylims{a}, 'ZLimMode', Zlims{a},...
'XTickMode', Xtick{a}, 'YTickMode', Ytick{a}, 'ZTickMode', Ztick{a},...
'XTickLabelMode', Xlabel{a}, 'YTickLabelMode', Ylabel{a}, 'ZTickLabelMode', Zlabel{a});
'XTickLabelMode', Xlabel{a}, 'YTickLabelMode', Ylabel{a}, 'ZTickLabelMode', Zlabel{a});
try % only in R2021a+
set(Hlims(a), 'XTickLabelRotationMode', Xtkrot{a}, ...
'YTickLabelRotationMode', Ytkrot{a}, ...
'ZTickLabelRotationMode', Ztkrot{a});
catch
% ignore - possibly R2020b or earlier
end
catch
% ignore - fix issue #4 (using HG2 on R2014a and earlier)
end
Expand Down Expand Up @@ -1891,7 +1903,10 @@ function add_bookmark(fname, bookmark_text)
fclose(fh);
end

function set_tick_mode(Hlims, ax)
function set_manual_axes_modes(Hlims, ax)
% Set the axes limits mode to manual
set(Hlims, [ax 'LimMode'], 'manual');

% Set the tick mode of linear axes to manual
% Leave log axes alone as these are tricky
M = get(Hlims, [ax 'Scale']);
Expand All @@ -1909,6 +1924,14 @@ function set_tick_mode(Hlims, ax)
props = {[ax 'TickMode'],'manual', [ax 'TickLabelMode'],'manual'};
tickVals = get(hAxes,[ax 'Tick']);
tickStrs = get(hAxes,[ax 'TickLabel']);
try % TickLabelRotation is available since R2021a
propName = [ax,'TickLabelRotationMode'];
if ~isempty(get(hAxes,propName)) %this will croak in R2020b-
props = [props, propName,'manual']; %#ok<AGROW>
end
catch
% ignore - probably R2020b or older
end
try % Fix issue #236
exponents = [hAxes.([ax 'Axis']).SecondaryLabel];
catch
Expand Down Expand Up @@ -2323,6 +2346,9 @@ function displayMsg(params, funcName, type, filenameParam)
handleName = 'hFigure';
end
msg = ['In Matlab R2020a+ you can also use ' funcName '(' handleName filenameParam params ') for simple ' type ' export'];
if ~isempty(strfind(params,'''vector''')) %#ok<STREMP>
msg = [msg ', which could also improve image vectorization, solving rasterization/pixelization problems.'];
end
oldWarn = warning('on','verbose');
warning(['export_fig:' funcName], msg);
warning(oldWarn);
Expand Down
7 changes: 6 additions & 1 deletion print2array.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
% 26/08/21: Added a short pause to avoid unintended image cropping (issue #318)
% 25/10/21: Avoid duplicate error message when retrying print2array with different resolution; display internal print error message
% 19/12/21: Speedups; fixed exporting non-current figure (hopefully fixes issue #318)
% 22/12/21: Avoid memory leak during screen-capture
%}

% Generate default input arguments, if needed
Expand Down Expand Up @@ -290,7 +291,8 @@
import java.awt.image.BufferedImage
try TYPE_INT_RGB = BufferedImage.TYPE_INT_RGB; catch, TYPE_INT_RGB = 1; end
jImage = BufferedImage(w, h, TYPE_INT_RGB);
jPanel.paint(jImage.createGraphics);
jGraphics = jImage.createGraphics;
jPanel.paint(jGraphics);
jPanel.paint(jOriginalGraphics); % repaint original figure to avoid a blank window

% Extract the RGB pixels from the BufferedImage (see screencapture.m)
Expand All @@ -303,6 +305,9 @@
% And now also the alpha channel (if available)
alpha = transpose(reshape(pixelsData(4, :, :), w, h));

% Avoid memory leaks (see \toolbox\matlab\toolstrip\+matlab\+ui\+internal\+toolstrip\Icon.m>localFromImgToURL)
jGraphics.dispose();

% Ensure that the results are the expected size, otherwise raise an error
figSize = getpixelposition(hFig);
expectedSize = [figSize(4), figSize(3), 3];
Expand Down

0 comments on commit 9371cae

Please sign in to comment.