Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions Runtime/Plugins/HyperlinkXNFT/HyperlinkXNFT.jslib

This file was deleted.

32 changes: 0 additions & 32 deletions Runtime/Plugins/HyperlinkXNFT/HyperlinkXNFT.jslib.meta

This file was deleted.

28 changes: 0 additions & 28 deletions Runtime/Plugins/HyperlinkXNFT/HyperlinkXnft.cs

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Runtime/Plugins/Unity-GifDecoder/Decode.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

114 changes: 114 additions & 0 deletions Runtime/Plugins/Unity-GifDecoder/Decode/GifBitBlockReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using System.IO;
using ThreeDISevenZeroR.UnityGifDecoder.Utils;

namespace ThreeDISevenZeroR.UnityGifDecoder
{
/// <summary>
/// Reader for GIF Blocks
/// </summary>
public class GifBitBlockReader
{
private Stream stream;
private int currentByte;
private int currentBitPosition;
private int currentBufferPosition;
private int currentBufferSize;
private bool endReached;
private readonly byte[] buffer;

public GifBitBlockReader()
{
buffer = new byte[256];
}

public GifBitBlockReader(Stream stream) : this()
{
SetStream(stream);
}

/// <summary>
/// Set new stream
/// </summary>
public void SetStream(Stream stream)
{
this.stream = stream;
}

/// <summary>
/// Read first block and initialize reading
/// </summary>
public void StartNewReading()
{
currentByte = 0;
currentBitPosition = 8;
ReadNextBlock();
}

/// <summary>
/// Skips to the last block, if end is not reached
/// </summary>
public void FinishReading()
{
while (!endReached)
{
ReadNextBlock();
}
}

/// <summary>
/// Read bits from stream and construct value
/// </summary>
/// <param name="count">Bit count to read</param>
/// <returns>Value from readed bits</returns>
public int ReadBits(int count)
{
var result = 0;
var bitsToRead = count;
var offset = 0;
var bitsAvailable = 8 - currentBitPosition;

while(bitsToRead > 0)
{
if (currentBitPosition >= 8)
{
currentBitPosition = 0;
bitsAvailable = 8;

if (endReached)
{
// Some gifs can read slightly past end of a stream
// (since there is a zero byte afterwards anyway, it is safe to return 0)
currentByte = 0;
}
else
{
currentByte = buffer[currentBufferPosition++];
if (currentBufferPosition == currentBufferSize)
ReadNextBlock();
}
}

var mask = (byte) (((1 << bitsToRead) - 1) << currentBitPosition);
var readCount = bitsAvailable < bitsToRead ? bitsAvailable : bitsToRead;

result += ((mask & currentByte) >> currentBitPosition) << offset;

currentBitPosition += readCount;
bitsToRead -= readCount;
offset += readCount;
}

return result;
}

private void ReadNextBlock()
{
currentBufferSize = stream.ReadByte8();
currentBufferPosition = 0;
endReached = currentBufferSize == 0;

if(!endReached)
stream.Read(buffer, 0, currentBufferSize);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading