Browse Source

Pack GifLogicalScreen value directly

af/merge-core
Jason Nelson 8 years ago
parent
commit
c9b56f85ac
  1. 28
      src/ImageSharp/Formats/Gif/Sections/GifLogicalScreenDescriptor.cs
  2. 23
      tests/ImageSharp.Tests/Formats/Gif/Sections/GifLogicalScreenDescriptorTests.cs

28
src/ImageSharp/Formats/Gif/Sections/GifLogicalScreenDescriptor.cs

@ -104,14 +104,30 @@ namespace SixLabors.ImageSharp.Formats.Gif
public static byte GetPackedValue(bool globalColorTableFlag, int colorResolution, bool sortFlag, int globalColorTableSize)
{
PackedField field = default;
/*
Global Color Table Flag | 1 Bit
Color Resolution | 3 Bits
Sort Flag | 1 Bit
Size of Global Color Table | 3 Bits
*/
field.SetBit(0, globalColorTableFlag); // 0 : Global Color Table Flag | 1 bit
field.SetBits(1, 3, globalColorTableSize); // 1-3 : Color Resolution | 3 bits
field.SetBit(4, sortFlag); // 4 : Sort Flag | 1 bits
field.SetBits(5, 3, globalColorTableSize); // 5-7 : Size of Global Color Table | 3 bits
byte value = 0;
return field.Byte;
if (globalColorTableFlag)
{
value |= 1 << 7;
}
value |= (byte)(colorResolution << 4);
if (sortFlag)
{
value |= 1 << 3;
}
value |= (byte)globalColorTableSize;
return value;
}
}
}

23
tests/ImageSharp.Tests/Formats/Gif/Sections/GifLogicalScreenDescriptorTests.cs

@ -0,0 +1,23 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using SixLabors.ImageSharp.Formats.Gif;
using Xunit;
namespace SixLabors.ImageSharp.Tests.Formats.Gif
{
public class GifLogicalScreenDescriptorTests
{
[Fact]
public void TestPackedValue()
{
Assert.Equal(0, GifLogicalScreenDescriptor.GetPackedValue(false, 0, false, 0));
Assert.Equal(128, GifLogicalScreenDescriptor.GetPackedValue(true, 0, false, 0)); // globalColorTableFlag
Assert.Equal(8, GifLogicalScreenDescriptor.GetPackedValue(false, 0, true, 0)); // sortFlag
Assert.Equal(48, GifLogicalScreenDescriptor.GetPackedValue(false, 3, false, 0));
Assert.Equal(155, GifLogicalScreenDescriptor.GetPackedValue(true, 1, true, 3));
Assert.Equal(55, GifLogicalScreenDescriptor.GetPackedValue(false, 3, false, 7));
}
}
}
Loading…
Cancel
Save