Skip to content

Commit 8da27c9

Browse files
Merge pull request #2751 from SixLabors/js/format-conversion
Add API for metadata conversion between formats.
2 parents 8ed06e8 + 3c1c9f3 commit 8da27c9

File tree

179 files changed

+4556
-2501
lines changed

Some content is hidden

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

179 files changed

+4556
-2501
lines changed

src/ImageSharp/Advanced/AotCompilerTools.cs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010
using SixLabors.ImageSharp.Formats.Gif;
1111
using SixLabors.ImageSharp.Formats.Jpeg;
1212
using SixLabors.ImageSharp.Formats.Jpeg.Components;
13+
using SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder;
1314
using SixLabors.ImageSharp.Formats.Pbm;
1415
using SixLabors.ImageSharp.Formats.Png;
16+
using SixLabors.ImageSharp.Formats.Qoi;
1517
using SixLabors.ImageSharp.Formats.Tga;
1618
using SixLabors.ImageSharp.Formats.Tiff;
19+
using SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors;
1720
using SixLabors.ImageSharp.Formats.Webp;
1821
using SixLabors.ImageSharp.Memory;
1922
using SixLabors.ImageSharp.PixelFormats;
@@ -129,6 +132,7 @@ private static void Seed<TPixel>()
129132
AotCompileImageDecoderInternals<TPixel>();
130133
AotCompileImageEncoders<TPixel>();
131134
AotCompileImageDecoders<TPixel>();
135+
AotCompileSpectralConverter<TPixel>();
132136
AotCompileImageProcessors<TPixel>();
133137
AotCompileGenericImageProcessors<TPixel>();
134138
AotCompileResamplers<TPixel>();
@@ -195,39 +199,41 @@ private static void AotCompileImageProcessingContextFactory<TPixel>()
195199
=> default(DefaultImageOperationsProviderFactory).CreateImageProcessingContext<TPixel>(default, default, default);
196200

197201
/// <summary>
198-
/// This method pre-seeds the all <see cref="IImageEncoderInternals"/> in the AoT compiler.
202+
/// This method pre-seeds the all core encoders in the AoT compiler.
199203
/// </summary>
200204
/// <typeparam name="TPixel">The pixel format.</typeparam>
201205
[Preserve]
202206
private static void AotCompileImageEncoderInternals<TPixel>()
203207
where TPixel : unmanaged, IPixel<TPixel>
204208
{
205-
default(WebpEncoderCore).Encode<TPixel>(default, default, default);
206209
default(BmpEncoderCore).Encode<TPixel>(default, default, default);
207210
default(GifEncoderCore).Encode<TPixel>(default, default, default);
208211
default(JpegEncoderCore).Encode<TPixel>(default, default, default);
209212
default(PbmEncoderCore).Encode<TPixel>(default, default, default);
210213
default(PngEncoderCore).Encode<TPixel>(default, default, default);
214+
default(QoiEncoderCore).Encode<TPixel>(default, default, default);
211215
default(TgaEncoderCore).Encode<TPixel>(default, default, default);
212216
default(TiffEncoderCore).Encode<TPixel>(default, default, default);
217+
default(WebpEncoderCore).Encode<TPixel>(default, default, default);
213218
}
214219

215220
/// <summary>
216-
/// This method pre-seeds the all <see cref="IImageDecoderInternals"/> in the AoT compiler.
221+
/// This method pre-seeds the all <see cref="ImageDecoderCore"/> in the AoT compiler.
217222
/// </summary>
218223
/// <typeparam name="TPixel">The pixel format.</typeparam>
219224
[Preserve]
220225
private static void AotCompileImageDecoderInternals<TPixel>()
221226
where TPixel : unmanaged, IPixel<TPixel>
222227
{
223-
default(WebpDecoderCore).Decode<TPixel>(default, default);
224-
default(BmpDecoderCore).Decode<TPixel>(default, default);
225-
default(GifDecoderCore).Decode<TPixel>(default, default);
226-
default(JpegDecoderCore).Decode<TPixel>(default, default);
227-
default(PbmDecoderCore).Decode<TPixel>(default, default);
228-
default(PngDecoderCore).Decode<TPixel>(default, default);
229-
default(TgaDecoderCore).Decode<TPixel>(default, default);
230-
default(TiffDecoderCore).Decode<TPixel>(default, default);
228+
default(BmpDecoderCore).Decode<TPixel>(default, default, default);
229+
default(GifDecoderCore).Decode<TPixel>(default, default, default);
230+
default(JpegDecoderCore).Decode<TPixel>(default, default, default);
231+
default(PbmDecoderCore).Decode<TPixel>(default, default, default);
232+
default(PngDecoderCore).Decode<TPixel>(default, default, default);
233+
default(QoiDecoderCore).Decode<TPixel>(default, default, default);
234+
default(TgaDecoderCore).Decode<TPixel>(default, default, default);
235+
default(TiffDecoderCore).Decode<TPixel>(default, default, default);
236+
default(WebpDecoderCore).Decode<TPixel>(default, default, default);
231237
}
232238

233239
/// <summary>
@@ -266,6 +272,17 @@ private static void AotCompileImageDecoders<TPixel>()
266272
AotCompileImageDecoder<TPixel, TiffDecoder>();
267273
}
268274

275+
[Preserve]
276+
private static void AotCompileSpectralConverter<TPixel>()
277+
where TPixel : unmanaged, IPixel<TPixel>
278+
{
279+
default(SpectralConverter<TPixel>).GetPixelBuffer(default);
280+
default(GrayJpegSpectralConverter<TPixel>).GetPixelBuffer(default);
281+
default(RgbJpegSpectralConverter<TPixel>).GetPixelBuffer(default);
282+
default(TiffJpegSpectralConverter<TPixel>).GetPixelBuffer(default);
283+
default(TiffOldJpegSpectralConverter<TPixel>).GetPixelBuffer(default);
284+
}
285+
269286
/// <summary>
270287
/// This method pre-seeds the <see cref="IImageEncoder"/> in the AoT compiler.
271288
/// </summary>

src/ImageSharp/Formats/AnimatedImageFrameMetadata.cs

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -30,65 +30,3 @@ internal class AnimatedImageFrameMetadata
3030
/// </summary>
3131
public FrameDisposalMode DisposalMode { get; set; }
3232
}
33-
34-
#pragma warning disable SA1201 // Elements should appear in the correct order
35-
internal enum FrameBlendMode
36-
#pragma warning restore SA1201 // Elements should appear in the correct order
37-
{
38-
/// <summary>
39-
/// Do not blend. Render the current frame on the canvas by overwriting the rectangle covered by the current frame.
40-
/// </summary>
41-
Source = 0,
42-
43-
/// <summary>
44-
/// Blend the current frame with the previous frame in the animation sequence within the rectangle covered
45-
/// by the current frame.
46-
/// If the current has any transparent areas, the corresponding areas of the previous frame will be visible
47-
/// through these transparent regions.
48-
/// </summary>
49-
Over = 1
50-
}
51-
52-
internal enum FrameDisposalMode
53-
{
54-
/// <summary>
55-
/// No disposal specified.
56-
/// The decoder is not required to take any action.
57-
/// </summary>
58-
Unspecified = 0,
59-
60-
/// <summary>
61-
/// Do not dispose. The current frame is not disposed of, or in other words, not cleared or altered when moving to
62-
/// the next frame. This means that the next frame is drawn over the current frame, and if the next frame contains
63-
/// transparency, the previous frame will be visible through these transparent areas.
64-
/// </summary>
65-
DoNotDispose = 1,
66-
67-
/// <summary>
68-
/// Restore to background color. When transitioning to the next frame, the area occupied by the current frame is
69-
/// filled with the background color specified in the image metadata.
70-
/// This effectively erases the current frame by replacing it with the background color before the next frame is displayed.
71-
/// </summary>
72-
RestoreToBackground = 2,
73-
74-
/// <summary>
75-
/// Restore to previous. This method restores the area affected by the current frame to what it was before the
76-
/// current frame was displayed. It essentially "undoes" the current frame, reverting to the state of the image
77-
/// before the frame was displayed, then the next frame is drawn. This is useful for animations where only a small
78-
/// part of the image changes from frame to frame.
79-
/// </summary>
80-
RestoreToPrevious = 3
81-
}
82-
83-
internal enum FrameColorTableMode
84-
{
85-
/// <summary>
86-
/// The frame uses the shared color table specified by the image metadata.
87-
/// </summary>
88-
Global,
89-
90-
/// <summary>
91-
/// The frame uses a color table specified by the frame metadata.
92-
/// </summary>
93-
Local
94-
}

src/ImageSharp/Formats/Bmp/BmpBitsPerPixel.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,35 @@ public enum BmpBitsPerPixel : short
1111
/// <summary>
1212
/// 1 bit per pixel.
1313
/// </summary>
14-
Pixel1 = 1,
14+
Bit1 = 1,
1515

1616
/// <summary>
1717
/// 2 bits per pixel.
1818
/// </summary>
19-
Pixel2 = 2,
19+
Bit2 = 2,
2020

2121
/// <summary>
2222
/// 4 bits per pixel.
2323
/// </summary>
24-
Pixel4 = 4,
24+
Bit4 = 4,
2525

2626
/// <summary>
2727
/// 8 bits per pixel. Each pixel consists of 1 byte.
2828
/// </summary>
29-
Pixel8 = 8,
29+
Bit8 = 8,
3030

3131
/// <summary>
3232
/// 16 bits per pixel. Each pixel consists of 2 bytes.
3333
/// </summary>
34-
Pixel16 = 16,
34+
Bit16 = 16,
3535

3636
/// <summary>
3737
/// 24 bits per pixel. Each pixel consists of 3 bytes.
3838
/// </summary>
39-
Pixel24 = 24,
39+
Bit24 = 24,
4040

4141
/// <summary>
4242
/// 32 bits per pixel. Each pixel consists of 4 bytes.
4343
/// </summary>
44-
Pixel32 = 32
44+
Bit32 = 32
4545
}

src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp;
2222
/// <remarks>
2323
/// A useful decoding source example can be found at <see href="https://dxr.mozilla.org/mozilla-central/source/image/decoders/nsBMPDecoder.cpp"/>
2424
/// </remarks>
25-
internal sealed class BmpDecoderCore : IImageDecoderInternals
25+
internal sealed class BmpDecoderCore : ImageDecoderCore
2626
{
2727
/// <summary>
2828
/// The default mask for the red part of the color for 16 bit rgb bitmaps.
@@ -114,8 +114,8 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals
114114
/// </summary>
115115
/// <param name="options">The options.</param>
116116
public BmpDecoderCore(BmpDecoderOptions options)
117+
: base(options.GeneralOptions)
117118
{
118-
this.Options = options.GeneralOptions;
119119
this.rleSkippedPixelHandling = options.RleSkippedPixelHandling;
120120
this.configuration = options.GeneralOptions.Configuration;
121121
this.memoryAllocator = this.configuration.MemoryAllocator;
@@ -125,14 +125,7 @@ public BmpDecoderCore(BmpDecoderOptions options)
125125
}
126126

127127
/// <inheritdoc />
128-
public DecoderOptions Options { get; }
129-
130-
/// <inheritdoc />
131-
public Size Dimensions => new(this.infoHeader.Width, this.infoHeader.Height);
132-
133-
/// <inheritdoc />
134-
public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken cancellationToken)
135-
where TPixel : unmanaged, IPixel<TPixel>
128+
protected override Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken cancellationToken)
136129
{
137130
Image<TPixel>? image = null;
138131
try
@@ -224,10 +217,10 @@ public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken
224217
}
225218

226219
/// <inheritdoc />
227-
public ImageInfo Identify(BufferedReadStream stream, CancellationToken cancellationToken)
220+
protected override ImageInfo Identify(BufferedReadStream stream, CancellationToken cancellationToken)
228221
{
229222
this.ReadImageHeaders(stream, out _, out _);
230-
return new ImageInfo(new PixelTypeInfo(this.infoHeader.BitsPerPixel), new(this.infoHeader.Width, this.infoHeader.Height), this.metadata);
223+
return new ImageInfo(new(this.infoHeader.Width, this.infoHeader.Height), this.metadata);
231224
}
232225

233226
/// <summary>
@@ -1463,6 +1456,8 @@ private void ReadInfoHeader(BufferedReadStream stream)
14631456
this.bmpMetadata = this.metadata.GetBmpMetadata();
14641457
this.bmpMetadata.InfoHeaderType = infoHeaderType;
14651458
this.bmpMetadata.BitsPerPixel = (BmpBitsPerPixel)bitsPerPixel;
1459+
1460+
this.Dimensions = new(this.infoHeader.Width, this.infoHeader.Height);
14661461
}
14671462

14681463
/// <summary>

src/ImageSharp/Formats/Bmp/BmpEncoder.cs

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

4-
using SixLabors.ImageSharp.Advanced;
54
using SixLabors.ImageSharp.Processing;
65

76
namespace SixLabors.ImageSharp.Formats.Bmp;

src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp;
1616
/// <summary>
1717
/// Image encoder for writing an image to a stream as a Windows bitmap.
1818
/// </summary>
19-
internal sealed class BmpEncoderCore : IImageEncoderInternals
19+
internal sealed class BmpEncoderCore
2020
{
2121
/// <summary>
2222
/// The amount to pad each row by.
@@ -146,10 +146,10 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
146146

147147
int colorPaletteSize = this.bitsPerPixel switch
148148
{
149-
BmpBitsPerPixel.Pixel8 => ColorPaletteSize8Bit,
150-
BmpBitsPerPixel.Pixel4 => ColorPaletteSize4Bit,
151-
BmpBitsPerPixel.Pixel2 => ColorPaletteSize2Bit,
152-
BmpBitsPerPixel.Pixel1 => ColorPaletteSize1Bit,
149+
BmpBitsPerPixel.Bit8 => ColorPaletteSize8Bit,
150+
BmpBitsPerPixel.Bit4 => ColorPaletteSize4Bit,
151+
BmpBitsPerPixel.Bit2 => ColorPaletteSize2Bit,
152+
BmpBitsPerPixel.Bit1 => ColorPaletteSize1Bit,
153153
_ => 0
154154
};
155155

@@ -248,7 +248,7 @@ private BmpInfoHeader CreateBmpInfoHeader(int width, int height, int infoHeaderS
248248
clrUsed: 0,
249249
clrImportant: 0);
250250

251-
if ((this.infoHeaderType is BmpInfoHeaderType.WinVersion4 or BmpInfoHeaderType.WinVersion5) && this.bitsPerPixel == BmpBitsPerPixel.Pixel32)
251+
if ((this.infoHeaderType is BmpInfoHeaderType.WinVersion4 or BmpInfoHeaderType.WinVersion5) && this.bitsPerPixel == BmpBitsPerPixel.Bit32)
252252
{
253253
infoHeader.AlphaMask = Rgba32AlphaMask;
254254
infoHeader.RedMask = Rgba32RedMask;
@@ -351,31 +351,31 @@ private void WriteImage<TPixel>(Configuration configuration, Stream stream, Imag
351351
Buffer2D<TPixel> pixels = image.Frames.RootFrame.PixelBuffer;
352352
switch (this.bitsPerPixel)
353353
{
354-
case BmpBitsPerPixel.Pixel32:
354+
case BmpBitsPerPixel.Bit32:
355355
this.Write32BitPixelData(configuration, stream, pixels);
356356
break;
357357

358-
case BmpBitsPerPixel.Pixel24:
358+
case BmpBitsPerPixel.Bit24:
359359
this.Write24BitPixelData(configuration, stream, pixels);
360360
break;
361361

362-
case BmpBitsPerPixel.Pixel16:
362+
case BmpBitsPerPixel.Bit16:
363363
this.Write16BitPixelData(configuration, stream, pixels);
364364
break;
365365

366-
case BmpBitsPerPixel.Pixel8:
366+
case BmpBitsPerPixel.Bit8:
367367
this.Write8BitPixelData(configuration, stream, image);
368368
break;
369369

370-
case BmpBitsPerPixel.Pixel4:
370+
case BmpBitsPerPixel.Bit4:
371371
this.Write4BitPixelData(configuration, stream, image);
372372
break;
373373

374-
case BmpBitsPerPixel.Pixel2:
374+
case BmpBitsPerPixel.Bit2:
375375
this.Write2BitPixelData(configuration, stream, image);
376376
break;
377377

378-
case BmpBitsPerPixel.Pixel1:
378+
case BmpBitsPerPixel.Bit1:
379379
this.Write1BitPixelData(configuration, stream, image);
380380
break;
381381
}

0 commit comments

Comments
 (0)