// --- extract clip region --- /// <summary>Extracts a clip region from a texture.</summary> /// <param name="texture">The original texture.</param> /// <param name="region">The clip region, or a null reference.</param> /// <returns>The texture with the extracted clip region.</returns> /// <exception cref="System.ArgumentException">Raised when the clip region is outside the texture bounds.</exception> /// <exception cref="System.NotSupportedException">Raised when the number of bits per pixel in the texture is not supported.</exception> internal static Texture ExtractClipRegion(Texture texture, TextureClipRegion region) { if (region == null || region.Left == 0 && region.Top == 0 && region.Width == texture.Width && region.Height == texture.Height) { return(texture); } if (region.Left < 0 || region.Top < 0 || region.Width <= 0 || region.Height <= 0 || region.Left + region.Width > texture.Width || region.Top + region.Height > texture.Height) { throw new ArgumentException(); } if (texture.BitsPerPixel == 24 | texture.BitsPerPixel == 32) { int width = texture.Width; int height = texture.Height; byte[] bytes = texture.Bytes; int clipLeft = region.Left; int clipTop = region.Top; int clipWidth = region.Width; int clipHeight = region.Height; if (texture.BitsPerPixel == 24) { byte[] newBytes = new byte[3 * clipWidth * clipHeight]; int i = 0; for (int y = 0; y < clipHeight; y++) { int j = 3 * width * (clipTop + y) + 3 * clipLeft; for (int x = 0; x < clipWidth; x++) { newBytes[i + 0] = bytes[j + 0]; newBytes[i + 1] = bytes[j + 1]; newBytes[i + 2] = bytes[j + 2]; i += 3; j += 3; } } return(new Texture(clipWidth, clipHeight, 24, newBytes)); } else { byte[] newBytes = new byte[4 * clipWidth * clipHeight]; int i = 0; for (int y = 0; y < clipHeight; y++) { int j = 4 * width * (clipTop + y) + 4 * clipLeft; for (int x = 0; x < clipWidth; x++) { newBytes[i + 0] = bytes[j + 0]; newBytes[i + 1] = bytes[j + 1]; newBytes[i + 2] = bytes[j + 2]; newBytes[i + 3] = bytes[j + 3]; i += 4; j += 4; } } return(new Texture(clipWidth, clipHeight, 32, newBytes)); } } throw new NotSupportedException(); }
// --- extract clip region --- /// <summary>Extracts a clip region from a texture.</summary> /// <param name="texture">The original texture.</param> /// <param name="region">The clip region, or a null reference.</param> /// <returns>The texture with the extracted clip region.</returns> /// <exception cref="System.ArgumentException">Raised when the clip region is outside the texture bounds.</exception> /// <exception cref="System.NotSupportedException">Raised when the number of bits per pixel in the texture is not supported.</exception> internal static Texture ExtractClipRegion(Texture texture, TextureClipRegion region) { if (region == null || region.Left == 0 && region.Top == 0 && region.Width == texture.Width && region.Height == texture.Height) { return texture; } if (region.Left < 0 || region.Top < 0 || region.Width <= 0 || region.Height <= 0 || region.Left + region.Width > texture.Width || region.Top + region.Height > texture.Height) { throw new ArgumentException(); } if (texture.BitsPerPixel == 24 | texture.BitsPerPixel == 32) { int width = texture.Width; int height = texture.Height; byte[] bytes = texture.Bytes; int clipLeft = region.Left; int clipTop = region.Top; int clipWidth = region.Width; int clipHeight = region.Height; if (texture.BitsPerPixel == 24) { byte[] newBytes = new byte[3 * clipWidth * clipHeight]; int i = 0; for (int y = 0; y < clipHeight; y++) { int j = 3 * width * (clipTop + y) + 3 * clipLeft; for (int x = 0; x < clipWidth; x++) { newBytes[i + 0] = bytes[j + 0]; newBytes[i + 1] = bytes[j + 1]; newBytes[i + 2] = bytes[j + 2]; i += 3; j += 3; } } return new Texture(clipWidth, clipHeight, 24, newBytes); } else { byte[] newBytes = new byte[4 * clipWidth * clipHeight]; int i = 0; for (int y = 0; y < clipHeight; y++) { int j = 4 * width * (clipTop + y) + 4 * clipLeft; for (int x = 0; x < clipWidth; x++) { newBytes[i + 0] = bytes[j + 0]; newBytes[i + 1] = bytes[j + 1]; newBytes[i + 2] = bytes[j + 2]; newBytes[i + 3] = bytes[j + 3]; i += 4; j += 4; } } return new Texture(clipWidth, clipHeight, 32, newBytes); } } throw new NotSupportedException(); }
/// <summary>Checks whether this instance is equal to the specified object.</summary> /// <param name="obj">The object.</param> /// <returns>Whether this instance is equal to the specified object.</returns> public override bool Equals(object obj) { if (object.ReferenceEquals(this, obj)) { return(true); } if (object.ReferenceEquals(this, null)) { return(false); } if (object.ReferenceEquals(obj, null)) { return(false); } if (!(obj is TextureClipRegion)) { return(false); } TextureClipRegion x = (TextureClipRegion)obj; if (this.MyLeft != x.MyLeft) { return(false); } if (this.MyTop != x.MyTop) { return(false); } if (this.MyWidth != x.MyWidth) { return(false); } if (this.MyHeight != x.MyHeight) { return(false); } return(true); }
// --- constructors --- /// <summary>Creates new texture parameters.</summary> /// <param name="clipRegion">The region in the texture to be extracted, or a null reference for the entire texture.</param> /// <param name="transparentColor">The color in the texture that should become transparent, or a null reference for no transparent color.</param> public TextureParameters(TextureClipRegion clipRegion, Nullable <Color24> transparentColor) { this.MyClipRegion = clipRegion; this.MyTransparentColor = transparentColor; }
// --- constructors --- /// <summary>Creates new texture parameters.</summary> /// <param name="clipRegion">The region in the texture to be extracted, or a null reference for the entire texture.</param> /// <param name="transparentColor">The color in the texture that should become transparent, or a null reference for no transparent color.</param> public TextureParameters(TextureClipRegion clipRegion, Nullable<Color24> transparentColor) { this.MyClipRegion = clipRegion; this.MyTransparentColor = transparentColor; }