sharplib/ar/ArithmeticDecompress.cs

100 lines
3.0 KiB
C#

using System;
using System.IO;
/*
* Reference arithmetic coding
* Copyright (c) Project Nayuki
*
* https://www.nayuki.io/page/reference-arithmetic-coding
* https://github.com/nayuki/Reference-arithmetic-coding
*/
/// <summary>
/// Decompression application using static arithmetic coding.
/// <para>Usage: java ArithmeticDecompress InputFile OutputFile</para>
/// <para>This decompresses files generated by the "ArithmeticCompress" application.</para>
/// </summary>
public class ArithmeticDecompress
{
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public static void main(String[] args) throws java.io.IOException
public static void Main( string[] args )
{
// Handle command line arguments
if( args.Length != 2 )
{
Console.Error.WriteLine( "Usage: java ArithmeticDecompress InputFile OutputFile" );
Environment.Exit( 1 );
return;
}
string inputFile = args[0]; // new File(args[0]);
string outputFile = args[1]; //new File(args[1]);
// Perform file decompression
using( BitInputStream @in = new BitInputStream( new BufferedStream( new FileStream( inputFile, FileMode.Open, FileAccess.Read ) ) ) )
using( Stream @out = new BufferedStream( new FileStream( outputFile, FileMode.Create, FileAccess.Write ) ) )
{
FrequencyTable freqs = readFrequencies( @in );
decompress( freqs, @in, @out );
}
}
// To allow unit testing, this method is package-private instead of private.
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: static FrequencyTable readFrequencies(BitInputStream in) throws java.io.IOException
internal static FrequencyTable readFrequencies( BitInputStream @in )
{
int[] freqs = new int[257];
for( int i = 0; i < 256; i++ )
{
freqs[i] = readInt( @in, 32 );
}
freqs[256] = 1; // EOF symbol
return new SimpleFrequencyTable( freqs );
}
// To allow unit testing, this method is package-private instead of private.
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: static void decompress(FrequencyTable freqs, BitInputStream in, java.io.OutputStream out) throws java.io.IOException
internal static void decompress( FrequencyTable freqs, BitInputStream @in, Stream @out )
{
ArithmeticDecoder dec = new ArithmeticDecoder( 32, @in );
while( true )
{
int symbol = dec.read( freqs );
if( symbol == 256 ) // EOF symbol
{
break;
}
@out.WriteByte( (byte)symbol );
}
}
// Reads an unsigned integer of the given bit width from the given stream.
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private static int readInt(BitInputStream in, int numBits) throws java.io.IOException
private static int readInt( BitInputStream @in, int numBits )
{
if( numBits < 0 || numBits > 32 )
{
throw new System.ArgumentException();
}
int result = 0;
for( int i = 0; i < numBits; i++ )
{
result = ( result << 1 ) | @in.readNoEof(); // Big endian
}
return result;
}
}