source: issm/trunk/externalpackages/export_fig/print2array.m@ 14310

Last change on this file since 14310 was 14310, checked in by Mathieu Morlighem, 12 years ago

merged trunk-jpl and trunk for revision 14308

File size: 6.3 KB
Line 
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-2012
28
29% 05/09/11: Set EraseModes to normal when using opengl or zbuffer
30% renderers. Thanks to Pawel Kocieniewski for reporting the
31% issue.
32% 21/09/11: Bug fix: unit8 -> uint8! Thanks to Tobias Lamour for reporting
33% the issue.
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
37% the issues.
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
42% it.
43% 26/10/12: Set PaperOrientation to portrait. Thanks to Michael Watts for
44% reporting the issue.
45
46function [A, bcol] = print2array(fig, res, renderer)
47% Generate default input arguments, if needed
48if nargin < 2
49 res = 1;
50 if nargin < 1
51 fig = gcf;
52 end
53end
54% Warn if output is large
55old_mode = get(fig, 'Units');
56set(fig, 'Units', 'pixels');
57px = get(fig, 'Position');
58set(fig, 'Units', old_mode);
59npx = prod(px(3:4)*res)/1e6;
60if npx > 30
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);
63end
64% Retrieve the background colour
65bcol = get(fig, 'Color');
66% Set the resolution parameter
67res_str = ['-r' num2str(ceil(get(0, 'ScreenPixelsPerInch')*res))];
68% Generate temporary file name
69tmp_nam = [tempname '.tif'];
70if nargin > 2 && strcmp(renderer, '-painters')
71 % Print to eps file
72 tmp_eps = [tempname '.eps'];
73 print2eps(tmp_eps, fig, renderer, '-loose');
74 try
75 % Initialize the command to export to tiff using ghostscript
76 cmd_str = ['-dEPSCrop -q -dNOPAUSE -dBATCH ' res_str ' -sDEVICE=tiff24nc'];
77 % Set the font path
78 fp = font_path();
79 if ~isempty(fp)
80 cmd_str = [cmd_str ' -sFONTPATH="' fp '"'];
81 end
82 % Add the filenames
83 cmd_str = [cmd_str ' -sOutputFile="' tmp_nam '" "' tmp_eps '"'];
84 % Execute the ghostscript command
85 ghostscript(cmd_str);
86 catch me
87 % Delete the intermediate file
88 delete(tmp_eps);
89 rethrow(me);
90 end
91 % Delete the intermediate file
92 delete(tmp_eps);
93 % Read in the generated bitmap
94 A = imread(tmp_nam);
95 % Delete the temporary bitmap file
96 delete(tmp_nam);
97 % Set border pixels to the correct colour
98 if isequal(bcol, 'none')
99 bcol = [];
100 elseif isequal(bcol, [1 1 1])
101 bcol = uint8([255 255 255]);
102 else
103 for l = 1:size(A, 2)
104 if ~all(reshape(A(:,l,:) == 255, [], 1))
105 break;
106 end
107 end
108 for r = size(A, 2):-1:l
109 if ~all(reshape(A(:,r,:) == 255, [], 1))
110 break;
111 end
112 end
113 for t = 1:size(A, 1)
114 if ~all(reshape(A(t,:,:) == 255, [], 1))
115 break;
116 end
117 end
118 for b = size(A, 1):-1:t
119 if ~all(reshape(A(b,:,:) == 255, [], 1))
120 break;
121 end
122 end
123 bcol = uint8(median(single([reshape(A(:,[l r],:), [], size(A, 3)); reshape(A([t b],:,:), [], size(A, 3))]), 1));
124 for c = 1:size(A, 3)
125 A(:,[1:l-1, r+1:end],c) = bcol(c);
126 A([1:t-1, b+1:end],:,c) = bcol(c);
127 end
128 end
129else
130 if nargin < 3
131 renderer = '-opengl';
132 end
133 err = false;
134 % Set paper size
135 old_pos_mode = get(fig, 'PaperPositionMode');
136 old_orientation = get(fig, 'PaperOrientation');
137 set(fig, 'PaperPositionMode', 'auto', 'PaperOrientation', 'portrait');
138 try
139 % Print to tiff file
140 print(fig, renderer, res_str, '-dtiff', tmp_nam);
141 % Read in the printed file
142 A = imread(tmp_nam);
143 % Delete the temporary file
144 delete(tmp_nam);
145 catch ex
146 err = true;
147 end
148 % Reset paper size
149 set(fig, 'PaperPositionMode', old_pos_mode, 'PaperOrientation', old_orientation);
150 % Throw any error that occurred
151 if err
152 rethrow(ex);
153 end
154 % Set the background color
155 if isequal(bcol, 'none')
156 bcol = [];
157 else
158 bcol = bcol * 255;
159 if isequal(bcol, round(bcol))
160 bcol = uint8(bcol);
161 else
162 bcol = squeeze(A(1,1,:));
163 end
164 end
165end
166% Check the output size is correct
167if 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)),:);
172 end
173end
174return
175
176% Function to return (and create, where necessary) the font path
177function fp = font_path()
178fp = user_string('gs_font_path');
179if ~isempty(fp)
180 return
181end
182% Create the path
183% Start with the default path
184fp = getenv('GS_FONTPATH');
185% Add on the typical directories for a given OS
186if ispc
187 if ~isempty(fp)
188 fp = [fp ';'];
189 end
190 fp = [fp getenv('WINDIR') filesep 'Fonts'];
191else
192 if ~isempty(fp)
193 fp = [fp ':'];
194 end
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'];
196end
197user_string('gs_font_path', fp);
198return
Note: See TracBrowser for help on using the repository browser.