nayuki.huffmancoding.AdaptiveHuffmanCompress Maven / Gradle / Ivy
/**
*
* Copyright © 2013 Nayuki Minase
*
* 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.
*/
package nayuki.huffmancoding;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
public final class AdaptiveHuffmanCompress {
public static void main(String[] args) throws IOException {
// Show what command line arguments to use
if (args.length == 0) {
System.err.println("Usage: java AdaptiveHuffmanCompress InputFile OutputFile");
System.exit(1);
return;
}
// Otherwise, compress
File inputFile = new File(args[0]);
File outputFile = new File(args[1]);
InputStream in = new BufferedInputStream(new FileInputStream(inputFile));
BitOutputStream out = new BitOutputStream(new BufferedOutputStream(new FileOutputStream(outputFile)));
try {
compress(in, out);
} finally {
out.close();
in.close();
}
}
public static void compress(InputStream in, BitOutputStream out) throws IOException {
int[] initFreqs = new int[257];
Arrays.fill(initFreqs, 1);
FrequencyTable freqTable = new FrequencyTable(initFreqs);
HuffmanEncoder enc = new HuffmanEncoder(out);
enc.codeTree = freqTable.buildCodeTree(); // We don't need to make a canonical code since we don't transmit the code tree
int count = 0;
while (true) {
int b = in.read();
if (b == -1)
break;
enc.write(b);
freqTable.increment(b);
count++;
if (count < 262144 && isPowerOf2(count) || count % 262144 == 0) // Update code tree
enc.codeTree = freqTable.buildCodeTree();
if (count % 262144 == 0) // Reset frequency table
freqTable = new FrequencyTable(initFreqs);
}
enc.write(256); // EOF
}
private static boolean isPowerOf2(int x) {
return x > 0 && (x & -x) == x;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy