Skip to content

Commit 9a0e289

Browse files
Complete conversion APIs
1 parent baaf29c commit 9a0e289

54 files changed

Lines changed: 606 additions & 874 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Buffers;
55
using System.Buffers.Binary;
66
using System.Runtime.InteropServices;
7-
using SixLabors.ImageSharp.Advanced;
87
using SixLabors.ImageSharp.Common.Helpers;
98
using SixLabors.ImageSharp.Memory;
109
using SixLabors.ImageSharp.Metadata;

src/ImageSharp/Formats/Bmp/BmpMetadata.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp;
99
/// <summary>
1010
/// Provides Bmp specific metadata information for the image.
1111
/// </summary>
12-
public class BmpMetadata : IFormatMetadata<BmpMetadata>, IFormatFrameMetadata<BmpMetadata>
12+
public class BmpMetadata : IFormatMetadata<BmpMetadata>
1313
{
1414
/// <summary>
1515
/// Initializes a new instance of the <see cref="BmpMetadata"/> class.
@@ -66,10 +66,6 @@ public static BmpMetadata FromFormatConnectingMetadata(FormatConnectingMetadata
6666
};
6767
}
6868

69-
/// <inheritdoc/>
70-
public static BmpMetadata FromFormatConnectingFrameMetadata(FormatConnectingFrameMetadata metadata)
71-
=> new();
72-
7369
/// <inheritdoc/>
7470
public PixelTypeInfo GetPixelTypeInfo()
7571
{
@@ -140,10 +136,6 @@ public FormatConnectingMetadata ToFormatConnectingMetadata()
140136
PixelTypeInfo = this.GetPixelTypeInfo()
141137
};
142138

143-
/// <inheritdoc/>
144-
public FormatConnectingFrameMetadata ToFormatConnectingFrameMetadata()
145-
=> new();
146-
147139
/// <inheritdoc/>
148140
IDeepCloneable IDeepCloneable.DeepClone() => this.DeepClone();
149141

src/ImageSharp/Formats/Bmp/MetadataExtensions.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
namespace SixLabors.ImageSharp.Formats;
5+
6+
/// <summary>
7+
/// Provides a way to specify the type of encoding to be used.
8+
/// </summary>
9+
public enum EncodingType
10+
{
11+
/// <summary>
12+
/// Lossless encoding, which compresses data without any loss of information.
13+
/// </summary>
14+
Lossless,
15+
16+
/// <summary>
17+
/// Lossy encoding, which compresses data by discarding some of it.
18+
/// </summary>
19+
Lossy
20+
}

src/ImageSharp/Formats/FormatConnectingMetadata.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ namespace SixLabors.ImageSharp.Formats;
1111
public class FormatConnectingMetadata
1212
{
1313
/// <summary>
14-
/// Gets the quality.
14+
/// Gets the encoding type.
15+
/// </summary>
16+
public EncodingType EncodingType { get; init; }
17+
18+
/// <summary>
19+
/// Gets the quality to use when <see cref="EncodingType"/> is <see cref="EncodingType.Lossy"/>.
1520
/// </summary>
1621
/// <remarks>
1722
/// The value is usually between 1 and 100. Defaults to 100.

src/ImageSharp/Formats/Gif/GifEncoderCore.cs

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
using System.Numerics;
66
using System.Runtime.CompilerServices;
77
using SixLabors.ImageSharp.Advanced;
8-
using SixLabors.ImageSharp.Formats.Png;
9-
using SixLabors.ImageSharp.Formats.Webp;
108
using SixLabors.ImageSharp.Memory;
119
using SixLabors.ImageSharp.Metadata;
1210
using SixLabors.ImageSharp.Metadata.Profiles.Xmp;
@@ -85,7 +83,7 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
8583
Guard.NotNull(image, nameof(image));
8684
Guard.NotNull(stream, nameof(stream));
8785

88-
GifMetadata gifMetadata = GetGifMetadata(image);
86+
GifMetadata gifMetadata = image.Metadata.CloneGifMetadata();
8987
this.colorTableMode ??= gifMetadata.ColorTableMode;
9088
bool useGlobalTable = this.colorTableMode == FrameColorTableMode.Global;
9189

@@ -178,56 +176,17 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
178176
quantized?.Dispose();
179177
}
180178

