|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Six Labors Split License. |
| 3 | + |
| 4 | +using SixLabors.ImageSharp.Formats.Ico.Common; |
| 5 | +using SixLabors.ImageSharp.Formats.Png; |
| 6 | +using SixLabors.ImageSharp.Formats.Png.Chunks; |
| 7 | + |
| 8 | +namespace SixLabors.ImageSharp.Formats.Cur; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Provides Cur specific png metadata information for the image. |
| 12 | +/// </summary> |
| 13 | +public class CurPngFrameMetadata : CurFrameMetadata, IDeepCloneable<CurPngFrameMetadata>, IPngMetadata |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// Initializes a new instance of the <see cref="CurPngFrameMetadata"/> class. |
| 17 | + /// </summary> |
| 18 | + public CurPngFrameMetadata() |
| 19 | + { |
| 20 | + } |
| 21 | + |
| 22 | + private CurPngFrameMetadata(CurFrameMetadata metadata) |
| 23 | + : base(metadata) |
| 24 | + { |
| 25 | + } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Initializes a new instance of the <see cref="CurPngFrameMetadata"/> class. |
| 29 | + /// </summary> |
| 30 | + /// <param name="width">width</param> |
| 31 | + /// <param name="height">height</param> |
| 32 | + /// <param name="colorCount">colorCount</param> |
| 33 | + /// <param name="planes">planes</param> |
| 34 | + /// <param name="bitCount">bitCount</param> |
| 35 | + public CurPngFrameMetadata(byte width, byte height, byte colorCount, ushort planes, ushort bitCount) |
| 36 | + : base(width, height, colorCount, planes, bitCount) |
| 37 | + { |
| 38 | + } |
| 39 | + |
| 40 | + /// <inheritdoc/> |
| 41 | + public override IcoLikeFrameCompression Compression { get; } = IcoLikeFrameCompression.Png; |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Gets or sets the number of bits per sample or per palette index (not per pixel). |
| 45 | + /// Not all values are allowed for all <see cref="ColorType"/> values. |
| 46 | + /// </summary> |
| 47 | + public PngBitDepth? BitDepth { get; set; } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Gets or sets the color type. |
| 51 | + /// </summary> |
| 52 | + public PngColorType? ColorType { get; set; } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Gets or sets a value indicating whether this instance should write an Adam7 interlaced image. |
| 56 | + /// </summary> |
| 57 | + public PngInterlaceMode? InterlaceMethod { get; set; } = PngInterlaceMode.None; |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Gets or sets the gamma value for the image. |
| 61 | + /// </summary> |
| 62 | + public float Gamma { get; set; } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Gets or sets the color table, if any. |
| 66 | + /// </summary> |
| 67 | + public ReadOnlyMemory<Color>? ColorTable { get; set; } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Gets or sets the transparent color used with non palette based images, if a transparency chunk and markers were decoded. |
| 71 | + /// </summary> |
| 72 | + public Color? TransparentColor { get; set; } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Gets or sets the collection of text data stored within the iTXt, tEXt, and zTXt chunks. |
| 76 | + /// Used for conveying textual information associated with the image. |
| 77 | + /// </summary> |
| 78 | + public IList<PngTextData> TextData { get; set; } = new List<PngTextData>(); |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Gets or sets the number of times to loop this APNG. 0 indicates infinite looping. |
| 82 | + /// </summary> |
| 83 | + public int RepeatCount { get; set; } |
| 84 | + |
| 85 | + /// <inheritdoc/> |
| 86 | + public override CurPngFrameMetadata DeepClone() => new(this); |
| 87 | + |
| 88 | + internal override void FromMetadata(IDeepCloneable other) |
| 89 | + { |
| 90 | + if (other is not PngMetadata metadata) |
| 91 | + { |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + this.BitDepth = metadata.BitDepth; |
| 96 | + this.ColorType = metadata.ColorType; |
| 97 | + this.Gamma = metadata.Gamma; |
| 98 | + this.InterlaceMethod = metadata.InterlaceMethod; |
| 99 | + this.TransparentColor = metadata.TransparentColor; |
| 100 | + this.RepeatCount = metadata.RepeatCount; |
| 101 | + |
| 102 | + if (metadata.ColorTable?.Length > 0) |
| 103 | + { |
| 104 | + this.ColorTable = metadata.ColorTable.Value.ToArray(); |
| 105 | + } |
| 106 | + |
| 107 | + for (int i = 0; i < metadata.TextData.Count; i++) |
| 108 | + { |
| 109 | + this.TextData.Add(metadata.TextData[i]); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + internal override PngMetadata ToMetadata() |
| 114 | + { |
| 115 | + PngMetadata result = new() |
| 116 | + { |
| 117 | + BitDepth = this.BitDepth, |
| 118 | + ColorType = this.ColorType, |
| 119 | + Gamma = this.Gamma, |
| 120 | + InterlaceMethod = this.InterlaceMethod, |
| 121 | + TransparentColor = this.TransparentColor, |
| 122 | + RepeatCount = this.RepeatCount, |
| 123 | + }; |
| 124 | + |
| 125 | + if (this.ColorTable?.Length > 0) |
| 126 | + { |
| 127 | + result.ColorTable = this.ColorTable.Value.ToArray(); |
| 128 | + } |
| 129 | + |
| 130 | + for (int i = 0; i < this.TextData.Count; i++) |
| 131 | + { |
| 132 | + result.TextData.Add(this.TextData[i]); |
| 133 | + } |
| 134 | + |
| 135 | + return result; |
| 136 | + } |
| 137 | +} |
0 commit comments