| 1 | %PRINT2ARRAY  Exports a figure to an image array | 
|---|
| 2 | % | 
|---|
| 3 | % Examples: | 
|---|
| 4 | %   A = print2array | 
|---|
| 5 | %   A = print2array(figure_handle) | 
|---|
| 6 | %   A = print2array(figure_handle, resolution) | 
|---|
| 7 | %   A = print2array(figure_handle, resolution, renderer) | 
|---|
| 8 | %   [A bcol] = print2array(...) | 
|---|
| 9 | % | 
|---|
| 10 | % This function outputs a bitmap image of the given figure, at the desired | 
|---|
| 11 | % resolution. | 
|---|
| 12 | % | 
|---|
| 13 | % If renderer is '-painters' then ghostcript needs to be installed. This | 
|---|
| 14 | % can be downloaded from: http://www.ghostscript.com | 
|---|
| 15 | % | 
|---|
| 16 | % IN: | 
|---|
| 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'. | 
|---|
| 22 | % | 
|---|
| 23 | % OUT: | 
|---|
| 24 | %   A - MxNx3 uint8 image of the figure. | 
|---|
| 25 | %   bcol - 1x3 uint8 vector of the background color | 
|---|
| 26 |  | 
|---|
| 27 | % Copyright (C) Oliver Woodford 2008-2011 | 
|---|
| 28 |  | 
|---|
| 29 | % 5/9/2011 Set EraseModes to normal when using opengl or zbuffer renderers. | 
|---|
| 30 | % Thanks to Pawel Kocieniewski for reporting the issue. | 
|---|
| 31 |  | 
|---|
| 32 | % 21/9/2011 Bug fix: unit8 -> uint8! | 
|---|
| 33 | % Thanks to Tobias Lamour for reporting the issue. | 
|---|
| 34 |  | 
|---|
| 35 | % 14/11/2011 Bug fix: stop using hardcopy(), as it interfered with figure | 
|---|
| 36 | % size and erasemode settings. Makes it a bit slower, but more reliable. | 
|---|
| 37 | % Thanks to Phil Trinh and Meelis Lootus for reporting the issues. | 
|---|
| 38 |  | 
|---|
| 39 | % 9/12/2011 Pass font path to ghostscript. | 
|---|
| 40 |  | 
|---|
| 41 | % 27/1/2012 Bug fix affecting painters rendering tall figures. Thanks to | 
|---|
| 42 | % Ken Campbell for reporting it. | 
|---|
| 43 |  | 
|---|
| 44 | function [A bcol] = print2array(fig, res, renderer) | 
|---|
| 45 | % Generate default input arguments, if needed | 
|---|
| 46 | if nargin < 2 | 
|---|
| 47 | res = 1; | 
|---|
| 48 | if nargin < 1 | 
|---|
| 49 | fig = gcf; | 
|---|
| 50 | end | 
|---|
| 51 | end | 
|---|
| 52 | % Warn if output is large | 
|---|
| 53 | old_mode = get(fig, 'Units'); | 
|---|
| 54 | set(fig, 'Units', 'pixels'); | 
|---|
| 55 | px = get(fig, 'Position'); | 
|---|
| 56 | set(fig, 'Units', old_mode); | 
|---|
| 57 | npx = prod(px(3:4)*res)/1e6; | 
|---|
| 58 | if npx > 30 | 
|---|
| 59 | % 30M pixels or larger! | 
|---|
| 60 | warning('MATLAB:LargeImage', 'print2array generating a %.1fM pixel image. This could be slow and might also cause memory problems.', npx); | 
|---|
| 61 | end | 
|---|
| 62 | % Retrieve the background colour | 
|---|
| 63 | bcol = get(fig, 'Color'); | 
|---|
| 64 | % Set the resolution parameter | 
|---|
| 65 | res_str = ['-r' num2str(ceil(get(0, 'ScreenPixelsPerInch')*res))]; | 
|---|
| 66 | % Generate temporary file name | 
|---|
| 67 | tmp_nam = [tempname '.tif']; | 
|---|
| 68 | if nargin > 2 && strcmp(renderer, '-painters') | 
|---|
| 69 | % Print to eps file | 
|---|
| 70 | tmp_eps = [tempname '.eps']; | 
|---|
| 71 | print2eps(tmp_eps, fig, renderer, '-loose'); | 
|---|
| 72 | try | 
|---|
| 73 | % Initialize the command to export to tiff using ghostscript | 
|---|
| 74 | cmd_str = ['-dEPSCrop -q -dNOPAUSE -dBATCH ' res_str ' -sDEVICE=tiff24nc']; | 
|---|
| 75 | % Set the font path | 
|---|
| 76 | fp = font_path(); | 
|---|
| 77 | if ~isempty(fp) | 
|---|
| 78 | cmd_str = [cmd_str ' -sFONTPATH="' fp '"']; | 
|---|
| 79 | end | 
|---|
| 80 | % Add the filenames | 
|---|
| 81 | cmd_str = [cmd_str ' -sOutputFile="' tmp_nam '" "' tmp_eps '"']; | 
|---|
| 82 | % Execute the ghostscript command | 
|---|
| 83 | ghostscript(cmd_str); | 
|---|
| 84 | catch | 
|---|
| 85 | % Delete the intermediate file | 
|---|
| 86 | delete(tmp_eps); | 
|---|
| 87 | rethrow(lasterror); | 
|---|
| 88 | end | 
|---|
| 89 | % Delete the intermediate file | 
|---|
| 90 | delete(tmp_eps); | 
|---|
| 91 | % Read in the generated bitmap | 
|---|
| 92 | A = imread(tmp_nam); | 
|---|
| 93 | % Delete the temporary bitmap file | 
|---|
| 94 | delete(tmp_nam); | 
|---|
| 95 | % Set border pixels to the correct colour | 
|---|
| 96 | if isequal(bcol, 'none') | 
|---|
| 97 | bcol = []; | 
|---|
| 98 | elseif isequal(bcol, [1 1 1]) | 
|---|
| 99 | bcol = uint8([255 255 255]); | 
|---|
| 100 | else | 
|---|
| 101 | for l = 1:size(A, 2) | 
|---|
| 102 | if ~all(reshape(A(:,l,:) == 255, [], 1)) | 
|---|
| 103 | break; | 
|---|
| 104 | end | 
|---|
| 105 | end | 
|---|
| 106 | for r = size(A, 2):-1:l | 
|---|
| 107 | if ~all(reshape(A(:,r,:) == 255, [], 1)) | 
|---|
| 108 | break; | 
|---|
| 109 | end | 
|---|
| 110 | end | 
|---|
| 111 | for t = 1:size(A, 1) | 
|---|
| 112 | if ~all(reshape(A(t,:,:) == 255, [], 1)) | 
|---|
| 113 | break; | 
|---|
| 114 | end | 
|---|
| 115 | end | 
|---|
| 116 | for b = size(A, 1):-1:t | 
|---|
| 117 | if ~all(reshape(A(b,:,:) == 255, [], 1)) | 
|---|
| 118 | break; | 
|---|
| 119 | end | 
|---|
| 120 | end | 
|---|
| 121 | bcol = median([reshape(A(:,[l r],:), [], size(A, 3)); reshape(A([t b],:,:), [], size(A, 3))], 1); | 
|---|
| 122 | for c = 1:size(A, 3) | 
|---|
| 123 | A(:,[1:l-1, r+1:end],c) = bcol(c); | 
|---|
| 124 | A([1:t-1, b+1:end],:,c) = bcol(c); | 
|---|
| 125 | end | 
|---|
| 126 | end | 
|---|
| 127 | else | 
|---|
| 128 | if nargin < 3 | 
|---|
| 129 | renderer = '-opengl'; | 
|---|
| 130 | end | 
|---|
| 131 | err = false; | 
|---|
| 132 | % Set paper size | 
|---|
| 133 | old_mode = get(fig, 'PaperPositionMode'); | 
|---|
| 134 | set(fig, 'PaperPositionMode', 'auto'); | 
|---|
| 135 | try | 
|---|
| 136 | % Print to tiff file | 
|---|
| 137 | print(fig, renderer, res_str, '-dtiff', tmp_nam); | 
|---|
| 138 | % Read in the printed file | 
|---|
| 139 | A = imread(tmp_nam); | 
|---|
| 140 | % Delete the temporary file | 
|---|
| 141 | delete(tmp_nam); | 
|---|
| 142 | catch ex | 
|---|
| 143 | err = true; | 
|---|
| 144 | end | 
|---|
| 145 | % Reset paper size | 
|---|
| 146 | set(fig, 'PaperPositionMode', old_mode); | 
|---|
| 147 | % Throw any error that occurred | 
|---|
| 148 | if err | 
|---|
| 149 | rethrow(ex); | 
|---|
| 150 | end | 
|---|
| 151 | % Set the background color | 
|---|
| 152 | if isequal(bcol, 'none') | 
|---|
| 153 | bcol = []; | 
|---|
| 154 | else | 
|---|
| 155 | bcol = bcol * 255; | 
|---|
| 156 | if isequal(bcol, round(bcol)) | 
|---|
| 157 | bcol = uint8(bcol); | 
|---|
| 158 | else | 
|---|
| 159 | bcol = squeeze(A(1,1,:)); | 
|---|
| 160 | end | 
|---|
| 161 | end | 
|---|
| 162 | end | 
|---|
| 163 | % Check the output size is correct | 
|---|
| 164 | if isequal(res, round(res)) | 
|---|
| 165 | px = [px([4 3])*res 3]; | 
|---|
| 166 | if ~isequal(size(A), px) | 
|---|
| 167 | % Correct the output size | 
|---|
| 168 | A = A(1:min(end,px(1)),1:min(end,px(2)),:); | 
|---|
| 169 | end | 
|---|
| 170 | end | 
|---|
| 171 | return | 
|---|
| 172 |  | 
|---|
| 173 | % Function to return (and create, where necessary) the font path | 
|---|
| 174 | function fp = font_path() | 
|---|
| 175 | fp = user_string('gs_font_path'); | 
|---|
| 176 | if ~isempty(fp) | 
|---|
| 177 | return | 
|---|
| 178 | end | 
|---|
| 179 | % Create the path | 
|---|
| 180 | % Start with the default path | 
|---|
| 181 | fp = getenv('GS_FONTPATH'); | 
|---|
| 182 | % Add on the typical directories for a given OS | 
|---|
| 183 | if ispc | 
|---|
| 184 | if ~isempty(fp) | 
|---|
| 185 | fp = [fp ';']; | 
|---|
| 186 | end | 
|---|
| 187 | fp = [fp getenv('WINDIR') filesep 'Fonts']; | 
|---|
| 188 | else | 
|---|
| 189 | if ~isempty(fp) | 
|---|
| 190 | fp = [fp ':']; | 
|---|
| 191 | end | 
|---|
| 192 | 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']; | 
|---|
| 193 | end | 
|---|
| 194 | user_string('gs_font_path', fp); | 
|---|
| 195 | return | 
|---|