Beispiel #1
0
        public static float[][] ReadMapFloat(string path, int2 headerlessSliceDims, long headerlessOffset, Type headerlessType, bool isBigEndian, int layer = -1, Stream stream = null)
        {
            MapHeader Header    = null;
            Type      ValueType = null;

            float[][] Data;

            if (MapHeader.GetHeaderType(path) != typeof(HeaderTiff))
            {
                using (BinaryReader Reader = isBigEndian ? new BinaryReaderBE(OpenWithBigBuffer(path)) : new BinaryReader(OpenWithBigBuffer(path)))
                {
                    Header    = MapHeader.ReadFromFile(Reader, path, headerlessSliceDims, headerlessOffset, headerlessType);
                    ValueType = Header.GetValueType();
                    Data      = new float[layer < 0 ? Header.Dimensions.Z : 1][];

                    for (int z = 0; z < Data.Length; z++)
                    {
                        if (layer >= 0)
                        {
                            Reader.BaseStream.Seek((long)Header.Dimensions.ElementsSlice() * (long)ImageFormatsHelper.SizeOf(ValueType) * layer, SeekOrigin.Current);
                        }

                        byte[] Bytes = Reader.ReadBytes((int)Header.Dimensions.ElementsSlice() * (int)ImageFormatsHelper.SizeOf(ValueType));
                        Data[z] = new float[(int)Header.Dimensions.ElementsSlice()];

                        if (isBigEndian)
                        {
                            if (ValueType == typeof(short) || ValueType == typeof(ushort))
                            {
                                for (int i = 0; i < Bytes.Length / 2; i++)
                                {
                                    Array.Reverse(Bytes, i * 2, 2);
                                }
                            }
                            else if (ValueType == typeof(int) || ValueType == typeof(float))
                            {
                                for (int i = 0; i < Bytes.Length / 4; i++)
                                {
                                    Array.Reverse(Bytes, i * 4, 4);
                                }
                            }
                            else if (ValueType == typeof(double))
                            {
                                for (int i = 0; i < Bytes.Length / 8; i++)
                                {
                                    Array.Reverse(Bytes, i * 8, 8);
                                }
                            }
                        }

                        unsafe
                        {
                            int Elements = (int)Header.Dimensions.ElementsSlice();

                            fixed(byte *BytesPtr = Bytes)
                            fixed(float *DataPtr = Data[z])
                            {
                                float *DataP = DataPtr;

                                if (ValueType == typeof(byte))
                                {
                                    byte *BytesP = BytesPtr;
                                    for (int i = 0; i < Elements; i++)
                                    {
                                        *DataP++ = (float)*BytesP++;
                                    }
                                }
                                else if (ValueType == typeof(short))
                                {
                                    short *BytesP = (short *)BytesPtr;
                                    for (int i = 0; i < Elements; i++)
                                    {
                                        *DataP++ = (float)*BytesP++;
                                    }
                                }
                                else if (ValueType == typeof(ushort))
                                {
                                    ushort *BytesP = (ushort *)BytesPtr;
                                    for (int i = 0; i < Elements; i++)
                                    {
                                        *DataP++ = (float)*BytesP++;
                                    }
                                }
                                else if (ValueType == typeof(int))
                                {
                                    int *BytesP = (int *)BytesPtr;
                                    for (int i = 0; i < Elements; i++)
                                    {
                                        *DataP++ = (float)*BytesP++;
                                    }
                                }
                                else if (ValueType == typeof(float))
                                {
                                    float *BytesP = (float *)BytesPtr;
                                    for (int i = 0; i < Elements; i++)
                                    {
                                        *DataP++ = *BytesP++;
                                    }
                                }
                                else if (ValueType == typeof(double))
                                {
                                    double *BytesP = (double *)BytesPtr;
                                    for (int i = 0; i < Elements; i++)
                                    {
                                        *DataP++ = (float)*BytesP++;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                Header = MapHeader.ReadFromFile(null, path, headerlessSliceDims, headerlessOffset, headerlessType, stream);
                Data   = ((HeaderTiff)Header).ReadData(stream, layer);
            }

            return(Data);
        }
Beispiel #2
0
        public static float[][] ReadMapFloat(string path, int2 headerlessSliceDims, long headerlessOffset, Type headerlessType, bool isBigEndian, int layer = -1, Stream stream = null, float[][] reuseBuffer = null)
        {
            MapHeader Header    = null;
            Type      ValueType = null;

            float[][] Data;

            if (MapHeader.GetHeaderType(path) != typeof(HeaderTiff))
            {
                using (BinaryReader Reader = isBigEndian ? new BinaryReaderBE(OpenWithBigBuffer(path)) : new BinaryReader(OpenWithBigBuffer(path)))
                {
                    Header    = MapHeader.ReadFromFile(Reader, path, headerlessSliceDims, headerlessOffset, headerlessType);
                    ValueType = Header.GetValueType();
                    Data      = reuseBuffer == null ? new float[layer < 0 ? Header.Dimensions.Z : 1][] : reuseBuffer;

                    int    ReadBatchSize = Math.Min((int)Header.Dimensions.ElementsSlice(), 1 << 20);
                    int    ValueSize     = (int)ImageFormatsHelper.SizeOf(ValueType);
                    byte[] Bytes         = new byte[ReadBatchSize * ValueSize];

                    for (int z = 0; z < Data.Length; z++)
                    {
                        if (layer >= 0)
                        {
                            Reader.BaseStream.Seek(Header.Dimensions.ElementsSlice() * ImageFormatsHelper.SizeOf(ValueType) * layer, SeekOrigin.Current);
                        }

                        if (reuseBuffer == null)
                        {
                            Data[z] = new float[(int)Header.Dimensions.ElementsSlice()];
                        }


                        unsafe
                        {
                            fixed(byte *BytesPtr = Bytes)
                            fixed(float *DataPtr = Data[z])
                            {
                                for (int b = 0; b < (int)Header.Dimensions.ElementsSlice(); b += ReadBatchSize)
                                {
                                    int CurBatch = Math.Min(ReadBatchSize, (int)Header.Dimensions.ElementsSlice() - b);

                                    Reader.Read(Bytes, 0, CurBatch * ValueSize);

                                    if (isBigEndian)
                                    {
                                        if (ValueType == typeof(short) || ValueType == typeof(ushort))
                                        {
                                            for (int i = 0; i < CurBatch * ValueSize / 2; i++)
                                            {
                                                Array.Reverse(Bytes, i * 2, 2);
                                            }
                                        }
                                        else if (ValueType == typeof(int) || ValueType == typeof(float))
                                        {
                                            for (int i = 0; i < CurBatch * ValueSize / 4; i++)
                                            {
                                                Array.Reverse(Bytes, i * 4, 4);
                                            }
                                        }
                                        else if (ValueType == typeof(double))
                                        {
                                            for (int i = 0; i < CurBatch * ValueSize / 8; i++)
                                            {
                                                Array.Reverse(Bytes, i * 8, 8);
                                            }
                                        }
                                    }

                                    float *DataP = DataPtr + b;

                                    if (ValueType == typeof(byte))
                                    {
                                        byte *BytesP = BytesPtr;
                                        for (int i = 0; i < CurBatch; i++)
                                        {
                                            *DataP++ = (float)*BytesP++;
                                        }
                                    }
                                    else if (ValueType == typeof(short))
                                    {
                                        short *BytesP = (short *)BytesPtr;
                                        for (int i = 0; i < CurBatch; i++)
                                        {
                                            *DataP++ = (float)*BytesP++;
                                        }
                                    }
                                    else if (ValueType == typeof(ushort))
                                    {
                                        ushort *BytesP = (ushort *)BytesPtr;
                                        for (int i = 0; i < CurBatch; i++)
                                        {
                                            *DataP++ = (float)*BytesP++;
                                        }
                                    }
                                    else if (ValueType == typeof(int))
                                    {
                                        int *BytesP = (int *)BytesPtr;
                                        for (int i = 0; i < CurBatch; i++)
                                        {
                                            *DataP++ = (float)*BytesP++;
                                        }
                                    }
                                    else if (ValueType == typeof(float))
                                    {
                                        float *BytesP = (float *)BytesPtr;
                                        for (int i = 0; i < CurBatch; i++)
                                        {
                                            *DataP++ = *BytesP++;
                                        }
                                    }
                                    else if (ValueType == typeof(double))
                                    {
                                        double *BytesP = (double *)BytesPtr;
                                        for (int i = 0; i < CurBatch; i++)
                                        {
                                            *DataP++ = (float)*BytesP++;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                Header = MapHeader.ReadFromFile(null, path, headerlessSliceDims, headerlessOffset, headerlessType, stream);
                Data   = ((HeaderTiff)Header).ReadData(stream, layer);
            }

            return(Data);
        }
Beispiel #3
0
        public static float[][] ReadMapFloat(string path, int2 headerlessSliceDims, long headerlessOffset, Type headerlessType, bool isBigEndian, int[] layers = null, Stream stream = null, float[][] reuseBuffer = null)
        {
            MapHeader Header    = null;
            Type      ValueType = null;

            float[][] Data;

            if (MapHeader.GetHeaderType(path) != typeof(HeaderTiff))
            {
                using (BinaryReader Reader = isBigEndian ? new BinaryReaderBE(OpenWithBigBuffer(path)) : new BinaryReader(OpenWithBigBuffer(path)))
                {
                    Header    = MapHeader.ReadFromFile(Reader, path, headerlessSliceDims, headerlessOffset, headerlessType);
                    ValueType = Header.GetValueType();
                    Data      = reuseBuffer == null ? new float[layers == null ? Header.Dimensions.Z : layers.Length][] : reuseBuffer;

                    int    ReadBatchSize = Math.Min((int)Header.Dimensions.ElementsSlice(), 1 << 20);
                    int    ValueSize     = (int)ImageFormatsHelper.SizeOf(ValueType);
                    byte[] Bytes         = new byte[ReadBatchSize * ValueSize];

                    long ReaderDataStart = Reader.BaseStream.Position;

                    for (int z = 0; z < Data.Length; z++)
                    {
                        if (layers != null)
                        {
                            Reader.BaseStream.Seek(Header.Dimensions.ElementsSlice() * ImageFormatsHelper.SizeOf(ValueType) * layers[z] + ReaderDataStart, SeekOrigin.Begin);
                        }

                        if (reuseBuffer == null)
                        {
                            Data[z] = new float[(int)Header.Dimensions.ElementsSlice()];
                        }


                        unsafe
                        {
                            fixed(byte *BytesPtr = Bytes)
                            fixed(float *DataPtr = Data[z])
                            {
                                for (int b = 0; b < (int)Header.Dimensions.ElementsSlice(); b += ReadBatchSize)
                                {
                                    int CurBatch = Math.Min(ReadBatchSize, (int)Header.Dimensions.ElementsSlice() - b);

                                    Reader.Read(Bytes, 0, CurBatch * ValueSize);

                                    if (isBigEndian)
                                    {
                                        if (ValueType == typeof(short) || ValueType == typeof(ushort))
                                        {
                                            for (int i = 0; i < CurBatch * ValueSize / 2; i++)
                                            {
                                                Array.Reverse(Bytes, i * 2, 2);
                                            }
                                        }
                                        else if (ValueType == typeof(int) || ValueType == typeof(float))
                                        {
                                            for (int i = 0; i < CurBatch * ValueSize / 4; i++)
                                            {
                                                Array.Reverse(Bytes, i * 4, 4);
                                            }
                                        }
                                        else if (ValueType == typeof(double))
                                        {
                                            for (int i = 0; i < CurBatch * ValueSize / 8; i++)
                                            {
                                                Array.Reverse(Bytes, i * 8, 8);
                                            }
                                        }
                                    }

                                    float *DataP = DataPtr + b;

                                    if (ValueType == typeof(byte))
                                    {
                                        byte *BytesP = BytesPtr;
                                        for (int i = 0; i < CurBatch; i++)
                                        {
                                            *DataP++ = (float)*BytesP++;
                                        }
                                    }
                                    else if (ValueType == typeof(short))
                                    {
                                        short *BytesP = (short *)BytesPtr;
                                        for (int i = 0; i < CurBatch; i++)
                                        {
                                            *DataP++ = (float)*BytesP++;
                                        }
                                    }
                                    else if (ValueType == typeof(ushort))
                                    {
                                        ushort *BytesP = (ushort *)BytesPtr;
                                        for (int i = 0; i < CurBatch; i++)
                                        {
                                            *DataP++ = (float)*BytesP++;
                                        }
                                    }
                                    else if (ValueType == typeof(int))
                                    {
                                        int *BytesP = (int *)BytesPtr;
                                        for (int i = 0; i < CurBatch; i++)
                                        {
                                            *DataP++ = (float)*BytesP++;
                                        }
                                    }
                                    else if (ValueType == typeof(float))
                                    {
                                        float *BytesP = (float *)BytesPtr;
                                        for (int i = 0; i < CurBatch; i++)
                                        {
                                            *DataP++ = *BytesP++;
                                        }
                                    }
                                    else if (ValueType == typeof(double))
                                    {
                                        double *BytesP = (double *)BytesPtr;
                                        for (int i = 0; i < CurBatch; i++)
                                        {
                                            *DataP++ = (float)*BytesP++;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                Header = MapHeader.ReadFromFile(null, path, headerlessSliceDims, headerlessOffset, headerlessType, stream);
                if (Helper.PathToExtension(path).ToLower() == ".eer")
                {
                    Data = Helper.ArrayOfFunction(i => new float[Header.Dimensions.ElementsSlice()], layers == null ? Header.Dimensions.Z : layers.Length);
                    for (int i = 0; i < Data.Length; i++)
                    {
                        int z = layers == null ? i : layers[i];
                        EERNative.ReadEER(path, z * HeaderEER.GroupNFrames, (z + 1) * HeaderEER.GroupNFrames, HeaderEER.SuperResolution, Data[i]);
                    }
                }
                else
                {
                    Data = ((HeaderTiff)Header).ReadData(stream, layers);
                }
            }

            return(Data);
        }