//-------------------------------------------------------------- #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> /// Determines the number of image array entries and pixel size. /// </summary> /// <param name="description">The texture description.</param> /// <param name="nImages">The number of entries in the image array.</param> /// <param name="pixelSize">The total pixel size.</param> private static void DetermineImages(TextureDescription description, out int nImages, out int pixelSize) { Debug.Assert(description.Width > 0 && description.Height > 0 && description.Depth > 0); Debug.Assert(description.ArraySize > 0); Debug.Assert(description.MipLevels > 0); pixelSize = 0; nImages = 0; switch (description.Dimension) { case TextureDimension.Texture1D: case TextureDimension.Texture2D: case TextureDimension.TextureCube: for (int item = 0; item < description.ArraySize; item++) { int w = description.Width; int h = description.Height; for (int level = 0; level < description.MipLevels; level++) { int rowPitch, slicePitch; TextureHelper.ComputePitch(description.Format, w, h, out rowPitch, out slicePitch, ComputePitchFlags.None); pixelSize += slicePitch; nImages++; if (h > 1) h >>= 1; if (w > 1) w >>= 1; } } break; case TextureDimension.Texture3D: { int w = description.Width; int h = description.Height; int d = description.Depth; for (int level = 0; level < description.MipLevels; level++) { int rowPitch, slicePitch; TextureHelper.ComputePitch(description.Format, w, h, out rowPitch, out slicePitch, ComputePitchFlags.None); for (int slice = 0; slice < d; slice++) { pixelSize += slicePitch; nImages++; } if (h > 1) h >>= 1; if (w > 1) w >>= 1; if (d > 1) d >>= 1; } } break; default: Debug.Fail("Unexpected texture dimension"); break; } }