source: MML/trunk/machine/SOLEIL/common/toolbox/export_fig/print2array.m @ 4

Last change on this file since 4 was 4, checked in by zhangj, 10 years ago

Initial import--MML version from SOLEIL@2013

File size: 3.4 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%
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-2009
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% Generate temporary file name
46tmp_nam = [tempname '.tif'];
47if nargin > 2 && strcmp(renderer, '-painters')
48    % Print to eps file
49    tmp_eps = [tempname '.eps'];
50    print2eps(tmp_eps, fig, renderer, '-loose');
51    try
52        % Export to tiff using ghostscript
53        ghostscript(['-dEPSCrop -q -dNOPAUSE -dBATCH -r' num2str(ceil(get(0, 'ScreenPixelsPerInch')*res)) ' -sDEVICE=tiff24nc -sOutputFile="' tmp_nam '" "' tmp_eps '"']);
54    catch
55        % Delete the intermediate file
56        delete(tmp_eps);
57        rethrow(lasterror);
58    end
59    % Delete the intermediate file
60    delete(tmp_eps);
61    % Read in the generated bitmap
62    A = imread(tmp_nam);
63    % Retrieve the background colour
64    bcol = get(fig, 'Color');
65    % Set border pixels to the correct colour
66    if ~isequal(bcol, 'none') && ~isequal(bcol, [1 1 1])
67        for l = 1:size(A, 2)
68            if ~all(reshape(A(:,l,:) == 255, [], 1))
69                break;
70            end
71        end
72        for r = size(A, 2):-1:l
73            if ~all(reshape(A(:,r,:) == 255, [], 1))
74                break;
75            end
76        end
77        for t = 1:size(A, 1)
78            if ~all(reshape(A(t,:,:) == 255, [], 1))
79                break;
80            end
81        end
82        for b = size(A, 1):-1:t
83            if ~all(reshape(A(b,:,:) == 255, [], 1))
84                break;
85            end
86        end
87        bcol = round(bcol * 255);
88        for c = 1:size(A, 3)
89            A(:,[1:l-1, r+1:end],c) = bcol(c);
90            A([1:t-1, b+1:end],:,c) = bcol(c);
91        end
92    end
93else
94    if nargin < 3
95        renderer = '-opengl';
96    end
97    % Set paper size
98    old_mode = get(fig, 'PaperPositionMode');
99    set(fig, 'PaperPositionMode', 'auto');
100    % Print to tiff file
101    print(fig, renderer, ['-r' num2str(ceil(get(0, 'ScreenPixelsPerInch')*res))], '-dtiff', tmp_nam);
102    % Reset paper size
103    set(fig, 'PaperPositionMode', old_mode);
104    % Read in the printed file
105    A = imread(tmp_nam);
106end
107% Delete the temporary file
108delete(tmp_nam);
109return
Note: See TracBrowser for help on using the repository browser.