[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)
|
---|
[10635] | 8 | % [A bcol] = print2array(...)
|
---|
[7182] | 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.
|
---|
[10635] | 25 | % bcol - 1x3 uint8 vector of the background color
|
---|
[7182] | 26 |
|
---|
[10635] | 27 | % Copyright (C) Oliver Woodford 2008-2011
|
---|
[7182] | 28 |
|
---|
[10635] | 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 | function [A bcol] = print2array(fig, res, renderer)
|
---|
[7182] | 40 | % Generate default input arguments, if needed
|
---|
| 41 | if nargin < 2
|
---|
| 42 | res = 1;
|
---|
| 43 | if nargin < 1
|
---|
| 44 | fig = gcf;
|
---|
| 45 | end
|
---|
| 46 | end
|
---|
| 47 | % Warn if output is large
|
---|
| 48 | old_mode = get(fig, 'Units');
|
---|
| 49 | set(fig, 'Units', 'pixels');
|
---|
| 50 | px = get(fig, 'Position');
|
---|
| 51 | set(fig, 'Units', old_mode);
|
---|
[10635] | 52 | npx = prod(px(3:4)*res)/1e6;
|
---|
| 53 | if npx > 30
|
---|
[7182] | 54 | % 30M pixels or larger!
|
---|
[10635] | 55 | warning('MATLAB:LargeImage', 'print2array generating a %.1fM pixel image. This could be slow and might also cause memory problems.', npx);
|
---|
[7182] | 56 | end
|
---|
[10635] | 57 | % Retrieve the background colour
|
---|
| 58 | bcol = get(fig, 'Color');
|
---|
[7182] | 59 | % Set the resolution parameter
|
---|
[10635] | 60 | res_str = ['-r' num2str(ceil(get(0, 'ScreenPixelsPerInch')*res))];
|
---|
[7182] | 61 | % Generate temporary file name
|
---|
| 62 | tmp_nam = [tempname '.tif'];
|
---|
| 63 | if 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
|
---|
[10635] | 69 | ghostscript(['-dEPSCrop -q -dNOPAUSE -dBATCH ' res_str ' -sDEVICE=tiff24nc -sOutputFile="' tmp_nam '" "' tmp_eps '"']);
|
---|
[7182] | 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
|
---|
[10635] | 82 | if isequal(bcol, 'none')
|
---|
| 83 | bcol = [];
|
---|
| 84 | elseif isequal(bcol, [1 1 1])
|
---|
| 85 | bcol = uint8([255 255 255]);
|
---|
| 86 | else
|
---|
[7182] | 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
|
---|
[10635] | 107 | bcol = median([reshape(A(:,[l r],:), [], size(A, 3)); reshape(A(:,[t b],:), [], size(A, 3))], 1);
|
---|
[7182] | 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
|
---|
| 113 | else
|
---|
| 114 | if nargin < 3
|
---|
| 115 | renderer = '-opengl';
|
---|
| 116 | end
|
---|
[10635] | 117 | err = false;
|
---|
[7182] | 118 | % Set paper size
|
---|
| 119 | old_mode = get(fig, 'PaperPositionMode');
|
---|
| 120 | set(fig, 'PaperPositionMode', 'auto');
|
---|
| 121 | try
|
---|
| 122 | % Print to tiff file
|
---|
[10635] | 123 | print(fig, renderer, res_str, '-dtiff', tmp_nam);
|
---|
[7182] | 124 | % Read in the printed file
|
---|
| 125 | A = imread(tmp_nam);
|
---|
| 126 | % Delete the temporary file
|
---|
| 127 | delete(tmp_nam);
|
---|
[10635] | 128 | catch ex
|
---|
| 129 | err = true;
|
---|
[7182] | 130 | end
|
---|
| 131 | % Reset paper size
|
---|
| 132 | set(fig, 'PaperPositionMode', old_mode);
|
---|
[10635] | 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
|
---|
[7182] | 148 | end
|
---|
[10635] | 149 | % Check the output size is correct
|
---|
| 150 | if 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
|
---|
| 156 | end
|
---|
[7182] | 157 | return
|
---|