ShrinkItArchive/src/test/java/com/webcodepro/shrinkit/io/LzwTest.java

116 lines
4.2 KiB
Java

package com.webcodepro.shrinkit.io;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
/**
* Exercise the LZW encoder and decoders.
*
* @author robgreene@users.sourceforge.net
*/
public class LzwTest extends TestCaseHelper {
public void testLzwDecoder() throws IOException {
LzwInputStream is = new LzwInputStream(new BitInputStream(new ByteArrayInputStream(getHgrColorsLzw1()), 9));
int[] expected = getHgrColorsUncompressed();
int[] actual = new int[expected.length];
for (int i=0; i<actual.length; i++) actual[i] = is.read();
assertEquals("Expecting end of stream", -1, is.read());
assertEquals(expected, actual);
}
public void testLzwEncoder() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
LzwOutputStream os = new LzwOutputStream(new BitOutputStream(baos, 9));
byte[] expected = getHgrColorsLzw1();
os.write(asBytes(getHgrColorsUncompressed()));
os.close();
byte[] actual = baos.toByteArray();
assertEquals(expected, actual);
}
public void testLzwDecoder2() throws IOException {
RleInputStream is = new RleInputStream(new LittleEndianByteInputStream(new LzwInputStream(new BitInputStream(new ByteArrayInputStream(getHgrColorsLzw1()), 9))));
int bytes = 0;
int b;
while ( (b = is.read()) != -1) {
if (bytes % 16 == 0) {
if (bytes > 0) System.out.println();
System.out.printf("%08x: ", bytes);
}
System.out.printf("%02x ", b);
bytes++;
}
}
public void testLzwDecoder3() throws IOException {
LzwInputStream is = new LzwInputStream(new BitInputStream(new ByteArrayInputStream(getTextFileLzw1()), 9));
System.out.printf("\n\nText File decoded...\n\n");
int i = 0;
int b = 0;
while (b != -1) {
b = is.read();
if (b != -1) System.out.printf("$%04x: %02x (%c)\n", i++, b, b);
}
System.out.printf("** END **");
}
protected byte[] asBytes(int[] source) {
byte[] array = new byte[source.length];
for (int i=0; i<source.length; i++) array[i] = (byte)(source[i] & 0xff);
return array;
}
protected int[] asInts(byte[] source) {
int[] array = new int[source.length];
for (int i=0; i<source.length; i++) array[i] = source[i] & 0xff;
return array;
}
protected byte[] getHgrColorsLzw1() {
return new byte[] {
(byte)0xdb, 0x00, 0x1c, (byte)0xd8, 0x56, 0x65, (byte)0xa0, (byte)0x8a,
(byte)0x81, 0x00, (byte)0xde, 0x6c, 0x3b, 0x48, 0x10, (byte)0xa1,
(byte)0xc2, 0x3f, 0x0f, 0x02, (byte)0xfe, (byte)0x93, 0x48, 0x11,
(byte)0xc0, 0x44, (byte)0x8b, 0x15, 0x2f, 0x6a, (byte)0xcc, (byte)0xc8,
0x11, 0x23, (byte)0x80, 0x73, 0x00
};
}
protected int[] getHgrColorsUncompressed() {
return new int[] {
0xdb, 0x00, 0x07, 0xdb, 0x55, 0x07, 0xdb, 0x2a,
0x07, 0xdb, 0x00, 0x6f, 0xdb, 0x2a, 0x07, 0xdb,
0x55, 0x07, 0xdb, 0x00, 0x6f, 0xdb, 0x7f, 0x0f,
0xdb, 0x00, 0xff, 0xdb, 0x00, 0xff, 0xdb, 0x00,
0xff, 0xdb, 0x00, 0xff, 0xdb, 0x00, 0xff, 0xdb,
0x00, 0xff, 0xdb, 0x00, 0xff, 0xdb, 0x00, 0xff,
0xdb, 0x00, 0xff, 0xdb, 0x00, 0xff, 0xdb, 0x00,
0xff, 0xdb, 0x00, 0xff, 0xdb, 0x00, 0xff, 0xdb,
0x00, 0xff, 0xdb, 0x00, 0xe7
};
}
protected byte[] getTextFileLzw1() {
return new byte[] {
0x54, (byte)0x90, 0x24, (byte)0x99, 0x02, 0x62, 0x20, (byte)0x88,
(byte)0x80, 0x45, 0x40, 0x5C, 0x09, (byte)0x92, 0x45, 0x61,
(byte)0xC2, (byte)0x85, 0x53, (byte)0x90, (byte)0x80, 0x78, 0x52, 0x45,
0x0A, (byte)0x88, 0x21, 0x4C, (byte)0x9E, 0x20, (byte)0x9C, (byte)0xC2,
0x42, 0x61, (byte)0x90, (byte)0x88, 0x13, 0x2B, 0x5E, (byte)0xCC,
(byte)0xB8, (byte)0xB1, 0x23, 0x44, (byte)0x89, 0x14, 0x2D, 0x62,
(byte)0xD4, (byte)0x88, (byte)0xA4, (byte)0xC8, 0x14, 0x17, 0x20, 0x0E,
0x0A, 0x24, 0x68, 0x10, (byte)0xA1, (byte)0xC7, (byte)0x86, 0x57,
0x1E, 0x7E, 0x44, 0x29, 0x72, 0x65, 0x49, 0x10,
0x53, (byte)0x9E, (byte)0x80, 0x28, 0x12, 0x44, 0x0A, (byte)0x93,
(byte)0x86, 0x49, (byte)0x9C, (byte)0xC8, 0x4C, (byte)0xD8, (byte)0xE4, (byte)0x89,
0x14, 0x27, 0x49, (byte)0x8F, (byte)0xB8, (byte)0xD8, 0x06, (byte)0xE0,
0x1F, 0x55, (byte)0xAB, 0x55, (byte)0xAF, 0x6A, (byte)0xCD, (byte)0xCA,
0x15, (byte)0xAB, (byte)0xD7, (byte)0xAD, 0x5F, (byte)0xBB, 0x52, (byte)0xC5,
0x03, 0x00
};
}
}