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

Last change on this file since 7182 was 7182, checked in by Mathieu Morlighem, 14 years ago

Added export_fig/ for pdf print

File size: 3.6 KB
RevLine 
[7182]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%
9% This function outputs a bitmap image of the given figure, at the desired
10% resolution.
11%
12% If renderer is '-painters' then ghostcript needs to be installed. This
13% can be downloaded from: http://www.ghostscript.com
14%
15% IN:
16% figure_handle - The handle of the figure to be exported. Default: gcf.
17% resolution - Resolution of the output, as a factor of screen
18% resolution. Default: 1.
19% renderer - string containing the renderer paramater to be passed to
20% print. Default: '-opengl'.
21%
22% OUT:
23% A - MxNx3 uint8 image of the figure.
24
25% Copyright (C) Oliver Woodford 2008-2010
26
27function A = print2array(fig, res, renderer)
28% Generate default input arguments, if needed
29if nargin < 2
30 res = 1;
31 if nargin < 1
32 fig = gcf;
33 end
34end
35% Warn if output is large
36old_mode = get(fig, 'Units');
37set(fig, 'Units', 'pixels');
38px = get(fig, 'Position');
39set(fig, 'Units', old_mode);
40px = prod(px(3:4)*res)/1e6;
41if px > 30
42 % 30M pixels or larger!
43 warning('MATLAB:LargeImage', 'print2array generating a %.1fM pixel image. This could be slow and might also cause memory problems.', px);
44end
45% Set the resolution parameter
46res = ['-r' num2str(ceil(get(0, 'ScreenPixelsPerInch')*res))];
47% Generate temporary file name
48tmp_nam = [tempname '.tif'];
49if nargin > 2 && strcmp(renderer, '-painters')
50 % Print to eps file
51 tmp_eps = [tempname '.eps'];
52 print2eps(tmp_eps, fig, renderer, '-loose');
53 try
54 % Export to tiff using ghostscript
55 ghostscript(['-dEPSCrop -q -dNOPAUSE -dBATCH ' res ' -sDEVICE=tiff24nc -sOutputFile="' tmp_nam '" "' tmp_eps '"']);
56 catch
57 % Delete the intermediate file
58 delete(tmp_eps);
59 rethrow(lasterror);
60 end
61 % Delete the intermediate file
62 delete(tmp_eps);
63 % Read in the generated bitmap
64 A = imread(tmp_nam);
65 % Delete the temporary bitmap file
66 delete(tmp_nam);
67 % Retrieve the background colour
68 bcol = get(fig, 'Color');
69 % Set border pixels to the correct colour
70 if ~isequal(bcol, 'none') && ~isequal(bcol, [1 1 1])
71 for l = 1:size(A, 2)
72 if ~all(reshape(A(:,l,:) == 255, [], 1))
73 break;
74 end
75 end
76 for r = size(A, 2):-1:l
77 if ~all(reshape(A(:,r,:) == 255, [], 1))
78 break;
79 end
80 end
81 for t = 1:size(A, 1)
82 if ~all(reshape(A(t,:,:) == 255, [], 1))
83 break;
84 end
85 end
86 for b = size(A, 1):-1:t
87 if ~all(reshape(A(b,:,:) == 255, [], 1))
88 break;
89 end
90 end
91 bcol = round(bcol * 255);
92 for c = 1:size(A, 3)
93 A(:,[1:l-1, r+1:end],c) = bcol(c);
94 A([1:t-1, b+1:end],:,c) = bcol(c);
95 end
96 end
97else
98 if nargin < 3
99 renderer = '-opengl';
100 end
101 % Set paper size
102 old_mode = get(fig, 'PaperPositionMode');
103 set(fig, 'PaperPositionMode', 'auto');
104 try
105 % Try hardcopy first - undocumented MATLAB function!
106 A = hardcopy(fig, ['-D' renderer(2:end)], res);
107 catch
108 % Print to tiff file
109 print(fig, renderer, res, '-dtiff', tmp_nam);
110 % Read in the printed file
111 A = imread(tmp_nam);
112 % Delete the temporary file
113 delete(tmp_nam);
114 end
115 % Reset paper size
116 set(fig, 'PaperPositionMode', old_mode);
117end
118return
Note: See TracBrowser for help on using the repository browser.