Skip to content

Commit 5c28129

Browse files
Merge pull request #2788 from SixLabors/js/multisize-tiff
Allow decoding Tiff of different frame size.
2 parents 7f720db + 7d35113 commit 5c28129

File tree

58 files changed

+699
-447
lines changed

Some content is hidden

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

58 files changed

+699
-447
lines changed

src/ImageSharp/Formats/AnimatedImageFrameMetadata.cs

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

src/ImageSharp/Formats/AnimatedImageMetadata.cs

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

src/ImageSharp/Formats/Bmp/BmpMetadata.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,10 @@ public FormatConnectingMetadata ToFormatConnectingMetadata()
154154

155155
/// <inheritdoc/>
156156
public BmpMetadata DeepClone() => new(this);
157+
158+
/// <inheritdoc/>
159+
public void AfterImageApply<TPixel>(Image<TPixel> destination)
160+
where TPixel : unmanaged, IPixel<TPixel>
161+
{
162+
}
157163
}

src/ImageSharp/Formats/Cur/CurDecoderCore.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ protected override void SetFrameMetadata(
3535
curMetadata.Compression = compression;
3636
curMetadata.BmpBitsPerPixel = bitsPerPixel;
3737
curMetadata.ColorTable = colorTable;
38-
curMetadata.EncodingWidth = curFrameMetadata.EncodingWidth;
39-
curMetadata.EncodingHeight = curFrameMetadata.EncodingHeight;
40-
curMetadata.HotspotX = curFrameMetadata.HotspotX;
41-
curMetadata.HotspotY = curFrameMetadata.HotspotY;
4238
}
4339
}
4440
}

src/ImageSharp/Formats/Cur/CurFrameMetadata.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,16 @@ public FormatConnectingFrameMetadata ToFormatConnectingFrameMetadata()
132132
EncodingHeight = this.EncodingHeight
133133
};
134134

135+
/// <inheritdoc/>
136+
public void AfterFrameApply<TPixel>(ImageFrame<TPixel> source, ImageFrame<TPixel> destination)
137+
where TPixel : unmanaged, IPixel<TPixel>
138+
{
139+
float ratioX = destination.Width / (float)source.Width;
140+
float ratioY = destination.Height / (float)source.Height;
141+
this.EncodingWidth = Scale(this.EncodingWidth, destination.Width, ratioX);
142+
this.EncodingHeight = Scale(this.EncodingHeight, destination.Height, ratioY);
143+
}
144+
135145
/// <inheritdoc/>
136146
IDeepCloneable IDeepCloneable.DeepClone() => this.DeepClone();
137147

@@ -222,4 +232,14 @@ private PixelTypeInfo GetPixelTypeInfo()
222232
ColorType = color
223233
};
224234
}
235+
236+
private static byte Scale(byte? value, int destination, float ratio)
237+
{
238+
if (value is null)
239+
{
240+
return (byte)Math.Clamp(destination, 0, 255);
241+
}
242+
243+
return Math.Min((byte)MathF.Ceiling(value.Value * ratio), (byte)Math.Clamp(destination, 0, 255));
244+
}
225245
}

src/ImageSharp/Formats/Cur/CurMetadata.cs

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ public CurMetadata()
2222
private CurMetadata(CurMetadata other)
2323
{
2424
this.Compression = other.Compression;
25-
this.HotspotX = other.HotspotX;
26-
this.HotspotY = other.HotspotY;
27-
this.EncodingWidth = other.EncodingWidth;
28-
this.EncodingHeight = other.EncodingHeight;
2925
this.BmpBitsPerPixel = other.BmpBitsPerPixel;
3026

3127
if (other.ColorTable?.Length > 0)
@@ -39,28 +35,6 @@ private CurMetadata(CurMetadata other)
3935
/// </summary>
4036
public IconFrameCompression Compression { get; set; }
4137

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-
6438
/// <summary>
6539
/// Gets or sets the number of bits per pixel.<br/>
6640
/// Used when <see cref="Compression"/> is <see cref="IconFrameCompression.Bmp"/>
@@ -175,6 +149,12 @@ public FormatConnectingMetadata ToFormatConnectingMetadata()
175149
ColorTable = this.ColorTable
176150
};
177151

152+
/// <inheritdoc/>
153+
public void AfterImageApply<TPixel>(Image<TPixel> destination)
154+
where TPixel : unmanaged, IPixel<TPixel>
155+
{
156+
}
157+
178158
/// <inheritdoc/>
179159
IDeepCloneable IDeepCloneable.DeepClone() => this.DeepClone();
180160

src/ImageSharp/Formats/Gif/GifEncoderCore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ private void EncodeAdditionalFrames<TPixel>(
209209
ImageFrame<TPixel> previousFrame = image.Frames.RootFrame;
210210

211211
// This frame is reused to store de-duplicated pixel buffers.
212-
using ImageFrame<TPixel> encodingFrame = new(previousFrame.Configuration, previousFrame.Size());
212+
using ImageFrame<TPixel> encodingFrame = new(previousFrame.Configuration, previousFrame.Size);
213213

214214
for (int i = 1; i < image.Frames.Count; i++)
215215
{

src/ImageSharp/Formats/Gif/GifFrameMetadata.cs

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -126,40 +126,15 @@ public FormatConnectingFrameMetadata ToFormatConnectingFrameMetadata()
126126
};
127127
}
128128

129+
/// <inheritdoc/>
130+
public void AfterFrameApply<TPixel>(ImageFrame<TPixel> source, ImageFrame<TPixel> destination)
131+
where TPixel : unmanaged, IPixel<TPixel>
132+
{
133+
}
134+
129135
/// <inheritdoc/>
130136
IDeepCloneable IDeepCloneable.DeepClone() => this.DeepClone();
131137

132138
/// <inheritdoc/>
133139
public GifFrameMetadata DeepClone() => new(this);
134-
135-
internal static GifFrameMetadata FromAnimatedMetadata(AnimatedImageFrameMetadata metadata)
136-
{
137-
// TODO: v4 How do I link the parent metadata to the frame metadata to get the global color table?
138-
int index = -1;
139-
const float background = 1f;
140-
if (metadata.ColorTable.HasValue)
141-
{
142-
ReadOnlySpan<Color> colorTable = metadata.ColorTable.Value.Span;
143-
for (int i = 0; i < colorTable.Length; i++)
144-
{
145-
Vector4 vector = colorTable[i].ToScaledVector4();
146-
if (vector.W < background)
147-
{
148-
index = i;
149-
}
150-
}
151-
}
152-
153-
bool hasTransparency = index >= 0;
154-
155-
return new()
156-
{
157-
LocalColorTable = metadata.ColorTable,
158-
ColorTableMode = metadata.ColorTableMode,
159-
FrameDelay = (int)Math.Round(metadata.Duration.TotalMilliseconds / 10),
160-
DisposalMode = metadata.DisposalMode,
161-
HasTransparency = hasTransparency,
162-
TransparencyIndex = hasTransparency ? unchecked((byte)index) : byte.MinValue,
163-
};
164-
}
165140
}

src/ImageSharp/Formats/Gif/GifMetadata.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ public FormatConnectingMetadata ToFormatConnectingMetadata()
130130
};
131131
}
132132

133+
/// <inheritdoc/>
134+
public void AfterImageApply<TPixel>(Image<TPixel> destination)
135+
where TPixel : unmanaged, IPixel<TPixel>
136+
{
137+
}
138+
133139
/// <inheritdoc/>
134140
IDeepCloneable IDeepCloneable.DeepClone() => this.DeepClone();
135141

src/ImageSharp/Formats/IFormatFrameMetadata.cs

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

4+
using SixLabors.ImageSharp.PixelFormats;
5+
46
namespace SixLabors.ImageSharp.Formats;
57

68
/// <summary>
@@ -13,6 +15,15 @@ public interface IFormatFrameMetadata : IDeepCloneable
1315
/// </summary>
1416
/// <returns>The <see cref="FormatConnectingFrameMetadata"/>.</returns>
1517
FormatConnectingFrameMetadata ToFormatConnectingFrameMetadata();
18+
19+
/// <summary>
20+
/// This method is called after a process has been applied to the image frame.
21+
/// </summary>
22+
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
23+
/// <param name="source">The source image frame.</param>
24+
/// <param name="destination">The destination image frame.</param>
25+
void AfterFrameApply<TPixel>(ImageFrame<TPixel> source, ImageFrame<TPixel> destination)
26+
where TPixel : unmanaged, IPixel<TPixel>;
1627
}
1728

1829
/// <summary>

0 commit comments

Comments
 (0)