readspe.m
2.82 KB
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
% Read images of TIFF, SPE2.2(WinSpec) and SPE3.0(Lightfield)
% Version: JTL Jun-9-2016
% ----------------- READ THIS FIRST !!!!! --------------------------------
% Change the file name to "readspe" before use
% Example:
% Z = readspe(filename)
% Z = readspe(filename,'info')
% Z = readspe(filename,frame_index)
% Z = readspe(filename,frame_index,'info')
% Input:
% filename - filename string, e.g. 'image.spe'
% frame_index - frame index, start from 1
% If you have multiple frames, use a "for" loop
% 'info' - flag to show file info, i.e. dimension, number of frames, version
% Output:
% Z - UINT16 image (convert to double if you need)
% ------------------------------------------------------------------------
% Z = readspe (filename,frame_index,'info')
function Z = readspe (filename,varargin)
if exist(filename) == 2
Nfr = 1; % default read first frame
if nargin >1
if isa(varargin{1},'numeric')
Nfr = varargin{1};
end
end
[~,name,ext] = fileparts(filename);
switch upper(ext)
case '.TIFF'
file_ver = 'TIFF';
Z = imread(filename);
[Y,X] = size(Z);
% datatype = class(Z)
case '.SPE'
fid = fopen(filename);
I = fread(fid,Inf,'uint8');
X = double(typecast(uint8(I(43:44)),'uint16'));
Y = double(typecast(uint8(I(657:658)),'uint16'));
fr = typecast(uint8(I(1447:1450)),'int32');
spe_ver = typecast(uint8(I(1993:1996)),'single');
file_ver = ['SPE ' num2str(spe_ver)];
datatypeN = typecast(uint8(I(109:110)),'int16');
switch datatypeN
case 0 % 32-bit float
datatype = 'single'; datalength = 4;
case 1 % 32-bit signed integer
datatype = 'int32'; datalength = 4;
case 2 % 16-bit signed integer
datatype = 'int16'; datalength = 2;
case 3 % 16-bit unsigned integer
datatype = 'uint16'; datalength = 2;
case 8 % 32-bit unsigned integer
datatype = 'uint32'; datalength = 4;
end
% A = I(4101:4100+X*Y*2); % Default read first frame
A = I(4101+X*Y*datalength*(Nfr-1):4100+X*Y*datalength*Nfr);
B = typecast(uint8(A),datatype); % important
Z = reshape(B,X,Y);
Z = Z';
fclose(fid);
end
if nargin >1
if varargin{end} == 'info'
display(['X = ' num2str(X)]);
display(['Y = ' num2str(Y)]);
if(exist('fr','var'));display(['Number of Frames: ' num2str(fr)]);end;
display(['File version: ' file_ver]);
end
end
elseif exist(filename) == 0
display('File does not exist!');
end