-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxdisp.m
121 lines (106 loc) · 3.41 KB
/
xdisp.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
function out = xdisp(image, var_name, xtra_cmd_opts)
% Usage: xdisp(image [, xdisp_window_title [, command_line_options]])
%
%
% e.g. xdisp(im1,'Cool_Image','-spectral -color_bar');
%
% Displays matrix im1 using xdisp and creates a window named 'Cool_Image'
% in which the color table is spectral and a color bar is shown at the
% right.
%
% xdisp.m
% v4.2, Oct 27, 1997
%
% Copyright (c) B Pike, 1997
% check for number of arguments
if nargin < 1
error('XDISP requires at least one input argument.');
elseif nargin == 1
fname = 'matlab_image'; % no variable name given: used default
xtra_cmd_opts = ' '; % no extra options selected: leave empty
elseif nargin == 2
fname = var_name; % use provided variable name
xtra_cmd_opts = ' '; % no extra options selected: leave empty
else
fname = var_name; % use provided variable name
end
% replace spaces with underscores in fname
for i=1:length(fname)
if (fname(i)==' ')
fname(i) = '_';
end
end
% set any standard options here
cmd_opts = ' -TrueColor ';
% check Matlab version to see if n-D arrays are supported
[ml_version ml_date] = version;
if (str2num(ml_version(1)) >= 5) % n-D stuff ok
% get the image size (handle n-D volumes as 3D)
[rows cols slices] = size(image);
if (length(size(image)) > 3) % notify use if greater than 3-D
disp(['NOTE: Your ', num2str(length(size(image))), '-D data ', ...
'will be interpreted as a 3-D image volume by xdisp.']);
end;
else % n-D stuff not ok
[rows cols] = size(image);
end;
% matlab stores data column wise so fix this
if (str2num(ml_version(1)) >= 5) % n-D allowed stuff
if (rows~=cols) % can't transpose non-square images in place
tmp_image=zeros(cols,rows,slices);
for i=1:slices
% Note: have to use eval to get around Matlab 4 complaints
% about illegal scripting despite the fact that this code is
% selected only if running Matlab 5.
eval('tmp_image(:,:,i) = transpose(image(:,:,i));');
end;
clear image;
image = tmp_image;
else % transpose in place
for i=1:slices
% Note: have to use eval to get around Matlab 4 complaints
% about illegal scripting despite the fact that this code is
% selected only if running Matlab 5.
eval('image(:,:,i) = transpose(image(:,:,i));');
end;
end;
else % n-D not allowed stuff
image = image';
end;
% now get correct dimensions
if (str2num(ml_version(1)) >= 5)
[rows cols slices] = size(image);
else
[rows cols] = size(image);
end;
% check for `flat' images
maxval = max(max(image));
minval = min(min(image));
if (minval==maxval)
disp(['WARNING: matrix minimum and maximum are equal (', ...
num2str(minval),')... I do not display flat images!']);
return;
end;
% compose a `unique' directory name using clock and create it
t = clock;
dir_name = ['/tmp/ml.',num2str(t(5)),'s',num2str(t(6))];
eval(['!mkdir ', dir_name]);
% now compose the complete file name
fname = [dir_name,'/',fname];
% open file and write the image as doubles and close
fid = fopen(fname,'wb');
fwrite(fid,image,'double');
fclose(fid);
% create shell command to run xdisp
xdisp_cmd=['!xdisp ', fname, ...
' -w ', int2str(rows), ...
' -h ', int2str(cols), ...
' -double ' ...
cmd_opts, ...
xtra_cmd_opts, ...
' ; ' ...
'\rm -f ' , fname , ' ; '...
'\rmdir ', dir_name ' & '];
% issue shell command
% disp(xdisp_cmd); % debug
eval(xdisp_cmd);