com.sun.jimi.core.util
Class Packbits

java.lang.Object
  |
  +--com.sun.jimi.core.util.Packbits

public class Packbits
extends java.lang.Object

Packbits compression utility class.

Contains both unpack and packing method for the Macintosh Packbits compression standard.

Contains Variation for 16 bit RLE packing used in PICT files.

Contains a main() method with some test code for the pack unpack methods.


Constructor Summary
Packbits()
           
 
Method Summary
static int getAllRuns(byte[] inb)
          Calculate length of every byte run in the input buffer and store into runb[] buffer.
static int packbits(byte[] inb, byte[] outb)
          Compress input buffer using RLE packbits compression.
static void unpackbits(byte[] inb, byte[] outb)
          Decompress an RLE compressed buffer of bytes as per format as described in source or in a document on TIFF packbits compression.
static void unpackbits(byte[] inb, int[] outb)
          Variation for 16 bit RLE packing used in PICT files.
static void unpackbits(byte[] inb, int ini, byte[] outb, int outi, int len)
          More general.
static void unpackbits(java.io.DataInputStream in, byte[] outb)
           
static void unpackbitsLimit(byte[] inb, int inbLen, byte[] outb)
          Variation for PICT - which provides short inb[] for a give outb[] This method when it exhausts input byte buffer it clears the remainder of the output buffer oub.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Packbits

public Packbits()
Method Detail

unpackbits

public static void unpackbits(byte[] inb,
                              int ini,
                              byte[] outb,
                              int outi,
                              int len)
More general. [ not really required so unimplemented at the moment.
Parameters:
inb - input buffer
ini - where compressed data in input buffer starts
outb - start output index
outi - where to uncompress data to in output buffer
len - length of the data after decompression.

unpackbitsLimit

public static void unpackbitsLimit(byte[] inb,
                                   int inbLen,
                                   byte[] outb)
                            throws java.lang.ArrayStoreException,
                                   java.lang.ArrayIndexOutOfBoundsException
Variation for PICT - which provides short inb[] for a give outb[] This method when it exhausts input byte buffer it clears the remainder of the output buffer oub.

unpackbits

public static void unpackbits(byte[] inb,
                              byte[] outb)
                       throws java.lang.ArrayStoreException,
                              java.lang.ArrayIndexOutOfBoundsException
Decompress an RLE compressed buffer of bytes as per format as described in source or in a document on TIFF packbits compression. Worst case for compression is that for every 128 bytes to compress 1 extra byte is required to encode as a copy run. therefore the input buffer should be large enough to accomodate the worst case compression to the output buffer. Throws ArrayStoreException if the array is not large enough for the unpacked data, which could be caused by corrupt input data or the array not be allocated large enough.
Parameters:
inb - input buffer with compressed data
outb - output buffer for decompressed data the length of the output buffer must be exact decompressed lenght of compressed input.

unpackbits

public static void unpackbits(byte[] inb,
                              int[] outb)
                       throws java.lang.ArrayStoreException,
                              java.lang.ArrayIndexOutOfBoundsException
Variation for 16 bit RLE packing used in PICT files.

unpackbits

public static void unpackbits(java.io.DataInputStream in,
                              byte[] outb)
                       throws java.lang.ArrayStoreException,
                              java.lang.ArrayIndexOutOfBoundsException,
                              java.io.IOException
Parameters:
in - data input stream to read packbits data stream from
outb - byte buffer to place unpacked byte data to. It is assumed that unpackbits is called with outb big enough for a single sequence of compressed bytes

getAllRuns

public static int getAllRuns(byte[] inb)
Calculate length of every byte run in the input buffer and store into runb[] buffer. Note that the count - 1 value is stored so data stored ranges from 0 to 127 which represents the values 1 to 128. e.g INPUT: 03 04 05 05 06 07 08 09 09 09 OUTPUT: 00 00 01 00 00 00 02 Note that a runlength of > 128 bytes is encoded as multiple runs of 128 bytes. Writing byte value of 128 into a byte array in java actually casts it to a signed value of -128.
Parameters:
inb - raw data to calculate byte runs on
runs - output to contain length of each byterun this buffer must be as long as input buffer to handle worst case.

packbits

public static int packbits(byte[] inb,
                           byte[] outb)
                    throws java.lang.ArrayStoreException,
                           java.lang.ArrayIndexOutOfBoundsException
Compress input buffer using RLE packbits compression. The second part of the code passes through the output compressed bytes and converts any 2 by replicate runs which are between literal runs into a literal run.. [ shit cant do if literal gets > 128 then i have start inserting bytes what a suck... ]
Parameters:
inb - input buffer containing bytes to be compressed using RLE packbits algorithm. buffer is assumed to be completely filled.
outb - output buffer containing compressed data. This must be large enough for worst case compression of input buffer. [ worst case is 1 additional byte for every 128 bytes of input buffer with a minimum size of 2 bytes ]
Returns:
the number of bytes in the output buffer. It should not be possible to return a value < 2.
 Example Input buffer.
   03 04 05 05 06 07 08
 Example Stoopid encoding
   01 03 04 -01 05 02 06 07 08
 Example Proper encoding
   06 03 04 05 05 06 07 08