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

Last change on this file since 13975 was 12707, checked in by Mathieu Morlighem, 13 years ago

merged trunk-jpl and trunk for revision 12703

File size: 6.0 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-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% 3/4/2012 Bug fix to median input. Thanks to Andy Matthews for reporting
45% it.
46
47function [A bcol] = print2array(fig, res, renderer)
48% Generate default input arguments, if needed
49if nargin < 2
50 res = 1;
51 if nargin < 1
52 fig = gcf;
53 end
54end
55% Warn if output is large
56old_mode = get(fig, 'Units');
57set(fig, 'Units', 'pixels');
58px = get(fig, 'Position');
59set(fig, 'Units', old_mode);
60npx = prod(px(3:4)*res)/1e6;
61if npx > 30
62 % 30M pixels or larger!
63 warning('MATLAB:LargeImage', 'print2array generating a %.1fM pixel image. This could be slow and might also cause memory problems.', npx);
64end
65% Retrieve the background colour
66bcol = get(fig, 'Color');
67% Set the resolution parameter
68res_str = ['-r' num2str(ceil(get(0, 'ScreenPixelsPerInch')*res))];
69% Generate temporary file name
70tmp_nam = [tempname '.tif'];
71if nargin > 2 && strcmp(renderer, '-painters')
72 % Print to eps file
73 tmp_eps = [tempname '.eps'];
74 print2eps(tmp_eps, fig, renderer, '-loose');
75 try
76 % Initialize the command to export to tiff using ghostscript
77 cmd_str = ['-dEPSCrop -q -dNOPAUSE -dBATCH ' res_str ' -sDEVICE=tiff24nc'];
78 % Set the font path
79 fp = font_path();
80 if ~isempty(fp)
81 cmd_str = [cmd_str ' -sFONTPATH="' fp '"'];
82 end
83 % Add the filenames
84 cmd_str = [cmd_str ' -sOutputFile="' tmp_nam '" "' tmp_eps '"'];
85 % Execute the ghostscript command
86 ghostscript(cmd_str);
87 catch
88 % Delete the intermediate file
89 delete(tmp_eps);
90 rethrow(lasterror);
91 end
92 % Delete the intermediate file
93 delete(tmp_eps);
94 % Read in the generated bitmap
95 A = imread(tmp_nam);
96 % Delete the temporary bitmap file
97 delete(tmp_nam);
98 % Set border pixels to the correct colour
99 if isequal(bcol, 'none')
100 bcol = [];
101 elseif isequal(bcol, [1 1 1])
102 bcol = uint8([255 255 255]);
103 else
104 for l = 1:size(A, 2)
105 if ~all(reshape(A(:,l,:) == 255, [], 1))
106 break;
107 end
108 end
109 for r = size(A, 2):-1:l
110 if ~all(reshape(A(:,r,:) == 255, [], 1))
111 break;
112 end
113 end
114 for t = 1:size(A, 1)
115 if ~all(reshape(A(t,:,:) == 255, [], 1))
116 break;
117 end
118 end
119 for b = size(A, 1):-1:t
120 if ~all(reshape(A(b,:,:) == 255, [], 1))
121 break;
122 end
123 end
124 bcol = uint8(median(single([reshape(A(:,[l r],:), [], size(A, 3)); reshape(A([t b],:,:), [], size(A, 3))]), 1));
125 for c = 1:size(A, 3)
126 A(:,[1:l-1, r+1:end],c) = bcol(c);
127 A([1:t-1, b+1:end],:,c) = bcol(c);
128 end
129 end
130else
131 if nargin < 3
132 renderer = '-opengl';
133 end
134 err = false;
135 % Set paper size
136 old_mode = get(fig, 'PaperPositionMode');
137 set(fig, 'PaperPositionMode', 'auto');
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_mode);
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.