Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.ccdtovcd.mpeg
Class DCT  view DCT download DCT.java

java.lang.Object
  extended byorg.ccdtovcd.mpeg.DCT

public class DCT
extends java.lang.Object

DCT - A Java implementation of the Discreet Cosine Transform




The discreet cosine transform converts spatial information to "frequency" or spectral information, with the X and Y axes representing frequencies of the signal in different dimensions. This allows for "lossy" compression of image data by determining which information can be thrown away without compromising the image.

The DCT is used in many compression and transmission codecs, such as JPEG, MPEG and others. The pixels when transformed are arraged from the most signifigant pixel to the least signifigant pixel. The DCT functions themselves are lossless. Pixel loss occurs when the least signifigant pixels are quantitized to 0.

This is NOT a JPEG or JFIF compliant implementation however it could be with very little extra work. (i.e. A huffman encoding stage needs to be added.) I am making this source availible in the hopes that someone will add this functionality to the class, if you do, please email me! As always, if you have any problems feel free to contact me. (Or comments, or praise, etc..)

Keep in mind that when compressing color images with this, you will need to break the image up into it's R G B components and preform the calculations three times!!

A general algorithim for DCT compression with this class:

1) Create a DCT Object.
2) Set up your program to read pixel information in 8*8 blocks. See example.
3) Run the forwardDCT() on all blocks.
4) Run the quantitizeImage() on all blocks.
5) If you want, send the information to the imageCompressor().

A general algorithim for DCT decompression with this class:

1) Create a DCT Object.
2) Set up the program to convert compressed data in 8*8 blocks. (if compressed)
3) Run the data through dequantitizeImage().
4) Run the data through inverseDCT().


A complete implementation of an image compressor which compares the quality of the two images is also availible. See the JEncode/Decode source code. The DCT.java source code is also availible. The implementation is also handy for seeing how to break the image down, read in 8x8 blocks, and reconstruct it. The sample graphic is handy to have too. (Bad pic of me) The best way to get ahold of me is through my homepage. There's lots of goodies there too.

Version:
1.0.1 August 22nd 1996

Field Summary
 double[][] c
          Cosine matrix.
 int COLS
          Image height - must correspond to imageArray bounds - default 200
 double[][] cT
          Transformed cosine matrix, N*N.
 int N
          DCT Block Size - default 8
 int QUALITY
          Image Quality (0-25) - default 25 (worst image / best compression)
 int[][] quantum
          Quantitization Matrix.
 int[][] resultDCT
          DCT Result Matrix
 int ROWS
          Image width - must correspond to imageArray bounds - default 320
 int[][] zigZag
          The ZigZag matrix.
 
Constructor Summary
DCT(int QUALITY)
          Constructs a new DCT object.
 
Method Summary
private  int bytetoInt(byte[] bitSet)
          This method uses bit shifting to convert an array of two bytes to a integer (16 bits max).
 int[] compressImage(int[] QDCT, boolean log)
          

This does not huffman code, it uses a minimal run-length encoding scheme.
 int[] decompressImage(int[] DCT, boolean log)
          This method determines the runs in the input data, decodes it and then returnss the corrected matrix.
 int[][] dequantitizeImage(int[][] inputData, boolean zigzag)
          This method reads in DCT codes dequanitizes them and places them in the correct location.
 int[][] forwardDCT(char[][] input)
          This method preforms a matrix multiplication of the input pixel data matrix by the transposed cosine matrix and store the result in a temporary N * N matrix.
private  void initMatrix(int quality)
          This method sets up the quantization matrix using the Quality parameter and then sets up the Cosine Transform Matrix and the Transposed CT.
private  void initZigZag()
          Initializes the ZigZag matrix.
private  byte[] inttoByte(int count)
          This method uses bit shifting to convert an integer to an array of two bytes, byte 0, the most signifigant and byte 1, the least signifigant.
 int[][] inverseDCT(int[][] input)
          This method is preformed using the reverse of the operations preformed in the DCT.
 int[][] quantitizeImage(int[][] inputData, boolean zigzag)
          This method orders the DCT result matrix into a zigzag pattern and then quantitizes the data.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

N

public final int N
DCT Block Size - default 8

See Also:
Constant Field Values

QUALITY

public int QUALITY
Image Quality (0-25) - default 25 (worst image / best compression)


ROWS

public int ROWS
Image width - must correspond to imageArray bounds - default 320


COLS

public int COLS
Image height - must correspond to imageArray bounds - default 200


zigZag

public int[][] zigZag
The ZigZag matrix.


c

public double[][] c
Cosine matrix. N * N.


cT

public double[][] cT
Transformed cosine matrix, N*N.


quantum

public int[][] quantum
Quantitization Matrix.


resultDCT

public int[][] resultDCT
DCT Result Matrix

Constructor Detail

DCT

public DCT(int QUALITY)
Constructs a new DCT object. Initializes the cosine transform matrix these are used when computing the DCT and it's inverse. This also initializes the run length counters and the ZigZag sequence. Note that the image quality can be worse than 25 however the image will be extemely pixelated, usually to a block size of N.

Method Detail

initMatrix

private void initMatrix(int quality)
This method sets up the quantization matrix using the Quality parameter and then sets up the Cosine Transform Matrix and the Transposed CT. These are used by the forward and inverse DCT. The RLE encoding variables are set up to track the number of consecutive zero values that have output or will be input.


initZigZag

private void initZigZag()
Initializes the ZigZag matrix.


forwardDCT

public int[][] forwardDCT(char[][] input)
This method preforms a matrix multiplication of the input pixel data matrix by the transposed cosine matrix and store the result in a temporary N * N matrix. This N * N matrix is then multiplied by the cosine matrix and the result is stored in the output matrix.


dequantitizeImage

public int[][] dequantitizeImage(int[][] inputData,
                                 boolean zigzag)
This method reads in DCT codes dequanitizes them and places them in the correct location. The codes are stored in the zigzag format so they need to be redirected to a N * N block through simple table lookup. After dequantitization the data needs to be run through an inverse DCT.


quantitizeImage

public int[][] quantitizeImage(int[][] inputData,
                               boolean zigzag)
This method orders the DCT result matrix into a zigzag pattern and then quantitizes the data. The quantitized value is rounded to the nearest integer. Pixels which round or divide to zero are the loss associated with quantitizing the image. These pixels do not display in the AWT. (null) Long runs of zeros and the small ints produced through this technique are responsible for the small image sizes. For block sizes < or > 8, disable the zigzag optimization. If zigzag is disabled on encode it must be disabled on decode as well.


compressImage

public int[] compressImage(int[] QDCT,
                           boolean log)


This does not huffman code, it uses a minimal run-length encoding scheme. Huffman, Adaptive Huffman or arithmetic encoding will give much better preformance. The information accepted is quantitized DCT data. The output array should be scanned to determine where the end is.


decompressImage

public int[] decompressImage(int[] DCT,
                             boolean log)
This method determines the runs in the input data, decodes it and then returnss the corrected matrix. It is used to decode the data from the compressImage method. Huffman encoding, Adaptive Huffman or Arithmetic will give much better compression.


inverseDCT

public int[][] inverseDCT(int[][] input)
This method is preformed using the reverse of the operations preformed in the DCT. This restores a N * N input block to the corresponding output block with values scaled to 0 to 255 and then stored in the input block of pixels.


bytetoInt

private int bytetoInt(byte[] bitSet)
This method uses bit shifting to convert an array of two bytes to a integer (16 bits max). Byte 0 is the most signifigant byte and Byte 1 is the least signifigant byte.


inttoByte

private byte[] inttoByte(int count)
This method uses bit shifting to convert an integer to an array of two bytes, byte 0, the most signifigant and byte 1, the least signifigant.