-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwrite_idx.m
79 lines (72 loc) · 1.78 KB
/
write_idx.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
function write_idx(data, path, endian, compress)
%WRITE_IDX Write IDX file format
% Usage: write_idx(data, path, endian='b', compress=true)
%
% For details about the IDX file format, see the bottom of
% http://yann.lecun.com/exdb/mnist/
%
% Hang Su 2016
switch class(data),
case 'uint8',
dtype = hex2dec('08');
dtypeMat = 'uint8';
dtypeStr = 'ubyte';
case 'int8',
dtype = hex2dec('09');
dtypeMat = 'int8';
dtypeStr = 'byte';
case 'int16',
dtype = hex2dec('0B');
dtypeMat = 'int16';
dtypeStr = 'short';
case 'int32',
dtype = hex2dec('0C');
dtypeMat = 'int32';
dtypeStr = 'int';
case 'single',
dtype = hex2dec('0D');
dtypeMat = 'single';
dtypeStr = 'float';
case 'double',
dtype = hex2dec('0E');
dtypeMat = 'double';
dtypeStr = 'double';
otherwise,
error('Data type not supported: %s', class(data));
end
if ~exist('endian', 'var') || isempty(endian),
endian = 'b';
end
if ~exist('compress', 'var') || isempty(compress),
compress = true;
end
ndim = ndims(data);
sz = size(data);
assert(all(sz>0));
if sz(end)==1,
assert(ndim==2);
sz = sz(1:end-1);
ndim = ndim-1;
end
if ~exist('path', 'var') || isempty(path),
path = sprintf('mat-idx%d-%s', ndim, dtypeStr);
else
[pathstr, name, ext] = fileparts(path);
if ~isempty(ext),
assert(strcmp(ext,'.gz'), 'Only .gz compression is supported');
path = fullfile(pathstr, name);
if ~compress, error('.gz save name given but compression is disabled'); end
end
end
if ndim>1,
data = permute(data, ndim:-1:1);
end
fid = fopen(path, 'w+');
fwrite(fid, dtype*256+ndim, 'int32', endian);
fwrite(fid, sz, 'int32', endian);
fwrite(fid, data, dtypeMat, endian);
fclose(fid);
if compress,
gzip(path);
delete(path);
end