Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. import jdk.nashorn.internal.runtime.regexp.joni.ast.QuantifierNode;
  2.  
  3. import javax.imageio.ImageIO;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.Arrays;
  9. import java.util.List;
  10.  
  11. import static java.lang.Math.cos;
  12. import static java.lang.Math.getExponent;
  13. import static java.lang.Math.sqrt;
  14.  
  15. /**
  16. * Created by Marcin on 03.12.2016.
  17. */
  18. public class DCT {
  19.  
  20. static int size = 8;
  21.  
  22. static int dane[][] = {
  23. {186,198,199,190,182,177,182,197},
  24. {179,184,183,176,173,172,175,184},
  25. {188,182,180,178,174,172,171,166},
  26. {132,130,139,146,151,169,191,201},
  27. {131,134,137,140,139,139,139,138},
  28. {153,157,161,172,177,145,89,49},
  29. {190,178,192,196,120,43,39,47},
  30. {176,184,187,112,41,39,43,44},
  31. };
  32.  
  33. public static double[][] countDCT(int array[][], int size){
  34. double dct[][] = new double[size][size];
  35. for(int v = 0; v < size; ++v){
  36. for(int u = 0; u < size; ++u){
  37. double cu = (u == 0)? 1.0 / sqrt(2) : 1.0;
  38. double cv = (v== 0)? 1.0 / sqrt(2) : 1.0;
  39. double suma = 0;
  40. for(int y = 0; y < size; ++y){
  41. for(int x = 0; x < size; ++x){
  42. double uCosFactor = cos((2 * x + 1) * Math.PI * u / (2 * size));
  43. double vCosFactor = cos((2 * y + 1) * Math.PI * v / (2 * size));
  44. suma += array[x][y] * uCosFactor * vCosFactor;
  45. }
  46. }
  47. suma *= ((double)2 / size) * cu * cv;
  48. dct[u][v] = suma;
  49. }
  50. }
  51. return dct;
  52. }
  53.  
  54. public static List<int[][]> chunks(int[][] array, int chunks){
  55. int size = array.length / chunks * (array[0].length / chunks);
  56. List<int[][]> subArrays = new ArrayList<>();
  57.  
  58. for (int c = 0; c < size; c++) {
  59. int[][] sub = new int[chunks][chunks];
  60. int startx = (chunks * (c / chunks)) % array.length;
  61. int starty = (chunks * c) % array[0].length;
  62.  
  63. if (starty + chunks > array[0].length) {
  64. starty = 0;
  65. }
  66. if (startx + chunks > array.length) {
  67. continue;
  68. }
  69. for (int row = 0; row < chunks; row++) {
  70. for (int col = 0; col < chunks; col++) {
  71. sub[row][col] = array[startx + row][col + starty];
  72. }
  73. }
  74. subArrays.add(sub);
  75. }
  76. return subArrays;
  77. }
  78.  
  79. public static int[][] joinArrays(List<int[][]> arrays, int size){
  80. int joined[][] = new int[size][size];
  81. int a = 0;
  82. int b = 0;
  83. int ilosc = 0;
  84. int max = size / 8;
  85. for(int[][] ar : arrays){
  86. for(int i = 0; i < 8; i++){
  87. for(int j = 0; j < 8; j++){
  88. joined[a][b] = ar[i][j];
  89. b++;
  90. }
  91. a++;
  92. }
  93. a=0;
  94. }
  95. return joined;
  96. }
  97.  
  98. public static void main(String[] args) throws IOException {
  99. BufferedImage bf = ImageIO.read(new File("1.png"));
  100. int width = bf.getWidth();
  101. int height = bf.getHeight();
  102. int red[][] = new int[width][height];
  103. for(int i = 0; i < width; i++){
  104. for(int j = 0; j<height;j++){
  105. int pixel = bf.getRGB(i,j);
  106. red[i][j] = (pixel >> 16) & 0xFF;
  107. }
  108. }
  109. int test[][] = new int[16][16];
  110. for(int i = 0; i < 16;)
  111. for(int j = 0; j < 16; j++)
  112. test[i][j] = j;
  113. //List<int[][]> chunks = chunks(red, 8);
  114. List<int[][]> chunks = chunks(test, 8);
  115. /*List<double[][]> quan = new ArrayList<>();
  116. for(int[][] array:chunks){
  117. double dct[][] = countDCT(array, 8);
  118. for(int i = 0; i < 8; i++){
  119. for(int j = 0; j < 8; j++){
  120. dct[i][j] = Math.floor(dct[i][j] / 100);
  121. }
  122. }
  123. quan.add(dct);
  124. }*/
  125. int afterQuantisation[][] = joinArrays(chunks, 16);
  126.  
  127. }
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement