GARbro-mirror/ArcFormats/BitStream.cs

142 lines
4.1 KiB
C#
Raw Permalink Normal View History

2015-08-23 11:30:29 +08:00
//! \file BitStream.cs
//! \date Sat Aug 22 21:33:39 2015
//! \brief Bit stream on top of the IO.Stream
//
// Copyright (C) 2015 by morkt
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//
using System;
using System.Diagnostics;
using System.IO;
namespace GameRes.Formats
{
2015-10-19 13:03:32 +08:00
public class BitStream : IDisposable
2015-08-23 11:30:29 +08:00
{
2015-10-19 13:03:32 +08:00
protected Stream m_input;
private bool m_should_dispose;
protected int m_bits = 0;
protected int m_cached_bits = 0;
2015-08-23 11:30:29 +08:00
public Stream Input { get { return m_input; } }
public int CacheSize { get { return m_cached_bits; } }
2015-08-23 11:30:29 +08:00
2015-10-19 13:03:32 +08:00
protected BitStream (Stream file, bool leave_open)
2015-08-23 11:30:29 +08:00
{
m_input = file;
m_should_dispose = !leave_open;
}
2015-08-27 07:07:38 +08:00
public void Reset ()
{
m_cached_bits = 0;
}
2015-10-19 13:03:32 +08:00
#region IDisposable Members
bool m_disposed = false;
public void Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this);
}
protected virtual void Dispose (bool disposing)
{
if (!m_disposed)
{
if (disposing && m_should_dispose && null != m_input)
m_input.Dispose();
m_disposed = true;
}
}
#endregion
}
2015-12-28 14:52:44 +08:00
public interface IBitStream
{
int GetBits (int count);
int GetNextBit ();
void Reset ();
}
public class MsbBitStream : BitStream, IBitStream
2015-10-19 13:03:32 +08:00
{
public MsbBitStream (Stream file, bool leave_open = false)
: base (file, leave_open)
2015-08-23 11:30:29 +08:00
{
}
public int GetBits (int count)
{
Debug.Assert (count <= 24, "MsbBitStream does not support sequences longer than 24 bits");
while (m_cached_bits < count)
{
int b = m_input.ReadByte();
if (-1 == b)
return -1;
m_bits = (m_bits << 8) | b;
m_cached_bits += 8;
}
int mask = (1 << count) - 1;
m_cached_bits -= count;
return (m_bits >> m_cached_bits) & mask;
}
2015-10-19 13:03:32 +08:00
public int GetNextBit ()
{
return GetBits (1);
}
}
2015-08-23 11:30:29 +08:00
2015-12-28 14:52:44 +08:00
public class LsbBitStream : BitStream, IBitStream
2015-10-19 13:03:32 +08:00
{
public LsbBitStream (Stream file, bool leave_open = false)
: base (file, leave_open)
2015-08-23 11:30:29 +08:00
{
}
2015-10-19 13:03:32 +08:00
public int GetBits (int count)
2015-08-23 11:30:29 +08:00
{
2015-10-19 13:03:32 +08:00
Debug.Assert (count <= 24, "LsbBitStream does not support sequences longer than 24 bits");
while (m_cached_bits < count)
2015-08-23 11:30:29 +08:00
{
2015-10-19 13:03:32 +08:00
int b = m_input.ReadByte();
if (-1 == b)
return -1;
m_bits |= b << m_cached_bits;
m_cached_bits += 8;
2015-08-23 11:30:29 +08:00
}
2015-10-19 13:03:32 +08:00
int mask = (1 << count) - 1;
int value = m_bits & mask;
m_bits = (int)((uint)m_bits >> count);
m_cached_bits -= count;
return value;
}
public int GetNextBit ()
{
return GetBits (1);
2015-08-23 11:30:29 +08:00
}
}
}