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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
// Applique un filtre sur des images PPM P3
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int net[3][3]={
{0, -1, 0},
{-1, 5, -1},
{0, -1, 0}
};
int coef[3][3]={
{0, 1, 0},
{1, -4, 1},
{0, 1, 0}
};
int id[3][3]={
{0, 0, 0},
{0, 1, 0},
{0, 0, 0},
};
// paramètre le filtre utilisé par l'algo
int (*filtre)[3][3] = &net;
int **ri, **gi, **bi; // canaux rgb des images d'entrée/sortie
int **ro, **go, **bo;
int HEIGHT, WIDTH;
int MAXVAL;
FILE *in, *out;
// print po
void ppo(){
printf("Rouge: \n");
for(int h=0; h<HEIGHT;h++){
for(int w=0; w<WIDTH; ++w)
printf("%4d", ro[h][w]);
printf("\n");
}
printf("Vert: \n");
for(int h=0; h<HEIGHT;h++){
for(int w=0; w<WIDTH; ++w)
printf("%4d", go[h][w]);
printf("\n");
}
printf("Bleu: \n");
for(int h=0; h<HEIGHT;h++){
for(int w=0; w<WIDTH; ++w)
printf("%4d", bo[h][w] );
printf("\n");
}
}
// print pi
void ppi(){
printf("Rouge: \n");
for(int h=0; h<HEIGHT+2;h++){
for(int w=0; w<WIDTH+2; ++w)
printf("%4d", ri[h][w]);
printf("\n");
}
printf("Vert: \n");
for(int h=0; h<HEIGHT+2;h++){
for(int w=0; w<WIDTH+2; ++w)
printf("%4d", gi[h][w]);
printf("\n");
}
printf("Bleu: \n");
for(int h=0; h<HEIGHT+2;h++){
for(int w=0; w<WIDTH+2; ++w)
printf("%4d", bi[h][w] );
printf("\n");
}
}
void fillpi(){
int step = 0; // 0 lire P3, 1 lire la taille
int r = 0; // test des retours
char ligne[BUFSIZ];
while( step <3 && fgets(ligne, BUFSIZ, in) != NULL){
#ifdef DEBUG
printf("lecture : %s", ligne);
#endif
// gestion des lignes commentés
if( '#' == ligne[0])
continue;
switch(step){
case 0:
// Si la première ligne non commenté est P3, chercher tailles
if( 0 == strcmp(ligne, "P3\n") ){
#ifdef DEBUG
printf("Ceci est une Image\n");
#endif
++step;
break;
}
// On ne gère que P3
else{
#ifdef DEBUG
fprintf(stderr, "Seul des fichiers PPM P3 sont gérés\n");
#endif
exit(EXIT_FAILURE);
}
case 1:
// Récupère la hauteur et la largeur
if(2 == sscanf(ligne, "%d %d", &HEIGHT, &WIDTH) ){
#ifdef DEBUG
printf("Hauteur %d ok\n", HEIGHT);
printf("largeur %d ok\n", WIDTH);
#endif
++step;
break;
}
// Note, Si on a encore des données sur la ligne, fuck off !
else
break;
case 2:
// Récupère la hauteur et la largeur
if(1 == sscanf(ligne, "%d", &MAXVAL ) ){
#ifdef DEBUG
printf("MAXVAL %d ok\n", MAXVAL);
#endif
++step;
break;
}
// Note, Si on a encore des données sur la ligne, fuck off !
else
break;
default:
if(step<2){
fprintf(stderr, "Problème lors de la lecture de l'entête\n");
exit(EXIT_FAILURE);
}
}
}
// À partir d'ici, on ne gère plus les commentaires
#ifdef DEBUG
printf("Lecture entête ok\n");
#endif
// Allocation mémoire
ri = malloc((HEIGHT+2) * sizeof(*ri));
gi = malloc((HEIGHT+2) * sizeof(*gi));
bi = malloc((HEIGHT+2) * sizeof(*bi));
int i;
//#pragma omp parallel for shared(ri, gi, bi) private(i)
for(i=0 ; i < HEIGHT+2 ; i++){
ri[i] = malloc((WIDTH+2) * sizeof(**ri) );
gi[i] = malloc((WIDTH+2) * sizeof(**gi) );
bi[i] = malloc((WIDTH+2) * sizeof(**bi) );
}
#ifdef DEBUG
printf("Alloc pi ok\n");
#endif
//remplie le centre de l'image d'entrée
for(int h=1; h<HEIGHT+1;++h)
for(int w=1; w<WIDTH+1; ++w)
r=fscanf(in, "%d %d %d",
&(ri[h][w]),
&(gi[h][w]),
&(bi[h][w]) );
//remplie les contours de l'image d'entrée
int h;
//#pragma omp parallel for shared(ri, gi, bi) private(h)
for(h=0; h<HEIGHT+2; ++h){
ri[h][0] = 0;
gi[h][0] = 0;
bi[h][0] = 0;
ri[h][WIDTH+1] = 0;
gi[h][WIDTH+1] = 0;
bi[h][WIDTH+1] = 0;
}
int w;
//#pragma omp parallel for shared(ri, gi, bi) private(w)
for(int w=1; w<WIDTH+1; ++w){
ri[0][w] = 0;
gi[0][w] = 0;
bi[0][w] = 0;
ri[HEIGHT+1][w] = 0;
gi[HEIGHT+1][w] = 0;
bi[HEIGHT+1][w] = 0;
}
#ifdef DEBUG
ppi();
#endif
}
// Imprime une partie spécifiée d'une matrice
void pmat(int **m,int bh, int eh, int bw, int ew){
printf("%d %d %d %d\n", bh, eh, bw, ew);
for(int i=bh; i < eh; ++i){
for(int j=bw; i < ew; ++j)
printf("%d ", m[i][j]);
printf("\n");
}
printf("\n");
}
// Calcul de l'image de sortie
void fillpo(){
int r; // test des retours
ro = malloc(HEIGHT * sizeof(*ro));
go = malloc(HEIGHT * sizeof(*go));
bo = malloc(HEIGHT * sizeof(*bo));
#ifdef DEBUG
// certes on ne test pas mais bon
printf("Alloc po ok\n");
#endif
int h,w,i,j; // index pour parcourir les matrices
//#pragma omp parallel for shared(ro, go, bo) private(i)
for(i=0 ; i < HEIGHT ; i++){
ro[i] = malloc(WIDTH * sizeof(**ro) );
go[i] = malloc(WIDTH * sizeof(**go) );
bo[i] = malloc(WIDTH * sizeof(**bo) );
}
#ifdef DEBUG
printf("Calcul de bo*:\n");
#endif
//crée l'image de sortie par application du filtre sur l'image d'entrée
//#pragma omp parallel for shared(ro, go, bo, ri, gi, bi, filtre) private(i,j,h,w)
//#pragma omp parallel for shared(ri, gi, bi, filtre) private(i,j,h,w)
#pragma omp parallel for private(i,j,h,w)
for(h = 0; h < HEIGHT; ++h){
for(w = 0; w < WIDTH; ++w){
ro[h][w] = 0;
go[h][w] = 0;
bo[h][w] = 0;
#ifdef DEBUG
printf("bo[%d][%d] =(", h, w);
#endif
for(i=0;i<3; ++i){
for(j=0; j<3; ++j){
#ifdef DEBUG
//if( (*filtre)[i][j]!=0 && bi[h+i][w+j] !=0)
printf(" %3d×%d ", bi[h+i][w+j],(*filtre)[i][j] );
#endif
ro[h][w] += (*filtre)[i][j] * ri[h+i][w+j];
go[h][w] += (*filtre)[i][j] * gi[h+i][w+j];
bo[h][w] += (*filtre)[i][j] * bi[h+i][w+j];
#ifdef DEBUG
//if( i!=2 && j!=2 && (*filtre)[i][j]!=0 && bi[h+i][w+j] !=0)
if( !(i==2 && j==2))
printf(" + ", i,j);
#endif
}
}
#ifdef DEBUG
printf(") = %3d\n", bo[h][w]);
#endif
// Normalisation des valeurs
if(ro[h][w] < 0)
ro[h][w] = 0;
else if(ro[h][w] > MAXVAL)
ro[h][w] = MAXVAL;
if(go[h][w] < 0)
go[h][w] = 0;
else if(go[h][w] > MAXVAL)
go[h][w] = MAXVAL;
if(bo[h][w] < 0)
bo[h][w] = 0;
else if(bo[h][w] > MAXVAL)
bo[h][w] = MAXVAL;
}
#ifdef DEBUG
printf("\n\n");
#endif
}
#ifdef DEBUG
ppo();
#endif
//libération mémoire
//#pragma omp parallel for shared(ri, gi, bi) private(i)
for(i=0 ; i < HEIGHT+2 ; i++){
free(ri[i]);
free(gi[i]);
free(bi[i]);
}
free(ri);
free(gi);
free(bi);
}
void savepo(){
fprintf(out, "P3\n");
fprintf(out, "%d %d\n", HEIGHT, WIDTH);
fprintf(out, "%d\n", MAXVAL);
for(int h=0; h<HEIGHT;h++){
for(int w=0; w<WIDTH; ++w)
fprintf(out, "%d\n%d\n%d\n",
ro[h][w],
go[h][w],
bo[h][w] );
}
//libération mémoire
int i;
//#pragma omp parallel for shared(ro, go, bo) private(i)
for(i=0 ; i < HEIGHT ; i++){
free(ro[i]);
free(go[i]);
free(bo[i]);
}
free(ro);
free(go);
free(bo);
}
int main(int argc, char** argv){
if(argc != 3){
fprintf(stderr, "%s : usage : %s entrée.ppm sortie.ppm\n", *argv, *argv);
exit(EXIT_FAILURE);
}
// Lecture de l'image d'entrée
in=fopen(argv[1], "r");
if(in==NULL){
perror("impossible d'ouvrir le fichier d'entrée\n");
exit(EXIT_FAILURE);
}
fillpi();
fclose(in);
// Calcul de l'image de sortie
fillpo();
//Enregistre le résultat du filtrage
out=fopen(argv[2], "w");
if(out==NULL){
perror("impossible d'ouvrir le fichier d'entrée\n");
exit(EXIT_FAILURE);
}
savepo();
fclose(out);
} |
Partager