/// <summary>
        /// Function to retrieve the image data for a sprite texture as a 32 bit RGBA pixel data.
        /// </summary>
        /// <param name="data">The data used for sprite extraction.</param>
        /// <returns>The image data for the texture.</returns>
        public async Task <IGorgonImage> GetSpriteTextureImageDataAsync(SpriteExtractionData data)
        {
            IGorgonImage imageData = data.Texture.Texture.ToImage();

            if ((imageData.Format == BufferFormat.R8G8B8A8_UNorm) ||
                (imageData.Format == BufferFormat.R8G8B8A8_UNorm_SRgb))
            {
                return(imageData);
            }

            if ((imageData.FormatInfo.IsSRgb) &&
                (imageData.CanConvertToFormat(BufferFormat.R8G8B8A8_UNorm_SRgb)))
            {
                await Task.Run(() => imageData.ConvertToFormat(BufferFormat.R8G8B8A8_UNorm_SRgb));

                return(imageData);
            }
            else if (imageData.CanConvertToFormat(BufferFormat.R8G8B8A8_UNorm))
            {
                await Task.Run(() => imageData.ConvertToFormat(BufferFormat.R8G8B8A8_UNorm));

                return(imageData);
            }

            // OK, so this is going to take drastic measures.
            imageData.Dispose();
            return(RenderImageData(data.Texture));
        }
Beispiel #2
0
        /// <summary>Initializes a new instance of the <see cref="ExtractParameters"/> class.</summary>
        /// <param name="settings">The plug in settings.</param>
        /// <param name="data">The data structure that will hold the information used to extract sprites.</param>
        /// <param name="textureFile">The file used as the texture.</param>
        /// <param name="extractor">The extractor used to build the sprites.</param>
        /// <param name="colorPicker">The color picker service.</param>
        /// <param name="folderBrowser">The project file system folder browser service.</param>
        /// <param name="commonServices">The common services.</param>
        /// <exception cref="ArgumentNullException">Thrown when the any parameters are <b>null</b>.</exception>
        /// <exception cref="ArgumentMissingException">Thrown when the <see cref="SpriteExtractionData.Texture"/> property of the <paramref name="data"/> parameter is <b>null</b>.</exception>
        public ExtractParameters(ExtractSpriteToolSettings settings, SpriteExtractionData data, IContentFile textureFile, IExtractorService extractor, IColorPickerService colorPicker, IFileSystemFolderBrowseService folderBrowser, IViewModelInjection commonServices)
            : base(commonServices)
        {
            Settings      = settings ?? throw new ArgumentNullException(nameof(settings));
            Data          = data ?? throw new ArgumentNullException(nameof(data));
            Extractor     = extractor ?? throw new ArgumentNullException(nameof(extractor));
            ColorPicker   = colorPicker ?? throw new ArgumentNullException(nameof(colorPicker));
            FolderBrowser = folderBrowser ?? throw new ArgumentNullException(nameof(folderBrowser));
            TextureFile   = textureFile ?? throw new ArgumentNullException(nameof(textureFile));

            if (data.Texture == null)
            {
                throw new ArgumentMissingException(nameof(data.Texture), nameof(data));
            }
        }
        /// <summary>Function to retrieve the sprites from the texture atlas.</summary>
        /// <param name="data">The data used to extract the sprites.</param>
        /// <param name="imageData">The system memory version of the texture.</param>
        /// <param name="progressCallback">A callback method used to report on progress of the operation.</param>
        /// <param name="cancelToken">The token used to cancel the operation.</param>
        /// <returns>A list of sprites generated by this method.</returns>
        public IReadOnlyList <GorgonSprite> ExtractSprites(SpriteExtractionData data, IGorgonImage imageData, Action <ProgressData> progressCallback, CancellationToken cancelToken)
        {
            var result = new List <GorgonSprite>();

            progressCallback?.Invoke(new ProgressData(0, data.SpriteCount));

            int count = 1;

            for (int array = 0; array < data.ArrayCount; ++array)
            {
                for (int y = 0; y < data.GridSize.Height; ++y)
                {
                    if (cancelToken.IsCancellationRequested)
                    {
                        return(result);
                    }

                    for (int x = 0; x < data.GridSize.Width; ++x)
                    {
                        if (cancelToken.IsCancellationRequested)
                        {
                            return(result);
                        }

                        DX.Rectangle spriteRect = GetSpriteRect(x, y, data.GridOffset, data.CellSize);

                        if ((data.SkipEmpty) && (IsEmpty(spriteRect, imageData.Buffers[0, array + data.StartArrayIndex], data.SkipColor)))
                        {
                            continue;
                        }

                        result.Add(new GorgonSprite
                        {
                            TextureArrayIndex = array + data.StartArrayIndex,
                            Texture           = data.Texture,
                            Anchor            = DX.Vector2.Zero,
                            Size           = new DX.Size2F(data.CellSize.Width, data.CellSize.Height),
                            TextureRegion  = data.Texture.ToTexel(spriteRect),
                            TextureSampler = GorgonSamplerState.PointFiltering
                        });

                        progressCallback?.Invoke(new ProgressData(count++, data.SpriteCount));
                    }
                }
            }

            return(result);
        }