-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImg.java
More file actions
150 lines (126 loc) · 3.96 KB
/
Copy pathImg.java
File metadata and controls
150 lines (126 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import java.util.*;
import java.io.*;
class Img {
private int[][] data; //data[i][j] représente pixel(i,j) avec i numero colonne et j num ligne avec ligne 0 en haut, et colonne 0 à gauche
private int hauteur;
private int largeur;
/**
*
* @param nomFichier : adresse d'un fichier pgm, ex "fichier.pgm"
*/
public Img(String nomFichier){
try {
Pixmap pm = new Pixmap(nomFichier);
data = new int[pm.getW()][pm.getH()];
for (int i = 0; i < pm.getW(); i++) {
for (int j = 0; j < pm.getH(); j++) {
data[i][j] = pm.get(i, j);
}
}
setHL();
}catch(IOException e) {
System.out.println(e.getMessage());
}
}
public Img(int[][] tab){
data = new int[tab.length][tab[0].length];
for(int i=0;i<tab.length;i++){
for(int j=0; j<tab[0].length;j++){
data[i][j] = tab[i][j];
}
}
setHL();
}
private void setHL(){
hauteur = data[0].length;
largeur = data.length;
}
public int[][] getData(){
return data;
}
public int get(int i, int j){
return data[i][j];
}
//utilisé dans InstanceDebruitage où le pixel (i,j) d'une Img correspondant au sommet numéro "calculIndice(i,j)" du graphe de l'instanceDebruitage associée à l'Img
public int calculIndice(int i, int j){
return largeur*j+i;
}
public Couple<Integer,Integer> calculCoord(int x){
int j = x/largeur;
int i = x%largeur;
Couple<Integer,Integer> res = new Couple<Integer,Integer>(i,j);
return res;
}
public int getPixel(int i, int j){
return data[i][j];
}
public String toString(){
String str = "";
for (int j = 0; j < nbLignes(); j++) {
for (int i = 0; i < nbColonnes(); i++) {
str = str + "\t" + data[i][j];
}
str = str + "\n";
}
return str;
}
public int nbColonnes(){
return largeur;
}
public int nbLignes(){
return hauteur;
}
public void bruiter(int n){
for (int x=0;x<n;x++){
Random r = new Random();
int i = r.nextInt(data.length);
int j = r.nextInt(data[0].length);
data[i][j] = 255-data[i][j];
}
}
/**
*
* @param solution : liste des indices des pixels (le pixel (i,j) à l'indice "calculIndice(i,j)") à mettre en blanc
* @return les coordonnées de ces pixels
*/
public ArrayList<Couple<Integer,Integer>> calculFiltre(ArrayList<Integer> solutionBlancs)
{
ArrayList<Couple<Integer,Integer>> res = new ArrayList<Couple<Integer,Integer>>();
for (Integer x : solutionBlancs){
res.add(calculCoord(x));
}
return res;
}
/**
param : liste des coordonnées de pixels à mettre en blanc
*/
public Img appliquerFiltre(ArrayList<Couple<Integer,Integer>> solutionBlancs) {
Img nouvelleImage = new Img(data);
for(int i = 0; i<this.nbColonnes();i++){
for(int j = 0;j<this.nbLignes();j++){
Couple<Integer,Integer> indice = new Couple<Integer,Integer>(i,j);
if(solutionBlancs.contains(indice)){
nouvelleImage.data[i][j] = 255;
}
else{
nouvelleImage.data[i][j] = 0;
}
}
}
return nouvelleImage;
}
/**
* Convertie l'image en un nouveau ficher pgm
* @param nomFile : nom du nouveau ficher
*/
public void creerImage(String nomFile) {
Pixmap pm = new Pixmap(this.nbColonnes(), this.nbLignes());
pm.setData();
for (int i = 0; i < nbColonnes(); i++) {
for (int j = 0; j < nbLignes(); j++) {
pm.set(i, j, data[i][j]);
}
}
pm.write(nomFile);
}
}