|
| 1 | +// Copyright (c) Christopher Whitley. All rights reserved. |
| 2 | +// Licensed under the MIT license. |
| 3 | +// See LICENSE file in the project root for full license information. |
| 4 | + |
| 5 | +using AsepriteDotNet.Common; |
| 6 | + |
| 7 | +namespace AsepriteDotNet.Aseprite.Types; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Provides extension methods for <see cref="AsepriteCel"/> instances. |
| 11 | +/// </summary> |
| 12 | +public static class AsepriteCelExtensions |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Extracts pixel data from an <see cref="AsepriteCel"/> and returns it as a <see cref="Texture"/>. |
| 16 | + /// </summary> |
| 17 | + /// <param name="cel">The <see cref="AsepriteCel"/> containing the pixel data to extract.</param> |
| 18 | + /// <param name="name"> |
| 19 | + /// Optional name for the extracted <see cref="Texture"/>. If not provided, an empty string is used. |
| 20 | + /// </param> |
| 21 | + /// <returns>A <see cref="Texture"/> object containing the extracted pixel data.</returns> |
| 22 | + /// <exception cref="ArgumentNullException"> |
| 23 | + /// Thrown when the input <see cref="AsepriteCel"/> is <see langword="null"/>. |
| 24 | + /// </exception> |
| 25 | + /// <exception cref="InvalidOperationException"> |
| 26 | + /// Thrown when the <see cref="AsepriteCel"/> does not contain pixel data. |
| 27 | + /// </exception> |
| 28 | + public static Texture ExtractCel(this AsepriteCel cel, string? name = null) |
| 29 | + { |
| 30 | + ArgumentNullException.ThrowIfNull(cel); |
| 31 | + |
| 32 | + |
| 33 | + if (cel is AsepriteImageCel imageCel) |
| 34 | + { |
| 35 | + return imageCel.ExtractCel(name); |
| 36 | + } |
| 37 | + |
| 38 | + if (cel is AsepriteTilemapCel tilemapCel) |
| 39 | + { |
| 40 | + return tilemapCel.ExtractCel(name); |
| 41 | + } |
| 42 | + |
| 43 | + throw new InvalidOperationException("This cel does not contain pixel data"); |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Extracts pixel data from an <see cref="AsepriteImageCel"/> and returns it as a <see cref="Texture"/>. |
| 48 | + /// </summary> |
| 49 | + /// <param name="cel">The <see cref="AsepriteImageCel"/> containing the pixel data to extract.</param> |
| 50 | + /// <param name="name"> |
| 51 | + /// Optional name for the extracted <see cref="Texture"/>. If not provided, an empty string is used. |
| 52 | + /// </param> |
| 53 | + /// <returns>A <see cref="Texture"/> object containing the extracted pixel data.</returns> |
| 54 | + /// <exception cref="ArgumentNullException"> |
| 55 | + /// Thrown when the input <see cref="AsepriteImageCel"/> is <see langword="null"/>. |
| 56 | + /// </exception> |
| 57 | + public static Texture ExtractCel(this AsepriteImageCel cel, string? name = null) |
| 58 | + { |
| 59 | + ArgumentNullException.ThrowIfNull(cel); |
| 60 | + |
| 61 | + return new Texture(name ?? string.Empty, cel.Size, cel.Pixels.ToArray()); |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Extracts pixel data from an <see cref="AsepriteTilemapCel"/> and returns it as a <see cref="Texture"/>. |
| 66 | + /// </summary> |
| 67 | + /// <param name="cel">The <see cref="AsepriteTilemapCel"/> containing the pixel data to extract.</param> |
| 68 | + /// <param name="name"> |
| 69 | + /// Optional name for the extracted <see cref="Texture"/>. If not provided, an empty string is used. |
| 70 | + /// </param> |
| 71 | + /// <returns>A <see cref="Texture"/> object containing the extracted pixel data.</returns> |
| 72 | + /// <exception cref="ArgumentNullException"> |
| 73 | + /// Thrown when the input <see cref="AsepriteTilemapCel"/> is <see langword="null"/>. |
| 74 | + /// </exception> |
| 75 | + public static Texture ExtractCel(this AsepriteTilemapCel cel, string? name = null) |
| 76 | + { |
| 77 | + ArgumentNullException.ThrowIfNull(cel); |
| 78 | + |
| 79 | + AsepriteTilemapLayer layer = (AsepriteTilemapLayer)cel.Layer; |
| 80 | + AsepriteTileset tileset = layer.Tileset; |
| 81 | + Size size = new Size(cel.Size.Width * tileset.TileSize.Width, cel.Size.Height * tileset.TileSize.Height); |
| 82 | + Rgba32[] pixels = new Rgba32[size.Width * size.Height]; |
| 83 | + |
| 84 | + Parallel.For(0, cel.Tiles.Length, new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount }, i => |
| 85 | + { |
| 86 | + AsepriteTile tile = cel.Tiles[i]; |
| 87 | + int column = i % cel.Size.Width; |
| 88 | + int row = i / cel.Size.Width; |
| 89 | + Parallel.For(0, tileset.Pixels.Length, new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount }, j => |
| 90 | + { |
| 91 | + int px = (j % tileset.TileSize.Width) + (column + tileset.TileSize.Height); |
| 92 | + int py = (j / tileset.TileSize.Width) + (row + tileset.TileSize.Height); |
| 93 | + int index = py * size.Width + px; |
| 94 | + pixels[index] = tileset.Pixels[j]; |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + return new Texture(name ?? string.Empty, size, pixels); |
| 99 | + } |
| 100 | +} |
0 commit comments