Browse Source

refactor to follow style rules

pull/2569/head
Poker 2 years ago
parent
commit
bd2d4550a9
No known key found for this signature in database GPG Key ID: C65A6AD457D5C8F8
  1. 2
      src/ImageSharp/Formats/Webp/AlphaDecoder.cs
  2. 17
      src/ImageSharp/Formats/Webp/AlphaEncoder.cs
  3. 2
      src/ImageSharp/Formats/Webp/AnimationFrameData.cs
  4. 4
      src/ImageSharp/Formats/Webp/BitWriter/Vp8BitWriter.cs
  5. 2
      src/ImageSharp/Formats/Webp/BitWriter/Vp8LBitWriter.cs
  6. 2
      src/ImageSharp/Formats/Webp/Lossless/BackwardReferenceEncoder.cs
  7. 2
      src/ImageSharp/Formats/Webp/Lossless/CostManager.cs
  8. 7
      src/ImageSharp/Formats/Webp/Lossless/PixOrCopy.cs
  9. 18
      src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs
  10. 4
      src/ImageSharp/Formats/Webp/Lossless/WebpLosslessDecoder.cs
  11. 6
      src/ImageSharp/Formats/Webp/Lossy/Vp8EncIterator.cs
  12. 16
      src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs
  13. 18
      src/ImageSharp/Formats/Webp/Lossy/WebpLossyDecoder.cs
  14. 12
      src/ImageSharp/Formats/Webp/WebpAnimationDecoder.cs
  15. 12
      src/ImageSharp/Formats/Webp/WebpAnimationEncoder.cs
  16. 20
      src/ImageSharp/Formats/Webp/WebpChunkParsingUtils.cs
  17. 9
      src/ImageSharp/Formats/Webp/WebpDecoder.cs
  18. 27
      src/ImageSharp/Formats/Webp/WebpDecoderCore.cs
  19. 2
      src/ImageSharp/Formats/Webp/WebpDecoderOptions.cs
  20. 2
      src/ImageSharp/Formats/Webp/WebpEncoder.cs
  21. 58
      src/ImageSharp/Formats/Webp/WebpEncoderCore.cs
  22. 6
      src/ImageSharp/Formats/Webp/WebpFormat.cs

2
src/ImageSharp/Formats/Webp/AlphaDecoder.cs

@ -59,7 +59,7 @@ internal class AlphaDecoder : IDisposable
if (this.Compressed)
{
Vp8LBitReader bitReader = new(data);
Vp8LBitReader bitReader = new Vp8LBitReader(data);
this.LosslessDecoder = new WebpLosslessDecoder(bitReader, memoryAllocator, configuration);
this.LosslessDecoder.DecodeImageStream(this.Vp8LDec, width, height, true);

17
src/ImageSharp/Formats/Webp/AlphaEncoder.cs

@ -43,17 +43,8 @@ internal static class AlphaEncoder
{
const WebpEncodingMethod effort = WebpEncodingMethod.Default;
const int quality = 8 * (int)effort;
using Vp8LEncoder lossLessEncoder = new(
memoryAllocator,
configuration,
width,
height,
quality,
skipMetadata,
effort,
WebpTransparentColorMode.Preserve,
false,
0);
using Vp8LEncoder lossLessEncoder = new Vp8LEncoder(memoryAllocator, configuration, width, height, quality,
skipMetadata, effort, WebpTransparentColorMode.Preserve, false, 0);
// The transparency information will be stored in the green channel of the ARGB quadruplet.
// The green channel is allowed extra transformation steps in the specification -- unlike the other channels,
@ -81,7 +72,7 @@ internal static class AlphaEncoder
{
int width = frame.Width;
int height = frame.Height;
ImageFrame<Rgba32> alphaAsFrame = new(Configuration.Default, width, height);
ImageFrame<Rgba32> alphaAsFrame = new ImageFrame<Rgba32>(Configuration.Default, width, height);
for (int y = 0; y < height; y++)
{
@ -91,7 +82,7 @@ internal static class AlphaEncoder
for (int x = 0; x < width; x++)
{
// Leave A/R/B channels zero'd.
pixelRow[x] = new(0, alphaRow[x], 0, 0);
pixelRow[x] = new Rgba32(0, alphaRow[x], 0, 0);
}
}

2
src/ImageSharp/Formats/Webp/AnimationFrameData.cs

@ -62,7 +62,7 @@ internal struct AnimationFrameData
{
Span<byte> buffer = stackalloc byte[4];
AnimationFrameData data = new()
AnimationFrameData data = new AnimationFrameData
{
DataSize = WebpChunkParsingUtils.ReadChunkSize(stream, buffer),

4
src/ImageSharp/Formats/Webp/BitWriter/Vp8BitWriter.cs

@ -399,7 +399,7 @@ internal class Vp8BitWriter : BitWriterBase
int mbSize = this.enc.Mbw * this.enc.Mbh;
int expectedSize = (int)((uint)mbSize * 7 / 8);
Vp8BitWriter bitWriterPartZero = new(expectedSize, this.enc);
Vp8BitWriter bitWriterPartZero = new Vp8BitWriter(expectedSize, this.enc);
// Partition #0 with header and partition sizes.
uint size0 = bitWriterPartZero.GeneratePartition0();
@ -545,7 +545,7 @@ internal class Vp8BitWriter : BitWriterBase
// Writes the partition #0 modes (that is: all intra modes)
private void CodeIntraModes()
{
Vp8EncIterator it = new(this.enc);
Vp8EncIterator it = new Vp8EncIterator(this.enc);
int predsWidth = this.enc.PredsWidth;
do

2
src/ImageSharp/Formats/Webp/BitWriter/Vp8LBitWriter.cs

@ -102,7 +102,7 @@ internal class Vp8LBitWriter : BitWriterBase
{
byte[] clonedBuffer = new byte[this.Buffer.Length];
System.Buffer.BlockCopy(this.Buffer, 0, clonedBuffer, 0, this.cur);
return new(clonedBuffer, this.bits, this.used, this.cur);
return new Vp8LBitWriter(clonedBuffer, this.bits, this.used, this.cur);
}
/// <inheritdoc/>

2
src/ImageSharp/Formats/Webp/Lossless/BackwardReferenceEncoder.cs

@ -775,7 +775,7 @@ internal static class BackwardReferenceEncoder
private static void BackwardRefsWithLocalCache(ReadOnlySpan<uint> bgra, int cacheBits, Vp8LBackwardRefs refs)
{
int pixelIndex = 0;
ColorCache colorCache = new(cacheBits);
ColorCache colorCache = new ColorCache(cacheBits);
for (int idx = 0; idx < refs.Refs.Count; idx++)
{
PixOrCopy v = refs.Refs[idx];

2
src/ImageSharp/Formats/Webp/Lossless/CostManager.cs

@ -17,7 +17,7 @@ internal sealed class CostManager : IDisposable
private const int FreeIntervalsStartCount = 25;
private readonly Stack<CostInterval> freeIntervals = new(FreeIntervalsStartCount);
private readonly Stack<CostInterval> freeIntervals = new Stack<CostInterval>(FreeIntervalsStartCount);
public CostManager(MemoryAllocator memoryAllocator, IMemoryOwner<ushort> distArray, int pixCount, CostModel costModel)
{

7
src/ImageSharp/Formats/Webp/Lossless/PixOrCopy.cs

@ -15,7 +15,7 @@ internal sealed class PixOrCopy
public uint BgraOrDistance { get; set; }
public static PixOrCopy CreateCacheIdx(int idx) =>
new()
new PixOrCopy
{
Mode = PixOrCopyMode.CacheIdx,
BgraOrDistance = (uint)idx,
@ -23,14 +23,15 @@ internal sealed class PixOrCopy
};
public static PixOrCopy CreateLiteral(uint bgra) =>
new()
new PixOrCopy
{
Mode = PixOrCopyMode.Literal,
BgraOrDistance = bgra,
Len = 1
};
public static PixOrCopy CreateCopy(uint distance, ushort len) => new()
public static PixOrCopy CreateCopy(uint distance, ushort len) =>
new PixOrCopy
{
Mode = PixOrCopyMode.Copy,
BgraOrDistance = distance,

18
src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs

@ -304,7 +304,7 @@ internal class Vp8LEncoder : IDisposable
if (hasAnimation)
{
prevPosition = BitWriterBase.WriteAnimationFrame(stream, new()
prevPosition = BitWriterBase.WriteAnimationFrame(stream, new AnimationFrameData
{
Width = (uint)frame.Width,
Height = (uint)frame.Height,
@ -547,7 +547,7 @@ internal class Vp8LEncoder : IDisposable
EntropyIx entropyIdx = this.AnalyzeEntropy(bgra, width, height, usePalette, this.PaletteSize, this.TransformBits, out redAndBlueAlwaysZero);
bool doNotCache = false;
List<CrunchConfig> crunchConfigs = new();
List<CrunchConfig> crunchConfigs = new List<CrunchConfig>();
if (this.method == WebpEncodingMethod.BestQuality && this.quality == 100)
{
@ -641,8 +641,8 @@ internal class Vp8LEncoder : IDisposable
Vp8LBackwardRefs refsTmp = this.Refs[refsBest.Equals(this.Refs[0]) ? 1 : 0];
this.bitWriter.Reset(bwInit);
Vp8LHistogram tmpHisto = new(cacheBits);
List<Vp8LHistogram> histogramImage = new(histogramImageXySize);
Vp8LHistogram tmpHisto = new Vp8LHistogram(cacheBits);
List<Vp8LHistogram> histogramImage = new List<Vp8LHistogram>(histogramImageXySize);
for (int i = 0; i < histogramImageXySize; i++)
{
histogramImage.Add(new Vp8LHistogram(cacheBits));
@ -839,9 +839,9 @@ internal class Vp8LEncoder : IDisposable
refsTmp1,
refsTmp2);
List<Vp8LHistogram> histogramImage = new()
List<Vp8LHistogram> histogramImage = new List<Vp8LHistogram>
{
new(cacheBits)
new Vp8LHistogram(cacheBits)
};
// Build histogram image and symbols from backward references.
@ -941,7 +941,7 @@ internal class Vp8LEncoder : IDisposable
int i;
byte[] codeLengthBitDepth = new byte[WebpConstants.CodeLengthCodes];
short[] codeLengthBitDepthSymbols = new short[WebpConstants.CodeLengthCodes];
HuffmanTreeCode huffmanCode = new()
HuffmanTreeCode huffmanCode = new HuffmanTreeCode
{
NumSymbols = WebpConstants.CodeLengthCodes,
CodeLengths = codeLengthBitDepth,
@ -1192,7 +1192,7 @@ internal class Vp8LEncoder : IDisposable
histo[(int)HistoIx.HistoBluePred * 256]++;
histo[(int)HistoIx.HistoAlphaPred * 256]++;
Vp8LBitEntropy bitEntropy = new();
Vp8LBitEntropy bitEntropy = new Vp8LBitEntropy();
for (int j = 0; j < (int)HistoIx.HistoTotal; j++)
{
bitEntropy.Init();
@ -1318,7 +1318,7 @@ internal class Vp8LEncoder : IDisposable
/// <returns>The number of palette entries.</returns>
private static int GetColorPalette(ReadOnlySpan<uint> bgra, int width, int height, Span<uint> palette)
{
HashSet<uint> colors = new();
HashSet<uint> colors = new HashSet<uint>();
for (int y = 0; y < height; y++)
{
ReadOnlySpan<uint> bgraRow = bgra.Slice(y * width, width);

4
src/ImageSharp/Formats/Webp/Lossless/WebpLosslessDecoder.cs

@ -95,7 +95,7 @@ internal sealed class WebpLosslessDecoder
public void Decode<TPixel>(Buffer2D<TPixel> pixels, int width, int height)
where TPixel : unmanaged, IPixel<TPixel>
{
using (Vp8LDecoder decoder = new(width, height, this.memoryAllocator))
using (Vp8LDecoder decoder = new Vp8LDecoder(width, height, this.memoryAllocator))
{
this.DecodeImageStream(decoder, width, height, true);
this.DecodeImageData(decoder, decoder.Pixels.Memory.Span);
@ -616,7 +616,7 @@ internal sealed class WebpLosslessDecoder
private void ReadTransformation(int xSize, int ySize, Vp8LDecoder decoder)
{
Vp8LTransformType transformType = (Vp8LTransformType)this.bitReader.ReadValue(2);
Vp8LTransform transform = new(transformType, xSize, ySize);
Vp8LTransform transform = new Vp8LTransform(transformType, xSize, ySize);
// Each transform is allowed to be used only once.
foreach (Vp8LTransform decoderTransform in decoder.Transforms)

6
src/ImageSharp/Formats/Webp/Lossy/Vp8EncIterator.cs

@ -396,7 +396,7 @@ internal class Vp8EncIterator
this.MakeLuma16Preds();
for (mode = 0; mode < maxMode; mode++)
{
Vp8Histogram histo = new();
Vp8Histogram histo = new Vp8Histogram();
histo.CollectHistogram(this.YuvIn.AsSpan(YOffEnc), this.YuvP.AsSpan(Vp8Encoding.Vp8I16ModeOffsets[mode]), 0, 16);
int alpha = histo.GetAlpha();
if (alpha > bestAlpha)
@ -414,7 +414,7 @@ internal class Vp8EncIterator
{
Span<byte> modes = stackalloc byte[16];
const int maxMode = MaxIntra4Mode;
Vp8Histogram totalHisto = new();
Vp8Histogram totalHisto = new Vp8Histogram();
int curHisto = 0;
this.StartI4();
do
@ -467,7 +467,7 @@ internal class Vp8EncIterator
this.MakeChroma8Preds();
for (mode = 0; mode < maxMode; ++mode)
{
Vp8Histogram histo = new();
Vp8Histogram histo = new Vp8Histogram();
histo.CollectHistogram(this.YuvIn.AsSpan(UOffEnc), this.YuvP.AsSpan(Vp8Encoding.Vp8UvModeOffsets[mode]), 16, 16 + 4 + 4);
int alpha = histo.GetAlpha();
if (alpha > bestAlpha)

16
src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs

@ -395,7 +395,7 @@ internal class Vp8Encoder : IDisposable
int yStride = width;
int uvStride = (yStride + 1) >> 1;
Vp8EncIterator it = new(this);
Vp8EncIterator it = new Vp8EncIterator(this);
Span<int> alphas = stackalloc int[WebpConstants.MaxAlpha + 1];
this.alpha = this.MacroBlockAnalysis(width, height, it, y, u, v, yStride, uvStride, alphas, out this.uvAlpha);
int totalMb = this.Mbw * this.Mbw;
@ -416,8 +416,8 @@ internal class Vp8Encoder : IDisposable
this.StatLoop(width, height, yStride, uvStride);
it.Init();
Vp8EncIterator.InitFilter();
Vp8ModeScore info = new();
Vp8Residual residual = new();
Vp8ModeScore info = new Vp8ModeScore();
Vp8Residual residual = new Vp8Residual();
do
{
bool dontUseSkip = !this.Proba.UseSkipProba;
@ -474,7 +474,7 @@ internal class Vp8Encoder : IDisposable
if (hasAnimation)
{
prevPosition = BitWriterBase.WriteAnimationFrame(stream, new()
prevPosition = BitWriterBase.WriteAnimationFrame(stream, new AnimationFrameData
{
Width = (uint)frame.Width,
Height = (uint)frame.Height,
@ -529,7 +529,7 @@ internal class Vp8Encoder : IDisposable
Vp8RdLevel rdOpt = this.method >= WebpEncodingMethod.Level3 || doSearch ? Vp8RdLevel.RdOptBasic : Vp8RdLevel.RdOptNone;
int nbMbs = this.Mbw * this.Mbh;
PassStats stats = new(targetSize, targetPsnr, QMin, QMax, this.quality);
PassStats stats = new PassStats(targetSize, targetPsnr, QMin, QMax, this.quality);
this.Proba.ResetTokenStats();
// Fast mode: quick analysis pass over few mbs. Better than nothing.
@ -597,7 +597,7 @@ internal class Vp8Encoder : IDisposable
Span<byte> y = this.Y.GetSpan();
Span<byte> u = this.U.GetSpan();
Span<byte> v = this.V.GetSpan();
Vp8EncIterator it = new(this);
Vp8EncIterator it = new Vp8EncIterator(this);
long size = 0;
long sizeP0 = 0;
long distortion = 0;
@ -605,7 +605,7 @@ internal class Vp8Encoder : IDisposable
it.Init();
this.SetLoopParams(stats.Q);
Vp8ModeScore info = new();
Vp8ModeScore info = new Vp8ModeScore();
do
{
info.Clear();
@ -1167,7 +1167,7 @@ internal class Vp8Encoder : IDisposable
private void RecordResiduals(Vp8EncIterator it, Vp8ModeScore rd)
{
int x, y, ch;
Vp8Residual residual = new();
Vp8Residual residual = new Vp8Residual();
bool i16 = it.CurrentMacroBlockInfo.MacroBlockType == Vp8MacroBlockType.I16X16;
it.NzToBytes();

18
src/ImageSharp/Formats/Webp/Lossy/WebpLossyDecoder.cs

@ -62,7 +62,7 @@ internal sealed class WebpLossyDecoder
// Paragraph 9.2: color space and clamp type follow.
sbyte colorSpace = (sbyte)this.bitReader.ReadValue(1);
sbyte clampType = (sbyte)this.bitReader.ReadValue(1);
Vp8PictureHeader pictureHeader = new()
Vp8PictureHeader pictureHeader = new Vp8PictureHeader
{
Width = (uint)width,
Height = (uint)height,
@ -73,10 +73,11 @@ internal sealed class WebpLossyDecoder
};
// Paragraph 9.3: Parse the segment header.
Vp8Proba proba = new();
Vp8Proba proba = new Vp8Proba();
Vp8SegmentHeader vp8SegmentHeader = this.ParseSegmentHeader(proba);
using (Vp8Decoder decoder = new(info.Vp8FrameHeader, pictureHeader, vp8SegmentHeader, proba, this.memoryAllocator))
using (Vp8Decoder decoder = new Vp8Decoder(info.Vp8FrameHeader, pictureHeader, vp8SegmentHeader, proba,
this.memoryAllocator))
{
Vp8Io io = InitializeVp8Io(decoder, pictureHeader);
@ -101,13 +102,8 @@ internal sealed class WebpLossyDecoder
if (info.Features?.Alpha == true)
{
using (AlphaDecoder alphaDecoder = new(
width,
height,
alphaData,
info.Features.AlphaChunkHeader,
this.memoryAllocator,
this.configuration))
using (AlphaDecoder alphaDecoder = new AlphaDecoder(width, height, alphaData,
info.Features.AlphaChunkHeader, this.memoryAllocator, this.configuration))
{
alphaDecoder.Decode();
DecodePixelValues(width, height, decoder.Pixels.Memory.Span, pixels, alphaDecoder.Alpha);
@ -1062,7 +1058,7 @@ internal sealed class WebpLossyDecoder
private Vp8SegmentHeader ParseSegmentHeader(Vp8Proba proba)
{
Vp8SegmentHeader vp8SegmentHeader = new()
Vp8SegmentHeader vp8SegmentHeader = new Vp8SegmentHeader
{
UseSegment = this.bitReader.ReadBool()
};

12
src/ImageSharp/Formats/Webp/WebpAnimationDecoder.cs

@ -153,7 +153,7 @@ internal class WebpAnimationDecoder : IDisposable
}
WebpImageInfo? webpInfo = null;
WebpFeatures features = new();
WebpFeatures features = new WebpFeatures();
switch (chunkType)
{
case WebpChunkType.Vp8:
@ -178,7 +178,7 @@ internal class WebpAnimationDecoder : IDisposable
ImageFrame<TPixel> imageFrame;
if (previousFrame is null)
{
image = new(this.configuration, (int)width, (int)height, backgroundColor.ToPixel<TPixel>(), this.metadata);
image = new Image<TPixel>(this.configuration, (int)width, (int)height, backgroundColor.ToPixel<TPixel>(), this.metadata);
SetFrameMetadata(image.Frames.RootFrame.Metadata, frameData.Duration);
@ -259,19 +259,21 @@ internal class WebpAnimationDecoder : IDisposable
private Buffer2D<TPixel> DecodeImageData<TPixel>(AnimationFrameData frameData, WebpImageInfo webpInfo)
where TPixel : unmanaged, IPixel<TPixel>
{
Image<TPixel> decodedImage = new((int)frameData.Width, (int)frameData.Height);
Image<TPixel> decodedImage = new Image<TPixel>((int)frameData.Width, (int)frameData.Height);
try
{
Buffer2D<TPixel> pixelBufferDecoded = decodedImage.GetRootFramePixelBuffer();
if (webpInfo.IsLossless)
{
WebpLosslessDecoder losslessDecoder = new(webpInfo.Vp8LBitReader, this.memoryAllocator, this.configuration);
WebpLosslessDecoder losslessDecoder =
new WebpLosslessDecoder(webpInfo.Vp8LBitReader, this.memoryAllocator, this.configuration);
losslessDecoder.Decode(pixelBufferDecoded, (int)webpInfo.Width, (int)webpInfo.Height);
}
else
{
WebpLossyDecoder lossyDecoder = new(webpInfo.Vp8BitReader, this.memoryAllocator, this.configuration);
WebpLossyDecoder lossyDecoder =
new WebpLossyDecoder(webpInfo.Vp8BitReader, this.memoryAllocator, this.configuration);
lossyDecoder.Decode(pixelBufferDecoded, (int)webpInfo.Width, (int)webpInfo.Height, webpInfo, this.alphaData);
}

12
src/ImageSharp/Formats/Webp/WebpAnimationEncoder.cs

@ -1,12 +0,0 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
namespace SixLabors.ImageSharp.Formats.Webp;
/// <summary>
/// Encoder for animated webp images.
/// </summary>
public class WebpAnimationEncoder
{
// 可能不需要这个屌东西
}

20
src/ImageSharp/Formats/Webp/WebpChunkParsingUtils.cs

@ -8,6 +8,8 @@ using SixLabors.ImageSharp.Formats.Webp.Lossy;
using SixLabors.ImageSharp.IO;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.Metadata;
using SixLabors.ImageSharp.Metadata.Profiles.Exif;
using SixLabors.ImageSharp.Metadata.Profiles.Xmp;
namespace SixLabors.ImageSharp.Formats.Webp;
@ -104,16 +106,16 @@ internal static class WebpChunkParsingUtils
WebpThrowHelper.ThrowImageFormatException("bad partition length");
}
Vp8FrameHeader vp8FrameHeader = new()
Vp8FrameHeader vp8FrameHeader = new Vp8FrameHeader
{
KeyFrame = true,
Profile = (sbyte)version,
PartitionLength = partitionLength
};
Vp8BitReader bitReader = new(stream, remaining, memoryAllocator, partitionLength) { Remaining = remaining };
Vp8BitReader bitReader = new Vp8BitReader(stream, remaining, memoryAllocator, partitionLength) { Remaining = remaining };
return new()
return new WebpImageInfo
{
Width = width,
Height = height,
@ -137,7 +139,7 @@ internal static class WebpChunkParsingUtils
// VP8 data size.
uint imageDataSize = ReadChunkSize(stream, buffer);
Vp8LBitReader bitReader = new(stream, imageDataSize, memoryAllocator);
Vp8LBitReader bitReader = new Vp8LBitReader(stream, imageDataSize, memoryAllocator);
// One byte signature, should be 0x2f.
uint signature = bitReader.ReadValue(8);
@ -166,7 +168,7 @@ internal static class WebpChunkParsingUtils
WebpThrowHelper.ThrowNotSupportedException($"Unexpected version number {version} found in VP8L header");
}
return new()
return new WebpImageInfo
{
Width = width,
Height = height,
@ -229,7 +231,7 @@ internal static class WebpChunkParsingUtils
uint height = ReadUInt24LittleEndian(stream, buffer) + 1;
// Read all the chunks in the order they occur.
WebpImageInfo info = new()
WebpImageInfo info = new WebpImageInfo
{
Width = width,
Height = height,
@ -291,7 +293,7 @@ internal static class WebpChunkParsingUtils
if (stream.Read(buffer) == 4)
{
uint chunkSize = BinaryPrimitives.ReadUInt32LittleEndian(buffer);
return (chunkSize % 2 == 0) ? chunkSize : chunkSize + 1;
return chunkSize % 2 == 0 ? chunkSize : chunkSize + 1;
}
throw new ImageFormatException("Invalid Webp data, could not read chunk size.");
@ -348,7 +350,7 @@ internal static class WebpChunkParsingUtils
if (metadata.ExifProfile != null)
{
metadata.ExifProfile = new(exifData);
metadata.ExifProfile = new ExifProfile(exifData);
}
break;
@ -362,7 +364,7 @@ internal static class WebpChunkParsingUtils
if (metadata.XmpProfile != null)
{
metadata.XmpProfile = new(xmpData);
metadata.XmpProfile = new XmpProfile(xmpData);
}
break;

9
src/ImageSharp/Formats/Webp/WebpDecoder.cs

@ -17,7 +17,7 @@ public sealed class WebpDecoder : SpecializedImageDecoder<WebpDecoderOptions>
/// <summary>
/// Gets the shared instance.
/// </summary>
public static WebpDecoder Instance { get; } = new();
public static WebpDecoder Instance { get; } = new WebpDecoder();
/// <inheritdoc/>
protected override ImageInfo Identify(DecoderOptions options, Stream stream, CancellationToken cancellationToken)
@ -25,7 +25,7 @@ public sealed class WebpDecoder : SpecializedImageDecoder<WebpDecoderOptions>
Guard.NotNull(options, nameof(options));
Guard.NotNull(stream, nameof(stream));
using WebpDecoderCore decoder = new(new WebpDecoderOptions() { GeneralOptions = options });
using WebpDecoderCore decoder = new WebpDecoderCore(new WebpDecoderOptions() { GeneralOptions = options });
return decoder.Identify(options.Configuration, stream, cancellationToken);
}
@ -35,7 +35,7 @@ public sealed class WebpDecoder : SpecializedImageDecoder<WebpDecoderOptions>
Guard.NotNull(options, nameof(options));
Guard.NotNull(stream, nameof(stream));
using WebpDecoderCore decoder = new(options);
using WebpDecoderCore decoder = new WebpDecoderCore(options);
Image<TPixel> image = decoder.Decode<TPixel>(options.GeneralOptions.Configuration, stream, cancellationToken);
ScaleToTargetSize(options.GeneralOptions, image);
@ -52,6 +52,5 @@ public sealed class WebpDecoder : SpecializedImageDecoder<WebpDecoderOptions>
=> this.Decode<Rgba32>(options, stream, cancellationToken);
/// <inheritdoc/>
protected override WebpDecoderOptions CreateDefaultSpecializedOptions(DecoderOptions options)
=> new() { GeneralOptions = options };
protected override WebpDecoderOptions CreateDefaultSpecializedOptions(DecoderOptions options) => new WebpDecoderOptions { GeneralOptions = options };
}

27
src/ImageSharp/Formats/Webp/WebpDecoderCore.cs

@ -8,7 +8,9 @@ using SixLabors.ImageSharp.Formats.Webp.Lossy;
using SixLabors.ImageSharp.IO;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.Metadata;
using SixLabors.ImageSharp.Metadata.Profiles.Exif;
using SixLabors.ImageSharp.Metadata.Profiles.Icc;
using SixLabors.ImageSharp.Metadata.Profiles.Xmp;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Formats.Webp;
@ -71,7 +73,7 @@ internal sealed class WebpDecoderCore : IImageDecoderInternals, IDisposable
public DecoderOptions Options { get; }
/// <inheritdoc/>
public Size Dimensions => new((int)this.webImageInfo!.Width, (int)this.webImageInfo.Height);
public Size Dimensions => new Size((int)this.webImageInfo!.Width, (int)this.webImageInfo.Height);
/// <inheritdoc />
public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken cancellationToken)
@ -80,7 +82,7 @@ internal sealed class WebpDecoderCore : IImageDecoderInternals, IDisposable
Image<TPixel>? image = null;
try
{
ImageMetadata metadata = new();
ImageMetadata metadata = new ImageMetadata();
Span<byte> buffer = stackalloc byte[4];
uint fileSize = ReadImageHeader(stream, buffer);
@ -89,7 +91,8 @@ internal sealed class WebpDecoderCore : IImageDecoderInternals, IDisposable
{
if (this.webImageInfo.Features is { Animation: true })
{
using WebpAnimationDecoder animationDecoder = new(this.memoryAllocator, this.configuration, this.maxFrames, this.backgroundColorHandling);
using WebpAnimationDecoder animationDecoder = new WebpAnimationDecoder(this.memoryAllocator,
this.configuration, this.maxFrames, this.backgroundColorHandling);
return animationDecoder.Decode<TPixel>(stream, this.webImageInfo.Features, this.webImageInfo.Width, this.webImageInfo.Height, fileSize);
}
@ -97,12 +100,14 @@ internal sealed class WebpDecoderCore : IImageDecoderInternals, IDisposable
Buffer2D<TPixel> pixels = image.GetRootFramePixelBuffer();
if (this.webImageInfo.IsLossless)
{
WebpLosslessDecoder losslessDecoder = new(this.webImageInfo.Vp8LBitReader, this.memoryAllocator, this.configuration);
WebpLosslessDecoder losslessDecoder = new WebpLosslessDecoder(this.webImageInfo.Vp8LBitReader,
this.memoryAllocator, this.configuration);
losslessDecoder.Decode(pixels, image.Width, image.Height);
}
else
{
WebpLossyDecoder lossyDecoder = new(this.webImageInfo.Vp8BitReader, this.memoryAllocator, this.configuration);
WebpLossyDecoder lossyDecoder = new WebpLossyDecoder(this.webImageInfo.Vp8BitReader,
this.memoryAllocator, this.configuration);
lossyDecoder.Decode(pixels, image.Width, image.Height, this.webImageInfo, this.alphaData);
}
@ -127,12 +132,12 @@ internal sealed class WebpDecoderCore : IImageDecoderInternals, IDisposable
{
ReadImageHeader(stream, stackalloc byte[4]);
ImageMetadata metadata = new();
ImageMetadata metadata = new ImageMetadata();
using (this.webImageInfo = this.ReadVp8Info(stream, metadata, true))
{
return new ImageInfo(
new PixelTypeInfo((int)this.webImageInfo.BitsPerPixel),
new((int)this.webImageInfo.Width, (int)this.webImageInfo.Height),
new Size((int)this.webImageInfo.Width, (int)this.webImageInfo.Height),
metadata);
}
}
@ -173,7 +178,7 @@ internal sealed class WebpDecoderCore : IImageDecoderInternals, IDisposable
Span<byte> buffer = stackalloc byte[4];
WebpChunkType chunkType = WebpChunkParsingUtils.ReadChunkType(stream, buffer);
WebpFeatures features = new();
WebpFeatures features = new WebpFeatures();
switch (chunkType)
{
case WebpChunkType.Vp8:
@ -327,7 +332,7 @@ internal sealed class WebpDecoderCore : IImageDecoderInternals, IDisposable
return;
}
metadata.ExifProfile = new(exifData);
metadata.ExifProfile = new ExifProfile(exifData);
}
}
@ -354,7 +359,7 @@ internal sealed class WebpDecoderCore : IImageDecoderInternals, IDisposable
return;
}
metadata.XmpProfile = new(xmpData);
metadata.XmpProfile = new XmpProfile(xmpData);
}
}
@ -380,7 +385,7 @@ internal sealed class WebpDecoderCore : IImageDecoderInternals, IDisposable
WebpThrowHelper.ThrowInvalidImageContentException("Not enough data to read the iccp chunk");
}
IccProfile profile = new(iccpData);
IccProfile profile = new IccProfile(iccpData);
if (profile.CheckIsValid())
{
metadata.IccProfile = profile;

2
src/ImageSharp/Formats/Webp/WebpDecoderOptions.cs

@ -9,7 +9,7 @@ namespace SixLabors.ImageSharp.Formats.Webp;
public sealed class WebpDecoderOptions : ISpecializedDecoderOptions
{
/// <inheritdoc/>
public DecoderOptions GeneralOptions { get; init; } = new();
public DecoderOptions GeneralOptions { get; init; } = new DecoderOptions();
/// <summary>
/// Gets the flag to decide how to handle the background color Animation Chunk.

2
src/ImageSharp/Formats/Webp/WebpEncoder.cs

@ -82,7 +82,7 @@ public sealed class WebpEncoder : ImageEncoder
/// <inheritdoc/>
protected override void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken cancellationToken)
{
WebpEncoderCore encoder = new(this, image.Configuration);
WebpEncoderCore encoder = new WebpEncoderCore(this, image.Configuration);
encoder.Encode(image, stream, cancellationToken);
}
}

58
src/ImageSharp/Formats/Webp/WebpEncoderCore.cs

@ -129,17 +129,9 @@ internal sealed class WebpEncoderCore : IImageEncoderInternals
if (lossless)
{
using Vp8LEncoder encoder = new(
this.memoryAllocator,
this.configuration,
image.Width,
image.Height,
this.quality,
this.skipMetadata,
this.method,
this.transparentColorMode,
this.nearLossless,
this.nearLosslessQuality);
using Vp8LEncoder encoder = new Vp8LEncoder(this.memoryAllocator, this.configuration, image.Width,
image.Height, this.quality, this.skipMetadata, this.method, this.transparentColorMode,
this.nearLossless, this.nearLosslessQuality);
bool hasAnimation = image.Frames.Count > 1;
encoder.EncodeHeader(image, stream, hasAnimation);
@ -147,17 +139,9 @@ internal sealed class WebpEncoderCore : IImageEncoderInternals
{
foreach (ImageFrame<TPixel> imageFrame in image.Frames)
{
using Vp8LEncoder enc = new(
this.memoryAllocator,
this.configuration,
image.Width,
image.Height,
this.quality,
this.skipMetadata,
this.method,
this.transparentColorMode,
this.nearLossless,
this.nearLosslessQuality);
using Vp8LEncoder enc = new Vp8LEncoder(this.memoryAllocator, this.configuration, image.Width,
image.Height, this.quality, this.skipMetadata, this.method, this.transparentColorMode,
this.nearLossless, this.nearLosslessQuality);
enc.Encode(imageFrame, stream, true);
}
@ -171,36 +155,18 @@ internal sealed class WebpEncoderCore : IImageEncoderInternals
}
else
{
using Vp8Encoder encoder = new(
this.memoryAllocator,
this.configuration,
image.Width,
image.Height,
this.quality,
this.skipMetadata,
this.method,
this.entropyPasses,
this.filterStrength,
this.spatialNoiseShaping,
this.alphaCompression);
using Vp8Encoder encoder = new Vp8Encoder(this.memoryAllocator, this.configuration, image.Width,
image.Height, this.quality, this.skipMetadata, this.method, this.entropyPasses, this.filterStrength,
this.spatialNoiseShaping, this.alphaCompression);
if (image.Frames.Count > 1)
{
encoder.EncodeHeader(image, stream, false, true);
foreach (ImageFrame<TPixel> imageFrame in image.Frames)
{
using Vp8Encoder enc = new(
this.memoryAllocator,
this.configuration,
image.Width,
image.Height,
this.quality,
this.skipMetadata,
this.method,
this.entropyPasses,
this.filterStrength,
this.spatialNoiseShaping,
this.alphaCompression);
using Vp8Encoder enc = new Vp8Encoder(this.memoryAllocator, this.configuration, image.Width,
image.Height, this.quality, this.skipMetadata, this.method, this.entropyPasses,
this.filterStrength, this.spatialNoiseShaping, this.alphaCompression);
enc.EncodeAnimation(imageFrame, stream);
}

6
src/ImageSharp/Formats/Webp/WebpFormat.cs

@ -15,7 +15,7 @@ public sealed class WebpFormat : IImageFormat<WebpMetadata, WebpFrameMetadata>
/// <summary>
/// Gets the shared instance.
/// </summary>
public static WebpFormat Instance { get; } = new();
public static WebpFormat Instance { get; } = new WebpFormat();
/// <inheritdoc/>
public string Name => "Webp";
@ -30,8 +30,8 @@ public sealed class WebpFormat : IImageFormat<WebpMetadata, WebpFrameMetadata>
public IEnumerable<string> FileExtensions => WebpConstants.FileExtensions;
/// <inheritdoc/>
public WebpMetadata CreateDefaultFormatMetadata() => new();
public WebpMetadata CreateDefaultFormatMetadata() => new WebpMetadata();
/// <inheritdoc/>
public WebpFrameMetadata CreateDefaultFormatFrameMetadata() => new();
public WebpFrameMetadata CreateDefaultFormatFrameMetadata() => new WebpFrameMetadata();
}

Loading…
Cancel
Save