Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 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.FileWriter;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9. import java.util.Arrays;
  10. import java.util.List;
  11.  
  12. import static java.lang.Math.cos;
  13. import static java.lang.Math.getExponent;
  14. import static java.lang.Math.sqrt;
  15.  
  16. /**
  17. * Created by Marcin on 03.12.2016.
  18. */
  19. public class DCT {
  20.  
  21. static int size = 8;
  22.  
  23. static int q[][] = {
  24. {16,11,10,16,24,40,51,61},
  25. {12,12,14,19,26,58,60,55},
  26. {14,13,16,24,40,57,69,56},
  27. {14,17,22,29,51,87,80,62},
  28. {18,22,37,56,68,109,103,77},
  29. {24,35,55,64,81,104,113,92},
  30. {49,64,78,87,103,121,120,101},
  31. {72,92,95,98,112,100,103,99}
  32. };
  33.  
  34. public static double[][] countDCT(int array[][], int size){
  35. double dct[][] = new double[size][size];
  36. for(int v = 0; v < size; ++v){
  37. for(int u = 0; u < size; ++u){
  38. double cu = (u == 0)? 1.0 / sqrt(2) : 1.0;
  39. double cv = (v== 0)? 1.0 / sqrt(2) : 1.0;
  40. double suma = 0;
  41. for(int y = 0; y < size; ++y){
  42. for(int x = 0; x < size; ++x){
  43. double uCosFactor = cos((2 * x + 1) * Math.PI * u / (2 * size));
  44. double vCosFactor = cos((2 * y + 1) * Math.PI * v / (2 * size));
  45. suma += array[x][y] * uCosFactor * vCosFactor;
  46. }
  47. }
  48. suma *= ((double)2 / size) * cu * cv;
  49. dct[u][v] = suma;
  50. }
  51. }
  52. return dct;
  53. }
  54.  
  55. public static List<int[][]> chunks(int[][] array, int chunks){
  56. int size = array.length / chunks * (array[0].length / chunks);
  57. List<int[][]> subArrays = new ArrayList<>();
  58.  
  59. for (int c = 0; c < size; c++) {
  60. int[][] sub = new int[chunks][chunks];
  61. int startx = (chunks * (c / chunks)) % array.length;
  62. int starty = (chunks * c) % array[0].length;
  63.  
  64. if (starty + chunks > array[0].length) {
  65. starty = 0;
  66. }
  67. if (startx + chunks > array.length) {
  68. continue;
  69. }
  70. for (int row = 0; row < chunks; row++) {
  71. for (int col = 0; col < chunks; col++) {
  72. sub[row][col] = array[startx + row][col + starty];
  73. }
  74. }
  75. subArrays.add(sub);
  76. }
  77. return subArrays;
  78. }
  79.  
  80. public static double[][] joinArrays(List<double[][]> arrays, int size){
  81. double joined[][] = new double[size][size];
  82. int counter = 0;
  83. int counter1 = 0;
  84. int lol = 0;
  85. for(double ar[][] : arrays)
  86. {
  87. for(int i = 0;i<8;i++)
  88. {
  89. for(int j = 0;j<8;j++)
  90. {
  91. if(counter1>7)lol+=8;
  92. joined[lol+i][counter+j] = ar[i][j];
  93. }
  94. }
  95. counter+=8;
  96. counter1+=1;
  97. }
  98. return joined;
  99. }
  100.  
  101. public static double[][] inverseDCT(double[][] array, int size){
  102. double inverse[][] = new double[8][8];
  103. for(int x = 0; x < size; x++){
  104. for(int y = 0; y < size; y++){
  105. double pixel = 0;
  106. for(int v = 0; v < size; ++v) {
  107. for (int u = 0; u < size; ++u) {
  108. double cu = (u == 0) ? 1.0 / sqrt(2) : 1.0;
  109. double cv = (v == 0) ? 1.0 / sqrt(2) : 1.0;
  110. double uCosFactor = cos((2 * x + 1) * Math.PI * u / (2 * size));
  111. double vCosFactor = cos((2 * y + 1) * Math.PI * v / (2 * size));
  112. double point = array[u][v];
  113. pixel += point * uCosFactor * vCosFactor * cu * cv;
  114. }
  115. }
  116. pixel *= (2 / (double) size);
  117. inverse[x][y] = pixel;
  118. }
  119. }
  120. return inverse;
  121. }
  122.  
  123. public static void main(String[] args) throws IOException {
  124. BufferedImage bf = ImageIO.read(new File("1.png"));
  125. int width = bf.getWidth();
  126. int height = bf.getHeight();
  127. int red[][] = new int[width][height];
  128. for(int i = 0; i < width; i++){
  129. for(int j = 0; j<height;j++){
  130. int pixel = bf.getRGB(i,j);
  131. red[i][j] = (pixel >> 16) & 0xFF;
  132. }
  133. }
  134. List<int[][]> chunks = chunks(red, 8);
  135. List<double[][]> quan = new ArrayList<>();
  136. for(int[][] array:chunks){
  137. double dct[][] = countDCT(array, 8);
  138. for(int i = 0; i < 8; i++){
  139. for(int j = 0; j < 8; j++){
  140. dct[i][j] = Math.round(dct[i][j]/q[i][j]);
  141. }
  142. }
  143. dct = inverseDCT(dct, 8);
  144. quan.add(dct);
  145. }
  146. double joined[][] = joinArrays(quan, 256);
  147. BufferedImage bufferedImage = new BufferedImage(width,height, BufferedImage.TYPE_INT_BGR);
  148. for (int i = 0; i < width; i++){
  149. for(int j = 0; j < height; j++){
  150. bufferedImage.setRGB(i,j,((int)joined[i][j]<<16));
  151. }
  152. }
  153. File file = new File("nowy.jpeg");
  154. ImageIO.write(bufferedImage, "jpeg", file);
  155. }
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement