Ejemplo n.º 1
0
 /// <summary>
 /// Write the Nifti header to an .hdr file.
 /// </summary>
 /// <param name="hdr"></param>
 /// <param name="path"></param>
 /// <param name="gzip"></param>
 public static void Write(NiftiHeader hdr, string path, bool gzip = false)
 {
     using (var stream = WriteStream(path, gzip))
     {
         Write(stream, hdr);
     }
 }
Ejemplo n.º 2
0
        private static void Write(Stream stream, NiftiHeader hdr)
        {
            Write(stream, NiftiHeader.EXPECTED_SIZE_OF_HDR);
            Write(stream, hdr.data_type);
            Write(stream, hdr.db_name);
            Write(stream, hdr.extents);
            Write(stream, hdr.session_error);
            Write(stream, hdr.regular);
            Write(stream, hdr.dim_info);
            Write(stream, hdr.dim);
            Write(stream, hdr.intent_p1);
            Write(stream, hdr.intent_p2);
            Write(stream, hdr.intent_p3);
            Write(stream, hdr.intent_code);
            Write(stream, hdr.datatype);
            Write(stream, hdr.bitpix);
            Write(stream, hdr.slice_start);
            Write(stream, hdr.pixdim);
            Write(stream, hdr.vox_offset);
            Write(stream, hdr.scl_slope);
            Write(stream, hdr.scl_inter);
            Write(stream, hdr.slice_end);
            Write(stream, hdr.slice_code);
            Write(stream, hdr.xyzt_units);
            Write(stream, hdr.cal_max);
            Write(stream, hdr.cal_min);
            Write(stream, hdr.slice_duration);
            Write(stream, hdr.toffset);
            Write(stream, hdr.glmax);
            Write(stream, hdr.glmin);

            Write(stream, hdr.descrip);
            Write(stream, hdr.aux_file);

            Write(stream, hdr.qform_code);
            Write(stream, hdr.sform_code);

            Write(stream, hdr.quatern_b);
            Write(stream, hdr.quatern_c);
            Write(stream, hdr.quatern_d);
            Write(stream, hdr.qoffset_x);
            Write(stream, hdr.qoffset_y);
            Write(stream, hdr.qoffset_z);

            Write(stream, hdr.srow_x);
            Write(stream, hdr.srow_y);
            Write(stream, hdr.srow_z);

            Write(stream, hdr.intent_name);
            Write(stream, hdr.magic);

            Write(stream, hdr.extension);
            if (hdr.extension[0] == 1)
            {
                Write(stream, hdr.esize);
                Write(stream, hdr.ecode);
                Write(stream, hdr.edata);
            }
        }
Ejemplo n.º 3
0
        public INiftiHeader DeepCopy()
        {
            NiftiHeader copy = new NiftiHeader();

            copy.sizeof_hdr = sizeof_hdr;
            copy.dim_info   = dim_info;
            copy.dim        = new short[dim.Length];
            dim.CopyTo(copy.dim, 0);
            copy.intent_p1   = intent_p1;
            copy.intent_p2   = intent_p2;
            copy.intent_p3   = intent_p3;
            copy.intent_code = intent_code;
            copy.datatype    = datatype;
            copy.bitpix      = bitpix;
            copy.slice_start = slice_start;
            copy.pix_dim     = new float[pix_dim.Length];
            pix_dim.CopyTo(copy.pix_dim, 0);
            copy.vox_offset     = vox_offset;
            copy.scl_slope      = scl_slope;
            copy.scl_inter      = scl_inter;
            copy.slice_end      = slice_end;
            copy.slice_code     = slice_code;
            copy.xyzt_units     = xyzt_units;
            copy.cal_max        = cal_max;
            copy.cal_min        = cal_min;
            copy.slice_duration = slice_duration;
            copy.toffset        = toffset;
            copy.descrip        = descrip;
            copy.aux_file       = aux_file;
            copy.qform_code     = qform_code;
            copy.sform_code     = sform_code;
            copy.quatern_b      = quatern_b;
            copy.quatern_c      = quatern_c;
            copy.quatern_d      = quatern_d;
            copy.qoffset_x      = qoffset_x;
            copy.qoffset_y      = qoffset_y;
            copy.qoffset_z      = qoffset_z;
            copy.srow_x         = new float[srow_x.Length];
            srow_x.CopyTo(copy.srow_x, 0);
            copy.srow_y = new float[srow_y.Length];
            srow_y.CopyTo(copy.srow_y, 0);
            copy.srow_z = new float[srow_z.Length];
            srow_z.CopyTo(copy.srow_z, 0);
            copy.intent_name = intent_name;
            copy.magic       = magic;

            return(copy);
        }
Ejemplo n.º 4
0
 private static FileType TypeOf(NiftiHeader hdr)
 {
     // Magic bytes are "ni1\0" for hdr/img files and "n+1\0" for nii.
     if (hdr.magic[0] != 0x6E || hdr.magic[2] != 0x31 || hdr.magic[3] != 0x00)
     {
         return(FileType.UNKNOWN);
     }
     else if (hdr.magic[1] == 0x69)
     {
         return(FileType.HDR);
     }
     else if (hdr.magic[1] == 0x2B)
     {
         return(FileType.NII);
     }
     else
     {
         return(FileType.UNKNOWN);
     }
 }
Ejemplo n.º 5
0
        private static NiftiHeader ReadHeader(Stream stream)
        {
            bool reverseBytes = false;

            //var memstrem = new MemoryStream();
            //stream.CopyTo(memstrem);

            var streamLen = stream.Length;

            NiftiHeader hdr = new NiftiHeader();

            hdr.sizeof_hdr = ReadInt(stream, reverseBytes);

            reverseBytes = hdr.SourceIsBigEndian();

            hdr.data_type     = ReadBytes(stream, 10);
            hdr.db_name       = ReadBytes(stream, 18);
            hdr.extents       = ReadInt(stream, reverseBytes);
            hdr.session_error = ReadShort(stream, reverseBytes);
            hdr.regular       = ReadByte(stream);
            hdr.dim_info      = ReadByte(stream);

            hdr.dim            = ReadMyShorts(stream, 8, reverseBytes);
            hdr.intent_p1      = ReadFloat(stream, reverseBytes);
            hdr.intent_p2      = ReadFloat(stream, reverseBytes);
            hdr.intent_p3      = ReadFloat(stream, reverseBytes);
            hdr.intent_code    = ReadShort(stream, reverseBytes);
            hdr.datatype       = ReadShort(stream, reverseBytes);
            hdr.bitpix         = ReadShort(stream, reverseBytes);
            hdr.slice_start    = ReadShort(stream, reverseBytes);
            hdr.pixdim         = ReadFloats(stream, 8, reverseBytes);
            hdr.vox_offset     = ReadFloat(stream, reverseBytes);
            hdr.scl_slope      = ReadFloat(stream, reverseBytes);
            hdr.scl_inter      = ReadFloat(stream, reverseBytes);
            hdr.slice_end      = ReadShort(stream, reverseBytes);
            hdr.slice_code     = ReadByte(stream);
            hdr.xyzt_units     = ReadByte(stream);
            hdr.cal_max        = ReadFloat(stream, reverseBytes);
            hdr.cal_min        = ReadFloat(stream, reverseBytes);
            hdr.slice_duration = ReadFloat(stream, reverseBytes);
            hdr.toffset        = ReadFloat(stream, reverseBytes);
            hdr.glmax          = ReadInt(stream, reverseBytes);
            hdr.glmin          = ReadInt(stream, reverseBytes);

            hdr.descrip  = ReadBytes(stream, 80);
            hdr.aux_file = ReadBytes(stream, 24);

            hdr.qform_code = ReadShort(stream, reverseBytes);
            hdr.sform_code = ReadShort(stream, reverseBytes);

            hdr.quatern_b = ReadFloat(stream, reverseBytes);
            hdr.quatern_c = ReadFloat(stream, reverseBytes);
            hdr.quatern_d = ReadFloat(stream, reverseBytes);
            hdr.qoffset_x = ReadFloat(stream, reverseBytes);
            hdr.qoffset_y = ReadFloat(stream, reverseBytes);
            hdr.qoffset_z = ReadFloat(stream, reverseBytes);

            hdr.srow_x = ReadFloats(stream, 4, reverseBytes);
            hdr.srow_y = ReadFloats(stream, 4, reverseBytes);
            hdr.srow_z = ReadFloats(stream, 4, reverseBytes);

            hdr.intent_name = ReadBytes(stream, 16);
            hdr.magic       = ReadBytes(stream, 4);

            if (streamLen >= 352)
            {
                hdr.extension = ReadBytes(stream, 4);

                if (hdr.extension[0] == 1) // Extension is present
                {
                    hdr.esize = ReadInt(stream, reverseBytes);
                    hdr.ecode = ReadInt(stream, reverseBytes);
                    hdr.edata = ReadBytes(stream, hdr.esize - 8);
                }
            }

            if (TypeOf(hdr) == FileType.UNKNOWN)
            {
                throw new InvalidDataException("Not a NIfTI file (no magic bytes)");
            }
            if (hdr.dim[0] > 7)
            {
                throw new InvalidDataException("NIFTI header is using more than 7 dimensions. I don't really know how to handle that :\\");
            }
            else if (hdr.dim[0] < 0)
            {
                throw new InvalidDataException("Somethings broken with the dimensions...");
            }

            return(hdr);
        }
Ejemplo n.º 6
0
        private static dynamic ReadData(Stream stream, NiftiHeader hdr, short forceType)
        {
            var datatype     = forceType != NiftiHeader.DT_UNKNOWN ? forceType : hdr.datatype;
            var reverseBytes = hdr.SourceIsBigEndian();
            var bytelen      = stream.Length - stream.Position;
            var data         = InitData(datatype, bytelen);

            switch (datatype)
            {
            case NiftiHeader.DT_FLOAT32:
                for (int i = 0; i < data.Length; ++i)
                {
                    data[i] = ReadFloat(stream, reverseBytes);
                }
                break;

            case NiftiHeader.DT_INT32:
                for (int i = 0; i < data.Length; ++i)
                {
                    data[i] = ReadInt(stream, reverseBytes);
                }
                break;

            case NiftiHeader.DT_UINT32:
                for (int i = 0; i < data.Length; ++i)
                {
                    data[i] = ReadInt(stream, reverseBytes);
                }
                break;

            case NiftiHeader.DT_INT16:
                for (int i = 0; i < data.Length; ++i)
                {
                    data[i] = ReadShort(stream, reverseBytes);
                }
                break;

            case NiftiHeader.DT_UINT16:
                for (int i = 0; i < data.Length; ++i)
                {
                    data[i] = ReadUShort(stream, reverseBytes);
                }
                break;

            case NiftiHeader.DT_DOUBLE:
                for (int i = 0; i < data.Length; ++i)
                {
                    data[i] = ReadDouble(stream, reverseBytes);
                }
                break;

            case NiftiHeader.DT_COMPLEX:
                for (int i = 0; i < data.Length; ++i)
                {
                    data[i] = ReadLong(stream, reverseBytes);
                }
                break;

            case NiftiHeader.DT_RGB24:
                for (int i = 0; i < data.Length; ++i)
                {
                    data[i] = ReadRGB(stream, reverseBytes);
                }
                break;

            case NiftiHeader.DT_RGBA32:
                for (int i = 0; i < data.Length; ++i)
                {
                    data[i] = ReadRGBA(stream, reverseBytes);
                }
                break;

            default:
                for (int i = 0; i < data.Length; ++i)
                {
                    data[i] = ReadByte(stream);
                }
                break;
            }

            return(data);
        }