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); }
void IImageFilter.Apply(ImageBase target, ImageBase source, Rectangle rectangle) { Contract.Requires<ArgumentNullException>(target != null, "Target image cannot be null."); Contract.Requires<ArgumentNullException>(source != null, "Source image cannot be null."); throw new NotImplementedException(); }
public void Resize(ImageBase source, ImageBase target, int width, int height) { 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."); Contract.Requires<ArgumentException>(width >= 0, "Width must be greater or equals than zero."); Contract.Requires<ArgumentException>(height >= 0, "Height must be greater or equals than zero."); throw new NotImplementedException(); }
/// <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; } }
public static WriteableBitmap ToBitmap(ImageBase image) { Guard.NotNull(image, "image"); var bitmap = new WriteableBitmap(image.Width, image.Height); ImageBase temp = image; byte[] pixels = temp.GetPixels(); int[] raster = bitmap.Pixels; Buffer.BlockCopy(pixels, 0, raster, 0, pixels.Length); for (int i = 0; i < raster.Length; i++) { int abgr = raster[i]; int a = (abgr >> 24) & 0xff; float m = a / 255f; int argb = a << 24 | (int)((abgr & 0xff) * m) << 16 | (int)(((abgr >> 8) & 0xff) * m) << 8 | (int)(((abgr >> 16) & 0xff) * m); raster[i] = argb; } bitmap.Invalidate(); return bitmap; }
/// <summary> /// Swaps the image at the Y-axis, which goes throug the middle /// of the width of the image. /// </summary> /// <param name="image">The image to swap at the y-axis. Cannot be null /// (Nothing in Visual Basic).</param> /// <exception cref="ArgumentNullException"><paramref name="image"/> is null /// (Nothing in Visual Basic).</exception> private static void FlipY(ImageBase image) { Contract.Requires <ArgumentNullException>(image != null, "Image cannot be null."); Contract.Requires <ArgumentException>(image.IsFilled, "Other image has not been loaded."); int oldIndex = 0, newIndex = 0; byte[] sourcePixels = image.Pixels; byte r, g, b, a; for (int y = 0; y < image.PixelHeight; y++) { for (int x = 0; x < image.PixelWidth / 2; x++) { oldIndex = (y * image.PixelWidth + x) * 4; r = sourcePixels[oldIndex + 0]; g = sourcePixels[oldIndex + 1]; b = sourcePixels[oldIndex + 2]; a = sourcePixels[oldIndex + 3]; newIndex = (y * image.PixelWidth + image.PixelWidth - x - 1) * 4; sourcePixels[oldIndex + 0] = sourcePixels[newIndex + 0]; sourcePixels[oldIndex + 1] = sourcePixels[newIndex + 1]; sourcePixels[oldIndex + 2] = sourcePixels[newIndex + 2]; sourcePixels[oldIndex + 3] = sourcePixels[newIndex + 3]; sourcePixels[newIndex + 0] = r; sourcePixels[newIndex + 1] = g; sourcePixels[newIndex + 2] = b; sourcePixels[newIndex + 3] = a; } } }
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); }
/// <summary> /// Initializes a new instance of the <see cref="ImageBase"/> class /// by making a copy from another image. /// </summary> /// <param name="other">The other, where the clone should be made from.</param> /// <exception cref="ArgumentNullException"><paramref name="other"/> is null /// (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentException"><paramref name="other"/> is not loaded.</exception> public ImageBase(ImageBase other) { Contract.Requires<ArgumentNullException>(other != null, "Other image cannot be null."); Contract.Requires<ArgumentException>(other.IsFilled, "Other image has not been loaded."); Contract.Ensures(IsFilled); byte[] pixels = other.Pixels; _pixelWidth = other.PixelWidth; _pixelHeight = other.PixelHeight; _pixels = new byte[pixels.Length]; Array.Copy(pixels, _pixels, pixels.Length); _isFilled = other.IsFilled; }
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); }
/// <summary> /// Swaps the image at the Y-axis, which goes throug the middle /// of the width of the image. /// </summary> /// <param name="image">The image to swap at the y-axis. Cannot be null /// (Nothing in Visual Basic).</param> /// <exception cref="ArgumentNullException"><paramref name="image"/> is null /// (Nothing in Visual Basic).</exception> private static void FlipY(ImageBase image) { Contract.Requires<ArgumentNullException>(image != null, "Image cannot be null."); Contract.Requires<ArgumentException>(image.IsFilled, "Other image has not been loaded."); int oldIndex = 0, newIndex = 0; byte[] sourcePixels = image.Pixels; byte r, g, b, a; for (int y = 0; y < image.PixelHeight; y++) { for (int x = 0; x < image.PixelWidth / 2; x++) { oldIndex = (y * image.PixelWidth + x) * 4; r = sourcePixels[oldIndex + 0]; g = sourcePixels[oldIndex + 1]; b = sourcePixels[oldIndex + 2]; a = sourcePixels[oldIndex + 3]; newIndex = (y * image.PixelWidth + image.PixelWidth - x - 1) * 4; sourcePixels[oldIndex + 0] = sourcePixels[newIndex + 0]; sourcePixels[oldIndex + 1] = sourcePixels[newIndex + 1]; sourcePixels[oldIndex + 2] = sourcePixels[newIndex + 2]; sourcePixels[oldIndex + 3] = sourcePixels[newIndex + 3]; sourcePixels[newIndex + 0] = r; sourcePixels[newIndex + 1] = g; sourcePixels[newIndex + 2] = b; sourcePixels[newIndex + 3] = a; } } }
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); }