mirror of https://github.com/SixLabors/ImageSharp
7 changed files with 555 additions and 87 deletions
@ -0,0 +1,64 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Formats.WebP.BitWriter |
||||
|
{ |
||||
|
internal abstract class BitWriterBase |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Buffer to write to.
|
||||
|
/// </summary>
|
||||
|
private byte[] buffer; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="BitWriterBase"/> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="expectedSize">The expected size in bytes.</param>
|
||||
|
protected BitWriterBase(int expectedSize) |
||||
|
{ |
||||
|
// TODO: use memory allocator here.
|
||||
|
this.buffer = new byte[expectedSize]; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="BitWriterBase"/> class.
|
||||
|
/// Used internally for cloning.
|
||||
|
/// </summary>
|
||||
|
private protected BitWriterBase(byte[] buffer) => this.buffer = buffer; |
||||
|
|
||||
|
public byte[] Buffer |
||||
|
{ |
||||
|
get { return this.buffer; } |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Resizes the buffer to write to.
|
||||
|
/// </summary>
|
||||
|
/// <param name="extraSize">The extra size in bytes needed.</param>
|
||||
|
public abstract void BitWriterResize(int extraSize); |
||||
|
|
||||
|
protected bool ResizeBuffer(int maxBytes, int sizeRequired) |
||||
|
{ |
||||
|
if (maxBytes > 0 && sizeRequired < maxBytes) |
||||
|
{ |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
int newSize = (3 * maxBytes) >> 1; |
||||
|
if (newSize < sizeRequired) |
||||
|
{ |
||||
|
newSize = sizeRequired; |
||||
|
} |
||||
|
|
||||
|
// Make new size multiple of 1k.
|
||||
|
newSize = ((newSize >> 10) + 1) << 10; |
||||
|
|
||||
|
// TODO: use memory allocator.
|
||||
|
Array.Resize(ref this.buffer, newSize); |
||||
|
|
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,62 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Formats.WebP.Lossy |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// On-the-fly info about the current set of residuals.
|
||||
|
/// </summary>
|
||||
|
internal class Vp8Residual |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="Vp8Residual"/> class.
|
||||
|
/// </summary>
|
||||
|
public Vp8Residual() |
||||
|
{ |
||||
|
this.Prob = new Vp8ProbaArray[3][]; |
||||
|
for (int i = 0; i < 3; i++) |
||||
|
{ |
||||
|
this.Prob[i] = new Vp8ProbaArray[11]; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public int First { get; set; } |
||||
|
|
||||
|
public int Last { get; set; } |
||||
|
|
||||
|
public int CoeffType { get; set; } |
||||
|
|
||||
|
public short[] Coeffs { get; set; } |
||||
|
|
||||
|
public Vp8ProbaArray[][] Prob { get; } |
||||
|
|
||||
|
public void Init(int first, int coeffType) |
||||
|
{ |
||||
|
this.First = first; |
||||
|
this.CoeffType = coeffType; |
||||
|
|
||||
|
// TODO:
|
||||
|
// res->prob = enc->proba_.coeffs_[coeff_type];
|
||||
|
// res->stats = enc->proba_.stats_[coeff_type];
|
||||
|
// res->costs = enc->proba_.remapped_costs_[coeff_type];
|
||||
|
} |
||||
|
|
||||
|
public void SetCoeffs(short[] coeffs) |
||||
|
{ |
||||
|
int n; |
||||
|
this.Last = -1; |
||||
|
for (n = 15; n >= 0; --n) |
||||
|
{ |
||||
|
if (coeffs[n] != 0) |
||||
|
{ |
||||
|
this.Last = n; |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
this.Coeffs = coeffs; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue