revert force frames size · SixLabors/ImageSharp@fd80365 · GitHub
Skip to content

Commit fd80365

Browse files
committed
revert force frames size
1 parent 7e76f6d commit fd80365

21 files changed

Lines changed: 149 additions & 159 deletions

‎src/ImageSharp/Configuration.cs‎

Lines changed: 3 additions & 3 deletions

src/ImageSharp/Formats/Icon/IconAssertHelper.cs renamed to src/ImageSharp/Formats/Ico/IcoAssertHelper.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3+
34
using SixLabors.ImageSharp.Formats.Bmp;
45

5-
namespace SixLabors.ImageSharp.Formats.Icon;
6+
namespace SixLabors.ImageSharp.Formats.Ico;
67

7-
internal class IconAssertHelper
8+
internal class IcoAssertHelper
89
{
910
internal static int EndOfStream(int v, int length)
1011
{

src/ImageSharp/Formats/Icon/IconConfigurationModule.cs renamed to src/ImageSharp/Formats/Ico/IcoConfigurationModule.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4-
namespace SixLabors.ImageSharp.Formats.Icon;
4+
namespace SixLabors.ImageSharp.Formats.Ico;
55

66
/// <summary>
77
/// Registers the image encoders, decoders and mime type detectors for the Ico format.
88
/// </summary>
9-
public sealed class IconConfigurationModule : IImageFormatConfigurationModule
9+
public sealed class IcoConfigurationModule : IImageFormatConfigurationModule
1010
{
1111
/// <inheritdoc/>
1212
public void Configure(Configuration configuration)
1313
{
1414
// TODO: IcoEncoder
1515
// configuration.ImageFormatsManager.SetEncoder(IcoFormat.Instance, new IcoEncoder());
16-
configuration.ImageFormatsManager.SetDecoder(IconFormat.Instance, IconDecoder.Instance);
16+
configuration.ImageFormatsManager.SetDecoder(IcoFormat.Instance, IcoDecoder.Instance);
1717

1818
// ICO(CUR) File haven't Magic Code.
1919
// configuration.ImageFormatsManager.AddImageFormatDetector(new IcoImageFormatDetector());

src/ImageSharp/Formats/Icon/IconConstants.cs renamed to src/ImageSharp/Formats/Ico/IcoConstants.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4-
namespace SixLabors.ImageSharp.Formats.Icon;
4+
namespace SixLabors.ImageSharp.Formats.Ico;
55

66
/// <summary>
77
/// Defines constants relating to ICOs
88
/// </summary>
9-
internal static class IconConstants
9+
internal static class IcoConstants
1010
{
1111
/// <summary>
1212
/// The list of mimetypes that equate to a ico.

src/ImageSharp/Formats/Icon/IconDecoder.cs renamed to src/ImageSharp/Formats/Ico/IcoDecoder.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,29 @@
33

44
using SixLabors.ImageSharp.PixelFormats;
55

6-
namespace SixLabors.ImageSharp.Formats.Icon;
6+
namespace SixLabors.ImageSharp.Formats.Ico;
77

88
/// <summary>
99
/// Decoder for generating an image out of a ico/cur encoded stream.
1010
/// </summary>
11-
public sealed class IconDecoder : ImageDecoder
11+
public sealed class IcoDecoder : ImageDecoder
1212
{
13-
private IconDecoder()
13+
private IcoDecoder()
1414
{
1515
}
1616

1717
/// <summary>
1818
/// Gets the shared instance.
1919
/// </summary>
20-
public static IconDecoder Instance { get; } = new();
20+
public static IcoDecoder Instance { get; } = new();
2121

2222
/// <inheritdoc/>
2323
protected override Image<TPixel> Decode<TPixel>(DecoderOptions options, Stream stream, CancellationToken cancellationToken)
2424
{
2525
Guard.NotNull(options, nameof(options));
2626
Guard.NotNull(stream, nameof(stream));
2727

28-
Image<TPixel> image = new IconDecoderCore(options).Decode<TPixel>(options.Configuration, stream, cancellationToken);
28+
Image<TPixel> image = new IcoDecoderCore(options).Decode<TPixel>(options.Configuration, stream, cancellationToken);
2929

3030
ScaleToTargetSize(options, image);
3131

@@ -42,6 +42,6 @@ protected override ImageInfo Identify(DecoderOptions options, Stream stream, Can
4242
Guard.NotNull(options, nameof(options));
4343
Guard.NotNull(stream, nameof(stream));
4444

45-
return new IconDecoderCore(options).Identify(options.Configuration, stream, cancellationToken);
45+
return new IcoDecoderCore(options).Identify(options.Configuration, stream, cancellationToken);
4646
}
4747
}

src/ImageSharp/Formats/Icon/IconDecoderCore.cs renamed to src/ImageSharp/Formats/Ico/IcoDecoderCore.cs

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,65 @@
55
using SixLabors.ImageSharp.IO;
66
using SixLabors.ImageSharp.Metadata;
77
using SixLabors.ImageSharp.PixelFormats;
8+
using SixLabors.ImageSharp.Processing;
89

9-
namespace SixLabors.ImageSharp.Formats.Icon;
10+
namespace SixLabors.ImageSharp.Formats.Ico;
1011

11-
internal sealed class IconDecoderCore : IImageDecoderInternals
12+
internal sealed class IcoDecoderCore : IImageDecoderInternals
1213
{
13-
private IconDir iconDir;
14-
private IconDirEntry[] iconDirEntries = Array.Empty<IconDirEntry>();
14+
private IcoDir icoDir;
15+
private IcoDirEntry[] icoDirEntries = Array.Empty<IcoDirEntry>();
1516
private readonly WeakReference<Png.PngDecoderCore?> pngDecoder = new(default);
1617
private readonly WeakReference<Bmp.BmpDecoderCore?> bmpDecoder = new(default);
1718

18-
public IconDecoderCore(DecoderOptions options) => this.Options = options;
19+
public IcoDecoderCore(DecoderOptions options) => this.Options = options;
1920

2021
public DecoderOptions Options { get; }
2122

22-
public Size Dimensions => new(
23-
this.iconDirEntries.Select(i => i.Width).Max(),
24-
this.iconDirEntries.Select(i => i.Height).Max());
23+
public Size Dimensions { get; private set; }
2524

2625
public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken cancellationToken)
2726
where TPixel : unmanaged, IPixel<TPixel>
2827
{
29-
ReadHeader(stream, out this.iconDir, out this.iconDirEntries);
28+
ReadHeader(stream, out this.icoDir, out this.icoDirEntries);
29+
this.Dimensions = new(
30+
this.icoDirEntries.Max(i => i.Width),
31+
this.icoDirEntries.Max(i => i.Height));
3032

3133
byte[] buffer = new byte[4096];
3234
Span<byte> flag = stackalloc byte[Png.PngConstants.HeaderBytes.Length];
3335

34-
ImageFrame<TPixel>[] frames = new ImageFrame<TPixel>[this.iconDirEntries.Length];
35-
for (int i = 0; i < this.iconDirEntries.Length; i++)
36+
Image<TPixel> image = new(this.Options.Configuration, this.Dimensions.Width, this.Dimensions.Height);
37+
image.Metadata.GetIcoMetadata().Set(this.icoDir);
38+
39+
for (int i = 0; i < this.icoDirEntries.Length; i++)
3640
{
37-
_ = stream.Seek(this.iconDirEntries[i].ImageOffset, SeekOrigin.Begin);
41+
_ = stream.Seek(this.icoDirEntries[i].ImageOffset, SeekOrigin.Begin);
3842

3943
int size = stream.Read(flag);
4044
_ = stream.Seek(-size, SeekOrigin.Current);
4145

4246
bool isPng = flag.SequenceEqual(Png.PngConstants.HeaderBytes);
4347

44-
Debug.WriteLine($"#{i}:\t{(isPng ? "PNG" : "BMP")}");
48+
Debug.WriteLine($"#{i}:\t" + (isPng ? "PNG" : "BMP"));
4549
using Image<TPixel> img = this.ReadImage<TPixel>(
4650
stream,
4751
isPng,
48-
(int)this.iconDirEntries[i].BytesInRes,
52+
(int)this.icoDirEntries[i].BytesInRes,
4953
buffer,
5054
cancellationToken);
5155

52-
frames[i] = img.Frames.RootFrameUnsafe.Clone();
53-
IconFrameMetadata iconFrameMetadata = frames[i].Metadata.GetIcoMetadata();
54-
iconFrameMetadata.Set(this.iconDirEntries[i]);
56+
// TODO: Any way to read the frame more efficiently?
57+
Size imgSize = img.Size;
58+
img.Mutate(x => x.Resize(new ResizeOptions
59+
{
60+
Size = this.Dimensions,
61+
Mode = ResizeMode.Manual,
62+
TargetRectangle = new Rectangle(Point.Empty, imgSize)
63+
}));
64+
ImageFrame<TPixel> newFrame = image.Frames.AddFrame(img.Frames.RootFrame);
65+
IcoFrameMetadata iconFrameMetadata = newFrame.Metadata.GetIcoMetadata();
66+
iconFrameMetadata.SetMetadata(this.icoDirEntries[i]);
5567

5668
if (isPng)
5769
{
@@ -63,41 +75,32 @@ public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken
6375
}
6476
}
6577

66-
ImageMetadata metadata = new();
67-
metadata.GetIcoMetadata().Set(this.iconDir);
68-
69-
Image<TPixel> image = new(
70-
this.Options.Configuration,
71-
metadata,
72-
frames,
73-
false);
74-
7578
return image;
7679
}
7780

7881
public ImageInfo Identify(BufferedReadStream stream, CancellationToken cancellationToken)
7982
{
80-
ReadHeader(stream, out this.iconDir, out this.iconDirEntries);
83+
ReadHeader(stream, out this.icoDir, out this.icoDirEntries);
8184
ImageMetadata metadata = new();
82-
ImageFrameMetadata[] frames = new ImageFrameMetadata[this.iconDir.Count];
83-
metadata.GetIcoMetadata().Set(this.iconDir);
85+
ImageFrameMetadata[] frames = new ImageFrameMetadata[this.icoDir.Count];
86+
metadata.GetIcoMetadata().Set(this.icoDir);
8487
for (int i = 0; i < frames.Length; i++)
8588
{
8689
frames[i] = new();
87-
IconFrameMetadata icoFrameMetadata = frames[i].GetIcoMetadata();
88-
icoFrameMetadata.Set(this.iconDirEntries[i]);
90+
IcoFrameMetadata icoFrameMetadata = frames[i].GetIcoMetadata();
91+
icoFrameMetadata.SetMetadata(this.icoDirEntries[i]);
8992
}
9093

9194
return new(new(32), new(0), metadata, frames);
9295
}
9396

94-
private static void ReadHeader(Stream stream, out IconDir iconDir, out IconDirEntry[] iconDirEntries)
97+
private static void ReadHeader(Stream stream, out IcoDir iconDir, out IcoDirEntry[] iconDirEntries)
9598
{
96-
_ = Read(stream, out iconDir, IconDir.Size);
97-
iconDirEntries = new IconDirEntry[iconDir.Count];
99+
_ = Read(stream, out iconDir, IcoDir.Size);
100+
iconDirEntries = new IcoDirEntry[iconDir.Count];
98101
for (int i = 0; i < iconDirEntries.Length; i++)
99102
{
100-
_ = Read(stream, out iconDirEntries[i], IconDirEntry.Size);
103+
_ = Read(stream, out iconDirEntries[i], IcoDirEntry.Size);
101104
}
102105
}
103106

@@ -121,7 +124,7 @@ private static void StreamClone(Stream src, Stream dst, byte[] buffer, int size)
121124
while (size > 0)
122125
{
123126
int length = Math.Min(buffer.Length, size);
124-
length = IconAssertHelper.EndOfStream(src.Read(buffer, 0, length), length);
127+
length = IcoAssertHelper.EndOfStream(src.Read(buffer, 0, length), length);
125128
size -= length;
126129
dst.Write(buffer, 0, length);
127130
}
@@ -143,10 +146,10 @@ private Image<TPixel> ReadImage<TPixel>(
143146
{
144147
const int headerSize = Bmp.BmpFileHeader.Size + Bmp.BmpInfoHeader.SizeV3;
145148
Span<byte> bytes = stackalloc byte[Bmp.BmpInfoHeader.SizeV3];
146-
_ = IconAssertHelper.EndOfStream(stream.Read(bytes), Bmp.BmpInfoHeader.SizeV3);
149+
_ = IcoAssertHelper.EndOfStream(stream.Read(bytes), Bmp.BmpInfoHeader.SizeV3);
147150

148151
Bmp.BmpInfoHeader infoHeader = Bmp.BmpInfoHeader.ParseV3(bytes);
149-
IconAssertHelper.IsV3Header(infoHeader);
152+
IcoAssertHelper.IsV3Header(infoHeader);
150153
infoHeader.Height >>= 1;
151154

152155
int paletteSize = infoHeader.ClrUsed;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
using System.Runtime.InteropServices;
55

6-
namespace SixLabors.ImageSharp.Formats.Icon;
6+
namespace SixLabors.ImageSharp.Formats.Ico;
77

88
[StructLayout(LayoutKind.Sequential, Pack = 1, Size = Size)]
9-
internal struct IconDir
9+
internal struct IcoDir
1010
{
1111
public const int Size = 3 * sizeof(ushort);
1212
public ushort Reserved;

src/ImageSharp/Formats/Icon/IconDirEntry.cs renamed to src/ImageSharp/Formats/Ico/IcoDirEntry.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
using System.Runtime.InteropServices;
55

6-
namespace SixLabors.ImageSharp.Formats.Icon;
6+
namespace SixLabors.ImageSharp.Formats.Ico;
77

88
[StructLayout(LayoutKind.Sequential, Pack = 1, Size = Size)]
9-
internal struct IconDirEntry
9+
internal struct IcoDirEntry
1010
{
1111
public const int Size = (4 * sizeof(byte)) + (2 * sizeof(ushort)) + (2 * sizeof(uint));
1212

Lines changed: 37 additions & 0 deletions

0 commit comments

Comments
 (0)