Skip to content

Commit

Permalink
reduce gif palette size when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
saucecontrol committed Nov 4, 2020
1 parent 9a560d3 commit 3ab3a52
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/MagicScaler/Magic/ColorQuantizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ internal class OctreeQuantizer : IDisposable

private uint leafLevel = 7;
private bool isSubsampled;
private int palEntries = maxPaletteSize;

private ArraySegment<byte> nodeBuffer, palBuffer;

public ReadOnlySpan<uint> Palette => MemoryMarshal.Cast<byte, uint>(palBuffer);
public ReadOnlySpan<uint> Palette => MemoryMarshal.Cast<byte, uint>(palBuffer).Slice(0, palEntries);

public OctreeQuantizer()
{
Expand Down Expand Up @@ -436,7 +437,10 @@ unsafe private nuint makePaletteMap(nuint minLevel)
}
}

Unsafe.InitBlockUnaligned(ppal + (maxPaletteSize - 1), 0, sizeof(uint));
ppal[palidx] = 0x00ff00ffu;
palEntries = (int)palidx + 1;

Unsafe.InitBlockUnaligned(ppal + palEntries, 0, (maxPaletteSize - (uint)palEntries) * sizeof(uint));
}

leafLevel = Math.Max(leafLevel, minLeafLevel);
Expand Down Expand Up @@ -622,7 +626,7 @@ unsafe private void addReducibleNodes(OctreeNode* ptree, ReducibleNode* preduce,
unsafe private void remapDitherSse2(byte* pimage, int* perr, byte* pout, uint* pilut, OctreeNode* ptree, uint* ppal, ref nuint nextFree, nint cp)
{
var transnode = new OctreeNode();
transnode.Sums[3] = maxPaletteSize - 1;
transnode.Sums[3] = (uint)palEntries - 1;

var vpmax = Vector128.Create((int)byte.MaxValue);
var vprnd = Vector128.Create(7);
Expand Down Expand Up @@ -761,7 +765,7 @@ unsafe private void remapDitherSse2(byte* pimage, int* perr, byte* pout, uint* p
unsafe private void remapDitherScalar(byte* pimage, int* perror, byte* pout, uint* pilut, OctreeNode* ptree, uint* ppal, ref nuint nextFree, nint cp)
{
var transnode = new OctreeNode();
transnode.Sums[3] = maxPaletteSize - 1;
transnode.Sums[3] = (uint)palEntries - 1;

nuint level = leafLevel;
var prnod = default(OctreeNode*);
Expand Down Expand Up @@ -900,7 +904,7 @@ unsafe private void remapDitherScalar(byte* pimage, int* perror, byte* pout, uin
unsafe private void remap(byte* pimage, byte* pout, uint* pilut, OctreeNode* ptree, uint* ppal, ref nuint nextFree, nint cp)
{
var transnode = new OctreeNode();
transnode.Sums[3] = maxPaletteSize - 1;
transnode.Sums[3] = (uint)palEntries - 1;

nuint level = leafLevel;
uint ppix = 0;
Expand Down Expand Up @@ -1078,9 +1082,10 @@ unsafe private void initNode(OctreeNode* node, int cb, int cg, int cr)
fixed (uint* ppal = MemoryMarshal.Cast<byte, uint>(palBuffer))
{
uint dist = uint.MaxValue;
uint pidx = maxPaletteSize - 1;
uint pidx = (uint)palEntries - 1;
nuint plen = pidx;

for (nuint i = 0; i < maxPaletteSize - 1; i++)
for (nuint i = 0; i < plen; i++)
{
uint pc = ppal[i];
int pb = (byte)(pc);
Expand Down

0 comments on commit 3ab3a52

Please sign in to comment.