Exemplo n.º 1
0
        private void AdditiveMultiplicativeFilter(Bitmap source, Bitmap destination, double[] matrix)
        {
            var initData   = source.LockBits(new Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadOnly, source.PixelFormat);
            var destData   = destination.LockBits(new Rectangle(0, 0, destination.Width, destination.Height), ImageLockMode.WriteOnly, source.PixelFormat);
            int byteLength = 4;

            if (source.PixelFormat.ToString().Contains("24"))
            {
                byteLength = 3;
            }


            var pixelCount = 3;

            unsafe
            {
                byte *sourcePtr = (byte *)initData.Scan0.ToPointer();
                byte *destPtr   = (byte *)destData.Scan0.ToPointer();

                int pixelOffset = pixelCount / 2;
                int byteOffset  = pixelOffset * byteLength;
                for (int i = byteOffset; i < (initData.Stride - byteOffset); i += byteLength)
                {
                    for (int j = pixelOffset; j < source.Height - pixelOffset; j++)
                    {
                        byte[][] window = PixelOperations.GetWindowOfPixels(sourcePtr, initData.Stride, i, j, byteLength, pixelCount);
                        byte[]   pixel  = GetAdditiveMultiplicatedPixel(Transform2Dto1DArray(window, byteLength * pixelCount * pixelCount), byteLength, matrix);
                        PixelOperations.SetPixelUnsafe(destPtr, pixel, initData.Stride, i, j, byteLength);
                    }
                }
            }

            source.UnlockBits(initData);
            destination.UnlockBits(destData);
        }