181-
private static GifMetadata GetGifMetadata<TPixel>(Image<TPixel> image)
182-
where TPixel : unmanaged, IPixel<TPixel>
183-
{
184-
if (image.Metadata.TryGetGifMetadata(out GifMetadata? gif))
185-
{
186-
return gif.DeepClone();
187-
}
188-
189-
if (image.Metadata.TryGetPngMetadata(out PngMetadata? png))
190-
{
191-
AnimatedImageMetadata ani = png.ToAnimatedImageMetadata();
192-
return GifMetadata.FromAnimatedMetadata(ani);
193-
}
194-
195-
if (image.Metadata.TryGetWebpMetadata(out WebpMetadata? webp))
196-
{
197-
AnimatedImageMetadata ani = webp.ToAnimatedImageMetadata();
198-
return GifMetadata.FromAnimatedMetadata(ani);
199-
}
200-
201-
// Return explicit new instance so we do not mutate the original metadata.
202-
return new();
203-
}
204-
205179
private static GifFrameMetadata GetGifFrameMetadata<TPixel>(ImageFrame<TPixel> frame, int transparencyIndex)
206180
where TPixel : unmanaged, IPixel<TPixel>
207181
{
208-
GifFrameMetadata? metadata = null;
209-
if (frame.Metadata.TryGetGifMetadata(out GifFrameMetadata? gif))
210-
{
211-
metadata = gif.DeepClone();
212-
}
213-
else if (frame.Metadata.TryGetPngMetadata(out PngFrameMetadata? png))
214-
{
215-
AnimatedImageFrameMetadata ani = png.ToAnimatedImageFrameMetadata();
216-
metadata = GifFrameMetadata.FromAnimatedMetadata(ani);
217-
}
218-
else if (frame.Metadata.TryGetWebpFrameMetadata(out WebpFrameMetadata? webp))
219-
{
220-
AnimatedImageFrameMetadata ani = webp.ToAnimatedImageFrameMetadata();
221-
metadata = GifFrameMetadata.FromAnimatedMetadata(ani);
222-
}
223-
224-
if (metadata?.ColorTableMode == FrameColorTableMode.Global && transparencyIndex > -1)
182+
GifFrameMetadata metadata = frame.Metadata.CloneGifMetadata();
183+
if (metadata.ColorTableMode == FrameColorTableMode.Global && transparencyIndex > -1)
225184
{
226185
metadata.HasTransparency = true;
227186
metadata.TransparencyIndex = ClampIndex(transparencyIndex);
228187
}
229188

230-
return metadata ?? new();
189+
return metadata;
231190
}
232191

233192
private void EncodeAdditionalFrames<TPixel>(

src/ImageSharp/Formats/Gif/GifMetadata.cs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -69,34 +69,6 @@ private GifMetadata(GifMetadata other)
6969
/// </summary>
7070
public IList<string> Comments { get; set; } = [];
7171

72-
internal static GifMetadata FromAnimatedMetadata(AnimatedImageMetadata metadata)
73-
{
74-
int index = 0;
75-
Color background = metadata.BackgroundColor;
76-
if (metadata.ColorTable.HasValue)
77-
{
78-
ReadOnlySpan<Color> colorTable = metadata.ColorTable.Value.Span;
79-
for (int i = 0; i < colorTable.Length; i++)
80-
{
81-
if (background != colorTable[i])
82-
{
83-
continue;
84-
}
85-
86-
index = i;
87-
break;
88-
}
89-
}
90-
91-
return new()
92-
{
93-
GlobalColorTable = metadata.ColorTable,
94-
ColorTableMode = metadata.ColorTableMode,
95-
RepeatCount = metadata.RepeatCount,
96-
BackgroundColorIndex = (byte)Numerics.Clamp(index, 0, 255),
97-
};
98-
}
99-
10072
/// <inheritdoc/>
10173
public static GifMetadata FromFormatConnectingMetadata(FormatConnectingMetadata metadata)
10274
{

src/ImageSharp/Formats/Gif/MetadataExtensions.cs

Lines changed: 0 additions & 97 deletions
This file was deleted.

src/ImageSharp/Formats/IFormatFrameMetadata.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public interface IFormatFrameMetadata : IDeepCloneable
2020
/// </summary>
2121
/// <typeparam name="TSelf">The metadata type implementing this interface.</typeparam>
2222
public interface IFormatFrameMetadata<TSelf> : IFormatFrameMetadata, IDeepCloneable<TSelf>
23-
where TSelf : class, IFormatFrameMetadata, new()
23+
where TSelf : class, IFormatFrameMetadata
2424
{
2525
/// <summary>
2626
/// Creates a new instance of the <typeparamref name="TSelf"/> class from the given <see cref="FormatConnectingFrameMetadata"/>.

src/ImageSharp/Formats/IFormatMetadata.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public interface IFormatMetadata : IDeepCloneable
2828
/// </summary>
2929
/// <typeparam name="TSelf">The metadata type implementing this interface.</typeparam>
3030
public interface IFormatMetadata<TSelf> : IFormatMetadata, IDeepCloneable<TSelf>
31-
where TSelf : class, IFormatMetadata, new()
31+
where TSelf : class, IFormatMetadata
3232
{
3333
/// <summary>
3434
/// Creates a new instance of the <typeparamref name="TSelf"/> class from the given <see cref="FormatConnectingMetadata"/>.

0 commit comments

Comments
 (0)