Class RunLengthCodec

All Implemented Interfaces:
Codec

public class RunLengthCodec extends AbstractVideoCodec
RunLengthCodec encodes a BufferedImage as a byte[] array.

This codec only works with the AVI file format. Other formats, such as QuickTime, use a different encoding for run-length compressed video.

This codec currently only supports encoding from a BufferedImage into the file format. Decoding support may be added in the future.

Supported input formats:

  • Format with BufferedImage.class, any width, any height, depth=8.
Supported output formats:
  • Format with byte[].class, same width and height as input format, depth=8.
The codec supports lossless delta- and key-frame encoding of images with 8 bits per pixel.

The codec does not encode the color palette of an image. This must be done separately.

A frame is compressed line by line from bottom to top.

Each line of a frame is compressed individually. A line consists of two-byte op-codes optionally followed by data. The end of the line is marked with the EOL op-code.

The following op-codes are supported:

  • 0x00 0x00
    Marks the end of a line.
  • 0x00 0x01
    Marks the end of the bitmap.
  • 0x00 0x02 x y
    Marks a delta (skip). x and y indicate the horizontal and vertical offset from the current position. x and y are unsigned 8-bit values.
  • 0x00 n data{n} 0x00?
    Marks a literal run. n gives the number of data bytes that follow. n must be between 3 and 255. If n is odd, a pad byte with the value 0x00 must be added.
  • n data
    Marks a repetition. n gives the number of times the data byte is repeated. n must be between 1 and 255.
Example:
 Compressed data         Expanded data

 03 04                   04 04 04
 05 06                   06 06 06 06 06
 00 03 45 56 67 00       45 56 67
 02 78                   78 78
 00 02 05 01             Move 5 right and 1 down
 02 78                   78 78
 00 00                   End of line
 09 1E                   1E 1E 1E 1E 1E 1E 1E 1E 1E
 00 01                   End of RLE bitmap
 

References:
http://wiki.multimedia.cx/index.php?title=Microsoft_RLE

Author:
Werner Randelshofer
  • Constructor Details

    • RunLengthCodec

      public RunLengthCodec()
  • Method Details

    • setOutputFormat

      public Format setOutputFormat(Format f)
      Description copied from interface: Codec
      Sets the output format. Returns the format that was actually set. This is the closest format that the Codec supports. Returns null if the specified format is not supported and no reasonable match could be found.
      Specified by:
      setOutputFormat in interface Codec
      Overrides:
      setOutputFormat in class AbstractCodec
    • reset

      public void reset()
      Description copied from class: AbstractCodec
      Empty implementation of the reset method. Don't call super.
      Specified by:
      reset in interface Codec
      Overrides:
      reset in class AbstractCodec
    • process

      public int process(Buffer in, Buffer out)
      Description copied from interface: Codec
      Performs the media processing defined by this codec.

      Copies the data from the input buffer into the output buffer.

      Returns:
      A combination of processing flags.
    • writeKey8

      public void writeKey8(OutputStream out, byte[] data, int width, int height, int offset, int scanlineStride) throws IOException
      Encodes an 8-bit key frame.
      Parameters:
      out - The output stream.
      data - The image data.
      width - The width of the image in data elements.
      offset - The offset to the first pixel in the data array.
      scanlineStride - The number to append to offset to get to the next scanline.
      Throws:
      IOException
    • writeKey8

      public void writeKey8(ImageOutputStream out, byte[] data, int width, int height, int offset, int scanlineStride) throws IOException
      Encodes an 8-bit key frame.
      Parameters:
      out - The output stream.
      data - The image data.
      width - The width of the image in data elements.
      offset - The offset to the first pixel in the data array.
      scanlineStride - The number to append to offset to get to the next scanline.
      Throws:
      IOException
    • writeDelta8

      public void writeDelta8(OutputStream out, byte[] data, byte[] prev, int width, int height, int offset, int scanlineStride) throws IOException
      Encodes an 8-bit key frame.
      Parameters:
      out - The output stream.
      data - The image data.
      width - The width of the image in data elements.
      offset - The offset to the first pixel in the data array.
      scanlineStride - The number to append to offset to get to the next scanline.
      Throws:
      IOException
    • writeDelta8

      public void writeDelta8(ImageOutputStream out, byte[] data, byte[] prev, int width, int height, int offset, int scanlineStride) throws IOException
      Encodes an 8-bit delta frame.
      Parameters:
      out - The output stream.
      data - The image data.
      prev - The image data of the previous frame.
      width - The width of the image in data elements.
      offset - The offset to the first pixel in the data array.
      scanlineStride - The number to append to offset to get to the next scanline.
      Throws:
      IOException