Пример #1
0
        private static void Rotate90(ImageBase source, ImageBase target)
        {
            Contract.Requires(source != null);
            Contract.Requires(source.IsFilled);
            Contract.Requires(target != null);
            Contract.Ensures(target.IsFilled);

            int oldIndex = 0, newIndex = 0;

            byte[] sourcePixels = source.Pixels;
            byte[] targetPixels = new byte[source.PixelWidth * source.PixelHeight * 4];

            for (int y = 0; y < source.PixelHeight; y++)
            {
                for (int x = 0; x < source.PixelWidth; x++)
                {
                    oldIndex = (y * source.PixelWidth + x) * 4;

                    // The new index will just be calculated by swapping
                    // the x and the y value for the pixel.
                    newIndex = ((x + 1) * source.PixelHeight - y - 1) * 4;

                    targetPixels[newIndex + 0] = sourcePixels[oldIndex + 0];
                    targetPixels[newIndex + 1] = sourcePixels[oldIndex + 1];
                    targetPixels[newIndex + 2] = sourcePixels[oldIndex + 2];
                    targetPixels[newIndex + 3] = sourcePixels[oldIndex + 3];
                }
            }
            target.SetPixels(source.PixelHeight, source.PixelWidth, targetPixels);
        }
Пример #2
0
        private static void Rotate270(ImageBase source, ImageBase target)
        {
            Contract.Requires(source != null);
            Contract.Requires(source.IsFilled);
            Contract.Requires(target != null);
            Contract.Ensures(target.IsFilled);

            int oldIndex = 0, newIndex = 0;

            byte[] sourcePixels = source.Pixels;
            byte[] targetPixels = new byte[source.PixelWidth * source.PixelHeight * 4];

            for (int y = 0; y < source.PixelHeight; y++)
            {
                for (int x = 0; x < source.PixelWidth; x++)
                {
                    oldIndex = (y * source.PixelWidth + x) * 4;

                    // The new index will be calculated as if the image would be flipped
                    // at the x and the y axis and rotated about 90 degrees.
                    newIndex = ((source.PixelWidth - x - 1) * source.PixelHeight + y) * 4;

                    targetPixels[newIndex + 0] = sourcePixels[oldIndex + 0];
                    targetPixels[newIndex + 1] = sourcePixels[oldIndex + 1];
                    targetPixels[newIndex + 2] = sourcePixels[oldIndex + 2];
                    targetPixels[newIndex + 3] = sourcePixels[oldIndex + 3];
                }
            }
            target.SetPixels(source.PixelHeight, source.PixelWidth, targetPixels);
        }
Пример #3
0
        internal static void Crop(ImageBase source, ImageBase target, Rectangle bounds)
        {
            Contract.Requires <ArgumentNullException>(source != null, "Source image cannot be null.");
            Contract.Requires <ArgumentException>(source.IsFilled, "Source image has not been loaded.");
            Contract.Requires <ArgumentNullException>(target != null, "Target image cannot be null.");

            Guard.GreaterThan(bounds.Width, 0, "bounds",
                              "Width of the rectangle must be greater than zero.");

            Guard.GreaterThan(bounds.Height, 0, "bounds",
                              "Height of the rectangle must be greater than zero.");

            if (bounds.Right > source.PixelWidth || bounds.Bottom > source.PixelHeight)
            {
                throw new ArgumentException(
                          "Rectangle must be in the range of the image's dimension.", "bounds");
            }

            byte[] sourcePixels = source.Pixels;
            byte[] targetPixels = new byte[bounds.Width * bounds.Height * 4];

            for (int y = bounds.Top, i = 0; y < bounds.Bottom; y++, i++)
            {
                Array.Copy(sourcePixels, (y * source.PixelWidth + bounds.Left) * 4, targetPixels, i * bounds.Width * 4, bounds.Width * 4);
            }

            target.SetPixels(bounds.Width, bounds.Height, targetPixels);
        }
Пример #4
0
        /// <summary>
        /// Transforms the specified image by flipping and rotating it. First the image
        /// will be rotated, then the image will be flipped. A new image will be returned. The original image
        /// will not be changed.
        /// </summary>
        /// <param name="source">The image, which should be transformed.</param>
        /// <param name="target">The new transformed image.</param>
        /// <param name="rotationType">Type of the rotation.</param>
        /// <param name="flippingType">Type of the flipping.</param>
        /// <exception cref="ArgumentNullException">
        ///     <para><paramref name="source"/> is null (Nothing in Visual Basic).</para>
        ///     <para>- or -</para>
        ///     <para><paramref name="target"/> is null (Nothing in Visual Basic).</para>
        /// </exception>
        internal static void Transform(ImageBase source, ImageBase target, RotationType rotationType, FlippingType flippingType)
        {
            Contract.Requires <ArgumentNullException>(source != null, "Source image cannot be null.");
            Contract.Requires <ArgumentException>(source.IsFilled, "Source image has not been loaded.");
            Contract.Requires <ArgumentNullException>(target != null, "Target image cannot be null.");

            switch (rotationType)
            {
            case RotationType.None:
            {
                byte[] targetPixels = source.Pixels;
                byte[] sourcePixels = new byte[targetPixels.Length];

                Array.Copy(targetPixels, sourcePixels, targetPixels.Length);

                target.SetPixels(source.PixelWidth, source.PixelHeight, sourcePixels);
            }
            break;

            case RotationType.Rotate90:
            {
                Rotate90(source, target);
            }
            break;

            case RotationType.Rotate180:
            {
                Rotate180(source, target);
            }
            break;

            case RotationType.Rotate270:
            {
                Rotate270(source, target);
            }
            break;

            default:
                throw new InvalidOperationException();
            }

            switch (flippingType)
            {
            case FlippingType.FlipX:
                FlipX(target);
                break;

            case FlippingType.FlipY:
                FlipY(target);
                break;
            }
        }
        /// <summary>
        /// Transforms the specified image by flipping and rotating it. First the image
        /// will be rotated, then the image will be flipped. A new image will be returned. The original image
        /// will not be changed.
        /// </summary>
        /// <param name="source">The image, which should be transformed.</param>
        /// <param name="target">The new transformed image.</param>
        /// <param name="rotationType">Type of the rotation.</param>
        /// <param name="flippingType">Type of the flipping.</param>
        /// <exception cref="ArgumentNullException">
        /// 	<para><paramref name="source"/> is null (Nothing in Visual Basic).</para>
        /// 	<para>- or -</para>
        /// 	<para><paramref name="target"/> is null (Nothing in Visual Basic).</para>
        /// </exception>
        internal static void Transform(ImageBase source, ImageBase target, RotationType rotationType, FlippingType flippingType)
        {
            Contract.Requires<ArgumentNullException>(source != null, "Source image cannot be null.");
            Contract.Requires<ArgumentException>(source.IsFilled, "Source image has not been loaded.");
            Contract.Requires<ArgumentNullException>(target != null, "Target image cannot be null.");

            switch (rotationType)
            {
                case RotationType.None:
                    {
                        byte[] targetPixels = source.Pixels;
                        byte[] sourcePixels = new byte[targetPixels.Length];

                        Array.Copy(targetPixels, sourcePixels, targetPixels.Length);

                        target.SetPixels(source.PixelWidth, source.PixelHeight, sourcePixels);
                    }
                    break;
                case RotationType.Rotate90:
                    {
                        Rotate90(source, target);
                    }
                    break;
                case RotationType.Rotate180:
                    {
                        Rotate180(source, target);
                    }
                    break;
                case RotationType.Rotate270:
                    {
                        Rotate270(source, target);
                    }
                    break;
                default:
                    throw new InvalidOperationException();
            }

            switch (flippingType)
            {
                case FlippingType.FlipX:
                    FlipX(target);
                    break;
                case FlippingType.FlipY:
                    FlipY(target);
                    break;
            }
        }
Пример #6
0
        public void Resize(ImageBase source, ImageBase target, int width, int height)
        {
            byte[] newPixels = new byte[width * height * 4];

            byte[] sourcePixels = source.Pixels;

            var GetColor = new Func<double, double, int, byte>((x, y, offset) => sourcePixels[(int)((y * source.PixelWidth + x) * 4 + offset)]);

            double factorX = (double)source.PixelWidth  / width;
            double factorY = (double)source.PixelHeight / height;

            double fractionX, oneMinusX, l, r;
            double fractionY, oneMinusY, t, b;

            byte c1, c2, c3, c4, b1, b2;

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    int dstOffset = (y * width + x) * 4;

                    l = (int)System.Math.Floor(x * factorX);
                    t = (int)System.Math.Floor(y * factorY);

                    r = l + 1;
                    b = t + 1;

                    if (r >= source.PixelWidth)
                    {
                        r = l;
                    }

                    if (b >= source.PixelHeight)
                    {
                        b = t;
                    }

                    fractionX = x * factorX - l;
                    fractionY = y * factorY - t;

                    oneMinusX = 1.0 - fractionX;
                    oneMinusY = 1.0 - fractionY;

                    var function = new Func<int, byte>(offset => 
                        {
                            c1 = GetColor(l, t, offset);
                            c2 = GetColor(r, t, offset);
                            c3 = GetColor(l, b, offset);
                            c4 = GetColor(r, b, offset);

                            b1 = (byte)(oneMinusX * c1 + fractionX * c2);
                            b2 = (byte)(oneMinusX * c3 + fractionX * c4);

                            return (byte)(oneMinusY * b1 + fractionY * b2);
                        });

                    newPixels[dstOffset + 0] = function(0);
                    newPixels[dstOffset + 1] = function(1);
                    newPixels[dstOffset + 2] = function(2);
                    newPixels[dstOffset + 3] = function(3);
                }
            }

            target.SetPixels(width, height, newPixels);
        }
        private static void Rotate270(ImageBase source, ImageBase target)
        {
            Contract.Requires(source != null);
            Contract.Requires(source.IsFilled);
            Contract.Requires(target != null);
            Contract.Ensures(target.IsFilled);

            int oldIndex = 0, newIndex = 0;

            byte[] sourcePixels = source.Pixels;
            byte[] targetPixels = new byte[source.PixelWidth * source.PixelHeight * 4];

            for (int y = 0; y < source.PixelHeight; y++)
            {
                for (int x = 0; x < source.PixelWidth; x++)
                {
                    oldIndex = (y * source.PixelWidth + x) * 4;

                    // The new index will be calculated as if the image would be flipped
                    // at the x and the y axis and rotated about 90 degrees.
                    newIndex = ((source.PixelWidth - x - 1) * source.PixelHeight + y) * 4;

                    targetPixels[newIndex + 0] = sourcePixels[oldIndex + 0];
                    targetPixels[newIndex + 1] = sourcePixels[oldIndex + 1];
                    targetPixels[newIndex + 2] = sourcePixels[oldIndex + 2];
                    targetPixels[newIndex + 3] = sourcePixels[oldIndex + 3];
                }
            }
            target.SetPixels(source.PixelHeight, source.PixelWidth, targetPixels);
        }
        internal static void Crop(ImageBase source, ImageBase target, Rectangle bounds)
        {
            Contract.Requires<ArgumentNullException>(source != null, "Source image cannot be null.");
            Contract.Requires<ArgumentException>(source.IsFilled, "Source image has not been loaded.");
            Contract.Requires<ArgumentNullException>(target != null, "Target image cannot be null.");

            Guard.GreaterThan(bounds.Width, 0, "bounds",
                "Width of the rectangle must be greater than zero.");

            Guard.GreaterThan(bounds.Height, 0, "bounds",
                "Height of the rectangle must be greater than zero.");

            if (bounds.Right > source.PixelWidth || bounds.Bottom > source.PixelHeight)
            {
                throw new ArgumentException(
                    "Rectangle must be in the range of the image's dimension.", "bounds");
            }

            byte[] sourcePixels = source.Pixels;
            byte[] targetPixels = new byte[bounds.Width * bounds.Height * 4];

            for (int y = bounds.Top, i = 0; y < bounds.Bottom; y++, i++)
            {
                Array.Copy(sourcePixels, (y * source.PixelWidth + bounds.Left) * 4, targetPixels, i * bounds.Width * 4, bounds.Width * 4);
            }

            target.SetPixels(bounds.Width, bounds.Height, targetPixels);
        }
        private static void Rotate90(ImageBase source, ImageBase target)
        {
            Contract.Requires(source != null);
            Contract.Requires(source.IsFilled);
            Contract.Requires(target != null);
            Contract.Ensures(target.IsFilled);

            int oldIndex = 0, newIndex = 0;

            byte[] sourcePixels = source.Pixels;
            byte[] targetPixels = new byte[source.PixelWidth * source.PixelHeight * 4];

            for (int y = 0; y < source.PixelHeight; y++)
            {
                for (int x = 0; x < source.PixelWidth; x++)
                {
                    oldIndex = (y * source.PixelWidth + x) * 4;

                    // The new index will just be calculated by swapping
                    // the x and the y value for the pixel.
                    newIndex = ((x + 1) * source.PixelHeight - y - 1) * 4;

                    targetPixels[newIndex + 0] = sourcePixels[oldIndex + 0];
                    targetPixels[newIndex + 1] = sourcePixels[oldIndex + 1];
                    targetPixels[newIndex + 2] = sourcePixels[oldIndex + 2];
                    targetPixels[newIndex + 3] = sourcePixels[oldIndex + 3];
                }
            }
            target.SetPixels(source.PixelHeight, source.PixelWidth, targetPixels);
        }