|
1 | 1 | // Copyright (c) Six Labors.
|
2 | 2 | // Licensed under the Six Labors Split License.
|
3 | 3 |
|
| 4 | +using SixLabors.ImageSharp.Formats.Bmp; |
| 5 | +using SixLabors.ImageSharp.Formats.Icon; |
| 6 | +using SixLabors.ImageSharp.PixelFormats; |
| 7 | + |
4 | 8 | namespace SixLabors.ImageSharp.Formats.Cur;
|
5 | 9 |
|
6 | 10 | /// <summary>
|
7 |
| -/// Provides Ico specific metadata information for the image. |
| 11 | +/// Provides Cur specific metadata information for the image. |
8 | 12 | /// </summary>
|
9 |
| -public class CurMetadata : IDeepCloneable<CurMetadata>, IDeepCloneable |
| 13 | +public class CurMetadata : IFormatMetadata<CurMetadata> |
10 | 14 | {
|
| 15 | + /// <summary> |
| 16 | + /// Initializes a new instance of the <see cref="CurMetadata"/> class. |
| 17 | + /// </summary> |
| 18 | + public CurMetadata() |
| 19 | + { |
| 20 | + } |
| 21 | + |
| 22 | + private CurMetadata(CurMetadata other) |
| 23 | + { |
| 24 | + this.Compression = other.Compression; |
| 25 | + this.HotspotX = other.HotspotX; |
| 26 | + this.HotspotY = other.HotspotY; |
| 27 | + this.EncodingWidth = other.EncodingWidth; |
| 28 | + this.EncodingHeight = other.EncodingHeight; |
| 29 | + this.BmpBitsPerPixel = other.BmpBitsPerPixel; |
| 30 | + |
| 31 | + if (other.ColorTable?.Length > 0) |
| 32 | + { |
| 33 | + this.ColorTable = other.ColorTable.Value.ToArray(); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Gets or sets the frame compressions format. Derived from the root frame. |
| 39 | + /// </summary> |
| 40 | + public IconFrameCompression Compression { get; set; } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Gets or sets the horizontal coordinates of the hotspot in number of pixels from the left. Derived from the root frame. |
| 44 | + /// </summary> |
| 45 | + public ushort HotspotX { get; set; } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Gets or sets the vertical coordinates of the hotspot in number of pixels from the top. Derived from the root frame. |
| 49 | + /// </summary> |
| 50 | + public ushort HotspotY { get; set; } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Gets or sets the encoding width. <br /> |
| 54 | + /// Can be any number between 0 and 255. Value 0 means a frame height of 256 pixels or greater. Derived from the root frame. |
| 55 | + /// </summary> |
| 56 | + public byte EncodingWidth { get; set; } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Gets or sets the encoding height. <br /> |
| 60 | + /// Can be any number between 0 and 255. Value 0 means a frame height of 256 pixels or greater. Derived from the root frame. |
| 61 | + /// </summary> |
| 62 | + public byte EncodingHeight { get; set; } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Gets or sets the number of bits per pixel.<br/> |
| 66 | + /// Used when <see cref="Compression"/> is <see cref="IconFrameCompression.Bmp"/> |
| 67 | + /// </summary> |
| 68 | + public BmpBitsPerPixel BmpBitsPerPixel { get; set; } = BmpBitsPerPixel.Bit32; |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Gets or sets the color table, if any. Derived from the root frame.<br/> |
| 72 | + /// The underlying pixel format is represented by <see cref="Bgr24"/>. |
| 73 | + /// </summary> |
| 74 | + public ReadOnlyMemory<Color>? ColorTable { get; set; } |
| 75 | + |
11 | 76 | /// <inheritdoc/>
|
12 |
| - public CurMetadata DeepClone() => new(); |
| 77 | + public static CurMetadata FromFormatConnectingMetadata(FormatConnectingMetadata metadata) |
| 78 | + { |
| 79 | + int bpp = metadata.PixelTypeInfo.BitsPerPixel; |
| 80 | + BmpBitsPerPixel bbpp = bpp switch |
| 81 | + { |
| 82 | + 1 => BmpBitsPerPixel.Bit1, |
| 83 | + 2 => BmpBitsPerPixel.Bit2, |
| 84 | + <= 4 => BmpBitsPerPixel.Bit4, |
| 85 | + <= 8 => BmpBitsPerPixel.Bit8, |
| 86 | + <= 16 => BmpBitsPerPixel.Bit16, |
| 87 | + <= 24 => BmpBitsPerPixel.Bit24, |
| 88 | + _ => BmpBitsPerPixel.Bit32 |
| 89 | + }; |
| 90 | + |
| 91 | + IconFrameCompression compression = IconFrameCompression.Bmp; |
| 92 | + if (bbpp is BmpBitsPerPixel.Bit32) |
| 93 | + { |
| 94 | + compression = IconFrameCompression.Png; |
| 95 | + } |
| 96 | + |
| 97 | + return new CurMetadata |
| 98 | + { |
| 99 | + BmpBitsPerPixel = bbpp, |
| 100 | + Compression = compression, |
| 101 | + ColorTable = compression == IconFrameCompression.Bmp ? metadata.ColorTable : null |
| 102 | + }; |
| 103 | + } |
| 104 | + |
| 105 | + /// <inheritdoc/> |
| 106 | + public PixelTypeInfo GetPixelTypeInfo() |
| 107 | + { |
| 108 | + int bpp = (int)this.BmpBitsPerPixel; |
| 109 | + PixelComponentInfo info; |
| 110 | + PixelColorType color; |
| 111 | + PixelAlphaRepresentation alpha = PixelAlphaRepresentation.None; |
| 112 | + |
| 113 | + if (this.Compression is IconFrameCompression.Png) |
| 114 | + { |
| 115 | + bpp = 32; |
| 116 | + info = PixelComponentInfo.Create(4, bpp, 8, 8, 8, 8); |
| 117 | + color = PixelColorType.RGB | PixelColorType.Alpha; |
| 118 | + alpha = PixelAlphaRepresentation.Unassociated; |
| 119 | + } |
| 120 | + else |
| 121 | + { |
| 122 | + switch (this.BmpBitsPerPixel) |
| 123 | + { |
| 124 | + case BmpBitsPerPixel.Bit1: |
| 125 | + info = PixelComponentInfo.Create(1, bpp, 1); |
| 126 | + color = PixelColorType.Binary; |
| 127 | + break; |
| 128 | + case BmpBitsPerPixel.Bit2: |
| 129 | + info = PixelComponentInfo.Create(1, bpp, 2); |
| 130 | + color = PixelColorType.Indexed; |
| 131 | + break; |
| 132 | + case BmpBitsPerPixel.Bit4: |
| 133 | + info = PixelComponentInfo.Create(1, bpp, 4); |
| 134 | + color = PixelColorType.Indexed; |
| 135 | + break; |
| 136 | + case BmpBitsPerPixel.Bit8: |
| 137 | + info = PixelComponentInfo.Create(1, bpp, 8); |
| 138 | + color = PixelColorType.Indexed; |
| 139 | + break; |
| 140 | + |
| 141 | + // Could be 555 with padding but 565 is more common in newer bitmaps and offers |
| 142 | + // greater accuracy due to extra green precision. |
| 143 | + case BmpBitsPerPixel.Bit16: |
| 144 | + info = PixelComponentInfo.Create(3, bpp, 5, 6, 5); |
| 145 | + color = PixelColorType.RGB; |
| 146 | + break; |
| 147 | + case BmpBitsPerPixel.Bit24: |
| 148 | + info = PixelComponentInfo.Create(3, bpp, 8, 8, 8); |
| 149 | + color = PixelColorType.RGB; |
| 150 | + break; |
| 151 | + case BmpBitsPerPixel.Bit32 or _: |
| 152 | + info = PixelComponentInfo.Create(4, bpp, 8, 8, 8, 8); |
| 153 | + color = PixelColorType.RGB | PixelColorType.Alpha; |
| 154 | + alpha = PixelAlphaRepresentation.Unassociated; |
| 155 | + break; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + return new PixelTypeInfo(bpp) |
| 160 | + { |
| 161 | + AlphaRepresentation = alpha, |
| 162 | + ComponentInfo = info, |
| 163 | + ColorType = color |
| 164 | + }; |
| 165 | + } |
| 166 | + |
| 167 | + /// <inheritdoc/> |
| 168 | + public FormatConnectingMetadata ToFormatConnectingMetadata() |
| 169 | + => new() |
| 170 | + { |
| 171 | + EncodingType = this.Compression == IconFrameCompression.Bmp && this.BmpBitsPerPixel <= BmpBitsPerPixel.Bit8 |
| 172 | + ? EncodingType.Lossy |
| 173 | + : EncodingType.Lossless, |
| 174 | + PixelTypeInfo = this.GetPixelTypeInfo(), |
| 175 | + ColorTable = this.ColorTable |
| 176 | + }; |
13 | 177 |
|
14 | 178 | /// <inheritdoc/>
|
15 | 179 | IDeepCloneable IDeepCloneable.DeepClone() => this.DeepClone();
|
| 180 | + |
| 181 | + /// <inheritdoc/> |
| 182 | + public CurMetadata DeepClone() => new(this); |
16 | 183 | }
|
0 commit comments