forked from cultpenguin/mGstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_mul.m
147 lines (129 loc) · 3.92 KB
/
print_mul.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
% print_mul : prints both EPS, PNG, and PDF figures of current plot
%
% CALL :
% print_mul(fname,types,trim,transp,res,do_watermark);
% fname : filename
% ['test'] (def)
% types : type of output files
% [0] no hardcopy
% [1] png
% [2] pdf
% [3] eps
% [4] png,pdf
% [5] png,pdf,eps
% [6] png transparent with export_fig (+1,+2)
% trim : trim PNG image (remove borders - requires mogrify)
% [0]: no trimminf (def)
% [1]: trimming
% trans : transparency of PNG image(requires mogrify)
% [0]: no transparency (def)
% [1]: white as transparent
% ['red']: red as transparent
% res : resolution
% [300] (def)
% do_watermark : add fname as watermark to figure in lower right corner
% [0] no watermark(def)
% [1] watermark(def)
%
% Ex :
% print_mul('test') % prints test.eps, test.png, and test.pdf
% print_mul('test',1,1) % prints test.eps, test.png, and test.pdf
% % test.png will be trimmed and white set as
% % transparent color
%
% print_mul('test',0,'black',600,1) % prints test.eps, test.png, and test.pdf
% % test.png will NOT be trimmed and BLACK set as
% % transparent color. resolution is set to 600dpi
% % and a watermark is added
%
%% /TMH 2005-2012
%
function print_mul(fname,types,trim,transp,res,do_watermark);
if nargin<1, fname='test';end
if nargin<2, types=1;end
if types==0; return;end
if nargin<5, res=300;end
if nargin<3, trim=0;end
if trim>1
res=trim;
trim=0;
end
if nargin<4,
%transp=1;
transp=0;
end
if nargin<6, do_watermark=0;end
save_fig=0;
fname=space2char(fname);
fname=space2char(fname,'_','\.');
if do_watermark==1
watermark(fname);
end
fname=space2char(fname);
i=0;
if ((types==1)||(types==4)||(types==5)||(types==6))
i=i+1;P{i}.type='-dpng';P{i}.ext='.png';
end
if ((types==2)||(types==4)||(types==6))
i=i+1;P{i}.type='-dpdf';P{i}.ext='.pdf';
end
if ((types==3)||(types==5))
i=i+1;P{i}.type='-depsc';P{i}.ext='.eps';
end
for i=1:length(P)
res_string=sprintf('-r%d',res);
file_out=[fname,P{i}.ext];
%file_out=[fname,'];
try
print(gcf, P{i}.type,res_string,[file_out])
catch
disp(sprintf('%s : failed to print %s as %s',mfilename,fname,P{i}.ext))
end
end
%% SAVE FIG FILE
if (save_fig==1)
saveas(gcf,[fname,'.fig'],'fig');
end
%% EXPORT FIG
if types==6,
if exist('export_fig','file')
scale=res/180;
export_fig(sprintf('%s_transp.png',fname),'-transparent',sprintf('-m%3.1f',scale))
%export_fig(sprintf('%s_transp.png',fname),'-transparent','-m8')
else
disp(sprintf('%s: export_fig is not available -- skipping',mfilename))
end
end
%% TRIM IMAGE USING MOGRIFY
if (trim==1)||(transp~=0)
if isunix
[a,mogrifybin]=unix('which mogrify');
mogrifybin=mogrifybin(1:length(mogrifybin)-1);
else
mogrifybin='c:\cygwin\bin\mogrify.exe';
%disp(sprintf('%s : trimming only supported on Unix',mfilename))
end
% TRANSPAREMCY
if ischar(transp)
transp_cmd=['-transparent ',transp];
else
if transp==1
transp_cmd=['-transparent white'];
else
transp_cmd='';
end
end
if trim==1;
trim_cmd='-trim';
else
trim_cmd='';
end
% MOGRIFY PNG FILE
if exist(mogrifybin,'file')
cmd=sprintf('%s %s %s %s.png',mogrifybin,trim_cmd,transp_cmd,fname);
[status,result]=system(cmd);
if status~=0; disp(sprintf('%s : %s',mfilename,result));end
else
% disp(sprintf('%s : MOGRIFY binary not found',mfilename))
end
end