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

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

New export_fig bug with text color fixed

File size: 4.8 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
39function [A bcol] = print2array(fig, res, renderer)
40% Generate default input arguments, if needed
41if nargin < 2
42 res = 1;
43 if nargin < 1
44 fig = gcf;
45 end
46end
47% Warn if output is large
48old_mode = get(fig, 'Units');
49set(fig, 'Units', 'pixels');
50px = get(fig, 'Position');
51set(fig, 'Units', old_mode);
52npx = prod(px(3:4)*res)/1e6;
53if npx > 30
54 % 30M pixels or larger!
55 warning('MATLAB:LargeImage', 'print2array generating a %.1fM pixel image. This could be slow and might also cause memory problems.', npx);
56end
57% Retrieve the background colour
58bcol = get(fig, 'Color');
59% Set the resolution parameter
60res_str = ['-r' num2str(ceil(get(0, 'ScreenPixelsPerInch')*res))];
61% Generate temporary file name
62tmp_nam = [tempname '.tif'];
63if nargin > 2 && strcmp(renderer, '-painters')
64 % Print to eps file
65 tmp_eps = [tempname '.eps'];
66 print2eps(tmp_eps, fig, renderer, '-loose');
67 try
68 % Export to tiff using ghostscript
69 ghostscript(['-dEPSCrop -q -dNOPAUSE -dBATCH ' res_str ' -sDEVICE=tiff24nc -sOutputFile="' tmp_nam '" "' tmp_eps '"']);
70 catch
71 % Delete the intermediate file
72 delete(tmp_eps);
73 rethrow(lasterror);
74 end
75 % Delete the intermediate file
76 delete(tmp_eps);
77 % Read in the generated bitmap
78 A = imread(tmp_nam);
79 % Delete the temporary bitmap file
80 delete(tmp_nam);
81 % Set border pixels to the correct colour
82 if isequal(bcol, 'none')
83 bcol = [];
84 elseif isequal(bcol, [1 1 1])
85 bcol = uint8([255 255 255]);
86 else
87 for l = 1:size(A, 2)
88 if ~all(reshape(A(:,l,:) == 255, [], 1))
89 break;
90 end
91 end
92 for r = size(A, 2):-1:l
93 if ~all(reshape(A(:,r,:) == 255, [], 1))
94 break;
95 end
96 end
97 for t = 1:size(A, 1)
98 if ~all(reshape(A(t,:,:) == 255, [], 1))
99 break;
100 end
101 end
102 for b = size(A, 1):-1:t
103 if ~all(reshape(A(b,:,:) == 255, [], 1))
104 break;
105 end
106 end
107 bcol = median([reshape(A(:,[l r],:), [], size(A, 3)); reshape(A(:,[t b],:), [], size(A, 3))], 1);
108 for c = 1:size(A, 3)
109 A(:,[1:l-1, r+1:end],c) = bcol(c);
110 A([1:t-1, b+1:end],:,c) = bcol(c);
111 end
112 end
113else
114 if nargin < 3
115 renderer = '-opengl';
116 end
117 err = false;
118 % Set paper size
119 old_mode = get(fig, 'PaperPositionMode');
120 set(fig, 'PaperPositionMode', 'auto');
121 try
122 % Print to tiff file
123 print(fig, renderer, res_str, '-dtiff', tmp_nam);
124 % Read in the printed file
125 A = imread(tmp_nam);
126 % Delete the temporary file
127 delete(tmp_nam);
128 catch ex
129 err = true;
130 end
131 % Reset paper size
132 set(fig, 'PaperPositionMode', old_mode);
133 % Throw any error that occurred
134 if err
135 rethrow(ex);
136 end
137 % Set the background color
138 if isequal(bcol, 'none')
139 bcol = [];
140 else
141 bcol = bcol * 255;
142 if isequal(bcol, round(bcol))
143 bcol = uint8(bcol);
144 else
145 bcol = squeeze(A(1,1,:));
146 end
147 end
148end
149% Check the output size is correct
150if isequal(res, round(res))
151 px = [px([4 3])*res 3];
152 if ~isequal(size(A), px)
153 % Correct the output size
154 A = A(1:min(end,px(1)),1:min(end,px(2)),:);
155 end
156end
157return
Note: See TracBrowser for help on using the repository browser.