Exemplo n.º 1
0
        public static MapHeader ReadFromFile(string path)
        {
            MapHeader Header = null;
            FileInfo  Info   = new FileInfo(path);

            using (BinaryReader Reader = new BinaryReader(File.OpenRead(path)))
            {
                Header = ReadFromFile(Reader, Info);
            }

            return(Header);
        }
Exemplo n.º 2
0
        public static MapHeader ReadFromFile(BinaryReader reader, FileInfo info)
        {
            MapHeader Header = null;

            if (info.Extension.ToLower() == ".mrc")
            {
                Header = new HeaderMRC(reader);
            }
            else if (info.Extension.ToLower() == ".em")
            {
                Header = new HeaderEM(reader);
            }
            else
            {
                throw new Exception("File type not supported.");
            }

            return(Header);
        }
Exemplo n.º 3
0
        public static void WriteMapFloat(string path, MapHeader header, float[] data)
        {
            Type  ValueType = header.GetValueType();
            ulong Elements  = header.Dimensions.Elements();

            using (BinaryWriter Writer = new BinaryWriter(File.Create(path)))
            {
                header.Write(Writer);
                byte[] Bytes = null;

                if (ValueType == typeof(byte))
                {
                    Bytes = new byte[Elements * sizeof(byte)];
                }
                else if (ValueType == typeof(short))
                {
                    Bytes = new byte[Elements * sizeof(short)];
                }
                else if (ValueType == typeof(ushort))
                {
                    Bytes = new byte[Elements * sizeof(ushort)];
                }
                else if (ValueType == typeof(int))
                {
                    Bytes = new byte[Elements * sizeof(int)];
                }
                else if (ValueType == typeof(float))
                {
                    Bytes = new byte[Elements * sizeof(float)];
                }
                else if (ValueType == typeof(double))
                {
                    Bytes = new byte[Elements * sizeof(double)];
                }

                unsafe
                {
                    fixed(float *DataPtr = data)
                    fixed(byte *BytesPtr = Bytes)
                    {
                        float *DataP = DataPtr;

                        if (ValueType == typeof(byte))
                        {
                            byte *BytesP = BytesPtr;
                            for (ulong i = 0; i < Elements; i++)
                            {
                                *BytesP++ = (byte)*DataP++;
                            }
                        }
                        else if (ValueType == typeof(short))
                        {
                            short *BytesP = (short *)BytesPtr;
                            for (ulong i = 0; i < Elements; i++)
                            {
                                *BytesP++ = (short)*DataP++;
                            }
                        }
                        else if (ValueType == typeof(ushort))
                        {
                            ushort *BytesP = (ushort *)BytesPtr;
                            for (ulong i = 0; i < Elements; i++)
                            {
                                *BytesP++ = (ushort)*DataP++;
                            }
                        }
                        else if (ValueType == typeof(int))
                        {
                            int *BytesP = (int *)BytesPtr;
                            for (ulong i = 0; i < Elements; i++)
                            {
                                *BytesP++ = (int)*DataP++;
                            }
                        }
                        else if (ValueType == typeof(float))
                        {
                            float *BytesP = (float *)BytesPtr;
                            for (ulong i = 0; i < Elements; i++)
                            {
                                *BytesP++ = *DataP++;
                            }
                        }
                        else if (ValueType == typeof(double))
                        {
                            double *BytesP = (double *)BytesPtr;
                            for (ulong i = 0; i < Elements; i++)
                            {
                                *BytesP++ = (double)*DataP++;
                            }
                        }
                    }
                }

                Writer.Write(Bytes);
            }
        }
Exemplo n.º 4
0
        public static float[] ReadMapFloat(string path)
        {
            MapHeader Header    = null;
            Type      ValueType = null;

            byte[]   Bytes = null;
            FileInfo Info  = new FileInfo(path);

            using (BinaryReader Reader = new BinaryReader(File.OpenRead(path)))
            {
                Header    = MapHeader.ReadFromFile(Reader, Info);
                ValueType = Header.GetValueType();

                if (ValueType == typeof(byte))
                {
                    Bytes = Reader.ReadBytes((int)Header.Dimensions.Elements() * sizeof(byte));
                }
                else if (ValueType == typeof(short))
                {
                    Bytes = Reader.ReadBytes((int)Header.Dimensions.Elements() * sizeof(short));
                }
                else if (ValueType == typeof(ushort))
                {
                    Bytes = Reader.ReadBytes((int)Header.Dimensions.Elements() * sizeof(ushort));
                }
                else if (ValueType == typeof(int))
                {
                    Bytes = Reader.ReadBytes((int)Header.Dimensions.Elements() * sizeof(int));
                }
                else if (ValueType == typeof(float))
                {
                    Bytes = Reader.ReadBytes((int)Header.Dimensions.Elements() * sizeof(float));
                }
                else if (ValueType == typeof(double))
                {
                    Bytes = Reader.ReadBytes((int)Header.Dimensions.Elements() * sizeof(double));
                }
            }

            float[] Data = new float[Header.Dimensions.Elements()];

            unsafe
            {
                fixed(byte *BytesPtr = Bytes)
                fixed(float *DataPtr = Data)
                {
                    float *DataP = DataPtr;

                    if (ValueType == typeof(byte))
                    {
                        byte *BytesP = BytesPtr;
                        for (int i = 0; i < Data.Length; i++)
                        {
                            *DataP++ = (float)*BytesP++;
                        }
                    }
                    else if (ValueType == typeof(short))
                    {
                        short *BytesP = (short *)BytesPtr;
                        for (int i = 0; i < Data.Length; i++)
                        {
                            *DataP++ = (float)*BytesP++;
                        }
                    }
                    else if (ValueType == typeof(ushort))
                    {
                        ushort *BytesP = (ushort *)BytesPtr;
                        for (int i = 0; i < Data.Length; i++)
                        {
                            *DataP++ = (float)*BytesP++;
                        }
                    }
                    else if (ValueType == typeof(int))
                    {
                        int *BytesP = (int *)BytesPtr;
                        for (int i = 0; i < Data.Length; i++)
                        {
                            *DataP++ = (float)*BytesP++;
                        }
                    }
                    else if (ValueType == typeof(float))
                    {
                        float *BytesP = (float *)BytesPtr;
                        for (int i = 0; i < Data.Length; i++)
                        {
                            *DataP++ = *BytesP++;
                        }
                    }
                    else if (ValueType == typeof(double))
                    {
                        double *BytesP = (double *)BytesPtr;
                        for (int i = 0; i < Data.Length; i++)
                        {
                            *DataP++ = (float)*BytesP++;
                        }
                    }
                }
            }

            return(Data);
        }