예제 #1
0
        static DefectModel LoadAndPrepareDefectMap(string path, bool flipX, bool flipY, bool transpose)
        {
            Image Defects = Image.FromFilePatient(50, 500,
                                                  path,
                                                  HeaderlessDims,
                                                  (int)HeaderlessOffset,
                                                  ImageFormatsHelper.StringToType(HeaderlessType));

            if (flipX)
            {
                Defects = Defects.AsFlippedX();
            }
            if (flipY)
            {
                Defects = Defects.AsFlippedY();
            }
            if (transpose)
            {
                Defects = Defects.AsTransposed();
            }

            DefectModel Model = new DefectModel(Defects, 4);

            Defects.Dispose();

            return(Model);
        }
예제 #2
0
        static Image LoadAndPrepareGainReference(string path, bool flipX, bool flipY, bool transpose)
        {
            Image Gain = Image.FromFilePatient(50, 500,
                                               path,
                                               HeaderlessDims,
                                               (int)HeaderlessOffset,
                                               ImageFormatsHelper.StringToType(HeaderlessType));

            float Mean = MathHelper.Mean(Gain.GetHost(Intent.Read)[0]);

            Gain.TransformValues(v => v == 0 ? 1 : v / Mean);

            if (flipX)
            {
                Gain = Gain.AsFlippedX();
            }
            if (flipY)
            {
                Gain = Gain.AsFlippedY();
            }
            if (transpose)
            {
                Gain = Gain.AsTransposed();
            }

            return(Gain);
        }
예제 #3
0
        static Image LoadAndPrepareGainReference(string path, bool flipX, bool flipY, bool transpose)
        {
            Image Gain = Image.FromFilePatient(50, 500,
                                               path,
                                               HeaderlessDims,
                                               (int)HeaderlessOffset,
                                               ImageFormatsHelper.StringToType(HeaderlessType));

            if (flipX)
            {
                Gain = Gain.AsFlippedX();
            }
            if (flipY)
            {
                Gain = Gain.AsFlippedY();
            }
            if (transpose)
            {
                Gain = Gain.AsTransposed();
            }

            return(Gain);
        }
예제 #4
0
        static Image LoadAndPrepareStack(string path, decimal scaleFactor, int maxThreads = 8)
        {
            Image stack = null;

            MapHeader header = MapHeader.ReadFromFilePatient(50, 500,
                                                             path,
                                                             HeaderlessDims,
                                                             (int)HeaderlessOffset,
                                                             ImageFormatsHelper.StringToType(HeaderlessType));

            string Extension = Helper.PathToExtension(path).ToLower();
            bool   IsTiff    = header.GetType() == typeof(HeaderTiff);
            bool   IsEER     = header.GetType() == typeof(HeaderEER);

            if (GainRef != null)
            {
                if (!IsEER)
                {
                    if (header.Dimensions.X != GainRef.Dims.X || header.Dimensions.Y != GainRef.Dims.Y)
                    {
                        throw new Exception("Gain reference dimensions do not match image.");
                    }
                }
            }

            int EERSupersample = 1;

            if (GainRef != null && IsEER)
            {
                if (header.Dimensions.X == GainRef.Dims.X)
                {
                    EERSupersample = 1;
                }
                else if (header.Dimensions.X * 2 == GainRef.Dims.X)
                {
                    EERSupersample = 2;
                }
                else if (header.Dimensions.X * 4 == GainRef.Dims.X)
                {
                    EERSupersample = 3;
                }
                else
                {
                    throw new Exception("Invalid supersampling factor requested for EER based on gain reference dimensions");
                }
            }

            HeaderEER.SuperResolution = EERSupersample;

            if (IsEER && GainRef != null)
            {
                header.Dimensions.X = GainRef.Dims.X;
                header.Dimensions.Y = GainRef.Dims.Y;
            }

            int NThreads   = (IsTiff || IsEER) ? 6 : 2;
            int GPUThreads = 2;

            int CurrentDevice = GPU.GetDevice();

            if (RawLayers == null || RawLayers.Length != NThreads || RawLayers[0].Length != header.Dimensions.ElementsSlice())
            {
                RawLayers = Helper.ArrayOfFunction(i => new float[header.Dimensions.ElementsSlice()], NThreads);
            }

            Image[] GPULayers  = Helper.ArrayOfFunction(i => new Image(IntPtr.Zero, header.Dimensions.Slice()), GPUThreads);
            Image[] GPULayers2 = Helper.ArrayOfFunction(i => new Image(IntPtr.Zero, header.Dimensions.Slice()), GPUThreads);

            if (scaleFactor == 1M)
            {
                stack = new Image(header.Dimensions);
                float[][] OriginalStackData = stack.GetHost(Intent.Write);

                object[] Locks = Helper.ArrayOfFunction(i => new object(), GPUThreads);

                Helper.ForCPU(0, header.Dimensions.Z, NThreads, threadID => GPU.SetDevice(DeviceID), (z, threadID) =>
                {
                    if (IsTiff)
                    {
                        TiffNative.ReadTIFFPatient(50, 500, path, z, true, RawLayers[threadID]);
                    }
                    else if (IsEER)
                    {
                        EERNative.ReadEERPatient(50, 500, path, z * 10, (z + 1) * 10, EERSupersample, RawLayers[threadID]);
                    }
                    else
                    {
                        IOHelper.ReadMapFloatPatient(50, 500,
                                                     path,
                                                     HeaderlessDims,
                                                     (int)HeaderlessOffset,
                                                     ImageFormatsHelper.StringToType(HeaderlessType),
                                                     new[] { z },
                                                     null,
                                                     new[] { RawLayers[threadID] });
                    }

                    int GPUThreadID = threadID % GPUThreads;

                    lock (Locks[GPUThreadID])
                    {
                        GPU.CopyHostToDevice(RawLayers[threadID], GPULayers[GPUThreadID].GetDevice(Intent.Write), RawLayers[threadID].Length);

                        if (GainRef != null)
                        {
                            if (IsEER)
                            {
                                GPULayers[GPUThreadID].DivideSlices(GainRef);
                            }
                            else
                            {
                                GPULayers[GPUThreadID].MultiplySlices(GainRef);
                            }
                        }

                        if (DefectMap != null)
                        {
                            GPU.CopyDeviceToDevice(GPULayers[GPUThreadID].GetDevice(Intent.Read),
                                                   GPULayers2[GPUThreadID].GetDevice(Intent.Write),
                                                   header.Dimensions.Elements());
                            DefectMap.Correct(GPULayers2[GPUThreadID], GPULayers[GPUThreadID]);
                        }

                        GPU.Xray(GPULayers[GPUThreadID].GetDevice(Intent.Read),
                                 GPULayers2[GPUThreadID].GetDevice(Intent.Write),
                                 20f,
                                 new int2(header.Dimensions),
                                 1);

                        GPU.CopyDeviceToHost(GPULayers2[GPUThreadID].GetDevice(Intent.Read),
                                             OriginalStackData[z],
                                             header.Dimensions.ElementsSlice());
                    }
                }, null);
            }
            else
            {
                int3 ScaledDims = new int3((int)Math.Round(header.Dimensions.X * scaleFactor) / 2 * 2,
                                           (int)Math.Round(header.Dimensions.Y * scaleFactor) / 2 * 2,
                                           header.Dimensions.Z);

                stack = new Image(ScaledDims);
                float[][] OriginalStackData = stack.GetHost(Intent.Write);

                int[] PlanForw = Helper.ArrayOfFunction(i => GPU.CreateFFTPlan(header.Dimensions.Slice(), 1), GPUThreads);
                int[] PlanBack = Helper.ArrayOfFunction(i => GPU.CreateIFFTPlan(ScaledDims.Slice(), 1), GPUThreads);

                Image[] GPULayersInputFT  = Helper.ArrayOfFunction(i => new Image(IntPtr.Zero, header.Dimensions.Slice(), true, true), GPUThreads);
                Image[] GPULayersOutputFT = Helper.ArrayOfFunction(i => new Image(IntPtr.Zero, ScaledDims.Slice(), true, true), GPUThreads);

                Image[] GPULayersScaled = Helper.ArrayOfFunction(i => new Image(IntPtr.Zero, ScaledDims.Slice()), GPUThreads);

                object[] Locks = Helper.ArrayOfFunction(i => new object(), GPUThreads);

                Helper.ForCPU(0, ScaledDims.Z, NThreads, threadID => GPU.SetDevice(DeviceID), (z, threadID) =>
                {
                    if (IsTiff)
                    {
                        TiffNative.ReadTIFFPatient(50, 500, path, z, true, RawLayers[threadID]);
                    }
                    else if (IsEER)
                    {
                        EERNative.ReadEERPatient(50, 500, path, z * 10, (z + 1) * 10, EERSupersample, RawLayers[threadID]);
                    }
                    else
                    {
                        IOHelper.ReadMapFloatPatient(50, 500,
                                                     path,
                                                     HeaderlessDims,
                                                     (int)HeaderlessOffset,
                                                     ImageFormatsHelper.StringToType(HeaderlessType),
                                                     new[] { z },
                                                     null,
                                                     new[] { RawLayers[threadID] });
                    }

                    int GPUThreadID = threadID % GPUThreads;

                    lock (Locks[GPUThreadID])
                    {
                        GPU.CopyHostToDevice(RawLayers[threadID], GPULayers[GPUThreadID].GetDevice(Intent.Write), RawLayers[threadID].Length);

                        if (GainRef != null)
                        {
                            if (IsEER)
                            {
                                GPULayers[GPUThreadID].DivideSlices(GainRef);
                            }
                            else
                            {
                                GPULayers[GPUThreadID].MultiplySlices(GainRef);
                            }
                        }

                        if (DefectMap != null)
                        {
                            GPU.CopyDeviceToDevice(GPULayers[GPUThreadID].GetDevice(Intent.Read),
                                                   GPULayers2[GPUThreadID].GetDevice(Intent.Write),
                                                   header.Dimensions.Elements());
                            DefectMap.Correct(GPULayers2[GPUThreadID], GPULayers[GPUThreadID]);
                        }

                        GPU.Xray(GPULayers[GPUThreadID].GetDevice(Intent.Read),
                                 GPULayers2[GPUThreadID].GetDevice(Intent.Write),
                                 20f,
                                 new int2(header.Dimensions),
                                 1);

                        GPU.Scale(GPULayers2[GPUThreadID].GetDevice(Intent.Read),
                                  GPULayersScaled[GPUThreadID].GetDevice(Intent.Write),
                                  header.Dimensions.Slice(),
                                  ScaledDims.Slice(),
                                  1,
                                  PlanForw[GPUThreadID],
                                  PlanBack[GPUThreadID],
                                  GPULayersInputFT[GPUThreadID].GetDevice(Intent.Write),
                                  GPULayersOutputFT[GPUThreadID].GetDevice(Intent.Write));

                        GPU.CopyDeviceToHost(GPULayersScaled[GPUThreadID].GetDevice(Intent.Read),
                                             OriginalStackData[z],
                                             ScaledDims.ElementsSlice());
                    }
                }, null);

                for (int i = 0; i < GPUThreads; i++)
                {
                    GPU.DestroyFFTPlan(PlanForw[i]);
                    GPU.DestroyFFTPlan(PlanBack[i]);
                    GPULayersInputFT[i].Dispose();
                    GPULayersOutputFT[i].Dispose();
                    GPULayersScaled[i].Dispose();
                }
            }

            foreach (var layer in GPULayers)
            {
                layer.Dispose();
            }
            foreach (var layer in GPULayers2)
            {
                layer.Dispose();
            }

            return(stack);
        }
예제 #5
0
        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);
        }
예제 #6
0
        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);
                }
            }
        }
예제 #7
0
        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);
        }
예제 #8
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);
        }
예제 #9
0
        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);
        }
예제 #10
0
        private async void ButtonWrite_OnClick(object sender, RoutedEventArgs e)
        {
            string Suffix            = TextSuffix.Text;
            float  ScaleFactorCoords = (float)(Options.Tasks.InputPixelSize / Options.BinnedPixelSizeMean);
            float  ScaleFactorShifts = (float)(Options.Tasks.InputShiftPixelSize / Options.BinnedPixelSizeMean);

            ProgressWrite.Visibility = Visibility.Visible;
            PanelButtons.Visibility  = Visibility.Hidden;

            await Task.Run(async() =>
            {
                Star TableIn = new Star(ImportPath);

                #region Make sure everything is there

                if (TableIn.RowCount == 0 || !TableIn.HasColumn("rlnCoordinateX") || !TableIn.HasColumn("rlnCoordinateY") || !TableIn.HasColumn("rlnMicrographName"))
                {
                    await((MainWindow)Application.Current.MainWindow).ShowMessageAsync("Oopsie",
                                                                                       "Please make sure the table contains data with rlnCoordinateX, rlnCoordinateY and rlnMicrographName columns.");
                    Close?.Invoke();
                    return;
                }

                if (Movies.Length == 0)
                {
                    Close?.Invoke();
                    return;
                }

                #endregion

                Directory.CreateDirectory(Movies[0].MatchingDir);

                Dispatcher.Invoke(() => ProgressWrite.Maximum = Movies.Length);

                int IndexMicName = TableIn.GetColumnID("rlnMicrographName");
                TableIn.ModifyAllValuesInColumn("rlnMicrographName", v => Helper.PathToName(v));

                int IndexCoordX = TableIn.GetColumnID("rlnCoordinateX");
                int IndexCoordY = TableIn.GetColumnID("rlnCoordinateY");

                #region Subtract origin from coordinates

                int IndexOriginX = TableIn.GetColumnID("rlnOriginX");
                if (IndexOriginX >= 0)
                {
                    TableIn.ModifyAllValuesInColumn("rlnCoordinateX", (v, r) => (float.Parse(v) * ScaleFactorCoords - float.Parse(TableIn.GetRowValue(r, IndexOriginX)) * ScaleFactorShifts).ToString("F4"));
                    TableIn.SetColumn("rlnOriginX", Helper.ArrayOfConstant("0.0", TableIn.RowCount));
                }
                else
                {
                    TableIn.ModifyAllValuesInColumn("rlnCoordinateX", v => (float.Parse(v) * ScaleFactorCoords).ToString("F4"));
                }
                int IndexOriginY = TableIn.GetColumnID("rlnOriginY");
                if (IndexOriginY >= 0)
                {
                    TableIn.ModifyAllValuesInColumn("rlnCoordinateY", (v, r) => (float.Parse(v) * ScaleFactorCoords - float.Parse(TableIn.GetRowValue(r, IndexOriginY)) * ScaleFactorShifts).ToString("F4"));
                    TableIn.SetColumn("rlnOriginY", Helper.ArrayOfConstant("0.0", TableIn.RowCount));
                }
                else
                {
                    TableIn.ModifyAllValuesInColumn("rlnCoordinateY", v => (float.Parse(v) * ScaleFactorCoords).ToString("F4"));
                }

                #endregion

                var GroupedRows = TableIn.GetAllRows().GroupBy(row => row[IndexMicName]).ToDictionary(g => g.Key, g => g.ToList());

                foreach (var movie in Movies)
                {
                    if (GroupedRows.ContainsKey(movie.RootName))
                    {
                        int3 Dims = MapHeader.ReadFromFile(movie.Path,
                                                           new int2(Options.Import.HeaderlessWidth, Options.Import.HeaderlessHeight),
                                                           Options.Import.HeaderlessOffset,
                                                           ImageFormatsHelper.StringToType(Options.Import.HeaderlessType)).Dimensions;
                        float3 BinnedDims = new float3(Dims) * (float)Options.PixelSizeMean / (float)Options.BinnedPixelSizeMean;

                        List <List <string> > Rows = GroupedRows[movie.RootName];
                        foreach (var row in Rows)
                        {
                            row[IndexMicName] = movie.Name;

                            float CoordX = float.Parse(row[IndexCoordX]);
                            if (Options.Tasks.InputFlipX)
                            {
                                CoordX = BinnedDims.X - CoordX;
                            }

                            float CoordY = float.Parse(row[IndexCoordY]);
                            if (Options.Tasks.InputFlipY)
                            {
                                CoordY = BinnedDims.Y - CoordY;
                            }

                            row[IndexCoordX] = CoordX.ToString("F4");
                            row[IndexCoordY] = CoordY.ToString("F4");
                        }

                        Star TableOut = new Star(TableIn.GetColumnNames());
                        foreach (var row in Rows)
                        {
                            TableOut.AddRow(row);
                        }

                        TableOut.Save(movie.MatchingDir + movie.RootName + "_" + Suffix + ".star");

                        movie.UpdateParticleCount("_" + Suffix);
                    }

                    Dispatcher.Invoke(() => ProgressWrite.Value++);
                }
            });

            Close?.Invoke();
        }
예제 #11
0
파일: IOHelper.cs 프로젝트: smart111/warp
        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);
        }
예제 #12
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);
        }
예제 #13
0
        static Image LoadAndPrepareStack(string path, Image imageGain, decimal scaleFactor, int maxThreads = 8)
        {
            Image stack = null;

            MapHeader header = MapHeader.ReadFromFilePatient(50, 500,
                                                             path,
                                                             HeaderlessDims,
                                                             (int)HeaderlessOffset,
                                                             ImageFormatsHelper.StringToType(HeaderlessType));

            if (imageGain != null)
            {
                if (header.Dimensions.X != imageGain.Dims.X || header.Dimensions.Y != imageGain.Dims.Y)
                {
                    throw new Exception("Gain reference dimensions do not match image.");
                }
            }

            bool IsTiff = header.GetType() == typeof(HeaderTiff);

            int NThreads   = IsTiff ? 6 : 2;
            int GPUThreads = 2;

            int CurrentDevice = GPU.GetDevice();

            if (RawLayers == null || RawLayers.Length != NThreads || RawLayers[0].Length != header.Dimensions.ElementsSlice())
            {
                RawLayers = Helper.ArrayOfFunction(i => new float[header.Dimensions.ElementsSlice()], NThreads);
            }

            Image[] GPULayers  = Helper.ArrayOfFunction(i => new Image(IntPtr.Zero, header.Dimensions.Slice()), GPUThreads);
            Image[] GPULayers2 = Helper.ArrayOfFunction(i => new Image(IntPtr.Zero, header.Dimensions.Slice()), GPUThreads);

            if (scaleFactor == 1M)
            {
                stack = new Image(header.Dimensions);
                float[][] OriginalStackData = stack.GetHost(Intent.Write);

                object[] Locks = Helper.ArrayOfFunction(i => new object(), GPUThreads);

                Helper.ForCPU(0, header.Dimensions.Z, NThreads, threadID => GPU.SetDevice(DeviceID), (z, threadID) =>
                {
                    if (IsTiff)
                    {
                        TiffNative.ReadTIFFPatient(50, 500, path, z, true, RawLayers[threadID]);
                    }
                    else
                    {
                        IOHelper.ReadMapFloatPatient(50, 500,
                                                     path,
                                                     HeaderlessDims,
                                                     (int)HeaderlessOffset,
                                                     ImageFormatsHelper.StringToType(HeaderlessType),
                                                     z,
                                                     null,
                                                     new[] { RawLayers[threadID] });
                    }

                    int GPUThreadID = threadID % GPUThreads;

                    lock (Locks[GPUThreadID])
                    {
                        GPU.CopyHostToDevice(RawLayers[threadID], GPULayers[GPUThreadID].GetDevice(Intent.Write), RawLayers[threadID].Length);

                        if (imageGain != null)
                        {
                            GPULayers[GPUThreadID].MultiplySlices(imageGain);
                        }

                        GPU.Xray(GPULayers[GPUThreadID].GetDevice(Intent.Read),
                                 GPULayers2[GPUThreadID].GetDevice(Intent.Write),
                                 20f,
                                 new int2(header.Dimensions),
                                 1);

                        GPU.CopyDeviceToHost(GPULayers2[GPUThreadID].GetDevice(Intent.Read),
                                             OriginalStackData[z],
                                             header.Dimensions.ElementsSlice());
                    }
                }, null);
            }
            else
            {
                int3 ScaledDims = new int3((int)Math.Round(header.Dimensions.X * scaleFactor) / 2 * 2,
                                           (int)Math.Round(header.Dimensions.Y * scaleFactor) / 2 * 2,
                                           header.Dimensions.Z);

                stack = new Image(ScaledDims);
                float[][] OriginalStackData = stack.GetHost(Intent.Write);

                int[] PlanForw = Helper.ArrayOfFunction(i => GPU.CreateFFTPlan(header.Dimensions.Slice(), 1), GPUThreads);
                int[] PlanBack = Helper.ArrayOfFunction(i => GPU.CreateIFFTPlan(ScaledDims.Slice(), 1), GPUThreads);

                Image[] GPULayersInputFT  = Helper.ArrayOfFunction(i => new Image(IntPtr.Zero, header.Dimensions.Slice(), true, true), GPUThreads);
                Image[] GPULayersOutputFT = Helper.ArrayOfFunction(i => new Image(IntPtr.Zero, ScaledDims.Slice(), true, true), GPUThreads);

                Image[] GPULayersScaled = Helper.ArrayOfFunction(i => new Image(IntPtr.Zero, ScaledDims.Slice()), GPUThreads);

                object[] Locks = Helper.ArrayOfFunction(i => new object(), GPUThreads);

                Helper.ForCPU(0, ScaledDims.Z, NThreads, threadID => GPU.SetDevice(DeviceID), (z, threadID) =>
                {
                    if (IsTiff)
                    {
                        TiffNative.ReadTIFFPatient(50, 500, path, z, true, RawLayers[threadID]);
                    }
                    else
                    {
                        IOHelper.ReadMapFloatPatient(50, 500,
                                                     path,
                                                     HeaderlessDims,
                                                     (int)HeaderlessOffset,
                                                     ImageFormatsHelper.StringToType(HeaderlessType),
                                                     z,
                                                     null,
                                                     new[] { RawLayers[threadID] });
                    }

                    int GPUThreadID = threadID % GPUThreads;

                    lock (Locks[GPUThreadID])
                    {
                        GPU.CopyHostToDevice(RawLayers[threadID], GPULayers[GPUThreadID].GetDevice(Intent.Write), RawLayers[threadID].Length);

                        if (imageGain != null)
                        {
                            GPULayers[GPUThreadID].MultiplySlices(imageGain);
                        }

                        GPU.Xray(GPULayers[GPUThreadID].GetDevice(Intent.Read),
                                 GPULayers2[GPUThreadID].GetDevice(Intent.Write),
                                 20f,
                                 new int2(header.Dimensions),
                                 1);

                        GPU.Scale(GPULayers2[GPUThreadID].GetDevice(Intent.Read),
                                  GPULayersScaled[GPUThreadID].GetDevice(Intent.Write),
                                  header.Dimensions.Slice(),
                                  ScaledDims.Slice(),
                                  1,
                                  PlanForw[GPUThreadID],
                                  PlanBack[GPUThreadID],
                                  GPULayersInputFT[GPUThreadID].GetDevice(Intent.Write),
                                  GPULayersOutputFT[GPUThreadID].GetDevice(Intent.Write));

                        GPU.CopyDeviceToHost(GPULayersScaled[GPUThreadID].GetDevice(Intent.Read),
                                             OriginalStackData[z],
                                             ScaledDims.ElementsSlice());
                    }
                }, null);

                for (int i = 0; i < GPUThreads; i++)
                {
                    GPU.DestroyFFTPlan(PlanForw[i]);
                    GPU.DestroyFFTPlan(PlanBack[i]);
                    GPULayersInputFT[i].Dispose();
                    GPULayersOutputFT[i].Dispose();
                    GPULayersScaled[i].Dispose();
                }
            }

            foreach (var layer in GPULayers)
            {
                layer.Dispose();
            }
            foreach (var layer in GPULayers2)
            {
                layer.Dispose();
            }

            return(stack);
        }