-
Notifications
You must be signed in to change notification settings - Fork 0
/
IDX.jl
executable file
·41 lines (26 loc) · 832 Bytes
/
IDX.jl
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
module IDX
export read_idx
using Base: ntoh, OneTo
using LinearAlgebra: transpose!
const number_format::Dict{UInt8, DataType} = Dict(
0x08 => UInt8,
0x09 => Int8,
0x0B => Int16,
0x0C => Int32,
0x0D => Float32,
0x0E => Float64
)
function read_idx(file_name)::AbstractArray{Real}
open(file_name, "r") do file
magic_number = read(file, 4)
idx_type::Type = number_format[magic_number[3]]
dims_count::Int8 = magic_number[4]
dims = OneTo(dims_count) .|> (_ -> read(file, Int32)) .|> ntoh |> collect |> Tuple
contents = OneTo(prod(dims)) .|> (_ -> read(file, idx_type)) |> collect
if idx_type != UInt8 && idx_type != Int8
contents = ntoh(contents)
end
reshape(contents, Int64.(dims)...)
end
end
end #module