- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package jpgcompression;
- import java.awt.event.*;
- import java.awt.*;
- import java.awt.image.*;
- import javax.swing.*;
- import java.io.*;
- import java.io.DataOutputStream;
- import java.io.FileOutputStream;
- import java.util.zip.Deflater;
- import java.util.zip.Inflater;
- import java.io.IOException;
- import java.util.zip.DataFormatException;
- import java.util.ArrayList;
- public class Main extends JFrame {
- Dimension size;
- Image image;
- public Image imageobj;
- public int BLOK = 8;
- public int h;
- public int w;
- public int ZZMatrix[][] = new int[64][2];
- public int[][] chrominance = {
- {99, 99, 99, 99, 99, 99, 99, 99},
- {99, 99, 99, 99, 99, 99, 99, 99},
- {24, 26, 56, 99, 99, 99, 99, 99},
- {47, 66, 99, 99, 99, 99, 99, 99},
- {99, 99, 99, 99, 99, 99, 99, 99},
- {99, 99, 99, 99, 99, 99, 99, 99},
- {99, 99, 99, 99, 99, 99, 99, 99},
- {99, 99, 99, 99, 99, 99, 99, 99}};
- public int[][] luminance = {
- {16, 11, 10, 16, 24, 40, 51, 61},
- {12, 12, 14, 19, 26, 58, 60, 55},
- {14, 13, 16, 24, 40, 57, 69, 56},
- {14, 17, 22, 29, 51, 87, 80, 62},
- {18, 22, 37, 56, 68, 109, 103, 77},
- {24, 35, 55, 64, 81, 104, 113, 92},
- {49, 64, 78, 87, 103, 121, 120, 101},
- {72, 92, 95, 98, 112, 100, 103, 99}
- };
- public final static String DEFLATE_SUFFIX = ".dfl";
- public Main() {
- //setLocation(100, 10); //move(100,00);
- //setSize(320, 240); //resize(320,240);
- //repaint();
- //setVisible(false);
- String nazwa = "test5.jpg";
- image = Toolkit.getDefaultToolkit().getImage(nazwa);
- MediaTracker tracker = new MediaTracker(this);
- tracker.addImage(image, 0);
- try {
- tracker.waitForID(0);
- } catch (Exception e) {
- return;
- }
- size = new Dimension(image.getWidth(null), image.getHeight(null));
- w = size.width % BLOK;//dlugosc
- h = size.height % BLOK;//wysokosc
- w = size.width - w;
- h = size.height - h;
- System.out.println("Obraz załadowany.");
- System.out.println("Rozmiar: " + h + "x" + w);
- int[][] red = new int[h][w];
- int[][] green = new int[h][w];
- int[][] blue = new int[h][w];
- getColorArray(red, green, blue);
- //wyswietl_i2(green);
- int[][] y = new int[h][w];
- int[][] cb = new int[h][w];
- int[][] cr = new int[h][w];
- convertRGBtoYCbCr(red, green, blue, y, cb, cr);
- double[][] data_y = new double[h][w];
- double[][] data_cb = new double[h][w];
- double[][] data_cr = new double[h][w];
- // wyswietl_i2(y);
- dct2d(y, cb, cr, data_y, data_cb, data_cr);
- //wyswietl_i2(y);
- //wyswietl_d2(data_y);
- //wyswietl_d2(data_y);
- quantize(data_y, data_cb, data_cr);
- int tmp[] = new int[h * w];
- tmp = SetToArray(data_y, data_cb, data_cr);
- //wyswietl_d2(data_y);
- //wyswietl_d2(data_cb);
- //wyswietl_d2(data_cb);
- //wyswietl_i1(tmp);
- try {
- zapis(tmp);
- deflate();
- inflate();
- } catch (IOException e) {
- System.err.println("Error: " + e.getMessage());
- }
- delete("wyjscie");
- int h1 = 0, w1 = 0;
- ArrayList<Integer> Lista = new ArrayList<Integer>();
- try {
- czytaj2(Lista);
- } catch (IOException e) {
- System.err.println("Error: " + e.getMessage());
- }
- h1 = Lista.get(0);
- w1 = Lista.get(1);
- //System.err.println(Lista.size());
- //wyswiel_lista(Lista);
- nazwa2(Lista, h1, w1, data_y, data_cb, data_cr);
- //wyswietl_i2(y);
- //wyswietl_d2(data_y);
- deQuantitize(data_y, data_cb, data_cr);
- //wyswietl_d2(data_y);
- idct2d(y, cb, cr, data_y, data_cb, data_cr);
- //wyswietl_i2(y);
- convertYCbCrToRGB(red, green, blue, y, cb, cr);
- //wyswietl_i2(red);
- tmp = SetToArray2(red, green, blue);
- ImageWindow iw = new ImageWindow(tmp, nazwa, h, w);
- //wyswietl_px(red, green, blue);
- }
- public void nazwa2(ArrayList<Integer> Lista, int h1, int w1,
- double[][] data_y, double[][] data_cb, double[][] data_cr) {
- /*System.out.println();
- System.out.println();
- System.out.println();
- System.out.println();
- System.out.println("nazwa2-----------------");
- System.out.println("");
- *
- */
- //output[index] = ((int) input1[i][j] << 16) | ((int) input1[i][j] << 8) | (int) input3[i][j];
- int size = Lista.size();
- int index = 2;
- for (int i = 0; i < h1; i++) {
- for (int j = 0; j < w1; j++) {
- data_y[i][j] = ((Lista.get(index) & 0x00ff0000) >> 16);
- data_cb[i][j] = ((Lista.get(index) & 0x0000ff00) >> 8);
- data_cr[i][j] = ((Lista.get(index) & 0x000000ff));
- /*System.out.println(Math.round(Lista.get(index)) + ">" + Math.round(data_y[i][j])
- + " " + Math.round(data_cb[i][j]) + " " + Math.round(data_cr[i][j]));
- */
- //System.out.print((Lista.get(index)) + " ") ;
- index++;
- }
- }
- }
- public int[] SetToArray(double[][] input1, double[][] input2, double[][] input3) {
- int output[] = new int[h * w];
- /*
- System.out.println();
- System.out.println();
- System.out.println();
- System.out.println("SET TO ARRAY --------------------------");
- * */
- int index = 0;
- for (int i = 0; i < h; i++) {
- for (int j = 0; j < w; j++) {
- /*if ((i + j) % 64 == 0) {
- // System.out.println();
- }
- */
- /* if (input1[i][j] < 0) {
- input1[i][j] = 0;
- }
- if (input2[i][j] < 0) {
- input2[i][j] = 0;
- }
- if (input3[i][j] < 0) {
- input3[i][j] = 0;
- }
- */
- output[index] = ((int) input1[i][j] << 16) | ((int) input2[i][j] << 8) | (int) input3[i][j];
- index++;
- /*
- System.out.println(Math.round(output[index - 1]) + ">>" + Math.round(input1[i][j])
- + " " + Math.round(input2[i][j]) + " " + Math.round(input3[i][j]));
- */
- //System.out.print(Math.round(input1[i][j]) + " ");
- //System.out.print(Math.round(output[index-1]) + " ");
- }
- }
- return output;
- }
- public int[] SetToArray2(int[][] input1, int[][] input2, int[][] input3) {
- int output[] = new int[h * w];
- int index = 0;
- for (int i = 0; i < h; i++) {
- for (int j = 0; j < w; j++) {
- if (input1[i][j] < 0) {
- input1[i][j] = 0;
- }
- if (input2[i][j] < 0) {
- input2[i][j] = 0;
- }
- if (input3[i][j] < 0) {
- input3[i][j] = 0;
- }
- //Math.abs();
- //output[index] = -16777216 | (int)(input1[i][j] << 16) | (int)(input2[i][j] << 8) | (int)input3[i][j];
- //output[index] = (255 << 24 | (int) Math.abs(input1[i][j]) << 16) | ((int) Math.abs(input2[i][j]) << 8) | (int) Math.abs(input3[i][j]);
- output[index] = (255 << 24 | (int) input1[i][j] << 16) | ((int) input2[i][j] << 8) | (int) input3[i][j];
- //output[index] = (-16777216 | (int) 0 << 16) | ((int) 0 << 8) | (int) 0;
- //System.out.println(input1[i][j] + " " + input2[i][j] + " " + input3[i][j] + " ");
- index++;
- }
- }
- return output;
- }
- public void czytaj2(ArrayList<Integer> Lista) throws IOException {
- //ArrayList<Integer> r = new ArrayList<Integer>();
- File file = new File("wejscie");
- byte[] bytes = new byte[(int) file.length()];
- try {
- InputStream in = new FileInputStream(file);
- in.read(bytes);
- //System.out.println();
- } catch (FileNotFoundException e) {
- System.err.println("Nie mozna odnaleść pliku.");
- e.printStackTrace();
- }
- int value;
- value = ((bytes[0] & 0xFF) << 24)
- + ((bytes[1] & 0xFF) << 16)
- + ((bytes[2] & 0xFF) << 8)
- + (bytes[3] & 0xFF);
- Lista.add(value);
- value = ((bytes[4] & 0xFF) << 24)
- + ((bytes[5] & 0xFF) << 16)
- + ((bytes[6] & 0xFF) << 8)
- + (bytes[7] & 0xFF);
- Lista.add(value);
- int j = 8;
- int dstLength = Lista.get(0) * Lista.get(1);
- for (int i = 0; i < dstLength; i++) {
- value = ((bytes[j++] & 0xFF) << 24)
- + ((bytes[j++] & 0xFF) << 16)
- + ((bytes[j++] & 0xFF) << 8)
- + (bytes[j++] & 0xFF);
- Lista.add(value);
- /*
- if (i % 8 == 0) {
- System.out.println();
- }
- System.out.print(Lista.get(i + 2) + " ");
- */
- }
- }
- public static void delete(String fileName) {
- try {
- // Construct a File object for the file to be deleted.
- File target = new File(fileName);
- if (!target.exists()) {
- System.err.println("Plik " + fileName
- + " nie istnieje!");
- return;
- }
- // Quick, now, delete it immediately:
- if (target.delete()) {
- System.err.println("Usinięto plik tymczasowy '" + fileName + "' ");
- } else {
- System.err.println("Usuwanie zakończone niepowodzeniem " + fileName);
- }
- } catch (SecurityException e) {
- System.err.println("Nie można usunąć " + fileName + "("
- + e.getMessage() + ")");
- }
- }
- public static void deletefile() {
- File f1 = new File("wyjscie");
- boolean success = f1.delete();
- if (!success) {
- System.out.println("Deletion failed.");
- System.exit(0);
- } else {
- System.out.println("File deleted.");
- }
- }
- public void deflate() throws IOException {
- Deflater def = new Deflater();
- byte[] input = new byte[1024];
- byte[] output = new byte[1024];
- FileInputStream fin = new FileInputStream("wyjscie");
- FileOutputStream fout = new FileOutputStream("wyjscie" + DEFLATE_SUFFIX);
- while (true) { // read and deflate the data
- // Fill the input array.
- int numRead = fin.read(input);
- if (numRead == -1) { // end of stream
- // Deflate any data that remains in the input buffer.
- def.finish();
- while (!def.finished()) {
- int numCompressedBytes = def.deflate(output, 0, output.length);
- if (numCompressedBytes > 0) {
- fout.write(output, 0, numCompressedBytes);
- } // end if
- } // end while
- break; // Exit while loop.
- } // end if
- else { // Deflate the input.
- def.setInput(input, 0, numRead);
- while (!def.needsInput()) {
- int numCompressedBytes = def.deflate(output, 0, output.length);
- if (numCompressedBytes > 0) {
- fout.write(output, 0, numCompressedBytes);
- } // end if
- } // end while
- } // end else
- } // end while
- fin.close();
- fout.flush();
- fout.close();
- def.reset();
- }
- public void inflate() throws IOException {
- Inflater inf = new Inflater();
- byte[] input = new byte[1024];
- byte[] output = new byte[1024];
- try {
- FileInputStream fin = new FileInputStream("wyjscie.dfl");
- FileOutputStream fout = new FileOutputStream("wejscie");
- while (true) { // Read and inflate the data.
- // Fill the input array.
- int numRead = fin.read(input);
- if (numRead != -1) { // End of stream, finish inflating.
- inf.setInput(input, 0, numRead);
- } // end if
- // Inflate the input.
- int numDecompressed = 0;
- while ((numDecompressed = inf.inflate(output, 0, output.length))
- != 0) {
- fout.write(output, 0, numDecompressed);
- }
- // At this point inflate() has returned 0.
- // Let's find out why.
- if (inf.finished()) { // all done
- break;
- } else if (inf.needsDictionary()) { // We don't handle dictionaries.
- System.err.println("Dictionary required! bailing...");
- break;
- } else if (inf.needsInput()) {
- continue;
- }
- } // end while
- // Close up and get ready for the next file.
- fin.close();
- fout.flush();
- fout.close();
- inf.reset();
- } // end try
- catch (IOException ex) {
- System.err.println(ex);
- } catch (DataFormatException ex) {
- System.err.println("wyjscie.dfl appears to be corrupt");
- System.err.println(ex);
- } // end catch
- }
- public static final byte[] intToByteArray(int value) {
- return new byte[]{
- (byte) (value >>> 24),
- (byte) (value >>> 16),
- (byte) (value >>> 8),
- (byte) value};
- }
- public static byte[] int2byte(int[] src) {
- int srcLength = src.length;
- byte[] dst = new byte[srcLength << 2];
- //System.out.println("ZAPIS---------------------------------------------------");
- for (int i = 0; i < srcLength; i++) {
- int x = src[i];
- int j = i << 2;
- if (i % 8 == 0) {
- //System.out.println();
- }
- dst[j++] = (byte) ((x >>> 24));
- //System.out.print(dst[j - 1] + " ");
- dst[j++] = (byte) ((x >>> 16));
- //System.out.print(dst[j - 1] + " ");
- dst[j++] = (byte) ((x >>> 8));
- //System.out.print(dst[j - 1] + " ");
- dst[j++] = (byte) (x);
- //System.out.print(dst[j - 1] + " ");
- //System.out.print(src[i] + " ");
- }
- //System.out.println("---------------------------------------------------");
- return dst;
- }
- public static final int byteArrayToInt(byte[] b) {
- return //(b[0] << 24)
- +((b[1] & 0xFF) << 16)
- + ((b[2] & 0xFF) << 8)
- + (b[3] & 0xFF);
- }
- public static int[] byte2int(byte[] src, int h1, int w1) {
- //int srcLength = src.length;
- //System.out.println(srcLength + "tu");
- //System.out.println("byte2int---------------------------------------------------");
- // int h1 = ((src[0] & 0xFF) << 24)
- // + ((src[1] & 0xFF) << 16)
- // + ((src[2] & 0xFF) << 8)
- // + (src[3] & 0xFF);
- // int w1 = ((src[4] & 0xFF) << 24)
- // + ((src[5] & 0xFF) << 16)
- // + ((src[6] & 0xFF) << 8)
- // + (src[7] & 0xFF);
- int[] dst = new int[h1 * w1];
- int dstLength = h1 * w1;
- System.out.println(h1 + " " + w1 + " " + (h1 * w1));
- int j = 8;
- int i;
- for (i = 0; i <= dstLength; i++) {
- dst[i] = ((src[j++] & 0xFF) << 24)
- + ((src[j++] & 0xFF) << 16)
- + ((src[j++] & 0xFF) << 8)
- + (src[j++] & 0xFF);
- if (i % 8 == 0) {
- //System.out.println();
- }
- //System.out.print(dst[i] + " ");
- }
- //System.out.print(i + " ");
- return dst;
- }
- public void zapis(int input[]) throws IOException {
- File file = new File("wyjscie");
- OutputStream out = new FileOutputStream(file);
- int Length = input.length;
- byte[] dst = new byte[Length << 2];
- dst = int2byte(input);
- out.write(intToByteArray(h));
- out.write(intToByteArray(w));
- out.write(dst);
- out.close();
- out.flush();
- }
- public static int[] czytaj(int h1, int w1) throws IOException {
- File file = new File("wejscie");
- byte[] bytes = new byte[(int) file.length()];
- try {
- InputStream in = new FileInputStream(file);
- in.read(bytes);
- //System.out.println();
- } catch (FileNotFoundException e) {
- System.err.println("Nie mozna odnaleść pliku.");
- e.printStackTrace();
- }
- h1 = ((bytes[0] & 0xFF) << 24)
- + ((bytes[1] & 0xFF) << 16)
- + ((bytes[2] & 0xFF) << 8)
- + (bytes[3] & 0xFF);
- w1 = ((bytes[4] & 0xFF) << 24)
- + ((bytes[5] & 0xFF) << 16)
- + ((bytes[6] & 0xFF) << 8)
- + (bytes[7] & 0xFF);
- int[] pixels = new int[h1 * w1];
- pixels = byte2int(bytes, h1, w1);
- for (int i = 0; i < h1; i++) {
- System.out.print(pixels[i] + " ");
- }
- //int[][] pixels2 = new int[h1][w1];
- //int index = 0;
- /*for(int i = 0; i < h1; i++){
- for(int j = 0; j < w1; j++){
- pixels2[i][j] = pixels[index++];
- System.out.print(pixels2[i][j] + " ");
- }
- }
- System.out.print(pixels[0] + " ");*/
- return pixels;
- }
- public void wyswietl_d2(double input[][]) {
- System.out.println("");
- for (int i = 0; i < h; i++) {
- for (int j = 0; j < w; j++) {
- System.out.print(Math.round(input[i][j]) + " ");
- }
- System.out.println("");
- }
- }
- public void wyswietl_i1(int input[]) {
- for (int i = 0; i < h * w; i++) {
- if (i % 64 == 0) {
- System.out.println();
- }
- System.out.print(input[i] + " ");
- }
- System.out.println();
- }
- public void wyswietl_i2(int input[][]) {
- System.out.println("");
- System.out.println("");
- System.out.println("");
- for (int i = 0; i < h; i++) {
- for (int j = 0; j < w; j++) {
- System.out.print((input[i][j]) + " ");
- }
- System.out.println("");
- }
- }
- public void wyswietl_b1(byte input[]) {
- System.out.println("");
- for (int i = 0; i < h * w; i++) {
- if (i % 64 == 0) {
- System.out.println();
- }
- System.out.print((input[i]) + " ");
- //System.out.println("");
- }
- }
- public void wyswietl_px(int input[][], int input2[][], int input3[][]) {
- System.out.println("");
- for (int i = 0; i < h * w; i++) {
- for (int j = 0; j < w; j++) {
- System.out.println((input[i][j]) + " " + (input2[i][j]) + " " + (input3[i][j]) + " ");
- }
- }
- }
- public void wyswiel_lista(ArrayList<Integer> Lista) {
- System.out.println("");
- for (int i = 2; i < h * w; i++) {
- if ((i - 2) % 64 == 0) {
- System.out.println();
- }
- System.out.print((Lista.get(i)) + " ");
- //System.out.println("");
- }
- }
- public void getColorArray(int red[][], int green[][], int blue[][]) {
- int values[] = new int[h * w];
- PixelGrabber grabber = new PixelGrabber(image.getSource(), 0, 0, w, h, values, 0, w);
- try {
- if (grabber.grabPixels() != true) {
- try {
- throw new AWTException("Grabber zwrocił false: " + grabber.status());
- } catch (Exception e) {
- }
- ;
- }
- } catch (InterruptedException e) {
- }
- ;
- int index = 0;
- for (int i = 0; i < h; ++i) {
- for (int j = 0; j < w; ++j) {
- red[i][j] = ((values[index] & 0x00ff0000) >> 16);
- //System.out.println(index + " " + pixel[i][j]);
- green[i][j] = ((values[index] & 0x0000ff00) >> 8);
- //System.out.println(index + " " + pixel[i][j]);
- blue[i][j] = (values[index] & 0x000000ff);
- //System.out.println(index + " " + pixel[i][j]);
- //System.out.print("[" + (i) + "]" + "[" + (j) + "]" + " ");
- index++;
- }
- }
- }
- public void convertRGBtoYCbCr(int red[][], int green[][], int blue[][],
- int y[][], int cb[][], int cr[][]) {
- /*
- Y = (77/256)R + (150/256)G + (29/256)B,
- Cb = -(44/256)R - (87/256)G + (131/256)B + 128,
- Cr = (131/256)R - (110/256)G - (21/256)B + 128;
- */
- for (int i = 0; i < h; i++) {
- for (int j = 0; j < w; j++) {
- y[i][j] = (int) (((double) 77 / 256) * red[i][j]
- + ((double) 150 / 256) * green[i][j]
- + ((double) 29 / 256) * blue[i][j]);
- cb[i][j] = (int) (-((double) 44 / 256) * red[i][j]
- - ((double) 87 / 256) * green[i][j]
- + ((double) 131 / 256) * blue[i][j]
- + 128);
- cr[i][j] = (int) (((double) 131 / 256) * red[i][j]
- - ((double) 110 / 256) * green[i][j]
- - ((double) 21 / 256) * blue[i][j]
- + 128);
- }
- }
- }
- public void convertYCbCrToRGB(int red[][], int green[][], int blue[][],
- int y[][], int cb[][], int cr[][]) {
- /*
- R = Y+1.371(Cr - 128),
- G = Y- 0.698(Cr - 128) - 0.336(Cb - 128),
- B = Y+1.732(Cb - 128).
- */
- for (int i = 0; i < h; i++) {
- for (int j = 0; j < w; j++) {
- //convert to R
- red[i][j] = (int) (y[i][j] + (1.371 * (cr[i][j] - 128)));
- //convert to G
- green[i][j] = (int) (y[i][j]
- - 0.698 * (cr[i][j] - 128)
- - 0.336 * (cb[i][j] - 128));
- //convert to B
- blue[i][j] = (int) (y[i][j]
- + 1.732 * (cb[i][j] - 128));
- //System.out.println(red[i][j] + " " + green[i][j] + " " + blue[i][j] + " ");
- }
- }
- }
- public void dct2d(int y[][], int cb[][], int cr[][],
- double data_y[][], double data_cb[][], double data_cr[][]) {
- double data1[][] = new double[BLOK][BLOK];
- double data2[][] = new double[BLOK][BLOK];
- double data3[][] = new double[BLOK][BLOK];
- int blokow_w = w / BLOK;
- int blokow_h = h / BLOK;
- int xpos, ypos;
- double Cu = 0.0, Cv = 0.0;
- double sum1 = 0.0, sum2 = 0.0, sum3 = 0.0;
- double s1 = 0.0, s2 = 0.0, s3 = 0.0;
- double[][] temp = new double[8][8];
- temp = cosin(temp);
- for (int i = 0; i < blokow_h; i++) {
- for (int j = 0; j < blokow_w; j++) {
- xpos = i * BLOK;
- ypos = j * BLOK;
- for (int a = 0; a < BLOK; a++) {
- for (int b = 0; b < BLOK; b++) {
- Cu = ((a == 0) ? 1.0 / Math.sqrt(2.0) : 1.0);
- Cv = ((b == 0) ? 1.0 / Math.sqrt(2.0) : 1.0);
- for (int c = 0; c < BLOK; c++) {
- for (int d = 0; d < BLOK; d++) {
- s1 = y[c + xpos][d + ypos] * temp[d][b] * temp[c][a];
- s2 = cb[c + xpos][d + ypos] * temp[d][b] * temp[c][a];
- s3 = cr[c + xpos][d + ypos] * temp[d][b] * temp[c][a];
- // * Math.cos((double) (2 * d + 1) * (double) b * Math.PI / 16.0)
- // * Math.cos((double) (2 * c + 1) * (double) a * (Math.PI / 16.0));
- sum1 += s1;
- sum2 += s2;
- sum3 += s3;
- }
- }
- data1[a][b] = 0.25 * Cu * Cv * sum1;
- data2[a][b] = 0.25 * Cu * Cv * sum2;
- data3[a][b] = 0.25 * Cu * Cv * sum3;
- sum1 = 0;
- sum2 = 0;
- sum3 = 0;
- //System.out.print("[" + (xpos + b ) + "]" + "[" + (ypos + a) + "]" + "");
- data_y[xpos + b][ypos + a] = data1[a][b];
- data_cb[xpos + b][ypos + a] = data2[a][b];
- data_cr[xpos + b][ypos + a] = data3[a][b];
- }
- }
- }
- }
- }
- public void idct2d(int y[][], int cb[][], int cr[][],
- double data_y[][], double data_cb[][], double data_cr[][]) {
- //wyswietl_d2(data_y);
- double data1[][] = new double[BLOK][BLOK];
- double data2[][] = new double[BLOK][BLOK];
- double data3[][] = new double[BLOK][BLOK];
- int blokow_w = w / BLOK;
- int blokow_h = h / BLOK;
- int xpos, ypos;
- double Cu = 0.0, Cv = 0.0;
- double sum1 = 0.0, sum2 = 0.0, sum3 = 0.0;
- double s1 = 0.0, s2 = 0.0, s3 = 0.0;
- for (int i = 0; i < blokow_h; i++) {
- for (int j = 0; j < blokow_w; j++) {
- xpos = i * BLOK;
- ypos = j * BLOK;
- for (int a = 0; a < BLOK; a++) {
- Cu = ((a == 0) ? 1.0 / Math.sqrt(2.0) : 1.0);
- for (int b = 0; b < BLOK; b++) {
- Cv = ((b == 0) ? 1.0 / Math.sqrt(2.0) : 1.0);
- for (int c = 0; c < BLOK; c++) {
- for (int d = 0; d < BLOK; d++) {
- // s1 = Math.cos(((2 * (double) a + 1) / 16.000 * (double) c * 3.14));
- // sum1 += s1;
- // * Math.cos((double) (2 * d + 1) * (double) b * Math.PI / 16.0)
- // * Math.cos((double) (2 * c + 1) * (double) a * (Math.PI / 16.0));
- /*s1 = data_y[c + xpos][d + ypos] * Math.cos((2 * b + 1) * d * Math.PI / 16.000)
- * Math.cos((2 * a + 1) * c * Math.PI / 16.000);
- sum1 += s1;
- s2 = data_cb[c + xpos][d + ypos] * Math.cos((2 * b + 1) * d * Math.PI / 16.000)
- * Math.cos((2 * a + 1) * c * Math.PI / 16.000);
- sum2 += s2;
- s3 = data_cr[c + xpos][d + ypos] * Math.cos((2 * b + 1) * d * Math.PI / 16.000)
- * Math.cos((2 * a + 1) * c * Math.PI / 16.000);
- sum3 += s3;*/
- s1 = Math.cos(((2 * (double) c + 1) / 16.000 * (double) a * Math.PI));
- s1 = s1 * Math.cos(((2 * (double) d + 1) / 16.000 * (double) b * Math.PI));
- sum1 += data_y[c + xpos][d + ypos] * s1;
- sum2 += data_cb[c + xpos][d + ypos] * s1;
- sum3 += data_cr[c + xpos][d + ypos] * s1;
- }
- }
- data1[a][b] = 0.25 * Cu * Cv * sum1;
- data2[a][b] = 0.25 * Cu * Cv * sum2;
- data3[a][b] = 0.25 * Cu * Cv * sum3;
- sum1 = 0;
- sum2 = 0;
- sum3 = 0;
- y[xpos + b][ypos + a] = (int) data1[a][b];
- cb[xpos + b][ypos + a] = (int) data2[a][b];
- cr[xpos + b][ypos + a] = (int) data3[a][b];
- //System.out.print(y[xpos + b][ypos + a] + " ");
- }
- }
- }
- }
- //wyswietl_i2(y);
- }
- double[][] cosin(double temp[][]) {
- for (int i = 0; i < BLOK; i++) {
- for (int j = 0; j < BLOK; j++) {
- temp[i][j] = Math.cos((double) (2 * i + 1) * (double) j * (Math.PI / 16.0));
- }
- }
- return temp;
- }
- public void quantize(double data_y[][], double data_cb[][], double data_cr[][]) {
- /*
- System.out.println();
- System.out.println("Przed kwantyzacja");
- System.out.println();
- *
- */
- int blokow_h = h / BLOK;
- int blokow_w = w / BLOK;
- int xpos, ypos, row, col;
- double wynik;
- initZZMatrix();
- for (int a = 0; a < blokow_h; a++) {
- for (int b = 0; b < blokow_w; b++) {
- xpos = a * BLOK;
- ypos = b * BLOK;
- for (int i = 0; i < BLOK * BLOK; i++) {
- /*
- data_y[xpos + i][ypos + j] = (int) (data_y[xpos + i][ypos + j] / luminance[i][j]);
- data_cb[xpos + i][ypos + j] = (int) (data_cb[xpos + i][ypos + j] / chrominance[i][j]);
- data_cr[xpos + i][ypos + j] = (int) (data_cr[xpos + i][ypos + j] / chrominance[i][j]);
- */
- row = ZZMatrix[i][0];
- col = ZZMatrix[i][1];
- //System.out.print(Math.round(data_y[xpos + row][ypos + col]) + " ");
- wynik = (int) (data_y[xpos + row][ypos + col] / luminance[row][col]);
- data_y[xpos + row][ypos + col] = (int) (Math.round(wynik));
- wynik = (int) (data_cb[xpos + row][ypos + col] / chrominance[row][col]);
- data_cb[xpos + row][ypos + col] = (int) (Math.round(wynik));
- wynik = (int) (data_cr[xpos + row][ypos + col] / chrominance[row][col]);
- data_cr[xpos + row][ypos + col] = (int) (Math.round(wynik));
- //System.out.print(Math.round(data_y[xpos + row][ypos + col]) + " ");
- }
- //System.out.println();
- }
- }
- }
- public void deQuantitize(double data_y[][], double data_cb[][], double data_cr[][]) {
- /*
- System.out.println();
- System.out.println("Po dekwantyzacji");
- *
- */
- int blokow_h = h / BLOK;
- int blokow_w = w / BLOK;
- int xpos, ypos, row, col;
- double wynik;
- for (int a = 0; a < blokow_h; a++) {
- for (int b = 0; b < blokow_w; b++) {
- xpos = a * BLOK;
- ypos = b * BLOK;
- for (int i = 0; i < BLOK * BLOK; i++) {
- /*
- data_y[xpos + i][ypos + j] = (int) (data_y[xpos + i][ypos + j] * luminance[i][j]);
- data_cb[xpos + i][ypos + j] = (int) (data_cb[xpos + i][ypos + j] * chrominance[i][j]);
- data_cr[xpos + i][ypos + j] = (int) (data_cr[xpos + i][ypos + j] * chrominance[i][j]);
- */
- row = ZZMatrix[i][0];
- col = ZZMatrix[i][1];
- wynik = (int) (data_y[xpos + row][ypos + col] * luminance[row][col]);
- data_y[xpos + row][ypos + col] = (int) (Math.round(wynik));
- wynik = (int) (data_cb[xpos + row][ypos + col] * chrominance[row][col]);
- data_cb[xpos + row][ypos + col] = (int) (Math.round(wynik));
- wynik = (int) (data_cr[xpos + row][ypos + col] * chrominance[row][col]);
- data_cr[xpos + row][ypos + col] = (int) (Math.round(wynik));
- //System.out.print(Math.round(data_y[xpos + row][ypos + col]) + " ");
- }
- //System.out.println();
- }
- }
- }
- private void initZZMatrix() {
- ZZMatrix[0][0] = 0; // 0,0
- ZZMatrix[0][1] = 0;
- ZZMatrix[1][0] = 0; // 0,1
- ZZMatrix[1][1] = 1;
- ZZMatrix[2][0] = 1; // 1,0
- ZZMatrix[2][1] = 0;
- ZZMatrix[3][0] = 2; // 2,0
- ZZMatrix[3][1] = 0;
- ZZMatrix[4][0] = 1; // 1,1
- ZZMatrix[4][1] = 1;
- ZZMatrix[5][0] = 0; // 0,2
- ZZMatrix[5][1] = 2;
- ZZMatrix[6][0] = 0; // 0,3
- ZZMatrix[6][1] = 3;
- ZZMatrix[7][0] = 1; // 1,2
- ZZMatrix[7][1] = 2;
- ZZMatrix[8][0] = 2; // 2,1
- ZZMatrix[8][1] = 1;
- ZZMatrix[9][0] = 3; // 3,0
- ZZMatrix[9][1] = 0;
- ZZMatrix[10][0] = 4; // 4,0
- ZZMatrix[10][1] = 0;
- ZZMatrix[11][0] = 3; // 3,1
- ZZMatrix[11][1] = 1;
- ZZMatrix[12][0] = 2; // 2,2
- ZZMatrix[12][1] = 2;
- ZZMatrix[13][0] = 1; // 1,3
- ZZMatrix[13][1] = 3;
- ZZMatrix[14][0] = 0; // 0,4
- ZZMatrix[14][1] = 4;
- ZZMatrix[15][0] = 0; // 0,5
- ZZMatrix[15][1] = 5;
- ZZMatrix[16][0] = 1; // 1,4
- ZZMatrix[16][1] = 4;
- ZZMatrix[17][0] = 2; // 2,3
- ZZMatrix[17][1] = 3;
- ZZMatrix[18][0] = 3; // 3,2
- ZZMatrix[18][1] = 2;
- ZZMatrix[19][0] = 4; // 4,1
- ZZMatrix[19][1] = 1;
- ZZMatrix[20][0] = 5; // 5,0
- ZZMatrix[20][1] = 0;
- ZZMatrix[21][0] = 6; // 6,0
- ZZMatrix[21][1] = 0;
- ZZMatrix[22][0] = 5; // 5,1
- ZZMatrix[22][1] = 1;
- ZZMatrix[23][0] = 4; // 4,2
- ZZMatrix[23][1] = 2;
- ZZMatrix[24][0] = 3; // 3,3
- ZZMatrix[24][1] = 3;
- ZZMatrix[25][0] = 2; // 2,4
- ZZMatrix[25][1] = 4;
- ZZMatrix[26][0] = 1; // 1,5
- ZZMatrix[26][1] = 5;
- ZZMatrix[27][0] = 0; // 0,6
- ZZMatrix[27][1] = 6;
- ZZMatrix[28][0] = 0; // 0,7
- ZZMatrix[28][1] = 7;
- ZZMatrix[29][0] = 1; // 1,6
- ZZMatrix[29][1] = 6;
- ZZMatrix[30][0] = 2; // 2,5
- ZZMatrix[30][1] = 5;
- ZZMatrix[31][0] = 3; // 3,4
- ZZMatrix[31][1] = 4;
- ZZMatrix[32][0] = 4; // 4,3
- ZZMatrix[32][1] = 3;
- ZZMatrix[33][0] = 5; // 5,2
- ZZMatrix[33][1] = 2;
- ZZMatrix[34][0] = 6; // 6,1
- ZZMatrix[34][1] = 1;
- ZZMatrix[35][0] = 7; // 7,0
- ZZMatrix[35][1] = 0;
- ZZMatrix[36][0] = 7; // 7,1
- ZZMatrix[36][1] = 1;
- ZZMatrix[37][0] = 6; // 6,2
- ZZMatrix[37][1] = 2;
- ZZMatrix[38][0] = 5; // 5,3
- ZZMatrix[38][1] = 3;
- ZZMatrix[39][0] = 4; // 4,4
- ZZMatrix[39][1] = 4;
- ZZMatrix[40][0] = 3; // 3,5
- ZZMatrix[40][1] = 5;
- ZZMatrix[41][0] = 2; // 2,6
- ZZMatrix[41][1] = 6;
- ZZMatrix[42][0] = 1; // 1,7
- ZZMatrix[42][1] = 7;
- ZZMatrix[43][0] = 2; // 2,7
- ZZMatrix[43][1] = 7;
- ZZMatrix[44][0] = 3; // 3,6
- ZZMatrix[44][1] = 6;
- ZZMatrix[45][0] = 4; // 4,5
- ZZMatrix[45][1] = 5;
- ZZMatrix[46][0] = 5; // 5,4
- ZZMatrix[46][1] = 4;
- ZZMatrix[47][0] = 6; // 6,3
- ZZMatrix[47][1] = 3;
- ZZMatrix[48][0] = 7; // 7,2
- ZZMatrix[48][1] = 2;
- ZZMatrix[49][0] = 7; // 7,3
- ZZMatrix[49][1] = 3;
- ZZMatrix[50][0] = 6; // 6,4
- ZZMatrix[50][1] = 4;
- ZZMatrix[51][0] = 5; // 5,5
- ZZMatrix[51][1] = 5;
- ZZMatrix[52][0] = 4; // 4,6
- ZZMatrix[52][1] = 6;
- ZZMatrix[53][0] = 3; // 3,7
- ZZMatrix[53][1] = 7;
- ZZMatrix[54][0] = 4; // 4,7
- ZZMatrix[54][1] = 7;
- ZZMatrix[55][0] = 5; // 5,6
- ZZMatrix[55][1] = 6;
- ZZMatrix[56][0] = 6; // 6,5
- ZZMatrix[56][1] = 5;
- ZZMatrix[57][0] = 7; // 7,4
- ZZMatrix[57][1] = 4;
- ZZMatrix[58][0] = 7; // 7,5
- ZZMatrix[58][1] = 5;
- ZZMatrix[59][0] = 6; // 6,6
- ZZMatrix[59][1] = 6;
- ZZMatrix[60][0] = 5; // 5,7
- ZZMatrix[60][1] = 7;
- ZZMatrix[61][0] = 6; // 6,7
- ZZMatrix[61][1] = 7;
- ZZMatrix[62][0] = 7; // 7,6
- ZZMatrix[62][1] = 6;
- ZZMatrix[63][0] = 7; // 7,7
- ZZMatrix[63][1] = 7;
- /*
- for (int i = 0; i < 64; i++) {
- System.out.println(ZZMatrix[i][0] + " " + ZZMatrix[i][1]);
- }
- *
- */
- }
- public static void main(String args[]) {
- long start = System.currentTimeMillis();
- try {
- new Main();
- } catch (Exception e) {
- }
- long stop = System.currentTimeMillis();
- System.out.println("Czas wykonania: " + (stop - start) + "ms");
- System.out.println("Hit enter");
- try {
- System.in.read();
- } catch (Exception e) {
- }
- System.exit(0);
- }
- }
- class ImageWindow extends Frame {
- Image image;
- Image image2;
- int imageArray_[] = new int[320 * 240];
- int h, w;
- public ImageWindow(int[] imageArray, String nazwa, int h1, int w1) {
- super("DCT Window");
- h = h1;
- w = w1;
- //int imageArray[] = new int[320*240];
- //imageArray_ = imageArray;
- setLocation(100, 100); //move(100,100);
- setSize(w * 2 + 50, h + 50); //resize(700,300);
- image = this.createImage(new MemoryImageSource(w, h, imageArray, 0, w));
- // Make sure it gets loaded
- MediaTracker tracker = new MediaTracker(this);
- tracker.addImage(image, 1);
- try {
- tracker.waitForID(1);
- } catch (Exception e) {
- return;
- }
- // Load in original image
- image2 = Toolkit.getDefaultToolkit().getImage(nazwa);
- tracker = new MediaTracker(this);
- tracker.addImage(image, 0);
- try {
- tracker.waitForID(0);
- } catch (Exception e) {
- return;
- }
- /*for (int i = 0; i < 320 * 240; i++) {
- System.out.print(imageArray[i] + " ");
- if (i % 64 == 0) {
- System.out.println();
- }
- }*/
- this.show();
- }
- public void paint(Graphics g) {
- g.drawImage(image2, 0, 20, this);//oryginal
- g.drawImage(image, w + 20, 20, this);//skompresowany
- }
- }