@@ -8,7 +8,7 @@ namespace SixLabors.ImageSharp.Formats.Gif;
88/// <summary>
99/// Provides Gif specific metadata information for the image.
1010/// </summary>
11- public class GifMetadata : IDeepCloneable
11+ public class GifMetadata : IFormatMetadata < GifMetadata >
1212{
1313 /// <summary>
1414 /// Initializes a new instance of the <see cref="GifMetadata"/> class.
@@ -49,7 +49,7 @@ private GifMetadata(GifMetadata other)
4949 /// <summary>
5050 /// Gets or sets the color table mode.
5151 /// </summary>
52- public GifColorTableMode ColorTableMode { get ; set ; }
52+ public FrameColorTableMode ColorTableMode { get ; set ; }
5353
5454 /// <summary>
5555 /// Gets or sets the global color table, if any.
@@ -69,9 +69,6 @@ private GifMetadata(GifMetadata other)
6969 /// </summary>
7070 public IList < string > Comments { get ; set ; } = [ ] ;
7171
72- /// <inheritdoc/>
73- public IDeepCloneable DeepClone ( ) => new GifMetadata ( this ) ;
74-
7572 internal static GifMetadata FromAnimatedMetadata ( AnimatedImageMetadata metadata )
7673 {
7774 int index = 0 ;
@@ -92,9 +89,64 @@ internal static GifMetadata FromAnimatedMetadata(AnimatedImageMetadata metadata)
9289 return new ( )
9390 {
9491 GlobalColorTable = metadata . ColorTable ,
95- ColorTableMode = metadata . ColorTableMode == FrameColorTableMode . Global ? GifColorTableMode . Global : GifColorTableMode . Local ,
92+ ColorTableMode = metadata . ColorTableMode ,
93+ RepeatCount = metadata . RepeatCount ,
94+ BackgroundColorIndex = ( byte ) Numerics . Clamp ( index , 0 , 255 ) ,
95+ } ;
96+ }
97+
98+ /// <inheritdoc/>
99+ public static GifMetadata FromFormatConnectingMetadata ( FormatConnectingMetadata metadata )
100+ {
101+ int index = 0 ;
102+ Color background = metadata . BackgroundColor ;
103+ if ( metadata . ColorTable . HasValue )
104+ {
105+ ReadOnlySpan < Color > colorTable = metadata . ColorTable . Value . Span ;
106+ for ( int i = 0 ; i < colorTable . Length ; i ++ )
107+ {
108+ if ( background == colorTable [ i ] )
109+ {
110+ index = i ;
111+ break ;
112+ }
113+ }
114+ }
115+
116+ return new ( )
117+ {
118+ GlobalColorTable = metadata . ColorTable ,
119+ ColorTableMode = metadata . ColorTableMode ,
96120 RepeatCount = metadata . RepeatCount ,
97121 BackgroundColorIndex = ( byte ) Numerics . Clamp ( index , 0 , 255 ) ,
98122 } ;
99123 }
124+
125+ /// <inheritdoc/>
126+ public FormatConnectingMetadata ToFormatConnectingMetadata ( )
127+ {
128+ Color color = this . GlobalColorTable . HasValue && this . GlobalColorTable . Value . Span . Length > this . BackgroundColorIndex
129+ ? this . GlobalColorTable . Value . Span [ this . BackgroundColorIndex ]
130+ : Color . Transparent ;
131+
132+ return new ( )
133+ {
134+ AnimateRootFrame = true ,
135+ BackgroundColor = color ,
136+ ColorTable = this . GlobalColorTable ,
137+ ColorTableMode = this . ColorTableMode ,
138+ PixelTypeInfo = new PixelTypeInfo ( 24 )
139+ {
140+ ColorType = PixelColorType . Indexed ,
141+ ComponentInfo = PixelComponentInfo . Create ( 3 , 24 , 8 , 8 , 8 ) ,
142+ } ,
143+ RepeatCount = this . RepeatCount ,
144+ } ;
145+ }
146+
147+ /// <inheritdoc/>
148+ public IDeepCloneable DeepClone ( ) => ( ( IDeepCloneable < GifMetadata > ) this ) . DeepClone ( ) ;
149+
150+ /// <inheritdoc/>
151+ GifMetadata IDeepCloneable < GifMetadata > . DeepClone ( ) => new ( this ) ;
100152}
0 commit comments