/// <summary> /// Gets the data of the entire <see cref="Texture1D"/>. /// </summary> /// <param name="texture">The <see cref="Texture1D"/> to get the image from.</param> /// <param name="image">The image in which to write the pixel data.</param> public static void GetData(this Texture1D texture, Image <Rgba32> image) { if (texture == null) { throw new ArgumentNullException(nameof(texture)); } if (texture.ImageFormat != TextureImageFormat.Color4b) { throw new ArgumentException(nameof(texture), ImageUtils.TextureFormatMustBeColor4bError); } if (image == null) { throw new ArgumentNullException(nameof(image)); } if (image.Width * image.Height != texture.Width) { throw new ArgumentException(nameof(image), ImageUtils.ImageSizeMustMatchTextureSizeError); } if (!image.TryGetSinglePixelSpan(out Span <Rgba32> pixels)) { throw new InvalidDataException(ImageUtils.ImageNotContiguousError); } texture.GetData(pixels, PixelFormat.Rgba); }
/// <summary> /// Saves this <see cref="Texture1D"/>'s image to a stream. /// </summary> /// <param name="texture">The <see cref="Texture1D"/> whose image to save.</param> /// <param name="stream">The stream to save the texture image to.</param> /// <param name="imageFormat">The format the image will be saved as.</param> public static void SaveAsImage(this Texture1D texture, Stream stream, SaveImageFormat imageFormat) { if (texture == null) { throw new ArgumentNullException(nameof(texture)); } if (stream == null) { throw new ArgumentException(nameof(stream)); } IImageFormat format = ImageUtils.GetFormatFor(imageFormat); using Image <Rgba32> image = new Image <Rgba32>((int)texture.Width, 1); texture.GetData(image); image.Save(stream, format); }