Skip to content

Instantly share code, notes, and snippets.

@Elzair
Created March 10, 2015 01:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Elzair/32d59b7c4c93c29f9851 to your computer and use it in GitHub Desktop.
Save Elzair/32d59b7c4c93c29f9851 to your computer and use it in GitHub Desktop.
Julia IDX Parser
import Base.ntoh
@vectorize_1arg Number Base.ntoh
function readIdx(file_name)
fs = open(file_name, "r")
magic_number = read(fs, UInt8, 4)
if magic_number[3] == 0x08
idx_type = UInt8
elseif magic_number[2] == 0x09
idx_type = Int8
elseif magic_number[2] == 0x0B
idx_type = Int16
elseif magic_number[2] == 0x0C
idx_type = Int32
elseif magic_number[2] == 0x0D
idx_type = Float32
elseif magic_number[2] == 0x0E
idx_type = Float64
end
dims = Array{Int64}(ntoh(read(fs, Int32, magic_number[4])))
contents = read(fs, idx_type, prod(dims))
if idx_type != UInt8 && idx_type != Int8
contents = ntoh(contents)
end
out = reshape(contents, tuple(dims...))
return out
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment