Exemplo n.º 1
0
        /// <summary>
        /// Read a Nifti file (or hdr/img pair).
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static Nifti Read(string path, short forceType = NiftiHeader.DT_UNKNOWN)
        {
            Nifti result = new Nifti();

            using (var stream = ReadStream(path))
            {
                var hdr = ReadHeader(stream);
                result.Header = hdr;
                if (FileType.NII == TypeOf(hdr))
                {
                    result.Data = ReadData(stream, hdr, forceType);
                }
            }

            if (FileType.HDR == TypeOf(result.Header))
            {
                var imgpath = path.ToLower().Replace(".hdr", ".img");
                if (File.Exists(imgpath))
                {
                    using (var stream = ReadStream(imgpath))
                    {
                        result.Data = ReadData(stream, result.Header, forceType);
                    }
                }
            }

            if (TypeOf(result.Header) == FileType.UNKNOWN)
            {
                throw new InvalidDataException("Not a NIfTI file (no magic bytes)");
            }

            return(result);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            string inPath;

            if (args.Length > 0)
            {
                inPath = args[0];
            }
            else
            {
                Console.WriteLine("Please give path to nii file");
                return;
            }

            Console.WriteLine($"Reading... From {inPath}");
            Nifti.NET.Nifti nifti = null;
            if (!inPath.EndsWith("ole"))
            {
                nifti = NiftiFile.Read(inPath);
            }
            Console.WriteLine($"Completed Nifti generation from {inPath}");

            var outPath = string.Empty;

            if (args.Length >= 2)
            {
                outPath = args[1];
            }

            var filetype = inPath.Split(".")
                           .Skip(1)                             // First part is not filetype
                           .Where(s => !int.TryParse(s, out _)) // ".01." is subversion and should be part of filename
                           .Aggregate((acc, s) => $"{acc}.{s}");
            var filename = inPath.Split("\\/".ToCharArray()).Last().Replace("." + filetype, "");

            var jsonPath = $"{outPath}{filename}.json";

            if (outPath.EndsWith("json", StringComparison.InvariantCultureIgnoreCase))
            {
                jsonPath = outPath;
            }


            Console.WriteLine($"Serializing... To JSON at {jsonPath}");
            if (!inPath.EndsWith("ole"))
            {
                JsonSerialize(nifti, jsonPath);
            }
            Console.WriteLine($"Done! JSON file at {jsonPath}");
        }
Exemplo n.º 3
0
        /// <summary>
        /// Write the Nifti file according to the path.
        /// If the Header.magic[1] byte is 0x69 then the file will be split as hdr and img (we will assume the .hdr file is the path).
        /// Otherwise the magic[1] should be 0x2B
        /// </summary>
        /// <param name="nifti"></param>
        /// <param name="path"></param>
        /// <param name="gzip"></param>
        public static void Write(Nifti nifti, string path, bool gzip = false)
        {
            if (typeof(Color[]) == nifti.Data.GetType())
            {
                System.Console.WriteLine("Detected Color data. Converting to 24bit RBG representation.");
                nifti = ConvertToRGB(nifti);
            }

            FileType type = TypeOf(nifti.Header);

            if (gzip && !path.EndsWith(".gz"))
            {
                path += ".gz";
            }
            using (var stream = WriteStream(path, gzip))
            {
                Write(stream, nifti.Header);

                // For a nifti file we can just keep on writing.
                if (FileType.NII == type)
                {
                    Write(stream, nifti.Data);
                }
                stream.Flush();
            }

            // If the file type is hdr/img we want to split the data.
            if (FileType.HDR == type && nifti.Data != null)
            {
                var newPath = path.Replace(".hdr", ".img");
                if (newPath.Equals(path))
                {
                    newPath += ".img";
                }
                using (var stream = WriteStream(newPath, gzip))
                {
                    Write(stream, nifti.Data);
                    stream.Flush();
                }
            }
        }
Exemplo n.º 4
0
        private static Nifti ConvertToRGB(Nifti nifti)
        {
            // Handle 32bit RGBA data
            if (nifti.Header.datatype == NiftiHeader.DT_RGBA32)
            {
                var bytedata = new byte[nifti.Data.Length * 4];
                for (int i = 0; i < bytedata.Length; i += 4)
                {
                    // Convert RGB to uint
                    bytedata[i]     = nifti.Data[i / 4].R;
                    bytedata[i + 1] = nifti.Data[i / 4].G;
                    bytedata[i + 2] = nifti.Data[i / 4].B;
                    bytedata[i + 3] = nifti.Data[i / 4].A;
                }

                nifti.Data = bytedata;
            }
            else
            {
                // Setup the header info in case someone converted from non-color input
                nifti.Header.dim[0]      = 5; // RGB and RGBA both have 5 dimensions
                nifti.Header.dim[4]      = 1;
                nifti.Header.dim[5]      = 1;
                nifti.Header.bitpix      = 24;
                nifti.Header.datatype    = NiftiHeader.DT_RGB;
                nifti.Header.intent_code = NiftiHeader.NIFTI_INTENT_RGB_VECTOR;

                var bytedata = new byte[nifti.Data.Length * 3];
                for (int i = 0; i < bytedata.Length; i += 3)
                {
                    // Convert RGB to uint
                    bytedata[i]     = nifti.Data[i / 3].R;
                    bytedata[i + 1] = nifti.Data[i / 3].G;
                    bytedata[i + 2] = nifti.Data[i / 3].B;
                }

                nifti.Data = bytedata;
            }

            return(nifti);
        }