diff --git a/src/ImageSharp/Formats/WebP/ColorCache.cs b/src/ImageSharp/Formats/WebP/ColorCache.cs new file mode 100644 index 000000000..eac1721e8 --- /dev/null +++ b/src/ImageSharp/Formats/WebP/ColorCache.cs @@ -0,0 +1,22 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System.Collections.Generic; + +namespace SixLabors.ImageSharp.Formats.WebP +{ + internal class ColorCache + { + /// + /// Color entries. + /// + public List Colors { get; set; } + + /// + /// Hash shift: 32 - hashBits. + /// + public uint HashShift { get; set; } + + public uint HashBits { get; set; } + } +} diff --git a/src/ImageSharp/Formats/WebP/WebPLosslessDecoder.cs b/src/ImageSharp/Formats/WebP/WebPLosslessDecoder.cs index ee33af73e..7bf819035 100644 --- a/src/ImageSharp/Formats/WebP/WebPLosslessDecoder.cs +++ b/src/ImageSharp/Formats/WebP/WebPLosslessDecoder.cs @@ -65,17 +65,33 @@ namespace SixLabors.ImageSharp.Formats.WebP // Read color cache, if present. bool colorCachePresent = this.bitReader.ReadBit(); int colorCacheBits = 0; + int colorCacheSize = 0; + var colorCache = new ColorCache(); if (colorCachePresent) { colorCacheBits = (int)this.bitReader.ReadBits(4); - int colorCacheSize = 1 << colorCacheBits; + colorCacheSize = 1 << colorCacheBits; if (!(colorCacheBits >= 1 && colorCacheBits <= WebPConstants.MaxColorCacheBits)) { WebPThrowHelper.ThrowImageFormatException("Invalid color cache bits found"); } + + int hashSize = 1 << colorCacheBits; + colorCache.Colors = new List(hashSize); + colorCache.HashBits = (uint)colorCacheBits; + colorCache.HashShift = (uint)(32 - colorCacheBits); } this.ReadHuffmanCodes(xsize, ysize, colorCacheBits); + + int lastPixel = 0; + int row = lastPixel / width; + int col = lastPixel % width; + int lenCodeLimit = WebPConstants.NumLiteralCodes + WebPConstants.NumLengthCodes; + int colorCacheLimit = lenCodeLimit + colorCacheSize; + bool decIsIncremental = false; // TODO: determine correct value for decIsIncremental + int nextSyncRow = decIsIncremental ? row : 1 << 24; + } private void ReadHuffmanCodes(int xsize, int ysize, int colorCacheBits, bool allowRecursion = true)