| 1 | function [A, bcol] = print2array(fig, res, renderer, gs_options)
|
|---|
| 2 | %PRINT2ARRAY Exports a figure to an 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] = print2array(...)
|
|---|
| 11 | %
|
|---|
| 12 | % This function outputs a bitmap image of the given figure, at the desired
|
|---|
| 13 | % resolution.
|
|---|
| 14 | %
|
|---|
| 15 | % If renderer is '-painters' then ghostcript needs to be installed. This
|
|---|
| 16 | % can be downloaded from: http://www.ghostscript.com
|
|---|
| 17 | %
|
|---|
| 18 | % IN:
|
|---|
| 19 | % figure_handle - The handle of the figure to be exported. Default: gcf.
|
|---|
| 20 | % resolution - Resolution of the output, as a factor of screen
|
|---|
| 21 | % resolution. Default: 1.
|
|---|
| 22 | % renderer - string containing the renderer paramater to be passed to
|
|---|
| 23 | % print. Default: '-opengl'.
|
|---|
| 24 | % gs_options - optional ghostscript options (e.g.: '-dNoOutputFonts'). If
|
|---|
| 25 | % multiple options are needed, enclose in call array: {'-a','-b'}
|
|---|
| 26 | %
|
|---|
| 27 | % OUT:
|
|---|
| 28 | % A - MxNx3 uint8 image of the figure.
|
|---|
| 29 | % bcol - 1x3 uint8 vector of the background color
|
|---|
| 30 |
|
|---|
| 31 | % Copyright (C) Oliver Woodford 2008-2014, Yair Altman 2015-
|
|---|
| 32 | %{
|
|---|
| 33 | % 05/09/11: Set EraseModes to normal when using opengl or zbuffer
|
|---|
| 34 | % renderers. Thanks to Pawel Kocieniewski for reporting the issue.
|
|---|
| 35 | % 21/09/11: Bug fix: unit8 -> uint8! Thanks to Tobias Lamour for reporting it.
|
|---|
| 36 | % 14/11/11: Bug fix: stop using hardcopy(), as it interfered with figure size
|
|---|
| 37 | % and erasemode settings. Makes it a bit slower, but more reliable.
|
|---|
| 38 | % Thanks to Phil Trinh and Meelis Lootus for reporting the issues.
|
|---|
| 39 | % 09/12/11: Pass font path to ghostscript.
|
|---|
| 40 | % 27/01/12: Bug fix affecting painters rendering tall figures. Thanks to
|
|---|
| 41 | % Ken Campbell for reporting it.
|
|---|
| 42 | % 03/04/12: Bug fix to median input. Thanks to Andy Matthews for reporting it.
|
|---|
| 43 | % 26/10/12: Set PaperOrientation to portrait. Thanks to Michael Watts for
|
|---|
| 44 | % reporting the issue.
|
|---|
| 45 | % 26/02/15: If temp dir is not writable, use the current folder for temp
|
|---|
| 46 | % EPS/TIF files (Javier Paredes)
|
|---|
| 47 | % 27/02/15: Display suggested workarounds to internal print() error (issue #16)
|
|---|
| 48 | % 28/02/15: Enable users to specify optional ghostscript options (issue #36)
|
|---|
| 49 | % 10/03/15: Fixed minor warning reported by Paul Soderlind; fixed code indentation
|
|---|
| 50 | % 28/05/15: Fixed issue #69: patches with LineWidth==0.75 appear wide (internal bug in Matlab's print() func)
|
|---|
| 51 | % 07/07/15: Fixed issue #83: use numeric handles in HG1
|
|---|
| 52 | %}
|
|---|
| 53 |
|
|---|
| 54 | % Generate default input arguments, if needed
|
|---|
| 55 | if nargin < 2
|
|---|
| 56 | res = 1;
|
|---|
| 57 | if nargin < 1
|
|---|
| 58 | fig = gcf;
|
|---|
| 59 | end
|
|---|
| 60 | end
|
|---|
| 61 | % Warn if output is large
|
|---|
| 62 | old_mode = get(fig, 'Units');
|
|---|
| 63 | set(fig, 'Units', 'pixels');
|
|---|
| 64 | px = get(fig, 'Position');
|
|---|
| 65 | set(fig, 'Units', old_mode);
|
|---|
| 66 | npx = prod(px(3:4)*res)/1e6;
|
|---|
| 67 | if npx > 30
|
|---|
| 68 | % 30M pixels or larger!
|
|---|
| 69 | warning('MATLAB:LargeImage', 'print2array generating a %.1fM pixel image. This could be slow and might also cause memory problems.', npx);
|
|---|
| 70 | end
|
|---|
| 71 | % Retrieve the background colour
|
|---|
| 72 | bcol = get(fig, 'Color');
|
|---|
| 73 | % Set the resolution parameter
|
|---|
| 74 | res_str = ['-r' num2str(ceil(get(0, 'ScreenPixelsPerInch')*res))];
|
|---|
| 75 | % Generate temporary file name
|
|---|
| 76 | tmp_nam = [tempname '.tif'];
|
|---|
| 77 | try
|
|---|
| 78 | % Ensure that the temp dir is writable (Javier Paredes 26/2/15)
|
|---|
| 79 | fid = fopen(tmp_nam,'w');
|
|---|
| 80 | fwrite(fid,1);
|
|---|
| 81 | fclose(fid);
|
|---|
| 82 | delete(tmp_nam); % cleanup
|
|---|
| 83 | isTempDirOk = true;
|
|---|
| 84 | catch
|
|---|
| 85 | % Temp dir is not writable, so use the current folder
|
|---|
| 86 | [dummy,fname,fext] = fileparts(tmp_nam); %#ok<ASGLU>
|
|---|
| 87 | fpath = pwd;
|
|---|
| 88 | tmp_nam = fullfile(fpath,[fname fext]);
|
|---|
| 89 | isTempDirOk = false;
|
|---|
| 90 | end
|
|---|
| 91 | % Enable users to specify optional ghostscript options (issue #36)
|
|---|
| 92 | if nargin > 3 && ~isempty(gs_options)
|
|---|
| 93 | if iscell(gs_options)
|
|---|
| 94 | gs_options = sprintf(' %s',gs_options{:});
|
|---|
| 95 | elseif ~ischar(gs_options)
|
|---|
| 96 | error('gs_options input argument must be a string or cell-array of strings');
|
|---|
| 97 | else
|
|---|
| 98 | gs_options = [' ' gs_options];
|
|---|
| 99 | end
|
|---|
| 100 | else
|
|---|
| 101 | gs_options = '';
|
|---|
| 102 | end
|
|---|
| 103 | if nargin > 2 && strcmp(renderer, '-painters')
|
|---|
| 104 | % Print to eps file
|
|---|
| 105 | if isTempDirOk
|
|---|
| 106 | tmp_eps = [tempname '.eps'];
|
|---|
| 107 | else
|
|---|
| 108 | tmp_eps = fullfile(fpath,[fname '.eps']);
|
|---|
| 109 | end
|
|---|
| 110 | print2eps(tmp_eps, fig, 0, renderer, '-loose');
|
|---|
| 111 | try
|
|---|
| 112 | % Initialize the command to export to tiff using ghostscript
|
|---|
| 113 | cmd_str = ['-dEPSCrop -q -dNOPAUSE -dBATCH ' res_str ' -sDEVICE=tiff24nc'];
|
|---|
| 114 | % Set the font path
|
|---|
| 115 | fp = font_path();
|
|---|
| 116 | if ~isempty(fp)
|
|---|
| 117 | cmd_str = [cmd_str ' -sFONTPATH="' fp '"'];
|
|---|
| 118 | end
|
|---|
| 119 | % Add the filenames
|
|---|
| 120 | cmd_str = [cmd_str ' -sOutputFile="' tmp_nam '" "' tmp_eps '"' gs_options];
|
|---|
| 121 | % Execute the ghostscript command
|
|---|
| 122 | ghostscript(cmd_str);
|
|---|
| 123 | catch me
|
|---|
| 124 | % Delete the intermediate file
|
|---|
| 125 | delete(tmp_eps);
|
|---|
| 126 | rethrow(me);
|
|---|
| 127 | end
|
|---|
| 128 | % Delete the intermediate file
|
|---|
| 129 | delete(tmp_eps);
|
|---|
| 130 | % Read in the generated bitmap
|
|---|
| 131 | A = imread(tmp_nam);
|
|---|
| 132 | % Delete the temporary bitmap file
|
|---|
| 133 | delete(tmp_nam);
|
|---|
| 134 | % Set border pixels to the correct colour
|
|---|
| 135 | if isequal(bcol, 'none')
|
|---|
| 136 | bcol = [];
|
|---|
| 137 | elseif isequal(bcol, [1 1 1])
|
|---|
| 138 | bcol = uint8([255 255 255]);
|
|---|
| 139 | else
|
|---|
| 140 | for l = 1:size(A, 2)
|
|---|
| 141 | if ~all(reshape(A(:,l,:) == 255, [], 1))
|
|---|
| 142 | break;
|
|---|
| 143 | end
|
|---|
| 144 | end
|
|---|
| 145 | for r = size(A, 2):-1:l
|
|---|
| 146 | if ~all(reshape(A(:,r,:) == 255, [], 1))
|
|---|
| 147 | break;
|
|---|
| 148 | end
|
|---|
| 149 | end
|
|---|
| 150 | for t = 1:size(A, 1)
|
|---|
| 151 | if ~all(reshape(A(t,:,:) == 255, [], 1))
|
|---|
| 152 | break;
|
|---|
| 153 | end
|
|---|
| 154 | end
|
|---|
| 155 | for b = size(A, 1):-1:t
|
|---|
| 156 | if ~all(reshape(A(b,:,:) == 255, [], 1))
|
|---|
| 157 | break;
|
|---|
| 158 | end
|
|---|
| 159 | end
|
|---|
| 160 | bcol = uint8(median(single([reshape(A(:,[l r],:), [], size(A, 3)); reshape(A([t b],:,:), [], size(A, 3))]), 1));
|
|---|
| 161 | for c = 1:size(A, 3)
|
|---|
| 162 | A(:,[1:l-1, r+1:end],c) = bcol(c);
|
|---|
| 163 | A([1:t-1, b+1:end],:,c) = bcol(c);
|
|---|
| 164 | end
|
|---|
| 165 | end
|
|---|
| 166 | else
|
|---|
| 167 | if nargin < 3
|
|---|
| 168 | renderer = '-opengl';
|
|---|
| 169 | end
|
|---|
| 170 | err = false;
|
|---|
| 171 | % Set paper size
|
|---|
| 172 | old_pos_mode = get(fig, 'PaperPositionMode');
|
|---|
| 173 | old_orientation = get(fig, 'PaperOrientation');
|
|---|
| 174 | set(fig, 'PaperPositionMode', 'auto', 'PaperOrientation', 'portrait');
|
|---|
| 175 | try
|
|---|
| 176 | % Workaround for issue #69: patches with LineWidth==0.75 appear wide (internal bug in Matlab's print() function)
|
|---|
| 177 | fp = []; % in case we get an error below
|
|---|
| 178 | fp = findall(fig, 'Type','patch', 'LineWidth',0.75);
|
|---|
| 179 | set(fp, 'LineWidth',0.5);
|
|---|
| 180 | % Fix issue #83: use numeric handles in HG1
|
|---|
| 181 | if ~using_hg2(fig), fig = double(fig); end
|
|---|
| 182 | % Print to tiff file
|
|---|
| 183 | print(fig, renderer, res_str, '-dtiff', tmp_nam);
|
|---|
| 184 | % Read in the printed file
|
|---|
| 185 | A = imread(tmp_nam);
|
|---|
| 186 | % Delete the temporary file
|
|---|
| 187 | delete(tmp_nam);
|
|---|
| 188 | catch ex
|
|---|
| 189 | err = true;
|
|---|
| 190 | end
|
|---|
| 191 | set(fp, 'LineWidth',0.75); % restore original figure appearance
|
|---|
| 192 | % Reset paper size
|
|---|
| 193 | set(fig, 'PaperPositionMode', old_pos_mode, 'PaperOrientation', old_orientation);
|
|---|
| 194 | % Throw any error that occurred
|
|---|
| 195 | if err
|
|---|
| 196 | % Display suggested workarounds to internal print() error (issue #16)
|
|---|
| 197 | fprintf(2, 'An error occured with Matlab''s builtin print function.\nTry setting the figure Renderer to ''painters'' or use opengl(''software'').\n\n');
|
|---|
| 198 | rethrow(ex);
|
|---|
| 199 | end
|
|---|
| 200 | % Set the background color
|
|---|
| 201 | if isequal(bcol, 'none')
|
|---|
| 202 | bcol = [];
|
|---|
| 203 | else
|
|---|
| 204 | bcol = bcol * 255;
|
|---|
| 205 | if isequal(bcol, round(bcol))
|
|---|
| 206 | bcol = uint8(bcol);
|
|---|
| 207 | else
|
|---|
| 208 | bcol = squeeze(A(1,1,:));
|
|---|
| 209 | end
|
|---|
| 210 | end
|
|---|
| 211 | end
|
|---|
| 212 | % Check the output size is correct
|
|---|
| 213 | if isequal(res, round(res))
|
|---|
| 214 | px = round([px([4 3])*res 3]); % round() to avoid an indexing warning below
|
|---|
| 215 | if ~isequal(size(A), px)
|
|---|
| 216 | % Correct the output size
|
|---|
| 217 | A = A(1:min(end,px(1)),1:min(end,px(2)),:);
|
|---|
| 218 | end
|
|---|
| 219 | end
|
|---|
| 220 | end
|
|---|
| 221 |
|
|---|
| 222 | % Function to return (and create, where necessary) the font path
|
|---|
| 223 | function fp = font_path()
|
|---|
| 224 | fp = user_string('gs_font_path');
|
|---|
| 225 | if ~isempty(fp)
|
|---|
| 226 | return
|
|---|
| 227 | end
|
|---|
| 228 | % Create the path
|
|---|
| 229 | % Start with the default path
|
|---|
| 230 | fp = getenv('GS_FONTPATH');
|
|---|
| 231 | % Add on the typical directories for a given OS
|
|---|
| 232 | if ispc
|
|---|
| 233 | if ~isempty(fp)
|
|---|
| 234 | fp = [fp ';'];
|
|---|
| 235 | end
|
|---|
| 236 | fp = [fp getenv('WINDIR') filesep 'Fonts'];
|
|---|
| 237 | else
|
|---|
| 238 | if ~isempty(fp)
|
|---|
| 239 | fp = [fp ':'];
|
|---|
| 240 | end
|
|---|
| 241 | 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'];
|
|---|
| 242 | end
|
|---|
| 243 | user_string('gs_font_path', fp);
|
|---|
| 244 | end
|
|---|