1 %PRINT2ARRAY Exports a figure to an image array
5 % A = print2array(figure_handle)
6 % A = print2array(figure_handle, resolution)
7 % A = print2array(figure_handle, resolution, renderer)
8 % [A bcol] = print2array(...)
10 % This
function outputs a bitmap image of the given figure, at the desired
13 % If renderer is
'-painters' then ghostcript needs to be installed. This
14 % can be downloaded from: http:
17 % figure_handle - The handle of the figure to be exported. Default: gcf.
18 % resolution - Resolution of the output, as a factor of screen
19 % resolution. Default: 1.
20 % renderer -
string containing the renderer paramater to be passed to
21 % print. Default:
'-opengl'.
24 % A - MxNx3 uint8 image of the figure.
25 % bcol - 1x3 uint8 vector of the background color
27 % Copyright (C) Oliver Woodford 2008-2012
29 % 05/09/11: Set EraseModes to normal when using opengl or zbuffer
30 % renderers. Thanks to Pawel Kocieniewski for reporting the
32 % 21/09/11: Bug fix: unit8 -> uint8! Thanks to Tobias Lamour for reporting
34 % 14/11/11: Bug fix: stop using hardcopy(), as it interfered with figure
35 % size and erasemode settings. Makes it a bit slower, but more
36 % reliable. Thanks to Phil Trinh and Meelis Lootus for reporting
38 % 09/12/11: Pass font path to ghostscript.
39 % 27/01/12: Bug fix affecting painters rendering tall figures. Thanks to
40 % Ken Campbell for reporting it.
41 % 03/04/12: Bug fix to median input. Thanks to Andy Matthews for reporting
43 % 26/10/12: Set PaperOrientation to portrait. Thanks to Michael Watts for
44 % reporting the issue.
46 function [A, bcol] = print2array(fig, res, renderer)
47 % Generate default input arguments, if needed
54 % Warn if output is large
55 old_mode = get(fig, 'Units');
56 set(fig, 'Units', 'pixels');
57 px = get(fig, 'Position');
58 set(fig, 'Units', old_mode);
59 npx = prod(px(3:4)*res)/1e6;
61 % 30M pixels or larger!
62 warning('MATLAB:LargeImage', 'print2array generating a %.1fM pixel image. This could be slow and might also cause memory problems.', npx);
64 % Retrieve the background colour
65 bcol = get(fig, 'Color');
66 % Set the resolution parameter
67 res_str = ['-r' num2str(ceil(get(0, 'ScreenPixelsPerInch')*res))];
68 % Generate temporary file name
69 tmp_nam = [tempname '.tif'];
70 if nargin > 2 && strcmp(renderer, '-painters')
72 tmp_eps = [tempname '.eps'];
73 print2eps(tmp_eps, fig, renderer, '-loose');
75 % Initialize the command to export to tiff using ghostscript
76 cmd_str = ['-dEPSCrop -q -dNOPAUSE -dBATCH ' res_str ' -sDEVICE=tiff24nc'];
80 cmd_str = [cmd_str ' -sFONTPATH="' fp '"'];
83 cmd_str = [cmd_str ' -sOutputFile="' tmp_nam '" "' tmp_eps '"'];
84 % Execute the ghostscript command
87 % Delete the intermediate file
91 % Delete the intermediate file
93 % Read in the generated bitmap
95 % Delete the temporary bitmap file
97 % Set border pixels to the correct colour
98 if isequal(bcol, 'none')
100 elseif isequal(bcol, [1 1 1])
101 bcol = uint8([255 255 255]);
104 if ~all(reshape(A(:,l,:) == 255, [], 1))
108 for r = size(A, 2):-1:l
109 if ~all(reshape(A(:,r,:) == 255, [], 1))
114 if ~all(reshape(A(t,:,:) == 255, [], 1))
118 for b = size(A, 1):-1:t
119 if ~all(reshape(A(b,:,:) == 255, [], 1))
123 bcol = uint8(median(single([reshape(A(:,[l r],:), [], size(A, 3)); reshape(A([t b],:,:), [], size(A, 3))]), 1));
125 A(:,[1:l-1, r+1:end],c) = bcol(c);
126 A([1:t-1, b+1:end],:,c) = bcol(c);
131 renderer = '-opengl';
135 old_pos_mode = get(fig, 'PaperPositionMode');
136 old_orientation = get(fig, 'PaperOrientation');
137 set(fig, 'PaperPositionMode', 'auto', 'PaperOrientation', 'portrait');
140 print(fig, renderer, res_str, '-dtiff', tmp_nam);
141 % Read in the printed file
143 % Delete the temporary file
149 set(fig, 'PaperPositionMode', old_pos_mode, 'PaperOrientation', old_orientation);
150 % Throw any error that occurred
154 % Set the background color
155 if isequal(bcol, 'none')
159 if isequal(bcol, round(bcol))
162 bcol = squeeze(A(1,1,:));
166 % Check the output size is correct
167 if isequal(res, round(res))
168 px = [px([4 3])*res 3];
169 if ~isequal(size(A), px)
170 % Correct the output size
171 A = A(1:min(end,px(1)),1:min(end,px(2)),:);
176 % Function to return (and create, where necessary) the font path
177 function fp = font_path()
178 fp = user_string('gs_font_path');
183 % Start with the default path
184 fp = getenv('GS_FONTPATH');
185 % Add on the typical directories for a given OS
190 fp = [fp getenv('WINDIR') filesep 'Fonts'];
195 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'];
197 user_string('gs_font_path', fp);