Пример #1
0
        /// <summary>
        /// Creates a <see cref="Texture1D"/> from an <see cref="Image{Rgba32}"/>.
        /// </summary>
        /// <param name="graphicsDevice">The <see cref="GraphicsDevice"/> the resource will use.</param>
        /// <param name="image">The image to create the <see cref="Texture1D"/> with.</param>
        /// <param name="generateMipmaps">Whether to generate mipmaps for the <see cref="Texture1D"/>.</param>
        public static Texture1D FromImage(GraphicsDevice graphicsDevice, Image <Rgba32> image, bool generateMipmaps = false)
        {
            if (graphicsDevice == null)
            {
                throw new ArgumentNullException(nameof(graphicsDevice));
            }

            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            if (!image.TryGetSinglePixelSpan(out Span <Rgba32> pixels))
            {
                throw new InvalidDataException(ImageUtils.ImageNotContiguousError);
            }

            Texture1D texture = new Texture1D(graphicsDevice, (uint)(image.Width * image.Height));

            try
            {
                texture.SetData <Rgba32>(pixels, 0, PixelFormat.Rgba);

                if (generateMipmaps)
                {
                    texture.GenerateMipmaps();
                }

                return(texture);
            }
            catch
            {
                texture.Dispose();
                throw;
            }
        }