InvertEffect

Feb 28, 2012 at 4:19 PM

Great library, I added a few more effects:

public class InvertEffect : IEffect
    {
        public string Name { get { return "Invert"; } }

        public InvertEffect()
        {

        }

        /// <summary>
        /// Processes a bitmap and returns a new processed WriteabelBitmap.
        /// </summary>
        /// <param name="input">The input bitmap.</param>
        /// <returns>The result of WriteabelBitmap processing.</returns>
        public WriteableBitmap Process(WriteableBitmap input)
        {
            // Prepare some variables
            var width = input.PixelWidth;
            var height = input.PixelHeight;
            return Process(input.Pixels, width, height).ToWriteableBitmap(width, height);
        }

        public int[] Process(int[] inputPixels, int width, int height)
        {
            var resultPixels = new int[inputPixels.Length];

            for (int i = 0; i < inputPixels.Length; i++ )
            {
                // Extract color components
                var c = inputPixels[i];
                var a = (byte)(c >> 24); // Leave alpha
                var r = byte.MaxValue - (byte)(c >> 16);
                var g = byte.MaxValue - (byte)(c >> 8);
                var b = byte.MaxValue - (byte)(c);

                resultPixels[i] = (a << 24) | (r << 16) | (g << 8) | b;
            }

            return resultPixels;
        }
    }