From c9b56f85acc7a5e4ea7a115c2927f63508daeeab Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Wed, 18 Apr 2018 15:40:50 -0700 Subject: [PATCH] Pack GifLogicalScreen value directly --- .../Sections/GifLogicalScreenDescriptor.cs | 28 +++++++++++++++---- .../GifLogicalScreenDescriptorTests.cs | 23 +++++++++++++++ 2 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 tests/ImageSharp.Tests/Formats/Gif/Sections/GifLogicalScreenDescriptorTests.cs diff --git a/src/ImageSharp/Formats/Gif/Sections/GifLogicalScreenDescriptor.cs b/src/ImageSharp/Formats/Gif/Sections/GifLogicalScreenDescriptor.cs index 35056c6e7..1cfec4763 100644 --- a/src/ImageSharp/Formats/Gif/Sections/GifLogicalScreenDescriptor.cs +++ b/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; } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Formats/Gif/Sections/GifLogicalScreenDescriptorTests.cs b/tests/ImageSharp.Tests/Formats/Gif/Sections/GifLogicalScreenDescriptorTests.cs new file mode 100644 index 000000000..c6458d22f --- /dev/null +++ b/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)); + } + } +} \ No newline at end of file