Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gti310.tp4;
- import java.io.BufferedWriter;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStreamWriter;
- import java.util.ArrayList;
- import com.sun.xml.internal.ws.model.SOAPSEIModel;
- ;
- /**
- * La Classe YUVEncoder permet d'encoder une imade
- * RGB en YUV
- * @author julien labrosse
- *
- */
- public class YUV{
- //Les 3 tableau Y,U,V
- private double[][] y = null;
- private double[][] u = null;
- private double[][] v = null;
- private double[][] newy = null;
- private double[][] newu = null;
- private double[][] newv = null;
- private int width;
- private int heigth;
- private int color;
- /**
- * Methode qui effectue l'encodage en YUV à partir
- * d'un fichier RGB
- * @param input
- * @return T double
- * Complexiter : O(n^2)
- */
- public ArrayList<double[][]> encode(String input) {
- ArrayList<double[][]> out = new ArrayList<double[][]>();
- int RGB[][][] = PPMReaderWriter.readPPMFile(input);
- this.width = RGB[0][0].length;
- this.heigth = RGB[0].length;
- this.color = 255;
- if(this.width != 0) {
- y = new double[this.width][this.heigth];
- u = new double[this.width][this.heigth];
- v = new double[this.width][this.heigth];
- for(int i = 0 ; i < this.heigth; i++) {
- for(int j = 0 ; j < this.width; j++) {
- y[i][j] = /*(int)*/ (RGB[0][i][j] * 0.229 + RGB[1][i][j] * 0.587 + RGB[2][i][j] * 0.114);
- u[i][j] = /*(int)*/ (RGB[0][i][j] * -0.14713 + RGB[1][i][j] * -0.28886 + RGB[2][i][j] * 0.436);
- v[i][j] = /*(int)*/ (RGB[0][i][j] * 0.615 + RGB[1][i][j] * -0.51499 + RGB[2][i][j] * -0.10001);
- //y[i][j] = Double.parseDouble( Utils.DF.format(y[i][j]) );
- //u[i][j] = Double.parseDouble( Utils.DF.format(u[i][j]) );
- //v[i][j] = Double.parseDouble( Utils.DF.format(v[i][j]) );
- }
- }
- }
- if(y.length% Utils.BLOCK_SIZE != 0 || y[0].length%Utils.BLOCK_SIZE != 0) {
- this.newy = new double[y.length + (Utils.BLOCK_SIZE - (y.length%Utils.BLOCK_SIZE))]
- [y[0].length + (Utils.BLOCK_SIZE - (y[0].length%Utils.BLOCK_SIZE))];
- this.newu = new double[u.length + ( Utils.BLOCK_SIZE - (u.length%Utils.BLOCK_SIZE))]
- [u[0].length + (Utils.BLOCK_SIZE - (u[0].length%Utils.BLOCK_SIZE))];
- this.newv = new double[v.length + (Utils.BLOCK_SIZE - (v.length%Utils.BLOCK_SIZE))]
- [v[0].length + (Utils.BLOCK_SIZE- (v[0].length%Utils.BLOCK_SIZE))];
- for(int i = 0 ; i < newy.length ; i++){
- for (int j = 0; j < newy[i].length; j++) {
- if(j > y[i].length || i > y.length) {
- this.newy[i][j] = 0;
- this.newu[i][j] = 0;
- this.newv[i][j] = 0;
- }else {
- this.newy[i][j] = y[i][j];
- this.newu[i][j] = u[i][j];
- this.newv[i][j] = v[i][j];
- }
- }
- }
- out.add(this.newy);
- out.add(this.newu);
- out.add(this.newv);
- }else {
- out.add(y);
- out.add(u);
- out.add(v);
- }
- return out;
- }
- /**Methode qui décode les y u v et les mets en rgb
- * * Complexiter : O(n^2)
- * @param list
- * @param output
- * @return
- */
- public static void decode(ArrayList<double[][]> list , String output) {
- double y[][] = list.get(0);
- double u[][] = list.get(1);
- double v[][] = list.get(2);
- int RGB[][][] = new int[3][y.length][y[0].length];
- for( int i = 0 ; i < RGB.length ; i++) {
- for(int j = 0 ; j < RGB[i].length ; j+= 3) {
- RGB[0][i][j] = (int) (y[i][j] + 1.140 * v[i][j]);
- RGB[1][i][j] = (int) (y[i][j] - 0.395 * u[i][j] - 0.581 * v[i][j]);
- RGB[2][i][j] = (int) (y[i][j] + 2.032 * u[i][j]);
- }
- }
- PPMReaderWriter.writePPMFile(output, RGB);
- }
- /** Getters & Setters*/
- public double[][] getY() {
- if(this.newy != null) {
- return newy;
- }
- return y;
- }
- public double[][] getU() {
- if(this.newu != null) {
- return newu;
- }
- return u;
- }
- public double[][] getV() {
- if(this.newv != null) {
- return newv;
- }
- return v;
- }
- public int getWidth() {
- return width;
- }
- public void setWidth(int width) {
- this.width = width;
- }
- public int getHeigth() {
- return heigth;
- }
- public void setHeigth(int heigth) {
- this.heigth = heigth;
- }
- public int getColor() {
- return color;
- }
- public void setColor(int color) {
- this.color = color;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement