Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. package gti310.tp4;
  2.  
  3. import java.io.BufferedWriter;
  4. import java.io.DataInputStream;
  5. import java.io.DataOutputStream;
  6. import java.io.FileInputStream;
  7. import java.io.FileNotFoundException;
  8. import java.io.FileOutputStream;
  9. import java.io.IOException;
  10. import java.io.OutputStreamWriter;
  11. import java.util.ArrayList;
  12.  
  13. import com.sun.xml.internal.ws.model.SOAPSEIModel;
  14.  
  15. ;
  16.  
  17. /**
  18. * La Classe YUVEncoder permet d'encoder une imade
  19. * RGB en YUV
  20. * @author julien labrosse
  21. *
  22. */
  23. public class YUV{
  24.  
  25. //Les 3 tableau Y,U,V
  26. private double[][] y = null;
  27. private double[][] u = null;
  28. private double[][] v = null;
  29. private double[][] newy = null;
  30. private double[][] newu = null;
  31. private double[][] newv = null;
  32. private int width;
  33. private int heigth;
  34. private int color;
  35.  
  36. /**
  37. * Methode qui effectue l'encodage en YUV à partir
  38. * d'un fichier RGB
  39. * @param input
  40. * @return T double
  41. * Complexiter : O(n^2)
  42. */
  43. public ArrayList<double[][]> encode(String input) {
  44. ArrayList<double[][]> out = new ArrayList<double[][]>();
  45. int RGB[][][] = PPMReaderWriter.readPPMFile(input);
  46. this.width = RGB[0][0].length;
  47. this.heigth = RGB[0].length;
  48. this.color = 255;
  49. if(this.width != 0) {
  50. y = new double[this.width][this.heigth];
  51. u = new double[this.width][this.heigth];
  52. v = new double[this.width][this.heigth];
  53. for(int i = 0 ; i < this.heigth; i++) {
  54. for(int j = 0 ; j < this.width; j++) {
  55. y[i][j] = /*(int)*/ (RGB[0][i][j] * 0.229 + RGB[1][i][j] * 0.587 + RGB[2][i][j] * 0.114);
  56. u[i][j] = /*(int)*/ (RGB[0][i][j] * -0.14713 + RGB[1][i][j] * -0.28886 + RGB[2][i][j] * 0.436);
  57. v[i][j] = /*(int)*/ (RGB[0][i][j] * 0.615 + RGB[1][i][j] * -0.51499 + RGB[2][i][j] * -0.10001);
  58.  
  59. //y[i][j] = Double.parseDouble( Utils.DF.format(y[i][j]) );
  60. //u[i][j] = Double.parseDouble( Utils.DF.format(u[i][j]) );
  61. //v[i][j] = Double.parseDouble( Utils.DF.format(v[i][j]) );
  62.  
  63. }
  64. }
  65. }
  66. if(y.length% Utils.BLOCK_SIZE != 0 || y[0].length%Utils.BLOCK_SIZE != 0) {
  67. this.newy = new double[y.length + (Utils.BLOCK_SIZE - (y.length%Utils.BLOCK_SIZE))]
  68. [y[0].length + (Utils.BLOCK_SIZE - (y[0].length%Utils.BLOCK_SIZE))];
  69. this.newu = new double[u.length + ( Utils.BLOCK_SIZE - (u.length%Utils.BLOCK_SIZE))]
  70. [u[0].length + (Utils.BLOCK_SIZE - (u[0].length%Utils.BLOCK_SIZE))];
  71. this.newv = new double[v.length + (Utils.BLOCK_SIZE - (v.length%Utils.BLOCK_SIZE))]
  72. [v[0].length + (Utils.BLOCK_SIZE- (v[0].length%Utils.BLOCK_SIZE))];
  73. for(int i = 0 ; i < newy.length ; i++){
  74. for (int j = 0; j < newy[i].length; j++) {
  75. if(j > y[i].length || i > y.length) {
  76. this.newy[i][j] = 0;
  77. this.newu[i][j] = 0;
  78. this.newv[i][j] = 0;
  79. }else {
  80. this.newy[i][j] = y[i][j];
  81. this.newu[i][j] = u[i][j];
  82. this.newv[i][j] = v[i][j];
  83. }
  84. }
  85. }
  86. out.add(this.newy);
  87. out.add(this.newu);
  88. out.add(this.newv);
  89. }else {
  90. out.add(y);
  91. out.add(u);
  92. out.add(v);
  93. }
  94. return out;
  95. }
  96. /**Methode qui décode les y u v et les mets en rgb
  97. * * Complexiter : O(n^2)
  98. * @param list
  99. * @param output
  100. * @return
  101. */
  102. public static void decode(ArrayList<double[][]> list , String output) {
  103. double y[][] = list.get(0);
  104. double u[][] = list.get(1);
  105. double v[][] = list.get(2);
  106. int RGB[][][] = new int[3][y.length][y[0].length];
  107. for( int i = 0 ; i < RGB.length ; i++) {
  108. for(int j = 0 ; j < RGB[i].length ; j+= 3) {
  109. RGB[0][i][j] = (int) (y[i][j] + 1.140 * v[i][j]);
  110. RGB[1][i][j] = (int) (y[i][j] - 0.395 * u[i][j] - 0.581 * v[i][j]);
  111. RGB[2][i][j] = (int) (y[i][j] + 2.032 * u[i][j]);
  112. }
  113. }
  114. PPMReaderWriter.writePPMFile(output, RGB);
  115.  
  116. }
  117.  
  118.  
  119. /** Getters & Setters*/
  120. public double[][] getY() {
  121. if(this.newy != null) {
  122. return newy;
  123. }
  124. return y;
  125. }
  126.  
  127. public double[][] getU() {
  128. if(this.newu != null) {
  129. return newu;
  130. }
  131. return u;
  132. }
  133.  
  134. public double[][] getV() {
  135. if(this.newv != null) {
  136. return newv;
  137. }
  138. return v;
  139. }
  140. public int getWidth() {
  141. return width;
  142. }
  143.  
  144. public void setWidth(int width) {
  145. this.width = width;
  146. }
  147.  
  148. public int getHeigth() {
  149. return heigth;
  150. }
  151.  
  152. public void setHeigth(int heigth) {
  153. this.heigth = heigth;
  154. }
  155.  
  156. public int getColor() {
  157. return color;
  158. }
  159.  
  160. public void setColor(int color) {
  161. this.color = color;
  162. }
  163.  
  164.  
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement