Beispiel #1
0
        /*private unsafe static void convolve(KernelThread thread, float[,] source, float[,] kernel, float[,] destination)
         * {
         *  int kernelWidth = kernel.Width();
         *  int kernelHeight = kernel.Height();
         *
         *  var sum = 0f;
         *  for (int r = 0; r < kernelHeight; r++)
         *  {
         *      for (int c = 0; c < kernelWidth; c++)
         *      {
         *          sum += source[thread.Y + r, thread.X + c] * kernel[r, c];
         *      }
         *  }
         *
         *  destination[thread.Y + kernelHeight / 2, thread.X + kernelWidth / 2] = sum;
         * }*/

        private unsafe static void convolve(KernelThread thread, float *source, int sourceStride, int channelCount,
                                            float *kernel, int kernelWidth, int kernelHeight,
                                            float *destination, int destinationStride)
        {
            source      = (float *)((byte *)source + sourceStride * thread.Y) + thread.X;
            destination = (float *)((byte *)destination + destinationStride * (thread.Y + kernelHeight / 2)) + (thread.X + kernelWidth / 2);

            for (int ch = 0; ch < channelCount; ch++)
            {
                var srcPtr  = source + ch;
                var krnlPtr = kernel;

                var sum = 0f;
                for (int r = 0; r < kernelHeight; r++)
                {
                    for (int c = 0, srcCol = 0; c < kernelWidth; c++, srcCol += channelCount)
                    {
                        sum += srcPtr[srcCol] * krnlPtr[c];
                    }

                    srcPtr   = (float *)((byte *)srcPtr + sourceStride);
                    krnlPtr += kernelWidth;
                }

                destination[ch] = sum;
            }
        }
        public static void Launch(Action <KernelThread> kernel, int gridX, int gridY)
        {
            System.Threading.Tasks.Parallel.For(0, gridY, (j) =>
            {
                KernelThread th = new KernelThread();

                th.Y = j;
                for (int i = 0; i < gridX; i++)
                {
                    th.X = i;
                    kernel(th);
                }
            });
        }