2016-02-24 21:11:14 +00:00
|
|
|
package com.bytezone.diskbrowser.utilities;
|
2015-06-01 09:35:51 +00:00
|
|
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
2020-02-07 23:26:38 +00:00
|
|
|
// -----------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
class LZW1 extends LZW
|
2020-02-07 23:26:38 +00:00
|
|
|
// -----------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
2020-02-07 23:26:38 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
public LZW1 (byte[] buffer)
|
2020-02-07 23:26:38 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
|
|
|
bytes = Objects.requireNonNull (buffer);
|
|
|
|
|
2017-04-26 08:04:48 +00:00
|
|
|
crc = Utility.getWord (buffer, 0);
|
2015-06-01 09:35:51 +00:00
|
|
|
crcBase = 0;
|
|
|
|
|
|
|
|
volume = buffer[2] & 0xFF;
|
|
|
|
runLengthChar = (byte) (buffer[3] & 0xFF);
|
|
|
|
int ptr = 4;
|
|
|
|
|
|
|
|
while (ptr < buffer.length - 1) // what is in the last byte?
|
|
|
|
{
|
2017-04-26 08:04:48 +00:00
|
|
|
int rleLength = Utility.getWord (buffer, ptr);
|
2015-06-01 09:35:51 +00:00
|
|
|
int lzwPerformed = buffer[ptr + 2] & 0xFF;
|
|
|
|
ptr += 3;
|
|
|
|
|
|
|
|
if (lzwPerformed != 0)
|
|
|
|
{
|
|
|
|
setBuffer (buffer, ptr); // prepare to read n-bit integers
|
|
|
|
byte[] lzwBuffer = undoLZW (rleLength);
|
|
|
|
|
|
|
|
if (rleLength == TRACK_LENGTH) // no run length encoding
|
|
|
|
chunks.add (lzwBuffer);
|
|
|
|
else
|
|
|
|
chunks.add (undoRLE (lzwBuffer, 0, lzwBuffer.length));
|
|
|
|
|
|
|
|
ptr += bytesRead (); // since the setBuffer()
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (rleLength == TRACK_LENGTH) // no run length encoding
|
|
|
|
{
|
|
|
|
byte[] originalBuffer = new byte[TRACK_LENGTH];
|
|
|
|
System.arraycopy (buffer, ptr, originalBuffer, 0, originalBuffer.length);
|
|
|
|
chunks.add (originalBuffer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
chunks.add (undoRLE (buffer, ptr, rleLength));
|
|
|
|
|
|
|
|
ptr += rleLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 23:26:38 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
protected byte[] undoLZW (int rleLength)
|
2020-02-07 23:26:38 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
2020-02-07 23:26:38 +00:00
|
|
|
byte[] lzwBuffer = new byte[rleLength]; // must fill this array from input
|
2015-06-01 09:35:51 +00:00
|
|
|
int ptr = 0;
|
2020-02-07 23:26:38 +00:00
|
|
|
int nextEntry = 0x100; // always start with a fresh table
|
2015-06-01 09:35:51 +00:00
|
|
|
String prev = "";
|
|
|
|
|
|
|
|
while (ptr < rleLength)
|
|
|
|
{
|
|
|
|
int codeWord = readInt (width (nextEntry + 1));
|
|
|
|
String s = (nextEntry == codeWord) ? prev + prev.charAt (0) : st[codeWord];
|
|
|
|
|
|
|
|
if (nextEntry < st.length)
|
|
|
|
st[nextEntry++] = prev + s.charAt (0);
|
|
|
|
|
|
|
|
for (int i = 0; i < s.length (); i++)
|
|
|
|
lzwBuffer[ptr++] = (byte) s.charAt (i);
|
|
|
|
|
|
|
|
prev = s;
|
|
|
|
}
|
2021-04-17 04:41:36 +00:00
|
|
|
|
2015-06-01 09:35:51 +00:00
|
|
|
return lzwBuffer;
|
|
|
|
}
|
|
|
|
|
2020-02-07 23:26:38 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
@Override
|
|
|
|
public String toString ()
|
2020-02-07 23:26:38 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
|
|
|
StringBuilder text = new StringBuilder ();
|
|
|
|
|
|
|
|
text.append (String.format (" crc ............... %,d (%04X)%n", crc, crc));
|
|
|
|
text.append (String.format (" volume ............ %,d%n", volume));
|
|
|
|
text.append (String.format (" RLE char .......... $%02X", runLengthChar));
|
|
|
|
|
|
|
|
return text.toString ();
|
|
|
|
}
|
|
|
|
}
|