20 [fig, options] = parse_args(nargout, varargin[:]);
22 cls =
all(ismember(
get(fig,
" Type "), [
" axes ",
" uipanel "]));
28 if ~isequal(
get(fig,
" Type "),
" figure ");
29 error(
" Handle must be that of a figure, axes or uipanel ");
32 old_mode =
get(fig,
" InvertHardcopy ");
37 magnify = options.magnify * options.aa_factor;
38 if isbitmap(options) && magnify ~= 1
39 fontu = findobj(fig,
" FontUnits ",
" normalized ");
44 set(fig,
" Visible ",
" off ");
45 fontu = findobj(fig,
" FontUnits ",
" normalized ");
48 set(fontu,
" FontUnits ",
" points ");
52 Hlims = findall(fig,
" Type ",
" axes ");
55 Xlims = make_cell(
get(Hlims,
" XLimMode "));
56 Ylims = make_cell(
get(Hlims,
" YLimMode "));
57 Zlims = make_cell(
get(Hlims,
" ZLimMode "));
58 Xtick = make_cell(
get(Hlims,
" XTickMode "));
59 Ytick = make_cell(
get(Hlims,
" YTickMode "));
60 Ztick = make_cell(
get(Hlims,
" ZTickMode "));
63 set(Hlims,
" XLimMode ",
" manual ",
" YLimMode ",
" manual ",
" ZLimMode ",
" manual ",
" XTickMode ",
" manual ",
" YTickMode ",
" manual ",
" ZTickMode ",
" manual ");
65 set(fig,
" InvertHardcopy ",
" off ");
67 switch options.renderer
69 renderer =
" -opengl ";
71 renderer =
" -zbuffer ";
73 renderer =
" -painters ";
75 renderer =
" -opengl ";
81 if options.transparent && (options.png || options.alpha)
84 hCB = findobj(fig,
" Type ",
" axes ",
" Tag ",
" Colorbar ");
89 yCol =
get(hCB,
" YColor ");
90 xCol =
get(hCB,
" XColor ");
92 yCol = cell2mat(yCol);
93 xCol = cell2mat(xCol);
100 pos =
get(fig,
" Position ");
103 tcol =
get(fig,
" Color ");
104 set(fig,
" Color ",
" k ",
" Position ", pos);
106 set(hCB(yCol==0),
" YColor ", [0 0 0]);
107 set(hCB(xCol==0),
" XColor ", [0 0 0]);
111 B = downsize(single(B), options.aa_factor);
113 set(fig,
" Color ",
" w ",
" Position ", pos);
115 set(hCB(yCol==3),
" YColor ", [1 1 1]);
116 set(hCB(xCol==3),
" XColor ", [1 1 1]);
120 A = downsize(single(A), options.aa_factor);
122 set(fig,
" Color ", tcol,
" Position ", pos);
124 alpha = round(sum(B - A, 3)) / (255 * 3) + 1;
127 A = B ./ A(:,:,[1 1 1]);
130 if options.colourspace == 2
136 [alpha, v] = crop_background(alpha, 0);
137 A = A(v(1):v(2),v(3):v(4),:);
141 res = options.magnify *
get(0,
" ScreenPixelsPerInch ") / 25.4e-3;
143 imwrite(A, [options.name
" .png "],
" Alpha ",
double(alpha),
" ResolutionUnit ",
" meter ",
" XResolution ", res,
" YResolution ", res);
149 A = check_greyscale(A);
155 options.alpha=
false;
159 alph = alpha(:,:,ones(1, size(A, 3)));
160 A = uint8(single(A) .* alph + 255 * (1 - alph));
169 if options.transparent
172 pos =
get(fig,
" Position ");
173 tcol =
get(fig,
" Color ");
174 set(fig,
" Color ",
" w ",
" Position ", pos);
176 set(fig,
" Color ", tcol,
" Position ", pos);
183 A = crop_background(A, tcol);
186 A = downsize(A, options.aa_factor);
187 if options.colourspace == 2
192 A = check_greyscale(A);
200 alpha = zeros(size(A, 1), size(A, 2),
" single ");
205 res = options.magnify *
get(0,
" ScreenPixelsPerInch ") / 25.4e-3;
206 imwrite(A, [options.name
" .png "],
" ResolutionUnit ",
" meter ",
" XResolution ", res,
" YResolution ", res);
209 imwrite(A, [options.name
" .bmp "]);
213 quality = options.quality;
218 imwrite(A, [options.name
" .jpg "],
" Mode ",
" lossless ");
220 imwrite(A, [options.name
" .jpg "],
" Quality ", quality);
225 if options.colourspace == 1 && size(A, 3) == 3
228 K_ = 255 ./ max(255 - K, 1);
229 C = (A(:,:,1) - K) .* K_;
230 M = (A(:,:,2) - K) .* K_;
231 Y = (A(:,:,3) - K) .* K_;
232 A = uint8(cat(3, C, M,
Y, K));
235 append_mode = [
" overwrite ",
" append "];
236 imwrite(A, [options.name
" .tif "],
" Resolution ", options.magnify*
get(0,
" ScreenPixelsPerInch "),
" WriteMode ", append_mode[options.append+1]);
243 renderer =
" -painters ";
246 tmp_nam = [tempname
" .eps "];
248 pdf_nam = [options.name
" .pdf "];
250 pdf_nam = [tempname
" .pdf "];
253 p2eArgs = [renderer];
254 if options.colourspace == 1
255 p2eArgs = [p2eArgs [
" -cmyk "]];
258 p2eArgs = [p2eArgs [
" -loose "]];
264 if options.transparent && ~isequal(
get(fig,
" Color "),
" none ")
265 eps_remove_background(tmp_nam);
269 fig_nam =
get(fig,
" Name ");
271 warning(
" export_fig:EmptyBookmark ",
" Bookmark requested for figure with no name. Bookmark will be empty. ");
273 add_bookmark(tmp_nam, fig_nam);
276 eps2pdf(tmp_nam, pdf_nam, 1, options.append, options.colourspace==2, options.quality);
287 pdf2eps(pdf_nam, [options.name
" .eps "]);
306 set(fig,
" InvertHardcopy ", old_mode);
308 for a = 1:numel(Hlims)
309 set(Hlims(a),
" XLimMode ", Xlims[a],
" YLimMode ", Ylims[a],
" ZLimMode ", Zlims[a],
" XTickMode ", Xtick[a],
" YTickMode ", Ytick[a],
" ZTickMode ", Ztick[a]);
328 fig =
get(0,
" CurrentFigure ");
329 options =
struct(
" name ",
" export_fig_out ", ...
331 " transparent ",
false, ...
340 " colourspace ", 0, ...
342 " append ",
false, ...
343 " im ", nout == 1, ...
344 " alpha ", nout == 2, ...
345 " aa_factor ", 3, ...
347 " bookmark ",
false, ...
354 if all(ishandle(varargin[a]))
356 elseif ischar(varargin[a]) && ~isempty(varargin[a])
357 if varargin[a](1) ==
" - "
358 switch lower(varargin[a](2:end))
361 case [
" trans ",
" transparent "]
362 options.transparent=
true;
375 case [
" tif ",
" tiff "]
377 case [
" jpg ",
" jpeg "]
382 options.colourspace= 0;
384 options.colourspace= 1;
385 case [
" gray ",
" grey "]
386 options.colourspace= 2;
387 case [
" a1 ",
" a2 ",
" a3 ",
" a4 "]
388 options.aa_factor= str2double(varargin[a](3));
390 options.append=
true;
392 options.bookmark=
true;
396 val = str2double(regexp(varargin[a],
" (?<=-(m|M|r|R|q|Q))(\d*\.)?\d+(e-?\d+)? ",
" match "));
398 error(
" option %s not recognised ", varargin[a]);
400 switch lower(varargin[a](2))
402 options.magnify= val;
404 options.magnify= val ./
get(0,
" ScreenPixelsPerInch ");
406 options.quality= max(val, 0);
410 [p, options.name, ext] = fileparts(varargin[a]);
412 options.name= [p filesep options.name];
415 case [
" .tif ",
" .tiff "]
417 case [
" .jpg ",
" .jpeg "]
428 options.name= varargin[a];
436 error(
" No figure found ");
440 if ~isvector(options) && ~isbitmap(options)
445 if isequal(
get(ancestor(fig,
" figure "),
" Color "),
" none ")
446 options.transparent=
true;
451 if native && isbitmap(options)
453 list = findobj(fig,
" Type ",
" image ",
" Tag ",
" export_fig_native ");
455 list = findobj(fig,
" Type ",
" image ",
" Visible ",
" on ");
459 height = size(
get(hIm,
" CData "), 1);
465 yl =
get(hIm,
" YData ");
467 yl = [yl(1)-0.5 yl(1)+height+0.5];
472 yl = yl + [-0.5 0.5] * (diff(yl) / (height - 1));
474 hAx =
get(hIm,
" Parent ");
475 yl2 =
get(hAx,
" YLim ");
477 oldUnits =
get(hAx,
" Units ");
478 set(hAx,
" Units ",
" pixels ");
479 pos =
get(hAx,
" Position ");
480 set(hAx,
" Units ", oldUnits);
486 pbar =
get(hAx,
" PlotBoxAspectRatio ");
487 pos = min(pos(4), pbar(2)*pos(3)/pbar(1));
489 options.magnify= (height * diff(yl2)) / (pos * diff(yl));
504 A = imresize(A, 1/factor,
" bilinear ");
509 filt = (-factor-1:factor+1) / (factor * 0.6);
510 filt = exp(-filt .* filt);
512 filt = single(filt / sum(filt));
514 padding = floor(numel(filt) / 2);
516 A(:,:,a) = conv2(filt, filt
" , single(A([ones(1, padding) 1:end repmat(end, 1, padding)],[ones(1, padding) 1:end repmat(end, 1, padding)],a)), "valid^
t);
519 A = A(1+floor(mod(end-1, factor)/2):factor:end,1+floor(mod(end-1, factor)/2):factor:end,:);
526 A = cast(reshape(reshape(single(A), [], 3) * single([0.299; 0.587; 0.114]), size(A, 1), size(A, 2)),
class(A));
532 if size(A, 3) == 3 && ...
533 all(reshape(A(:,:,1) == A(:,:,2), [], 1)) && ...
534 all(reshape(A(:,:,2) == A(:,:,3), [], 1))
544 if isscalar(bcol) && c > 1
545 bcol = bcol(ones(1, c));
550 if ~
all(A(:,
l,a) == bcol(a))
562 if ~
all(A(:,r,a) == bcol(a))
574 if ~
all(A(
t,:,a) == bcol(a))
586 if ~
all(A(b,:,a) == bcol(a))
597 v = [max(
t-1, 1) min(b+1, h) max(
l-1, 1) min(r+1, w)];
598 A = A(v(1):v(2),v(3):v(4),:);
604 fh = fopen(fname,
" r+ ");
606 error(
" Not able to open file %s. ", fname);
617 if isequal(regexp(l,
" *0 +0 +\d+ +\d+ +rf *[\n\r]+ ",
" start "), 1)
619 l(1:regexp(l,
" [\n\r] ",
" start ",
" once ")-1) =
" ";
620 fseek(fh, -numel(l), 0);
632 b = options.pdf || options.eps;
638 b = options.png || options.tif || options.jpg || options.bmp || options.im || options.alpha;
653 fh = fopen(fname,
" r ");
655 error(
" File %s not found. ", fname);
658 fstrm = fread(fh,
" *char ")^
t;
666 fstrm = strrep(fstrm,
" %%BeginProlog ", sprintf(
" %%%%BeginProlog\n/pdfmark where {pop} {userdict /pdfmark /cleartomark load put} ifelse "));
668 fstrm = strrep(fstrm,
" %%EndPageSetup ", sprintf(
" %%%%EndPageSetup\n[ /Title (%s) /OUT pdfmark ",bookmark_text));
671 fh = fopen(fname,
" w ");
673 error(
" Unable to open %s for writing. ", fname);
676 fwrite(fh, fstrm,
" char*1 ");
function A = exportfig>check_greyscale(A)
function [ A , v ] = exportfig>crop_background(A, bcol)
function b = exportfig>isbitmap(options)
function [ im , alpha ] = export_fig(varargin)
Make sure the figure is rendered correctly now so that properties like axes limits are up-to-date...
function fh = isolate_axes(ah, vis)
Make sure we have an array of handles.
function print2eps(name, fig, varargin)
function exportfig>add_bookmark(fname, bookmark_text)
A variable number of input arguments.
function [ fig , options ] = exportfig>parse_args(nout, varargin)
function [ A , bcol ] = print2array(fig, res, renderer)
Generate default input arguments, if needed.
function A = exportfig>rgb2grey(A)
function A = exportfig>downsize(A, factor)
function exportfig>eps_remove_background(fname)
function eps2pdf(source, dest, crop, append, gray, quality)
Intialise the options string for ghostscript.
function b = exportfig>isvector(options)
function fh = copyfig(fh)
Set the default.
function A = exportfig>make_cell(A)
function pdf2eps(source, dest)
Construct the options string for pdftops.