Professional Documents
Culture Documents
Multimedia Final
Multimedia Final
Team’s Name
/***************************************************************
**************
* Jpeg("Imagefile", Quality, "OutFileName") According to JAVA virtual
* machine, the files which can be read are jpeg, tiff and gif files
****************************************************************
************/
// Version 1.0a
// Copyright (C) 1998, James R. Weeks and BioElectroMech.
// Visit BioElectroMech at www.obrador.com. Email James@obrador.com.
// See license.txt for details about the allowed used of this software.
// This software is based in part on the work of the Independent JPEG Group.
// See IJGreadme.txt for details about the Independent JPEG Group's license.
// This encoder is inspired by the Java Jpeg encoder by Florian Raemy,
// studwww.eurecom.fr/~raemy.
// It borrows a great deal of code and structure from the Independent
// Jpeg Group's Jpeg 6a library, Copyright Thomas G. Lane.
// See license.txt for details.
/*
* JpegEncoder - The JPEG main program which performs a jpeg compression
of an
* image.
*/
class JpegEncoder extends Frame {
Thread runner;
BufferedOutputStream outStream;
Image image;
JpegInfo JpegObj;
Huffman Huf;
DCT dct;
int Quality;
int code;
public static int[] jpegNaturalOrder = { 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25,
18, 11, 4, 5,
12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57,
50, 43, 36,
29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54,
47, 55, 62,
63, };
/*
* Getting picture information It takes the Width, Height and RGB scans of
* the image.
*/
JpegObj = new JpegInfo(image);
imageHeight = JpegObj.imageHeight;
imageWidth = JpegObj.imageWidth;
outStream = new BufferedOutputStream(out);
dct = new DCT(Quality);
Huf = new Huffman(imageWidth, imageHeight);
}
/*
* This method controls the compression of the image. Starting at the upper
* left of the image, it compresses 8x8 blocks of data until the entire
* image has been compressed.
*/
// Comment Header
String comment = "";
comment = JpegObj.getComment();
length = comment.length();
byte COM[] = new byte[length + 4];
COM[0] = (byte) 0xFF;
COM[1] = (byte) 0xFE;
COM[2] = (byte) ((length >> 8) & 0xFF);
COM[3] = (byte) (length & 0xFF);
java.lang.System.arraycopy(JpegObj.Comment.getBytes(), 0, COM, 4,
JpegObj.Comment.length());
WriteArray(COM, out);
class DCT {
/**
* DCT Block Size - default 8
*/
public int N = 8;
/**
* Image Quality (0-100) - default 80 (good image / good compression)
*/
public int QUALITY = 80;
/**
* Quantitization Matrix for luminace.
*/
public int quantum_luminance[] = new int[N * N];
/**
* 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.
*
* @param QUALITY
* The quality of the image (0 worst - 100 best)
*
*/
public DCT(int QUALITY) {
initMatrix(QUALITY);
}
/*
* This method sets up the quantization matrix for luminance and chrominance
* using the Quality parameter.
*/
private void initMatrix(int quality) {
double[] AANscaleFactor = { 1.0, 1.387039845, 1.306562965, 1.175875602,
1.0, 0.785694958,
0.541196100, 0.275899379 };
int i;
int j;
int index;
int Quality;
int temp;
Quality = quality;
if (Quality <= 0)
Quality = 1;
if (Quality > 100)
Quality = 100;
if (Quality < 50)
Quality = 5000 / Quality;
else
Quality = 200 - Quality * 2;
quantum_luminance[0] = 16;
quantum_luminance[1] = 11;
quantum_luminance[2] = 10;
quantum_luminance[3] = 16;
quantum_luminance[4] = 24;
quantum_luminance[5] = 40;
quantum_luminance[6] = 51;
quantum_luminance[7] = 61;
quantum_luminance[8] = 12;
quantum_luminance[9] = 12;
quantum_luminance[10] = 14;
quantum_luminance[11] = 19;
quantum_luminance[12] = 26;
quantum_luminance[13] = 58;
quantum_luminance[14] = 60;
quantum_luminance[15] = 55;
quantum_luminance[16] = 14;
quantum_luminance[17] = 13;
quantum_luminance[18] = 16;
quantum_luminance[19] = 24;
quantum_luminance[20] = 40;
quantum_luminance[21] = 57;
quantum_luminance[22] = 69;
quantum_luminance[23] = 56;
quantum_luminance[24] = 14;
quantum_luminance[25] = 17;
quantum_luminance[26] = 22;
quantum_luminance[27] = 29;
quantum_luminance[28] = 51;
quantum_luminance[29] = 87;
quantum_luminance[30] = 80;
quantum_luminance[31] = 62;
quantum_luminance[32] = 18;
quantum_luminance[33] = 22;
quantum_luminance[34] = 37;
quantum_luminance[35] = 56;
quantum_luminance[36] = 68;
quantum_luminance[37] = 109;
quantum_luminance[38] = 103;
quantum_luminance[39] = 77;
quantum_luminance[40] = 24;
quantum_luminance[41] = 35;
quantum_luminance[42] = 55;
quantum_luminance[43] = 64;
quantum_luminance[44] = 81;
quantum_luminance[45] = 104;
quantum_luminance[46] = 113;
quantum_luminance[47] = 92;
quantum_luminance[48] = 49;
quantum_luminance[49] = 64;
quantum_luminance[50] = 78;
quantum_luminance[51] = 87;
quantum_luminance[52] = 103;
quantum_luminance[53] = 121;
quantum_luminance[54] = 120;
quantum_luminance[55] = 101;
quantum_luminance[56] = 72;
quantum_luminance[57] = 92;
quantum_luminance[58] = 95;
quantum_luminance[59] = 98;
quantum_luminance[60] = 112;
quantum_luminance[61] = 100;
quantum_luminance[62] = 103;
quantum_luminance[63] = 99;
quantum_chrominance[0] = 17;
quantum_chrominance[1] = 18;
quantum_chrominance[2] = 24;
quantum_chrominance[3] = 47;
quantum_chrominance[4] = 99;
quantum_chrominance[5] = 99;
quantum_chrominance[6] = 99;
quantum_chrominance[7] = 99;
quantum_chrominance[8] = 18;
quantum_chrominance[9] = 21;
quantum_chrominance[10] = 26;
quantum_chrominance[11] = 66;
quantum_chrominance[12] = 99;
quantum_chrominance[13] = 99;
quantum_chrominance[14] = 99;
quantum_chrominance[15] = 99;
quantum_chrominance[16] = 24;
quantum_chrominance[17] = 26;
quantum_chrominance[18] = 56;
quantum_chrominance[19] = 99;
quantum_chrominance[20] = 99;
quantum_chrominance[21] = 99;
quantum_chrominance[22] = 99;
quantum_chrominance[23] = 99;
quantum_chrominance[24] = 47;
quantum_chrominance[25] = 66;
quantum_chrominance[26] = 99;
quantum_chrominance[27] = 99;
quantum_chrominance[28] = 99;
quantum_chrominance[29] = 99;
quantum_chrominance[30] = 99;
quantum_chrominance[31] = 99;
quantum_chrominance[32] = 99;
quantum_chrominance[33] = 99;
quantum_chrominance[34] = 99;
quantum_chrominance[35] = 99;
quantum_chrominance[36] = 99;
quantum_chrominance[37] = 99;
quantum_chrominance[38] = 99;
quantum_chrominance[39] = 99;
quantum_chrominance[40] = 99;
quantum_chrominance[41] = 99;
quantum_chrominance[42] = 99;
quantum_chrominance[43] = 99;
quantum_chrominance[44] = 99;
quantum_chrominance[45] = 99;
quantum_chrominance[46] = 99;
quantum_chrominance[47] = 99;
quantum_chrominance[48] = 99;
quantum_chrominance[49] = 99;
quantum_chrominance[50] = 99;
quantum_chrominance[51] = 99;
quantum_chrominance[52] = 99;
quantum_chrominance[53] = 99;
quantum_chrominance[54] = 99;
quantum_chrominance[55] = 99;
quantum_chrominance[56] = 99;
quantum_chrominance[57] = 99;
quantum_chrominance[58] = 99;
quantum_chrominance[59] = 99;
quantum_chrominance[60] = 99;
quantum_chrominance[61] = 99;
quantum_chrominance[62] = 99;
quantum_chrominance[63] = 99;
// quantum and Divisors are objects used to hold the appropriate matices
quantum[0] = quantum_luminance;
Divisors[0] = DivisorsLuminance;
quantum[1] = quantum_chrominance;
Divisors[1] = DivisorsChrominance;
/*
* This method preforms forward DCT on a block of image data using the
literal
* method specified for a 2-D Discrete Cosine Transform. It is included as a
* curiosity and can give you an idea of the difference in the compression
* result (the resulting image quality) by comparing its output to the output
* of the AAN method below. It is ridiculously inefficient.
*/
// For now the final output is unusable. The associated quantization step
// needs some tweaking. If you get this part working, please let me know.
public double[][] forwardDCTExtreme(float input[][]) {
double output[][] = new double[N][N];
int v, u, x, y;
for (v = 0; v < 8; v++) {
for (u = 0; u < 8; u++) {
for (x = 0; x < 8; x++) {
for (y = 0; y < 8; y++) {
output[v][u] += input[x][y]
* Math.cos(((double) (2 * x + 1) * (double) u * Math.PI) / 16)
* Math.cos(((double) (2 * y + 1) * (double) v * Math.PI) / 16);
}
}
output[v][u] *= (0.25) * ((u == 0) ? (1.0 / Math.sqrt(2)) : (double) 1.0)
* ((v == 0) ? (1.0 / Math.sqrt(2)) : (double) 1.0);
}
}
return output;
}
/*
* This method preforms a DCT on a block of image data using the AAN
method as
* implemented in the IJG Jpeg-6a library.
*/
public double[][] forwardDCT(float input[][]) {
double output[][] = new double[N][N];
double tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
double tmp10, tmp11, tmp12, tmp13;
double z1, z2, z3, z4, z5, z11, z13;
int i;
int j;
}
}
return output;
}
/*
* This method quantitizes data and rounds it to the nearest integer.
*/
public int[] quantizeBlock(double inputData[][], int code) {
int outputData[] = new int[N * N];
int i, j;
int index;
index = 0;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
// The second line results in significantly better compression.
outputData[index] = (int) (Math.round(inputData[i][j]
* (((double[]) (Divisors[code]))[index])));
// outputData[index] = (int)(((inputData[i][j] * (((double[])
// (Divisors[code]))[index])) + 16384.5) -16384);
index++;
}
}
return outputData;
}
/*
* This is the method for quantizing a block DCT'ed with forwardDCTExtreme
* This method quantitizes data and rounds it to the nearest integer.
*/
public int[] quantizeBlockExtreme(double inputData[][], int code) {
int outputData[] = new int[N * N];
int i, j;
int index;
index = 0;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
outputData[index] = (int) (Math.round(inputData[i][j] / (((int[])
(quantum[code]))[index])));
index++;
}
}
return outputData;
}
}
class Huffman {
int bufferPutBits, bufferPutBuffer;
public int[] valACluminance = { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05,
0x12, 0x21, 0x31, 0x41,
0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1,
0x08, 0x23, 0x42,
0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62, 0x72, 0x82, 0x09,
0x0a, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x34, 0x35, 0x36,
0x37, 0x38, 0x39,
0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55,
0x56, 0x57, 0x58,
0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74,
0x75, 0x76, 0x77,
0x78, 0x79, 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92,
0x93, 0x94, 0x95,
0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8,
0xa9, 0xaa, 0xb2,
0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
0xc6, 0xc7, 0xc8,
0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1,
0xe2, 0xe3, 0xe4,
0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
0xf8, 0xf9,
0xfa };
public int[] valACchrominance = { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05,
0x21, 0x31, 0x06,
0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81, 0x08, 0x14,
0x42, 0x91, 0xa1,
0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1, 0x0a,
0x16, 0x24, 0x34,
0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a,
0x35, 0x36, 0x37,
0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53,
0x54, 0x55, 0x56,
0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a,
0x73, 0x74, 0x75,
0x76, 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88,
0x89, 0x8a, 0x92,
0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
0xa6, 0xa7, 0xa8,
0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2,
0xc3, 0xc4, 0xc5,
0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8,
0xd9, 0xda, 0xe2,
0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5,
0xf6, 0xf7, 0xf8,
0xf9, 0xfa };
/*
* jpegNaturalOrder[i] is the natural-order position of the i'th element of
* zigzag order.
*/
public static int[] jpegNaturalOrder = { 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25,
18, 11, 4, 5,
12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57,
50, 43, 36,
29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54,
47, 55, 62,
63, };
/*
* The Huffman class constructor
*/
public Huffman(int Width, int Height) {
/**
* HuffmanBlockEncoder run length encodes and Huffman encodes the
quantized
* data.
*
* @param outStream
* @param zigzag
* @param prec
* @param DCcode
* @param ACcode
*/
NumOfDCTables = 2;
NumOfACTables = 2;
// The DC portion
// The AC portion
r = 0;
r = 0;
}
}
if (r > 0) {
bufferIt(outStream, ((int[][]) AC_matrix[ACcode])[0][0], ((int[][])
AC_matrix[ACcode])[0][1]);
}
// Uses an integer long (32 bits) buffer to store the Huffman encoded bits
// and sends them to outStream by the byte.
/*
* Initialisation of the Huffman codes for Luminance and Chrominance. This
* code results in the same tables created in the IJG Jpeg-6a library.
*/
/*
* init of the DC values for the chrominance [][0] is the code [][1] is the
* number of bit
*/
p = 0;
for (l = 1; l <= 16; l++) {
for (i = 1; i <= bitsDCchrominance[l]; i++) {
huffsize[p++] = l;
}
}
huffsize[p] = 0;
lastp = p;
code = 0;
si = huffsize[0];
p = 0;
while (huffsize[p] != 0) {
while (huffsize[p] == si) {
huffcode[p++] = code;
code++;
}
code <<= 1;
si++;
}
/*
* Init of the AC hufmann code for the chrominance matrix [][][0] is the
* code & matrix[][][1] is the number of bit needed
*/
p = 0;
for (l = 1; l <= 16; l++) {
for (i = 1; i <= bitsACchrominance[l]; i++) {
huffsize[p++] = l;
}
}
huffsize[p] = 0;
lastp = p;
code = 0;
si = huffsize[0];
p = 0;
while (huffsize[p] != 0) {
while (huffsize[p] == si) {
huffcode[p++] = code;
code++;
}
code <<= 1;
si++;
}
/*
* init of the DC values for the luminance [][0] is the code [][1] is the
* number of bit
*/
p = 0;
for (l = 1; l <= 16; l++) {
for (i = 1; i <= bitsDCluminance[l]; i++) {
huffsize[p++] = l;
}
}
huffsize[p] = 0;
lastp = p;
code = 0;
si = huffsize[0];
p = 0;
while (huffsize[p] != 0) {
while (huffsize[p] == si) {
huffcode[p++] = code;
code++;
}
code <<= 1;
si++;
}
/*
* Init of the AC hufmann code for luminance matrix [][][0] is the code &
* matrix[][][1] is the number of bit
*/
p = 0;
for (l = 1; l <= 16; l++) {
for (i = 1; i <= bitsACluminance[l]; i++) {
huffsize[p++] = l;
}
}
huffsize[p] = 0;
lastp = p;
code = 0;
si = huffsize[0];
p = 0;
while (huffsize[p] != 0) {
while (huffsize[p] == si) {
huffcode[p++] = code;
code++;
}
code <<= 1;
si++;
}
for (int q = 0; q < lastp; q++) {
AC_matrix0[valACluminance[q]][0] = huffcode[q];
AC_matrix0[valACluminance[q]][1] = huffsize[q];
}
DC_matrix[0] = DC_matrix0;
DC_matrix[1] = DC_matrix1;
AC_matrix[0] = AC_matrix0;
AC_matrix[1] = AC_matrix1;
}
/*
* JpegInfo - Given an image, sets default information about it and divides it
* into its constituant components, downsizing those that need to be.
*/
class JpegInfo {
String Comment;
public int Ah = 0;
public int Al = 0;
/*
* This method creates and fills three arrays, Y, Cb, and Cr using the input
* image.
*/
// The following three lines are a more correct color conversion but
// the current conversion technique is sufficient and results in a
// higher
// compression rate.
// Y[y][x] = 16 + (float)(0.8588*(0.299 * (float)r + 0.587 * (float)g +
// 0.114 * (float)b ));
// Cb1[y][x] = 128 + (float)(0.8784*(-0.16874 * (float)r - 0.33126 *
// (float)g + 0.5 * (float)b));
// Cr1[y][x] = 128 + (float)(0.8784*(0.5 * (float)r - 0.41869 * (float)g
// - 0.08131 * (float)b));
Y[y][x] = (float) ((0.299 * r + 0.587 * g + 0.114 * b));
Cb1[y][x] = 128 + (float) ((-0.16874 * r - 0.33126 * g + 0.5 * b));
Cr1[y][x] = 128 + (float) ((0.5 * r - 0.41869 * g - 0.08131 * b));
index++;
}
}
Components[0] = Y;
// Cb2 = DownSample(Cb1, 1);
Components[1] = Cb1;
// Cr2 = DownSample(Cr1, 2);
Components[2] = Cr1;
}