private static void FillUncompressedColorIndexBuffer(byte[] data, ref int currentEntrySize, ref bool firstRead) { BitReader reader = new BitReader(data); List<int> nextDictionaryEntry = new List<int>(); while ((reader.BitPosition + currentEntrySize) <= reader.BitLength) { int result = reader.Read(currentEntrySize); bool addToDictionary = !firstRead &&// don't add on the first read (mDictionary.Count < (1 << 12)); // dictionary capped at 12 bits List<int> pattern; if (result < mDictionary.Count) { pattern = mDictionary[result]; } else if (result == mDictionary.Count) { // special case for LZW pattern = new List<int>(nextDictionaryEntry); // pattern.Add(nextDictionaryEntry[nextDictionaryEntry.Count - 1]); pattern.Add(nextDictionaryEntry[0]); } else { int index = mUncompressedColorIndexBuffer.Count - 1; string error = "Error when reading pixel number " + (mUncompressedColorIndexBuffer.Count - 1); int row = (mUncompressedColorIndexBuffer.Count) / mGifInfo.Width + 1; int column = (mUncompressedColorIndexBuffer.Count) % mGifInfo.Width; error += "\n On the image that's (" + column + ", " + row + ")."; throw new Exception(error); } firstRead = false; // Check for constant values if (result == ClrConstant) { // Clear dictionary addToDictionary = false; firstRead = true; nextDictionaryEntry = new List<int>(); currentEntrySize = mGifInfo.LzwMin + 1; FillDictionary(); } else if (result == EndConstant) { // End of image break; } else { // It's just image data for (int i = 0; i < pattern.Count; i++) { mUncompressedColorIndexBuffer.Add((byte)pattern[i]); } #if DEBUG if (mUncompressedColorIndexBuffer.Count > mGifInfo.Width * mGifInfo.Height) { throw new InvalidOperationException("The uncompressed buffer is too big!"); } #endif } if (addToDictionary) { // add to dictionary List<int> newPattern = new List<int>(nextDictionaryEntry); // newPattern.Add(pattern[pattern.Count - 1]); newPattern.Add(pattern[0]); mDictionary.Add(newPattern); currentEntrySize = CurrentEntrySize; if (currentEntrySize == 13) { currentEntrySize = 12; } } if (result != ClrConstant) { nextDictionaryEntry = pattern; } } }
private static void FillUncompressedColorIndexBuffer(byte[] data, ref int currentEntrySize, ref bool firstRead) { BitReader reader = new BitReader(data); List <int> nextDictionaryEntry = new List <int>(); while ((reader.BitPosition + currentEntrySize) <= reader.BitLength) { int result = reader.Read(currentEntrySize); bool addToDictionary = !firstRead && // don't add on the first read (mDictionary.Count < (1 << 12)); // dictionary capped at 12 bits List <int> pattern; if (result < mDictionary.Count) { pattern = mDictionary[result]; } else if (result == mDictionary.Count) { // special case for LZW pattern = new List <int>(nextDictionaryEntry); // pattern.Add(nextDictionaryEntry[nextDictionaryEntry.Count - 1]); pattern.Add(nextDictionaryEntry[0]); } else { int index = mUncompressedColorIndexBuffer.Count - 1; string error = "Error when reading pixel number " + (mUncompressedColorIndexBuffer.Count - 1); int row = (mUncompressedColorIndexBuffer.Count) / mGifInfo.Width + 1; int column = (mUncompressedColorIndexBuffer.Count) % mGifInfo.Width; error += "\n On the image that's (" + column + ", " + row + ")."; throw new Exception(error); } firstRead = false; // Check for constant values if (result == ClrConstant) { // Clear dictionary addToDictionary = false; firstRead = true; nextDictionaryEntry = new List <int>(); currentEntrySize = mGifInfo.LzwMin + 1; FillDictionary(); } else if (result == EndConstant) { // End of image break; } else { // It's just image data for (int i = 0; i < pattern.Count; i++) { mUncompressedColorIndexBuffer.Add((byte)pattern[i]); } #if DEBUG if (mUncompressedColorIndexBuffer.Count > mGifInfo.Width * mGifInfo.Height) { throw new InvalidOperationException("The uncompressed buffer is too big!"); } #endif } if (addToDictionary) { // add to dictionary List <int> newPattern = new List <int>(nextDictionaryEntry); // newPattern.Add(pattern[pattern.Count - 1]); newPattern.Add(pattern[0]); mDictionary.Add(newPattern); currentEntrySize = CurrentEntrySize; if (currentEntrySize == 13) { currentEntrySize = 12; } } if (result != ClrConstant) { nextDictionaryEntry = pattern; } } }