示例#1
0
        public INifti <T> ReadNifti(string filepath)
        {
            using (var fileStream = new FileStream(filepath, FileMode.Open))
            {
                if (!fileStream.CanRead)
                {
                    throw new IOException($"Unable to access file: {filepath}");
                }

                var isBigEndian = IsBigEndian(fileStream);
                var reader      = new BinaryReaderBigAndLittleEndian(fileStream, isBigEndian);

                ReadNiftiHeader(reader);

                var bytesLength = Convert.ToInt32(reader.BaseStream.Length) - Convert.ToInt32(Header.vox_offset);
                Voxels = ReadVoxels(reader, bytesLength);

                if (reader.BaseStream.Position != reader.BaseStream.Length)
                {
                    throw new Exception("Not all bytes in the stream were read!");
                }
            }

            return(this);
        }
示例#2
0
        public void ReadNiftiHeader(string filepath)
        {
            using (var fileStream = new FileStream(filepath, FileMode.Open))
            {
                if (!fileStream.CanRead)
                {
                    throw new IOException($"Unable to access file: {filepath}");
                }

                var isBigEndian = IsBigEndian(fileStream);
                var reader      = new BinaryReaderBigAndLittleEndian(fileStream, isBigEndian);

                ReadNiftiHeader(reader);
            }
        }
示例#3
0
        private static bool IsBigEndian(Stream fileStream)
        {
            var reader     = new BinaryReader(fileStream);
            var headerSize = reader.ReadInt32();

            if (headerSize != 348) // this means it might be big endian, but let's make sure it is
                                   // (we should be able to check if we're reading 1543569408 and skip the extra read, but this way is less brittle)
            {
                fileStream.Position = 0;
                var beReader = new BinaryReaderBigAndLittleEndian(fileStream, true);
                headerSize = beReader.ReadInt32();
                if (headerSize != 348)
                {
                    throw new Exception("Either not a nifti File or type not supported! sizeof_hdr should be 348 for a nifti-1 file.");
                }
                fileStream.Position = 0;
                return(true);
            }
            fileStream.Position = 0;
            return(false);
        }