ParaMonte MATLAB 3.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
print2array.m
Go to the documentation of this file.
1function [A, bcol, alpha] = print2array(fig, res, renderer, gs_options)
2%PRINT2ARRAY Exports a figure to a bitmap RGB image array
3%
4% Examples:
5% A = print2array
6% A = print2array(figure_handle)
7% A = print2array(figure_handle, resolution)
8% A = print2array(figure_handle, resolution, renderer)
9% A = print2array(figure_handle, resolution, renderer, gs_options)
10% [A, bcol, alpha] = print2array(...)
11%
12% This function outputs a bitmap image of a figure, at the desired resolution.
13%
14% When resolution==1, fast Java screen-capture is attempted first.
15% If the Java screen-capture fails or if resolution~=1, the builtin print()
16% function is used to create a temp TIF file, which is then loaded and reported.
17% If this fails, print() is used to create a temp EPS file which is converted to
18% a TIF file using Ghostcript (http://www.ghostscript.com), loaded and reported.
19%
20% Inputs:
21% figure_handle - The handle of the figure to be exported. Default: gcf.
22% resolution - Output resolution as a factor of screen resolution. Default: 1
23% Note: resolution ~= 1 uses a slow print to/from image file
24% renderer - The renderer to be used by print() function. Default: '-opengl'
25% Note: only used when resolution ~= 1
26% gs_options - optional ghostscript parameters (e.g.: '-dNoOutputFonts').
27% Enclose multiple options in a cell array, e.g. {'-a','-b'}
28% Note: only used when resolution ~= 1 and basic print() fails
29%
30% Outputs:
31% A - MxNx3 uint8 bitmap image of the figure (MxN pixels x 3 RGB values)
32% bcol - 1x3 uint8 vector of the background RGB color
33% alpha - MxN uint8 array of alpha values (between 0=transparent, 255=opaque)
34
35% Copyright (C) Oliver Woodford 2008-2014, Yair Altman 2015-
36%{
37% 05/09/11: Set EraseModes to normal when using opengl or zbuffer
38% renderers. Thanks to Pawel Kocieniewski for reporting the issue.
39% 21/09/11: Bug fix: unit8 -> uint8! Thanks to Tobias Lamour for reporting it.
40% 14/11/11: Bug fix: stop using hardcopy(), as it interfered with figure size
41% and erasemode settings. Makes it a bit slower, but more reliable.
42% Thanks to Phil Trinh and Meelis Lootus for reporting the issues.
43% 09/12/11: Pass font path to ghostscript.
44% 27/01/12: Bug fix affecting painters rendering tall figures. Thanks to
45% Ken Campbell for reporting it.
46% 03/04/12: Bug fix to median input. Thanks to Andy Matthews for reporting it.
47% 26/10/12: Set PaperOrientation to portrait. Thanks to Michael Watts for
48% reporting the issue.
49% 26/02/15: If temp dir is not writable, use the current folder for temp
50% EPS/TIF files (Javier Paredes)
51% 27/02/15: Display suggested workarounds to internal print() error (issue #16)
52% 28/02/15: Enable users to specify optional ghostscript options (issue #36)
53% 10/03/15: Fixed minor warning reported by Paul Soderlind; fixed code indentation
54% 28/05/15: Fixed issue #69: patches with LineWidth==0.75 appear wide (internal bug in Matlab's print() func)
55% 07/07/15: Fixed issue #83: use numeric handles in HG1
56% 11/12/16: Fixed cropping issue reported by Harry D.
57% 29/09/18: Fixed issue #254: error in print2array>read_tif_img
58% 22/03/20: Alert if ghostscript.m is required but not found on Matlab path
59% 24/05/20: Significant performance speedup; added alpha values (where possible)
60% 07/07/20: Fixed issue #308: bug in R2019a and earlier
61% 07/10/20: Use JavaFrame_I where possible, to avoid evoking a JavaFrame warning
62% 07/03/21: Fixed edge-case in case a non-figure handle was provided as input arg
63% 10/03/21: Forced a repaint at top of function to ensure accurate image snapshot (issue #211)
64% 26/08/21: Added a short pause to avoid unintended image cropping (issue #318)
65% 25/10/21: Avoid duplicate error message when retrying print2array with different resolution; display internal print error message
66% 19/12/21: Speedups; fixed exporting non-current figure (hopefully fixes issue #318)
67% 22/12/21: Avoid memory leak during screen-capture
68% 30/03/23: Added another short pause to avoid unintended image cropping (issue #318)
69%}
70
71 % Generate default input arguments, if needed
72 if nargin < 1, fig = gcf; end
73 if nargin < 2, res = 1; end
74
75 % Force a repaint to ensure we get an accurate snapshot image (issue #211)
76 drawnow
77
78 % Get the figure size in pixels
79 old_mode = get(fig, 'Units');
80 set(fig, 'Units', 'pixels');
81 px = get(fig, 'Position');
82 set(fig, 'Units', old_mode);
83
84 pause(0.05); % add a short pause to avoid unintended cropping (issue #318)
85
86 % Retrieve the background colour
87 bcol = get(fig, 'Color');
88 try
89 % Try a direct Java screen-capture first - *MUCH* faster than print() to file
90 % Note: we could also use A=matlab.graphics.internal.getframeWithDecorations(fig,false) but it (1) returns no alpha and (2) does not exist in older Matlabs
91 if res == 1
92 [A, alpha] = getJavaImage(fig);
93 else
94 error('magnify/downscale via print() to image file and then import');
95 end
96 catch err %#ok<NASGU>
97 % Warn if output is large
98 npx = prod(px(3:4)*res)/1e6;
99 if npx > 30
100 % 30M pixels or larger!
101 warning('MATLAB:LargeImage', 'print2array generating a %.1fM pixel image. This could be slow and might also cause memory problems.', npx);
102 end
103 % Set the resolution parameter
104 res_str = ['-r' num2str(ceil(get(0, 'ScreenPixelsPerInch')*res))];
105 % Generate temporary file name
106 tmp_nam = [tempname '.tif'];
107 try
108 % Ensure that the temp dir is writable (Javier Paredes 26/2/15)
109 fid = fopen(tmp_nam,'w');
110 fwrite(fid,1);
111 fclose(fid);
112 delete(tmp_nam); % cleanup
113 isTempDirOk = true;
114 catch
115 % Temp dir is not writable, so use the current folder
116 [dummy,fname,fext] = fileparts(tmp_nam); %#ok<ASGLU>
117 fpath = pwd;
118 tmp_nam = fullfile(fpath,[fname fext]);
119 isTempDirOk = false;
120 end
121 % Enable users to specify optional ghostscript options (issue #36)
122 isRetry = false;
123 if nargin > 3 && ~isempty(gs_options)
124 if isequal(gs_options,'retry')
125 isRetry = true;
126 gs_options = '';
127 elseif iscell(gs_options)
128 gs_options = sprintf(' %s',gs_options{:});
129 elseif ~ischar(gs_options)
130 error('gs_options input argument must be a string or cell-array of strings');
131 else
132 gs_options = [' ' gs_options];
133 end
134 else
135 gs_options = '';
136 end
137 if nargin > 2 && strcmp(renderer, '-painters')
138 % First try to print directly to image file
139 try
140 % Print the file into a temporary image file and read it into array A
141 [A, alpha, err, ex] = getPrintImage(fig, res_str, renderer, tmp_nam);
142 if err, rethrow(ex); end
143 catch % error - try to print to EPS and then using Ghostscript to TIF
144 % Ensure that ghostscript() exists on the Matlab path
145 if ~exist('ghostscript','file') && isempty(which('ghostscript'))
146 error('export_fig:print2array:ghostscript', 'The ghostscript.m function is required by print2array.m. Install the complete export_fig package from https://www.mathworks.com/matlabcentral/fileexchange/23629-export_fig or https://github.com/altmany/export_fig')
147 end
148 % Print to eps file
149 if isTempDirOk
150 tmp_eps = [tempname '.eps'];
151 else
152 tmp_eps = fullfile(fpath,[fname '.eps']);
153 end
154 print2eps(tmp_eps, fig, 0, renderer, '-loose');
155 try
156 % Initialize the command to export to tiff using ghostscript
157 cmd_str = ['-dEPSCrop -q -dNOPAUSE -dBATCH ' res_str ' -sDEVICE=tiff24nc'];
158 % Set the font path
159 fp = font_path();
160 if ~isempty(fp)
161 cmd_str = [cmd_str ' -sFONTPATH="' fp '"'];
162 end
163 % Add the filenames
164 cmd_str = [cmd_str ' -sOutputFile="' tmp_nam '" "' tmp_eps '"' gs_options];
165 % Execute the ghostscript command
166 ghostscript(cmd_str);
167 catch me
168 % Delete the intermediate file
169 delete(tmp_eps);
170 rethrow(me);
171 end
172 % Delete the intermediate file
173 delete(tmp_eps);
174 % Read in the generated bitmap
175 A = imread(tmp_nam);
176 % Delete the temporary bitmap file
177 delete(tmp_nam);
178 end
179 else
180 if nargin < 3
181 renderer = '-opengl';
182 end
183 % Print the file into a temporary image file and read it into array A
184 [A, alpha, err, ex] = getPrintImage(fig, res_str, renderer, tmp_nam);
185 % Throw any error that occurred
186 if err
187 % Display suggested workarounds to internal print() error (issue #16)
188 if ~isRetry
189 fprintf(2, 'An error occurred in Matlab''s builtin print function:\n%s\nTry setting the figure Renderer to ''painters'' or use opengl(''software'').\n\n', ex.message);
190 end
191 rethrow(ex);
192 end
193 end
194 end
195
196 % Set the background color
197 if isequal(bcol, 'none')
198 bcol = squeeze(A(1,1,:));
199 if ~all(bcol==0) %if not black
200 bcol = [255,255,255]; %=white %=[];
201 end
202 else
203 if all(bcol <= 1)
204 bcol = bcol * 255;
205 end
206 if ~isequal(bcol, round(bcol))
207 bcol = squeeze(A(1,1,:));
208 %{
209 % Set border pixels to the correct colour
210 for l = 1:size(A, 2)
211 if ~all(reshape(A(:,l,:) == 255, [], 1))
212 break;
213 end
214 end
215 for r = size(A, 2):-1:l
216 if ~all(reshape(A(:,r,:) == 255, [], 1))
217 break;
218 end
219 end
220 for t = 1:size(A, 1)
221 if ~all(reshape(A(t,:,:) == 255, [], 1))
222 break;
223 end
224 end
225 for b = size(A, 1):-1:t
226 if ~all(reshape(A(b,:,:) == 255, [], 1))
227 break;
228 end
229 end
230 bcol = median(single([reshape(A(:,[l r],:), [], size(A, 3)); ...
231 reshape(A([t b],:,:), [], size(A, 3))]), 1));
232 for c = 1:size(A, 3)
233 A(:,[1:l-1, r+1:end],c) = bcol(c);
234 A([1:t-1, b+1:end],:,c) = bcol(c);
235 end
236 %}
237 end
238 end
239 bcol = uint8(bcol);
240
241 % Ensure that the output size is correct
242 if isequal(res, round(res))
243 px = round([px([4 3])*res 3]); % round() to avoid an indexing warning below
244 if any(size(A) > px) %~isequal(size(A), px)
245 A = A(1:min(end,px(1)),1:min(end,px(2)),:);
246 end
247 if any(size(alpha) > px(1:2))
248 alpha = alpha(1:min(end,px(1)),1:min(end,px(2)));
249 end
250 end
251end
252
253% Get the Java-based screen-capture of the figure's JFrame content-panel
254function [imgData, alpha] = getJavaImage(hFig)
255 % Get the figure's underlying Java frame
256 oldWarn = warning('off','MATLAB:HandleGraphics:ObsoletedProperty:JavaFrame');
257 warning('off','MATLAB:ui:javaframe:PropertyToBeRemoved');
258 try
259 jf = get(handle(hFig),'JavaFrame_I');
260 catch
261 jf = get(handle(hFig),'JavaFrame'); %#ok<JAVFM>
262 end
263 warning(oldWarn);
264
265 % Get the Java frame's root frame handle
266 %jframe = jf.getFigurePanelContainer.getComponent(0).getRootPane.getParent;
267 try
268 jClient = jf.fHG2Client; % This works from R2014b and up
269 catch
270 try
271 jClient = jf.fHG1Client; % This works from R2008b-R2014a
272 catch
273 jClient = jf.fFigureClient; % This works up to R2011a
274 end
275 end
276
277 % Get the content-pane
278 try
279 jPanel = jClient.getContentPane;
280 catch
281 jPanel = jClient.getFigurePanelContainer;
282 end
283 jPanel.repaint;
284 w = jPanel.getWidth;
285 h = jPanel.getHeight;
286
287 % Create a BufferedImage and paint the content-pane into it
288 % (https://coderanch.com/t/470601/java/screenshot-JPanel)
289 % Note: contrary to documentation and common-sense, it turns out that TYPE_INT_RGB
290 % ^^^^ returns non-opaque alpha, while TYPE_INT_ARGB only returns 255s in the alpha channel
291 jOriginalGraphics = jPanel.getGraphics;
292 import java.awt.image.BufferedImage
293 try TYPE_INT_RGB = BufferedImage.TYPE_INT_RGB; catch, TYPE_INT_RGB = 1; end
294 jImage = BufferedImage(w, h, TYPE_INT_RGB);
295 jGraphics = jImage.createGraphics;
296 pause(0.05); % add a short pause to avoid unintended cropping (issue #318)
297 jPanel.paint(jGraphics);
298 jPanel.paint(jOriginalGraphics); % repaint original figure to avoid a blank window
299 pause(0.05); % add a short pause to avoid unintended cropping (issue #318)
300
301 % Extract the RGB pixels from the BufferedImage (see screencapture.m)
302 pixelsData = reshape(typecast(jImage.getData.getDataStorage, 'uint8'), 4, w, h);
303 imgData = cat(3, ...
304 transpose(reshape(pixelsData(3, :, :), w, h)), ...
305 transpose(reshape(pixelsData(2, :, :), w, h)), ...
306 transpose(reshape(pixelsData(1, :, :), w, h)));
307
308 % And now also the alpha channel (if available)
309 alpha = transpose(reshape(pixelsData(4, :, :), w, h));
310
311 % Avoid memory leaks (see \toolbox\matlab\toolstrip\+matlab\+ui\+internal\+toolstrip\Icon.m>localFromImgToURL)
312 jGraphics.dispose();
313
314 % Ensure that the results are the expected size, otherwise raise an error
315 figSize = getpixelposition(hFig);
316 expectedSize = [figSize(4), figSize(3), 3];
317 if ~isequal(expectedSize, size(imgData))
318 error('bad Java screen-capture size!')
319 end
320end
321
322% Export an image file of the figure using print() and then read it into an array
323function [imgData, alpha, err, ex] = getPrintImage(fig, res_str, renderer, tmp_nam)
324 imgData = []; % fix for issue #254
325 err = false;
326 ex = [];
327 alpha = [];
328 % Temporarily set the paper size
329 fig = ancestor(fig, 'figure'); % just in case it's not a figure...
330 old_pos_mode = get(fig, 'PaperPositionMode');
331 old_orientation = get(fig, 'PaperOrientation');
332 set(fig, 'PaperPositionMode','auto', 'PaperOrientation','portrait');
333 try
334 % Workaround for issue #69: patches with LineWidth==0.75 appear wide (internal bug in Matlab's print() function)
335 fp = []; % in case we get an error below
336 fp = findall(fig, 'Type','patch', 'LineWidth',0.75);
337 set(fp, 'LineWidth',0.5);
338 try %if using_hg2(fig) % HG2 (R2014b or newer)
339 % Use print('-RGBImage') directly (a bit faster than via temp image file)
340 imgData = print(fig, renderer, res_str, '-RGBImage');
341 catch %else % HG1 (R2014a or older)
342 % Fix issue #83: use numeric handles in HG1
343 fig = double(fig);
344 % Print to image file
345 print(fig, renderer, res_str, '-dtiff', tmp_nam);
346 imgData = imread(tmp_nam);
347 % Delete the temporary file
348 delete(tmp_nam);
349 end
350 imgSize = size(imgData); imgSize = imgSize([1,2]); % Fix issue #308
351 alpha = 255 * ones(imgSize, 'uint8'); % =all pixels opaque
352 catch ex
353 err = true;
354 end
355 if ~isempty(fp) % this check is not really needed, but makes the code cleaner
356 set(fp, 'LineWidth',0.75); % restore original figure appearance
357 end
358 % Reset the paper size
359 set(fig, 'PaperPositionMode',old_pos_mode, 'PaperOrientation',old_orientation);
360end
361
362% Return (and create, where necessary) the font path (for use by ghostscript)
363function fp = font_path()
364 fp = user_string('gs_font_path');
365 if ~isempty(fp)
366 return
367 end
368 % Create the path
369 % Start with the default path
370 fp = getenv('GS_FONTPATH');
371 % Add on the typical directories for a given OS
372 if ispc
373 if ~isempty(fp)
374 fp = [fp ';'];
375 end
376 fp = [fp getenv('WINDIR') filesep 'Fonts'];
377 else
378 if ~isempty(fp)
379 fp = [fp ':'];
380 end
381 fp = [fp '/usr/share/fonts:/usr/local/share/fonts:/usr/share/fonts/X11:/usr/local/share/fonts/X11:/usr/share/fonts/truetype:/usr/local/share/fonts/truetype'];
382 end
383 user_string('gs_font_path', fp);
384end
function ghostscript(in cmd)
function font_path()
function getPrintImage(in fig, in res_str, in renderer, in tmp_nam)
function print2array(in fig, in res, in renderer, in gs_options)
function getJavaImage(in hFig)
function print2eps(in name, in fig, in export_options, in varargin)
function which(in vendor)
Return the a MATLAB string containing the path to the first mpiexec executable binary found in system...