Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 38.81 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package jpgcompression;
  6.  
  7. import java.awt.event.*;
  8. import java.awt.*;
  9. import java.awt.image.*;
  10. import javax.swing.*;
  11. import java.io.*;
  12. import java.io.DataOutputStream;
  13. import java.io.FileOutputStream;
  14. import java.util.zip.Deflater;
  15. import java.util.zip.Inflater;
  16. import java.io.IOException;
  17. import java.util.zip.DataFormatException;
  18. import java.util.ArrayList;
  19.  
  20. public class Main extends JFrame {
  21.  
  22.     Dimension size;
  23.     Image image;
  24.     public Image imageobj;
  25.     public int BLOK = 8;
  26.     public int h;
  27.     public int w;
  28.     public int ZZMatrix[][] = new int[64][2];
  29.     public int[][] chrominance = {
  30.         {99, 99, 99, 99, 99, 99, 99, 99},
  31.         {99, 99, 99, 99, 99, 99, 99, 99},
  32.         {24, 26, 56, 99, 99, 99, 99, 99},
  33.         {47, 66, 99, 99, 99, 99, 99, 99},
  34.         {99, 99, 99, 99, 99, 99, 99, 99},
  35.         {99, 99, 99, 99, 99, 99, 99, 99},
  36.         {99, 99, 99, 99, 99, 99, 99, 99},
  37.         {99, 99, 99, 99, 99, 99, 99, 99}};
  38.     public int[][] luminance = {
  39.         {16, 11, 10, 16, 24, 40, 51, 61},
  40.         {12, 12, 14, 19, 26, 58, 60, 55},
  41.         {14, 13, 16, 24, 40, 57, 69, 56},
  42.         {14, 17, 22, 29, 51, 87, 80, 62},
  43.         {18, 22, 37, 56, 68, 109, 103, 77},
  44.         {24, 35, 55, 64, 81, 104, 113, 92},
  45.         {49, 64, 78, 87, 103, 121, 120, 101},
  46.         {72, 92, 95, 98, 112, 100, 103, 99}
  47.     };
  48.     public final static String DEFLATE_SUFFIX = ".dfl";
  49.  
  50.     public Main() {
  51.         //setLocation(100, 10);   //move(100,00);
  52.         //setSize(320, 240);            //resize(320,240);
  53.         //repaint();
  54.         //setVisible(false);
  55.         String nazwa = "test5.jpg";
  56.         image = Toolkit.getDefaultToolkit().getImage(nazwa);
  57.  
  58.         MediaTracker tracker = new MediaTracker(this);
  59.         tracker.addImage(image, 0);
  60.         try {
  61.             tracker.waitForID(0);
  62.         } catch (Exception e) {
  63.             return;
  64.         }
  65.         size = new Dimension(image.getWidth(null), image.getHeight(null));
  66.  
  67.         w = size.width % BLOK;//dlugosc
  68.         h = size.height % BLOK;//wysokosc
  69.  
  70.         w = size.width - w;
  71.         h = size.height - h;
  72.  
  73.         System.out.println("Obraz załadowany.");
  74.         System.out.println("Rozmiar: " + h + "x" + w);
  75.  
  76.         int[][] red = new int[h][w];
  77.         int[][] green = new int[h][w];
  78.         int[][] blue = new int[h][w];
  79.  
  80.         getColorArray(red, green, blue);
  81. //wyswietl_i2(green);
  82.         int[][] y = new int[h][w];
  83.         int[][] cb = new int[h][w];
  84.         int[][] cr = new int[h][w];
  85.  
  86.         convertRGBtoYCbCr(red, green, blue, y, cb, cr);
  87.  
  88.         double[][] data_y = new double[h][w];
  89.         double[][] data_cb = new double[h][w];
  90.         double[][] data_cr = new double[h][w];
  91. // wyswietl_i2(y);
  92.         dct2d(y, cb, cr, data_y, data_cb, data_cr);
  93.  
  94. //wyswietl_i2(y);
  95. //wyswietl_d2(data_y);
  96. //wyswietl_d2(data_y);
  97.         quantize(data_y, data_cb, data_cr);
  98.  
  99.  
  100.         int tmp[] = new int[h * w];
  101.         tmp = SetToArray(data_y, data_cb, data_cr);
  102. //wyswietl_d2(data_y);
  103.  
  104. //wyswietl_d2(data_cb);
  105. //wyswietl_d2(data_cb);
  106. //wyswietl_i1(tmp);
  107.         try {
  108.         zapis(tmp);
  109.         deflate();
  110.         inflate();
  111.         } catch (IOException e) {
  112.         System.err.println("Error: " + e.getMessage());
  113.         }
  114.         delete("wyjscie");
  115.         int h1 = 0, w1 = 0;
  116.  
  117.  
  118.         ArrayList<Integer> Lista = new ArrayList<Integer>();
  119.         try {
  120.         czytaj2(Lista);
  121.         } catch (IOException e) {
  122.         System.err.println("Error: " + e.getMessage());
  123.         }
  124.         h1 = Lista.get(0);
  125.         w1 = Lista.get(1);
  126.          
  127. //System.err.println(Lista.size());
  128.  
  129. //wyswiel_lista(Lista);
  130.  
  131.         nazwa2(Lista, h1, w1, data_y, data_cb, data_cr);
  132. //wyswietl_i2(y);
  133. //wyswietl_d2(data_y);
  134.         deQuantitize(data_y, data_cb, data_cr);
  135. //wyswietl_d2(data_y);
  136.         idct2d(y, cb, cr, data_y, data_cb, data_cr);
  137. //wyswietl_i2(y);
  138.         convertYCbCrToRGB(red, green, blue, y, cb, cr);
  139. //wyswietl_i2(red);
  140.         tmp = SetToArray2(red, green, blue);
  141.  
  142.         ImageWindow iw = new ImageWindow(tmp, nazwa, h, w);
  143.         //wyswietl_px(red, green, blue);
  144.  
  145.     }
  146.  
  147.     public void nazwa2(ArrayList<Integer> Lista, int h1, int w1,
  148.             double[][] data_y, double[][] data_cb, double[][] data_cr) {
  149.         /*System.out.println();
  150.         System.out.println();
  151.         System.out.println();
  152.         System.out.println();
  153.         System.out.println("nazwa2-----------------");
  154.         System.out.println("");
  155.          *
  156.          */
  157.         //output[index] = ((int) input1[i][j] << 16) | ((int) input1[i][j] << 8) | (int) input3[i][j];
  158.         int size = Lista.size();
  159.         int index = 2;
  160.         for (int i = 0; i < h1; i++) {
  161.             for (int j = 0; j < w1; j++) {
  162.                 data_y[i][j] = ((Lista.get(index) & 0x00ff0000) >> 16);
  163.                 data_cb[i][j] = ((Lista.get(index) & 0x0000ff00) >> 8);
  164.                 data_cr[i][j] = ((Lista.get(index) & 0x000000ff));
  165.                 /*System.out.println(Math.round(Lista.get(index)) + ">" + Math.round(data_y[i][j])
  166.                 + " " + Math.round(data_cb[i][j]) + " " + Math.round(data_cr[i][j]));
  167.                  */
  168.                 //System.out.print((Lista.get(index)) + " ") ;
  169.                 index++;
  170.             }
  171.         }
  172.     }
  173.  
  174.     public int[] SetToArray(double[][] input1, double[][] input2, double[][] input3) {
  175.         int output[] = new int[h * w];
  176.         /*
  177.         System.out.println();
  178.         System.out.println();
  179.         System.out.println();
  180.         System.out.println("SET TO ARRAY --------------------------");
  181.          * */
  182.  
  183.         int index = 0;
  184.  
  185.         for (int i = 0; i < h; i++) {
  186.             for (int j = 0; j < w; j++) {
  187.                 /*if ((i + j) % 64 == 0) {
  188.                 //    System.out.println();
  189.                 }
  190.                  */
  191.                /* if (input1[i][j] < 0) {
  192.                     input1[i][j] = 0;
  193.                 }
  194.                 if (input2[i][j] < 0) {
  195.                     input2[i][j] = 0;
  196.                 }
  197.                 if (input3[i][j] < 0) {
  198.                     input3[i][j] = 0;
  199.                 }
  200. */
  201.                 output[index] = ((int) input1[i][j] << 16) | ((int) input2[i][j] << 8) | (int) input3[i][j];
  202.                 index++;
  203.                 /*
  204.                 System.out.println(Math.round(output[index - 1]) + ">>" + Math.round(input1[i][j])
  205.                 + " " + Math.round(input2[i][j]) + " " + Math.round(input3[i][j]));
  206.                  */
  207.                 //System.out.print(Math.round(input1[i][j]) + " ");
  208.                 //System.out.print(Math.round(output[index-1]) + " ");
  209.             }
  210.         }
  211.         return output;
  212.     }
  213.  
  214.     public int[] SetToArray2(int[][] input1, int[][] input2, int[][] input3) {
  215.         int output[] = new int[h * w];
  216.         int index = 0;
  217.  
  218.         for (int i = 0; i < h; i++) {
  219.             for (int j = 0; j < w; j++) {
  220.                 if (input1[i][j] < 0) {
  221.                 input1[i][j] = 0;
  222.                 }
  223.                 if (input2[i][j] < 0) {
  224.                 input2[i][j] = 0;
  225.                 }
  226.                 if (input3[i][j] < 0) {
  227.                 input3[i][j] = 0;
  228.                 }
  229.                 //Math.abs();
  230.                 //output[index] = -16777216 | (int)(input1[i][j] << 16) | (int)(input2[i][j] << 8) | (int)input3[i][j];
  231.                 //output[index] = (255 << 24 | (int) Math.abs(input1[i][j]) << 16) | ((int) Math.abs(input2[i][j]) << 8) | (int) Math.abs(input3[i][j]);
  232.                 output[index] = (255 << 24 | (int) input1[i][j] << 16) | ((int) input2[i][j] << 8) | (int) input3[i][j];
  233.                 //output[index] = (-16777216 | (int) 0 << 16) | ((int) 0 << 8) | (int) 0;
  234.                 //System.out.println(input1[i][j] + " " + input2[i][j] + " " + input3[i][j] + " ");
  235.                 index++;
  236.             }
  237.         }
  238.         return output;
  239.     }
  240.  
  241.     public void czytaj2(ArrayList<Integer> Lista) throws IOException {
  242.         //ArrayList<Integer> r = new ArrayList<Integer>();
  243.         File file = new File("wejscie");
  244.         byte[] bytes = new byte[(int) file.length()];
  245.         try {
  246.             InputStream in = new FileInputStream(file);
  247.             in.read(bytes);
  248.             //System.out.println();
  249.  
  250.         } catch (FileNotFoundException e) {
  251.             System.err.println("Nie mozna odnaleść pliku.");
  252.             e.printStackTrace();
  253.         }
  254.         int value;
  255.         value = ((bytes[0] & 0xFF) << 24)
  256.                 + ((bytes[1] & 0xFF) << 16)
  257.                 + ((bytes[2] & 0xFF) << 8)
  258.                 + (bytes[3] & 0xFF);
  259.         Lista.add(value);
  260.  
  261.         value = ((bytes[4] & 0xFF) << 24)
  262.                 + ((bytes[5] & 0xFF) << 16)
  263.                 + ((bytes[6] & 0xFF) << 8)
  264.                 + (bytes[7] & 0xFF);
  265.         Lista.add(value);
  266.  
  267.         int j = 8;
  268.         int dstLength = Lista.get(0) * Lista.get(1);
  269.         for (int i = 0; i < dstLength; i++) {
  270.             value = ((bytes[j++] & 0xFF) << 24)
  271.                     + ((bytes[j++] & 0xFF) << 16)
  272.                     + ((bytes[j++] & 0xFF) << 8)
  273.                     + (bytes[j++] & 0xFF);
  274.             Lista.add(value);
  275.             /*
  276.             if (i % 8 == 0) {
  277.             System.out.println();
  278.             }
  279.             System.out.print(Lista.get(i + 2) + " ");
  280.              */
  281.         }
  282.     }
  283.  
  284.     public static void delete(String fileName) {
  285.         try {
  286.             // Construct a File object for the file to be deleted.
  287.             File target = new File(fileName);
  288.  
  289.             if (!target.exists()) {
  290.                 System.err.println("Plik " + fileName
  291.                         + " nie istnieje!");
  292.                 return;
  293.             }
  294.  
  295.             // Quick, now, delete it immediately:
  296.             if (target.delete()) {
  297.                 System.err.println("Usinięto plik tymczasowy '" + fileName + "' ");
  298.             } else {
  299.                 System.err.println("Usuwanie zakończone niepowodzeniem " + fileName);
  300.             }
  301.         } catch (SecurityException e) {
  302.             System.err.println("Nie można usunąć " + fileName + "("
  303.                     + e.getMessage() + ")");
  304.         }
  305.     }
  306.  
  307.     public static void deletefile() {
  308.  
  309.         File f1 = new File("wyjscie");
  310.         boolean success = f1.delete();
  311.         if (!success) {
  312.             System.out.println("Deletion failed.");
  313.             System.exit(0);
  314.         } else {
  315.             System.out.println("File deleted.");
  316.         }
  317.     }
  318.  
  319.     public void deflate() throws IOException {
  320.  
  321.         Deflater def = new Deflater();
  322.         byte[] input = new byte[1024];
  323.         byte[] output = new byte[1024];
  324.  
  325.         FileInputStream fin = new FileInputStream("wyjscie");
  326.         FileOutputStream fout = new FileOutputStream("wyjscie" + DEFLATE_SUFFIX);
  327.  
  328.         while (true) { // read and deflate the data
  329.  
  330.             // Fill the input array.
  331.             int numRead = fin.read(input);
  332.             if (numRead == -1) { // end of stream
  333.                 // Deflate any data that remains in the input buffer.
  334.                 def.finish();
  335.                 while (!def.finished()) {
  336.                     int numCompressedBytes = def.deflate(output, 0, output.length);
  337.                     if (numCompressedBytes > 0) {
  338.                         fout.write(output, 0, numCompressedBytes);
  339.                     } // end if
  340.                 }  // end while
  341.                 break; // Exit while loop.
  342.             } // end if
  343.             else {  // Deflate the input.
  344.                 def.setInput(input, 0, numRead);
  345.                 while (!def.needsInput()) {
  346.                     int numCompressedBytes = def.deflate(output, 0, output.length);
  347.                     if (numCompressedBytes > 0) {
  348.                         fout.write(output, 0, numCompressedBytes);
  349.                     } // end if
  350.                 }  // end while
  351.             }  // end else
  352.         } // end while
  353.         fin.close();
  354.         fout.flush();
  355.         fout.close();
  356.         def.reset();
  357.     }
  358.  
  359.     public void inflate() throws IOException {
  360.  
  361.         Inflater inf = new Inflater();
  362.         byte[] input = new byte[1024];
  363.         byte[] output = new byte[1024];
  364.         try {
  365.             FileInputStream fin = new FileInputStream("wyjscie.dfl");
  366.             FileOutputStream fout = new FileOutputStream("wejscie");
  367.  
  368.             while (true) { // Read and inflate the data.
  369.  
  370.                 // Fill the input array.
  371.                 int numRead = fin.read(input);
  372.                 if (numRead != -1) { // End of stream, finish inflating.
  373.                     inf.setInput(input, 0, numRead);
  374.                 } // end if
  375.                 // Inflate the input.
  376.  
  377.                 int numDecompressed = 0;
  378.                 while ((numDecompressed = inf.inflate(output, 0, output.length))
  379.                         != 0) {
  380.                     fout.write(output, 0, numDecompressed);
  381.                 }
  382.                 // At this point inflate() has returned 0.
  383.                 // Let's find out why.
  384.                 if (inf.finished()) { // all done
  385.                     break;
  386.                 } else if (inf.needsDictionary()) { // We don't handle dictionaries.
  387.                     System.err.println("Dictionary required! bailing...");
  388.                     break;
  389.                 } else if (inf.needsInput()) {
  390.                     continue;
  391.                 }
  392.             } // end while
  393.  
  394.             // Close up and get ready for the next file.
  395.             fin.close();
  396.             fout.flush();
  397.             fout.close();
  398.             inf.reset();
  399.         } // end try
  400.         catch (IOException ex) {
  401.             System.err.println(ex);
  402.         } catch (DataFormatException ex) {
  403.             System.err.println("wyjscie.dfl appears to be corrupt");
  404.             System.err.println(ex);
  405.         } // end catch
  406.  
  407.     }
  408.  
  409.     public static final byte[] intToByteArray(int value) {
  410.         return new byte[]{
  411.                     (byte) (value >>> 24),
  412.                     (byte) (value >>> 16),
  413.                     (byte) (value >>> 8),
  414.                     (byte) value};
  415.     }
  416.  
  417.     public static byte[] int2byte(int[] src) {
  418.         int srcLength = src.length;
  419.         byte[] dst = new byte[srcLength << 2];
  420.         //System.out.println("ZAPIS---------------------------------------------------");
  421.         for (int i = 0; i < srcLength; i++) {
  422.             int x = src[i];
  423.             int j = i << 2;
  424.             if (i % 8 == 0) {
  425.                 //System.out.println();
  426.             }
  427.             dst[j++] = (byte) ((x >>> 24));
  428.             //System.out.print(dst[j - 1] + " ");
  429.             dst[j++] = (byte) ((x >>> 16));
  430.             //System.out.print(dst[j - 1] + " ");
  431.             dst[j++] = (byte) ((x >>> 8));
  432.             //System.out.print(dst[j - 1] + " ");
  433.             dst[j++] = (byte) (x);
  434.             //System.out.print(dst[j - 1] + " ");
  435.             //System.out.print(src[i] + " ");
  436.  
  437.         }
  438.         //System.out.println("---------------------------------------------------");
  439.         return dst;
  440.     }
  441.  
  442.     public static final int byteArrayToInt(byte[] b) {
  443.         return //(b[0] << 24)
  444.                 +((b[1] & 0xFF) << 16)
  445.                 + ((b[2] & 0xFF) << 8)
  446.                 + (b[3] & 0xFF);
  447.     }
  448.  
  449.     public static int[] byte2int(byte[] src, int h1, int w1) {
  450.         //int srcLength = src.length;
  451.         //System.out.println(srcLength + "tu");
  452.         //System.out.println("byte2int---------------------------------------------------");
  453. //        int h1 = ((src[0] & 0xFF) << 24)
  454. //                + ((src[1] & 0xFF) << 16)
  455. //                + ((src[2] & 0xFF) << 8)
  456. //                + (src[3] & 0xFF);
  457. //        int w1 = ((src[4] & 0xFF) << 24)
  458. //                + ((src[5] & 0xFF) << 16)
  459. //                + ((src[6] & 0xFF) << 8)
  460. //                + (src[7] & 0xFF);
  461.         int[] dst = new int[h1 * w1];
  462.         int dstLength = h1 * w1;
  463.         System.out.println(h1 + " " + w1 + " " + (h1 * w1));
  464.         int j = 8;
  465.         int i;
  466.         for (i = 0; i <= dstLength; i++) {
  467.             dst[i] = ((src[j++] & 0xFF) << 24)
  468.                     + ((src[j++] & 0xFF) << 16)
  469.                     + ((src[j++] & 0xFF) << 8)
  470.                     + (src[j++] & 0xFF);
  471.             if (i % 8 == 0) {
  472.                 //System.out.println();
  473.             }
  474.             //System.out.print(dst[i] + " ");
  475.         }
  476.         //System.out.print(i + " ");
  477.         return dst;
  478.     }
  479.  
  480.     public void zapis(int input[]) throws IOException {
  481.         File file = new File("wyjscie");
  482.         OutputStream out = new FileOutputStream(file);
  483.  
  484.         int Length = input.length;
  485.         byte[] dst = new byte[Length << 2];
  486.         dst = int2byte(input);
  487.  
  488.         out.write(intToByteArray(h));
  489.         out.write(intToByteArray(w));
  490.  
  491.         out.write(dst);
  492.  
  493.         out.close();
  494.         out.flush();
  495.     }
  496.  
  497.     public static int[] czytaj(int h1, int w1) throws IOException {
  498.         File file = new File("wejscie");
  499.         byte[] bytes = new byte[(int) file.length()];
  500.         try {
  501.             InputStream in = new FileInputStream(file);
  502.             in.read(bytes);
  503.             //System.out.println();
  504.  
  505.         } catch (FileNotFoundException e) {
  506.             System.err.println("Nie mozna odnaleść pliku.");
  507.             e.printStackTrace();
  508.         }
  509.         h1 = ((bytes[0] & 0xFF) << 24)
  510.                 + ((bytes[1] & 0xFF) << 16)
  511.                 + ((bytes[2] & 0xFF) << 8)
  512.                 + (bytes[3] & 0xFF);
  513.         w1 = ((bytes[4] & 0xFF) << 24)
  514.                 + ((bytes[5] & 0xFF) << 16)
  515.                 + ((bytes[6] & 0xFF) << 8)
  516.                 + (bytes[7] & 0xFF);
  517.         int[] pixels = new int[h1 * w1];
  518.         pixels = byte2int(bytes, h1, w1);
  519.         for (int i = 0; i < h1; i++) {
  520.             System.out.print(pixels[i] + " ");
  521.         }
  522.         //int[][] pixels2 = new int[h1][w1];
  523.         //int index = 0;
  524.         /*for(int i = 0; i < h1; i++){
  525.         for(int j = 0; j < w1; j++){
  526.         pixels2[i][j] = pixels[index++];
  527.         System.out.print(pixels2[i][j] + " ");
  528.         }
  529.         }
  530.         System.out.print(pixels[0] + " ");*/
  531.         return pixels;
  532.     }
  533.  
  534.     public void wyswietl_d2(double input[][]) {
  535.         System.out.println("");
  536.         for (int i = 0; i < h; i++) {
  537.             for (int j = 0; j < w; j++) {
  538.                 System.out.print(Math.round(input[i][j]) + " ");
  539.             }
  540.             System.out.println("");
  541.         }
  542.     }
  543.  
  544.     public void wyswietl_i1(int input[]) {
  545.         for (int i = 0; i < h * w; i++) {
  546.             if (i % 64 == 0) {
  547.                 System.out.println();
  548.             }
  549.             System.out.print(input[i] + " ");
  550.         }
  551.         System.out.println();
  552.     }
  553.  
  554.     public void wyswietl_i2(int input[][]) {
  555.         System.out.println("");
  556.         System.out.println("");
  557.         System.out.println("");
  558.         for (int i = 0; i < h; i++) {
  559.             for (int j = 0; j < w; j++) {
  560.                 System.out.print((input[i][j]) + " ");
  561.             }
  562.             System.out.println("");
  563.         }
  564.     }
  565.  
  566.     public void wyswietl_b1(byte input[]) {
  567.         System.out.println("");
  568.         for (int i = 0; i < h * w; i++) {
  569.             if (i % 64 == 0) {
  570.                 System.out.println();
  571.             }
  572.             System.out.print((input[i]) + " ");
  573.  
  574.             //System.out.println("");
  575.         }
  576.     }
  577.  
  578.     public void wyswietl_px(int input[][], int input2[][], int input3[][]) {
  579.         System.out.println("");
  580.         for (int i = 0; i < h * w; i++) {
  581.             for (int j = 0; j < w; j++) {
  582.                 System.out.println((input[i][j]) + " " + (input2[i][j]) + " " + (input3[i][j]) + " ");
  583.             }
  584.         }
  585.     }
  586.  
  587.     public void wyswiel_lista(ArrayList<Integer> Lista) {
  588.         System.out.println("");
  589.         for (int i = 2; i < h * w; i++) {
  590.             if ((i - 2) % 64 == 0) {
  591.                 System.out.println();
  592.             }
  593.             System.out.print((Lista.get(i)) + " ");
  594.  
  595.             //System.out.println("");
  596.         }
  597.     }
  598.  
  599.     public void getColorArray(int red[][], int green[][], int blue[][]) {
  600.         int values[] = new int[h * w];
  601.         PixelGrabber grabber = new PixelGrabber(image.getSource(), 0, 0, w, h, values, 0, w);
  602.  
  603.         try {
  604.             if (grabber.grabPixels() != true) {
  605.                 try {
  606.                     throw new AWTException("Grabber zwrocił false: " + grabber.status());
  607.                 } catch (Exception e) {
  608.                 }
  609.                 ;
  610.             }
  611.         } catch (InterruptedException e) {
  612.         }
  613.         ;
  614.  
  615.         int index = 0;
  616.         for (int i = 0; i < h; ++i) {
  617.             for (int j = 0; j < w; ++j) {
  618.  
  619.                 red[i][j] = ((values[index] & 0x00ff0000) >> 16);
  620.                 //System.out.println(index + " " + pixel[i][j]);
  621.  
  622.                 green[i][j] = ((values[index] & 0x0000ff00) >> 8);
  623.                 //System.out.println(index + " " + pixel[i][j]);
  624.  
  625.                 blue[i][j] = (values[index] & 0x000000ff);
  626.                 //System.out.println(index + " " + pixel[i][j]);
  627.                 //System.out.print("[" + (i) + "]" + "[" + (j) + "]" + " ");
  628.  
  629.                 index++;
  630.             }
  631.         }
  632.     }
  633.  
  634.     public void convertRGBtoYCbCr(int red[][], int green[][], int blue[][],
  635.             int y[][], int cb[][], int cr[][]) {
  636.         /*
  637.         Y = (77/256)R + (150/256)G + (29/256)B,
  638.         Cb = -(44/256)R - (87/256)G + (131/256)B + 128,
  639.         Cr = (131/256)R - (110/256)G - (21/256)B + 128;
  640.          */
  641.  
  642.         for (int i = 0; i < h; i++) {
  643.             for (int j = 0; j < w; j++) {
  644.  
  645.                 y[i][j] = (int) (((double) 77 / 256) * red[i][j]
  646.                         + ((double) 150 / 256) * green[i][j]
  647.                         + ((double) 29 / 256) * blue[i][j]);
  648.  
  649.                 cb[i][j] = (int) (-((double) 44 / 256) * red[i][j]
  650.                         - ((double) 87 / 256) * green[i][j]
  651.                         + ((double) 131 / 256) * blue[i][j]
  652.                         + 128);
  653.  
  654.                 cr[i][j] = (int) (((double) 131 / 256) * red[i][j]
  655.                         - ((double) 110 / 256) * green[i][j]
  656.                         - ((double) 21 / 256) * blue[i][j]
  657.                         + 128);
  658.  
  659.             }
  660.         }
  661.     }
  662.  
  663.     public void convertYCbCrToRGB(int red[][], int green[][], int blue[][],
  664.             int y[][], int cb[][], int cr[][]) {
  665.         /*
  666.         R = Y+1.371(Cr - 128),
  667.         G = Y- 0.698(Cr - 128) - 0.336(Cb - 128),
  668.         B = Y+1.732(Cb - 128).
  669.          */
  670.  
  671.         for (int i = 0; i < h; i++) {
  672.             for (int j = 0; j < w; j++) {
  673.                 //convert to R
  674.                 red[i][j] = (int) (y[i][j] + (1.371 * (cr[i][j] - 128)));
  675.  
  676.                 //convert to G
  677.                 green[i][j] = (int) (y[i][j]
  678.                         - 0.698 * (cr[i][j] - 128)
  679.                         - 0.336 * (cb[i][j] - 128));
  680.  
  681.                 //convert to B
  682.  
  683.                 blue[i][j] = (int) (y[i][j]
  684.                         + 1.732 * (cb[i][j] - 128));
  685.  
  686.                 //System.out.println(red[i][j] + " " + green[i][j] + " " + blue[i][j] + " ");
  687.             }
  688.         }
  689.     }
  690.  
  691.     public void dct2d(int y[][], int cb[][], int cr[][],
  692.             double data_y[][], double data_cb[][], double data_cr[][]) {
  693.  
  694.         double data1[][] = new double[BLOK][BLOK];
  695.         double data2[][] = new double[BLOK][BLOK];
  696.         double data3[][] = new double[BLOK][BLOK];
  697.         int blokow_w = w / BLOK;
  698.         int blokow_h = h / BLOK;
  699.         int xpos, ypos;
  700.  
  701.         double Cu = 0.0, Cv = 0.0;
  702.         double sum1 = 0.0, sum2 = 0.0, sum3 = 0.0;
  703.         double s1 = 0.0, s2 = 0.0, s3 = 0.0;
  704.         double[][] temp = new double[8][8];
  705.  
  706.         temp = cosin(temp);
  707.  
  708.         for (int i = 0; i < blokow_h; i++) {
  709.             for (int j = 0; j < blokow_w; j++) {
  710.  
  711.                 xpos = i * BLOK;
  712.                 ypos = j * BLOK;
  713.  
  714.                 for (int a = 0; a < BLOK; a++) {
  715.                     for (int b = 0; b < BLOK; b++) {
  716.  
  717.                         Cu = ((a == 0) ? 1.0 / Math.sqrt(2.0) : 1.0);
  718.                         Cv = ((b == 0) ? 1.0 / Math.sqrt(2.0) : 1.0);
  719.  
  720.                         for (int c = 0; c < BLOK; c++) {
  721.                             for (int d = 0; d < BLOK; d++) {
  722.                                 s1 = y[c + xpos][d + ypos] * temp[d][b] * temp[c][a];
  723.                                 s2 = cb[c + xpos][d + ypos] * temp[d][b] * temp[c][a];
  724.                                 s3 = cr[c + xpos][d + ypos] * temp[d][b] * temp[c][a];
  725.                                 //  * Math.cos((double) (2 * d + 1) * (double) b * Math.PI / 16.0)
  726.                                 //  * Math.cos((double) (2 * c + 1) * (double) a * (Math.PI / 16.0));
  727.                                 sum1 += s1;
  728.                                 sum2 += s2;
  729.                                 sum3 += s3;
  730.                             }
  731.                         }
  732.                         data1[a][b] = 0.25 * Cu * Cv * sum1;
  733.                         data2[a][b] = 0.25 * Cu * Cv * sum2;
  734.                         data3[a][b] = 0.25 * Cu * Cv * sum3;
  735.                         sum1 = 0;
  736.                         sum2 = 0;
  737.                         sum3 = 0;
  738.                         //System.out.print("[" + (xpos + b ) + "]" + "[" + (ypos + a) + "]" + "");
  739.                         data_y[xpos + b][ypos + a] = data1[a][b];
  740.                         data_cb[xpos + b][ypos + a] = data2[a][b];
  741.                         data_cr[xpos + b][ypos + a] = data3[a][b];
  742.                     }
  743.                 }
  744.             }
  745.         }
  746.     }
  747.  
  748.     public void idct2d(int y[][], int cb[][], int cr[][],
  749.             double data_y[][], double data_cb[][], double data_cr[][]) {
  750.         //wyswietl_d2(data_y);
  751.         double data1[][] = new double[BLOK][BLOK];
  752.         double data2[][] = new double[BLOK][BLOK];
  753.         double data3[][] = new double[BLOK][BLOK];
  754.         int blokow_w = w / BLOK;
  755.         int blokow_h = h / BLOK;
  756.         int xpos, ypos;
  757.  
  758.         double Cu = 0.0, Cv = 0.0;
  759.         double sum1 = 0.0, sum2 = 0.0, sum3 = 0.0;
  760.         double s1 = 0.0, s2 = 0.0, s3 = 0.0;
  761.  
  762.         for (int i = 0; i < blokow_h; i++) {
  763.             for (int j = 0; j < blokow_w; j++) {
  764.  
  765.                 xpos = i * BLOK;
  766.                 ypos = j * BLOK;
  767.  
  768.                 for (int a = 0; a < BLOK; a++) {
  769.                     Cu = ((a == 0) ? 1.0 / Math.sqrt(2.0) : 1.0);
  770.                     for (int b = 0; b < BLOK; b++) {
  771.                         Cv = ((b == 0) ? 1.0 / Math.sqrt(2.0) : 1.0);
  772.                         for (int c = 0; c < BLOK; c++) {
  773.                             for (int d = 0; d < BLOK; d++) {
  774.                                 // s1 = Math.cos(((2 * (double) a + 1) / 16.000 * (double) c * 3.14));
  775.                                 // sum1 += s1;
  776.                                 //  * Math.cos((double) (2 * d + 1) * (double) b * Math.PI / 16.0)
  777.                                 //  * Math.cos((double) (2 * c + 1) * (double) a * (Math.PI / 16.0));
  778.  
  779.                                 /*s1 = data_y[c + xpos][d + ypos] * Math.cos((2 * b + 1) * d * Math.PI / 16.000)
  780.                                  * Math.cos((2 * a + 1) * c * Math.PI / 16.000);
  781.                                 sum1 += s1;
  782.                                 s2 = data_cb[c + xpos][d + ypos] * Math.cos((2 * b + 1) * d * Math.PI / 16.000)
  783.                                  * Math.cos((2 * a + 1) * c * Math.PI / 16.000);
  784.                                 sum2 += s2;
  785.                                 s3 = data_cr[c + xpos][d + ypos] * Math.cos((2 * b + 1) * d * Math.PI / 16.000)
  786.                                  * Math.cos((2 * a + 1) * c * Math.PI / 16.000);
  787.                                 sum3 += s3;*/
  788.                                 s1 = Math.cos(((2 * (double) c + 1) / 16.000 * (double) a * Math.PI));
  789.                                 s1 = s1 * Math.cos(((2 * (double) d + 1) / 16.000 * (double) b * Math.PI));
  790.                                 sum1 += data_y[c + xpos][d + ypos] * s1;
  791.                                 sum2 += data_cb[c + xpos][d + ypos] * s1;
  792.                                 sum3 += data_cr[c + xpos][d + ypos] * s1;
  793.  
  794.                             }
  795.                         }
  796.                         data1[a][b] = 0.25 * Cu * Cv * sum1;
  797.                         data2[a][b] = 0.25 * Cu * Cv * sum2;
  798.                         data3[a][b] = 0.25 * Cu * Cv * sum3;
  799.                         sum1 = 0;
  800.                         sum2 = 0;
  801.                         sum3 = 0;
  802.                         y[xpos + b][ypos + a] = (int) data1[a][b];
  803.                         cb[xpos + b][ypos + a] = (int) data2[a][b];
  804.                         cr[xpos + b][ypos + a] = (int) data3[a][b];
  805.                         //System.out.print(y[xpos + b][ypos + a] + " ");
  806.                     }
  807.                 }
  808.             }
  809.         }
  810.         //wyswietl_i2(y);
  811.     }
  812.  
  813.     double[][] cosin(double temp[][]) {
  814.         for (int i = 0; i < BLOK; i++) {
  815.             for (int j = 0; j < BLOK; j++) {
  816.                 temp[i][j] = Math.cos((double) (2 * i + 1) * (double) j * (Math.PI / 16.0));
  817.             }
  818.         }
  819.         return temp;
  820.     }
  821.  
  822.     public void quantize(double data_y[][], double data_cb[][], double data_cr[][]) {
  823.         /*
  824.         System.out.println();
  825.         System.out.println("Przed kwantyzacja");
  826.         System.out.println();
  827.          *
  828.          */
  829.         int blokow_h = h / BLOK;
  830.         int blokow_w = w / BLOK;
  831.         int xpos, ypos, row, col;
  832.         double wynik;
  833.         initZZMatrix();
  834.         for (int a = 0; a < blokow_h; a++) {
  835.             for (int b = 0; b < blokow_w; b++) {
  836.                 xpos = a * BLOK;
  837.                 ypos = b * BLOK;
  838.                 for (int i = 0; i < BLOK * BLOK; i++) {
  839.  
  840.                     /*
  841.                     data_y[xpos + i][ypos + j] = (int) (data_y[xpos + i][ypos + j] / luminance[i][j]);
  842.                     data_cb[xpos + i][ypos + j] = (int) (data_cb[xpos + i][ypos + j] / chrominance[i][j]);
  843.                     data_cr[xpos + i][ypos + j] = (int) (data_cr[xpos + i][ypos + j] / chrominance[i][j]);
  844.                      */
  845.                     row = ZZMatrix[i][0];
  846.                     col = ZZMatrix[i][1];
  847.                     //System.out.print(Math.round(data_y[xpos + row][ypos + col]) + " ");
  848.                     wynik = (int) (data_y[xpos + row][ypos + col] / luminance[row][col]);
  849.                     data_y[xpos + row][ypos + col] = (int) (Math.round(wynik));
  850.  
  851.                     wynik = (int) (data_cb[xpos + row][ypos + col] / chrominance[row][col]);
  852.                     data_cb[xpos + row][ypos + col] = (int) (Math.round(wynik));
  853.  
  854.                     wynik = (int) (data_cr[xpos + row][ypos + col] / chrominance[row][col]);
  855.                     data_cr[xpos + row][ypos + col] = (int) (Math.round(wynik));
  856.  
  857.                     //System.out.print(Math.round(data_y[xpos + row][ypos + col]) + " ");
  858.                 }
  859.                 //System.out.println();
  860.             }
  861.         }
  862.     }
  863.  
  864.     public void deQuantitize(double data_y[][], double data_cb[][], double data_cr[][]) {
  865.         /*
  866.         System.out.println();
  867.         System.out.println("Po dekwantyzacji");
  868.          *
  869.          */
  870.         int blokow_h = h / BLOK;
  871.         int blokow_w = w / BLOK;
  872.         int xpos, ypos, row, col;
  873.         double wynik;
  874.         for (int a = 0; a < blokow_h; a++) {
  875.             for (int b = 0; b < blokow_w; b++) {
  876.                 xpos = a * BLOK;
  877.                 ypos = b * BLOK;
  878.                 for (int i = 0; i < BLOK * BLOK; i++) {
  879.                     /*
  880.                     data_y[xpos + i][ypos + j] = (int) (data_y[xpos + i][ypos + j] * luminance[i][j]);
  881.                     data_cb[xpos + i][ypos + j] = (int) (data_cb[xpos + i][ypos + j] * chrominance[i][j]);
  882.                     data_cr[xpos + i][ypos + j] = (int) (data_cr[xpos + i][ypos + j] * chrominance[i][j]);
  883.                      */
  884.                     row = ZZMatrix[i][0];
  885.                     col = ZZMatrix[i][1];
  886.  
  887.                     wynik = (int) (data_y[xpos + row][ypos + col] * luminance[row][col]);
  888.                     data_y[xpos + row][ypos + col] = (int) (Math.round(wynik));
  889.  
  890.                     wynik = (int) (data_cb[xpos + row][ypos + col] * chrominance[row][col]);
  891.                     data_cb[xpos + row][ypos + col] = (int) (Math.round(wynik));
  892.  
  893.                     wynik = (int) (data_cr[xpos + row][ypos + col] * chrominance[row][col]);
  894.                     data_cr[xpos + row][ypos + col] = (int) (Math.round(wynik));
  895.  
  896.                     //System.out.print(Math.round(data_y[xpos + row][ypos + col]) + " ");
  897.                 }
  898.                 //System.out.println();
  899.             }
  900.         }
  901.     }
  902.  
  903.     private void initZZMatrix() {
  904.         ZZMatrix[0][0] = 0; // 0,0
  905.         ZZMatrix[0][1] = 0;
  906.         ZZMatrix[1][0] = 0; // 0,1
  907.         ZZMatrix[1][1] = 1;
  908.         ZZMatrix[2][0] = 1; // 1,0
  909.         ZZMatrix[2][1] = 0;
  910.         ZZMatrix[3][0] = 2; // 2,0
  911.         ZZMatrix[3][1] = 0;
  912.         ZZMatrix[4][0] = 1; // 1,1
  913.         ZZMatrix[4][1] = 1;
  914.         ZZMatrix[5][0] = 0; // 0,2
  915.         ZZMatrix[5][1] = 2;
  916.         ZZMatrix[6][0] = 0; // 0,3
  917.         ZZMatrix[6][1] = 3;
  918.         ZZMatrix[7][0] = 1; // 1,2
  919.         ZZMatrix[7][1] = 2;
  920.         ZZMatrix[8][0] = 2; // 2,1
  921.         ZZMatrix[8][1] = 1;
  922.         ZZMatrix[9][0] = 3; // 3,0
  923.         ZZMatrix[9][1] = 0;
  924.         ZZMatrix[10][0] = 4; // 4,0
  925.         ZZMatrix[10][1] = 0;
  926.         ZZMatrix[11][0] = 3; // 3,1
  927.         ZZMatrix[11][1] = 1;
  928.         ZZMatrix[12][0] = 2; // 2,2
  929.         ZZMatrix[12][1] = 2;
  930.         ZZMatrix[13][0] = 1; // 1,3
  931.         ZZMatrix[13][1] = 3;
  932.         ZZMatrix[14][0] = 0; // 0,4
  933.         ZZMatrix[14][1] = 4;
  934.         ZZMatrix[15][0] = 0; // 0,5
  935.         ZZMatrix[15][1] = 5;
  936.         ZZMatrix[16][0] = 1; // 1,4
  937.         ZZMatrix[16][1] = 4;
  938.         ZZMatrix[17][0] = 2; // 2,3
  939.         ZZMatrix[17][1] = 3;
  940.         ZZMatrix[18][0] = 3; // 3,2
  941.         ZZMatrix[18][1] = 2;
  942.         ZZMatrix[19][0] = 4; // 4,1
  943.         ZZMatrix[19][1] = 1;
  944.         ZZMatrix[20][0] = 5; // 5,0
  945.         ZZMatrix[20][1] = 0;
  946.         ZZMatrix[21][0] = 6; // 6,0
  947.         ZZMatrix[21][1] = 0;
  948.         ZZMatrix[22][0] = 5; // 5,1
  949.         ZZMatrix[22][1] = 1;
  950.         ZZMatrix[23][0] = 4; // 4,2
  951.         ZZMatrix[23][1] = 2;
  952.         ZZMatrix[24][0] = 3; // 3,3
  953.         ZZMatrix[24][1] = 3;
  954.         ZZMatrix[25][0] = 2; // 2,4
  955.         ZZMatrix[25][1] = 4;
  956.         ZZMatrix[26][0] = 1; // 1,5
  957.         ZZMatrix[26][1] = 5;
  958.         ZZMatrix[27][0] = 0; // 0,6
  959.         ZZMatrix[27][1] = 6;
  960.         ZZMatrix[28][0] = 0; // 0,7
  961.         ZZMatrix[28][1] = 7;
  962.         ZZMatrix[29][0] = 1; // 1,6
  963.         ZZMatrix[29][1] = 6;
  964.         ZZMatrix[30][0] = 2; // 2,5
  965.         ZZMatrix[30][1] = 5;
  966.         ZZMatrix[31][0] = 3; // 3,4
  967.         ZZMatrix[31][1] = 4;
  968.         ZZMatrix[32][0] = 4; // 4,3
  969.         ZZMatrix[32][1] = 3;
  970.         ZZMatrix[33][0] = 5; // 5,2
  971.         ZZMatrix[33][1] = 2;
  972.         ZZMatrix[34][0] = 6; // 6,1
  973.         ZZMatrix[34][1] = 1;
  974.         ZZMatrix[35][0] = 7; // 7,0
  975.         ZZMatrix[35][1] = 0;
  976.         ZZMatrix[36][0] = 7; // 7,1
  977.         ZZMatrix[36][1] = 1;
  978.         ZZMatrix[37][0] = 6; // 6,2
  979.         ZZMatrix[37][1] = 2;
  980.         ZZMatrix[38][0] = 5; // 5,3
  981.         ZZMatrix[38][1] = 3;
  982.         ZZMatrix[39][0] = 4; // 4,4
  983.         ZZMatrix[39][1] = 4;
  984.         ZZMatrix[40][0] = 3; // 3,5
  985.         ZZMatrix[40][1] = 5;
  986.         ZZMatrix[41][0] = 2; // 2,6
  987.         ZZMatrix[41][1] = 6;
  988.         ZZMatrix[42][0] = 1; // 1,7
  989.         ZZMatrix[42][1] = 7;
  990.         ZZMatrix[43][0] = 2; // 2,7
  991.         ZZMatrix[43][1] = 7;
  992.         ZZMatrix[44][0] = 3; // 3,6
  993.         ZZMatrix[44][1] = 6;
  994.         ZZMatrix[45][0] = 4; // 4,5
  995.         ZZMatrix[45][1] = 5;
  996.         ZZMatrix[46][0] = 5; // 5,4
  997.         ZZMatrix[46][1] = 4;
  998.         ZZMatrix[47][0] = 6; // 6,3
  999.         ZZMatrix[47][1] = 3;
  1000.         ZZMatrix[48][0] = 7; // 7,2
  1001.         ZZMatrix[48][1] = 2;
  1002.         ZZMatrix[49][0] = 7; // 7,3
  1003.         ZZMatrix[49][1] = 3;
  1004.         ZZMatrix[50][0] = 6; // 6,4
  1005.         ZZMatrix[50][1] = 4;
  1006.         ZZMatrix[51][0] = 5; // 5,5
  1007.         ZZMatrix[51][1] = 5;
  1008.         ZZMatrix[52][0] = 4; // 4,6
  1009.         ZZMatrix[52][1] = 6;
  1010.         ZZMatrix[53][0] = 3; // 3,7
  1011.         ZZMatrix[53][1] = 7;
  1012.         ZZMatrix[54][0] = 4; // 4,7
  1013.         ZZMatrix[54][1] = 7;
  1014.         ZZMatrix[55][0] = 5; // 5,6
  1015.         ZZMatrix[55][1] = 6;
  1016.         ZZMatrix[56][0] = 6; // 6,5
  1017.         ZZMatrix[56][1] = 5;
  1018.         ZZMatrix[57][0] = 7; // 7,4
  1019.         ZZMatrix[57][1] = 4;
  1020.         ZZMatrix[58][0] = 7; // 7,5
  1021.         ZZMatrix[58][1] = 5;
  1022.         ZZMatrix[59][0] = 6; // 6,6
  1023.         ZZMatrix[59][1] = 6;
  1024.         ZZMatrix[60][0] = 5; // 5,7
  1025.         ZZMatrix[60][1] = 7;
  1026.         ZZMatrix[61][0] = 6; // 6,7
  1027.         ZZMatrix[61][1] = 7;
  1028.         ZZMatrix[62][0] = 7; // 7,6
  1029.         ZZMatrix[62][1] = 6;
  1030.         ZZMatrix[63][0] = 7; // 7,7
  1031.         ZZMatrix[63][1] = 7;
  1032.         /*
  1033.         for (int i = 0; i < 64; i++) {
  1034.         System.out.println(ZZMatrix[i][0] + " " + ZZMatrix[i][1]);
  1035.         }
  1036.          *
  1037.          */
  1038.     }
  1039.  
  1040.     public static void main(String args[]) {
  1041.         long start = System.currentTimeMillis();
  1042.  
  1043.         try {
  1044.             new Main();
  1045.         } catch (Exception e) {
  1046.         }
  1047.         long stop = System.currentTimeMillis();
  1048.         System.out.println("Czas wykonania: " + (stop - start) + "ms");
  1049.  
  1050.         System.out.println("Hit enter");
  1051.         try {
  1052.             System.in.read();
  1053.         } catch (Exception e) {
  1054.         }
  1055.         System.exit(0);
  1056.     }
  1057. }
  1058.  
  1059. class ImageWindow extends Frame {
  1060.  
  1061.     Image image;
  1062.     Image image2;
  1063.     int imageArray_[] = new int[320 * 240];
  1064.     int h, w;
  1065.  
  1066.     public ImageWindow(int[] imageArray, String nazwa, int h1, int w1) {
  1067.         super("DCT Window");
  1068.         h = h1;
  1069.         w = w1;
  1070.         //int imageArray[] = new int[320*240];
  1071.         //imageArray_ = imageArray;
  1072.         setLocation(100, 100);                  //move(100,100);
  1073.         setSize(w * 2 + 50, h + 50);            //resize(700,300);
  1074.  
  1075.         image = this.createImage(new MemoryImageSource(w, h, imageArray, 0, w));
  1076.  
  1077.         // Make sure it gets loaded
  1078.         MediaTracker tracker = new MediaTracker(this);
  1079.         tracker.addImage(image, 1);
  1080.         try {
  1081.             tracker.waitForID(1);
  1082.         } catch (Exception e) {
  1083.             return;
  1084.         }
  1085.  
  1086.         // Load in original image
  1087.         image2 = Toolkit.getDefaultToolkit().getImage(nazwa);
  1088.         tracker = new MediaTracker(this);
  1089.         tracker.addImage(image, 0);
  1090.         try {
  1091.             tracker.waitForID(0);
  1092.         } catch (Exception e) {
  1093.             return;
  1094.         }
  1095.         /*for (int i = 0; i < 320 * 240; i++) {
  1096.         System.out.print(imageArray[i] + " ");
  1097.         if (i % 64 == 0) {
  1098.         System.out.println();
  1099.         }
  1100.         }*/
  1101.         this.show();
  1102.  
  1103.     }
  1104.  
  1105.     public void paint(Graphics g) {
  1106.         g.drawImage(image2, 0, 20, this);//oryginal
  1107.         g.drawImage(image, w + 20, 20, this);//skompresowany
  1108.     }
  1109. }