//-------------------------------------------------------------- #region Creation & Cleanup //-------------------------------------------------------------- /// <overloads> /// <summary> /// Initializes a new instance of the <see cref="Image"/> class. /// </summary> /// </overloads> /// /// <summary> /// Initializes a new, empty instance of the <see cref="Image"/> class. /// </summary> /// <param name="width">The width in pixels.</param> /// <param name="height">The height in pixels.</param> /// <param name="format">The texture format.</param> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="width"/> or <paramref name="height"/> is 0 or negative. /// </exception> /// <exception cref="ArgumentException"> /// <paramref name="format"/> is invalid. /// </exception> public Image(int width, int height, DataFormat format) { if (width <= 0) { throw new ArgumentOutOfRangeException("width", "Image size must not be negative or 0."); } if (height <= 0) { throw new ArgumentOutOfRangeException("height", "Image size must not be negative or 0."); } if (!TextureHelper.IsValid(format)) { throw new ArgumentException("Invalid texture format.", "format"); } int rowPitch, slicePitch; TextureHelper.ComputePitch(format, width, height, out rowPitch, out slicePitch); Width = width; Height = height; Format = format; RowPitch = rowPitch; _data = new byte[slicePitch]; }
/// <summary> /// Initializes a new instance of the <see cref="Image"/> class with the specified data. /// </summary> /// <param name="width">The width in pixels.</param> /// <param name="height">The height in pixels.</param> /// <param name="format">The texture format.</param> /// <param name="data">The contents of the image.</param> /// <exception cref="ArgumentException"> /// <paramref name="format"/> is invalid, or <paramref name="data"/> has wrong size. /// </exception> /// <exception cref="ArgumentNullException"> /// <paramref name="data"/> is <see langword="null"/>. /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="width"/> or <paramref name="height"/>is 0 or negative. /// </exception> public Image(int width, int height, DataFormat format, byte[] data) { if (width <= 0) { throw new ArgumentOutOfRangeException("width", "Image size must not be negative or 0."); } if (height <= 0) { throw new ArgumentOutOfRangeException("height", "Image size must not be negative or 0."); } if (!TextureHelper.IsValid(format)) { throw new ArgumentException("Invalid texture format.", "format"); } if (data == null) { throw new ArgumentNullException("data"); } int rowPitch, slicePitch; TextureHelper.ComputePitch(format, width, height, out rowPitch, out slicePitch); if (data.Length != slicePitch) { throw new ArgumentException(string.Format("Buffer has invalid size. Expected size: {0} bytes; Actual size: {1}", slicePitch, data.Length)); } Width = width; Height = height; Format = format; RowPitch = rowPitch; _data = data; }
/// <summary> /// Validates the texture. /// </summary> /// <param name="description">The texture description.</param> /// <exception cref="ArgumentException"> /// The <paramref name="description"/> is invalid. /// </exception> /// <exception cref="NotSupportedException"> /// The specified format is not supported. /// </exception> private static void ValidateTexture(TextureDescription description) { if (!TextureHelper.IsValid(description.Format)) throw new ArgumentException("The specified texture format is not supported.", "description"); if (TextureHelper.IsPalettized(description.Format)) throw new ArgumentException("Palettized texture formats are not supported.", "description"); switch (description.Dimension) { case TextureDimension.Texture1D: if (description.Width <= 0 || description.Height != 1 || description.Depth != 1 || description.ArraySize <= 0) throw new ArgumentException("Invalid texture description.", "description"); //if (TextureHelper.IsVideo(description.Format)) // throw new NotSupportedException("Video formats are not supported."); if (!TextureHelper.ValidateMipLevels(description.Width, 1, description.MipLevels)) throw new ArgumentException("Invalid number of mipmap levels.", "description"); break; case TextureDimension.Texture2D: case TextureDimension.TextureCube: if (description.Width <= 0 || description.Height <= 0 || description.Depth != 1 || description.ArraySize <= 0) throw new ArgumentException("Invalid texture description.", "description"); if (description.Dimension == TextureDimension.TextureCube) { if ((description.ArraySize % 6) != 0) throw new ArgumentException("All six faces need to be specified for cube maps.", "description"); //if (TextureHelper.IsVideo(description.Format)) // throw new NotSupportedException("Video formats are not supported."); } if (!TextureHelper.ValidateMipLevels(description.Width, description.Height, description.MipLevels)) throw new ArgumentException("Invalid number of mipmaps levels.", "description"); break; case TextureDimension.Texture3D: if (description.Width <= 0 || description.Height <= 0 || description.Depth <= 0 || description.ArraySize != 1) throw new ArgumentException("Invalid texture description.", "description"); //if (TextureHelper.IsVideo(description.Format) || TextureHelper.IsPlanar(description.Format) // || TextureHelper.IsDepthStencil(description.Format)) // throw new NotSupportedException("The specified texture format is not supported."); if (!TextureHelper.ValidateMipLevels(description.Width, description.Height, description.Depth, description.MipLevels)) throw new ArgumentException("Invalid number of mipmaps levels.", "description"); break; default: throw new NotSupportedException("The specified texture dimension is not supported."); } }