public static void WriteMapFloat(string path, MapHeader header, float[][] data) { Type ValueType = header.GetValueType(); using (BinaryWriter Writer = new BinaryWriter(CreateWithBigBuffer(path))) { header.Write(Writer); long Elements = header.Dimensions.ElementsSlice(); for (int z = 0; z < data.Length; z++) { byte[] Bytes = new byte[Elements * ImageFormatsHelper.SizeOf(ValueType)]; unsafe { fixed(float *DataPtr = data[z]) fixed(byte *BytesPtr = Bytes) { float *DataP = DataPtr; if (ValueType == typeof(byte)) { byte *BytesP = BytesPtr; for (long i = 0; i < Elements; i++) { *BytesP++ = (byte)*DataP++; } } else if (ValueType == typeof(short)) { short *BytesP = (short *)BytesPtr; for (long i = 0; i < Elements; i++) { *BytesP++ = (short)*DataP++; } } else if (ValueType == typeof(ushort)) { ushort *BytesP = (ushort *)BytesPtr; for (long i = 0; i < Elements; i++) { *BytesP++ = (ushort)Math.Min(Math.Max(0f, *DataP++ *16f), 65536f); } } else if (ValueType == typeof(int)) { int *BytesP = (int *)BytesPtr; for (long i = 0; i < Elements; i++) { *BytesP++ = (int)*DataP++; } } else if (ValueType == typeof(float)) { float *BytesP = (float *)BytesPtr; for (long i = 0; i < Elements; i++) { *BytesP++ = *DataP++; } } else if (ValueType == typeof(double)) { double *BytesP = (double *)BytesPtr; for (long i = 0; i < Elements; i++) { *BytesP++ = (double)*DataP++; } } } } Writer.Write(Bytes); } } }
public static float[][] ReadMapFloat(string path, int2 headerlessSliceDims, long headerlessOffset, Type headerlessType, int layer = -1) { MapHeader Header = null; Type ValueType = null; FileInfo Info = new FileInfo(path); float[][] Data; using (BinaryReader Reader = new BinaryReader(OpenWithBigBuffer(path))) { Header = MapHeader.ReadFromFile(Reader, Info, headerlessSliceDims, headerlessOffset, headerlessType); ValueType = Header.GetValueType(); Data = new float[layer < 0 ? Header.Dimensions.Z : 1][]; if (Header.GetType() != typeof(HeaderTiff)) { for (int z = 0; z < Data.Length; z++) { if (layer >= 0) { Reader.BaseStream.Seek((int)Header.Dimensions.ElementsSlice() * (int)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()]; 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 { Data = ((HeaderTiff)Header).ReadData(layer); } } return(Data); }
public static float[] ReadSmallMapFloat(string path, int2 headerlessSliceDims, long headerlessOffset, Type headerlessType) { FileInfo Info = new FileInfo(path); float[] Data; using (BinaryReader Reader = new BinaryReader(OpenWithBigBuffer(path))) { MapHeader Header = MapHeader.ReadFromFile(Reader, Info, headerlessSliceDims, headerlessOffset, headerlessType); Type ValueType = Header.GetValueType(); Data = new float[Header.Dimensions.Elements()]; byte[] Bytes = Reader.ReadBytes((int)Header.Dimensions.Elements() * (int)ImageFormatsHelper.SizeOf(ValueType)); unsafe { int Elements = (int)Header.Dimensions.Elements(); fixed(byte *BytesPtr = Bytes) fixed(float *DataPtr = Data) { 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++; } } } } } return(Data); }
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); }
public static float[][] ReadMapFloat(BinaryReader reader, 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; BinaryReader Reader = reader; { 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++; } } } } } } } return(Data); }
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); }
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); }