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
|
#include "process.h"
Process::Process()
{
MPI_Comm_rank(MPI_COMM_WORLD,&identity);
MPI_Comm_size(MPI_COMM_WORLD,&nb_instance);
}
void Process::call() const
{
if(identity == 0)
controlProcess();
else
computeProcess();
}
void Process::controlProcess() const
{
char menu_a = 'z'; // Variable du menu principal: ouvrir un image ou quitter
while(menu_a != '2') // Tant que l'utilisateur ne quitte pas dans le menu principal
{
cout << endl;
menu_a = 'z';
unsigned short try_count = 0; // Variable du nombre d'erreurs de l'utilisateur pour le coix du menu
while((menu_a != '1') && (menu_a != '2')) // Tant que le choix n'est pas valide
{
if(try_count > 0) // Si ce n'est pas le permier essai
cout << "Processus de controle >> Option non valide !" << endl;
cout << "Processus de controle >> Choisissez une option: (1) Ouvrir une image, (2) Quitter." << endl
<< "Utilisateur >> ";
cin >> menu_a;
try_count++;
}
if(menu_a == '1') //Si choix = ouvrir une image
{
// Envoi d'un message général que du travail va arriver (au lieu de quitter)
unsigned short exit_process = 0;
MPI_Bcast(&exit_process,1,MPI_UNSIGNED_SHORT,0,MPI_COMM_WORLD);
// Chargement d'une image à partir d'un fichier
ImgMatrix image;
try_count = 0;
bool image_charged = false;
while(!image_charged) // Tant que l'image choisie par l'utilisateur ne peut être chargée correctement
{
if(try_count > 0) // Affichage de l'erreur de chargement
{
cout << "Processus de controle >> Erreur de chargement: ";
unsigned short load_error = image.getLastError();
switch(load_error)
{
case ERROR_NO_FILE: cout << "Le fichier spécifié n'existe pas." << endl; break;
case ERROR_DIB: cout << "Le fichier n'est pas au format DIB." << endl; break;
case ERROR_24BITS: cout << "Le fichier BMP n'est pas au format 24 bits/pxl." << endl; break;
case ERROR_INVERT: cout << "L'encodage du fichier est inversé." << endl; break;
default: cout << "Erreur inconnue..." << endl;
}
}
cout << "Processus de controle >> Entrez le nom du fichier" << endl
<< "Utilisateur >> ";
char load_file[100];
cin >> load_file;
image_charged = image.loadFromBMP24(load_file);
try_count++;
}
// Découpe de l'image en quatres fragments
unsigned char* part1 = NULL;
unsigned char* part2 = NULL;
unsigned char* part3 = NULL;
unsigned char* part4 = NULL;
unsigned int lenght1 = 0;
unsigned int lenght2 = 0;
unsigned int lenght3 = 0;
unsigned int lenght4 = 0;
image.exportVectors(part1,lenght1,part2,lenght2,part3,lenght3,part4,lenght4);
// Envoi des longeurs des vecteurs qui vont arriver aux 4 processus de calcul
MPI_Send(&lenght1,1,MPI_UNSIGNED,1,1,MPI_COMM_WORLD);
MPI_Send(&lenght2,1,MPI_UNSIGNED,2,1,MPI_COMM_WORLD);
MPI_Send(&lenght3,1,MPI_UNSIGNED,3,1,MPI_COMM_WORLD);
MPI_Send(&lenght4,1,MPI_UNSIGNED,4,1,MPI_COMM_WORLD);
// Envoi des fragments d'image aux 4 processus
MPI_Send(part1,lenght1,MPI_UNSIGNED_CHAR,1,1,MPI_COMM_WORLD);
MPI_Send(part2,lenght2,MPI_UNSIGNED_CHAR,2,1,MPI_COMM_WORLD);
MPI_Send(part3,lenght3,MPI_UNSIGNED_CHAR,3,1,MPI_COMM_WORLD);
MPI_Send(part4,lenght4,MPI_UNSIGNED_CHAR,4,1,MPI_COMM_WORLD);
bool modified = false; // Vérifie si des modification seront faites sur l'image
bool convolution_used = false; // Vérifie si la convolution sera utilisée sur l'image
char menu_b = 'z'; // Variable du menu de traitement d'image
while(menu_b != '7') //Tant l'utilisateur ne quitte pas le menu d'effets
{
menu_b = 'z';
try_count = 0;
while((menu_b < '1') || (menu_b > '7')) //Tant que l'option est non valide
{
if(try_count > 0)
cout << "Processus de controle >> Option non valide !" << endl;
cout << "Processus de controle >> Choisissez une option:" << endl
<< "Processus de controle >> (1) Filtre noir et blanc" << endl
<< "Processus de controle >> (2) Filtre Sépia" << endl
<< "Processus de controle >> (3) Ajouter un effet de flou" << endl
<< "Processus de controle >> (4) Augmenter la netteté" << endl
<< "Processus de controle >> (5) Détecter les contours" << endl
<< "Processus de controle >> (6) Ajouter un effet destampage" << endl
<< "Processus de controle >> (7) Quitter" << endl
<< "Utilisateur >> ";
cin >> menu_b;
try_count++;
}
// Traduction du menu
unsigned short transformation;
switch(menu_b)
{
case '1': transformation = TRANSFORM_BLACK_WHITE; break;
case '2': transformation = TRANSFORM_SEPIA; break;
case '3': transformation = TRANSFORM_BLUR; break;
case '4': transformation = TRANSFORM_SHARPNESS; break;
case '5': transformation = TRANSFORM_CONTOURS; break;
case '6': transformation = TRANSFORM_STAMPING; break;
}
// S'il s'agit d'une convolution sur une convolution l'image doit être rapatriée et renvoyée
if(transformation == TRANSFORM_BLUR || transformation == TRANSFORM_SHARPNESS
|| transformation == TRANSFORM_CONTOURS || transformation == TRANSFORM_STAMPING)
{
if(convolution_used) // Si la convolution a déjà été utilisé sur les fragments envoyés
{
// Rappelle les fragments d'images
unsigned short message = 0;
MPI_Bcast(&message,1,MPI_UNSIGNED_SHORT,0,MPI_COMM_WORLD);
// Recoit les fragments d'image
MPI_Status status;
MPI_Recv(part1,lenght1,MPI_UNSIGNED_CHAR,1,1,MPI_COMM_WORLD,&status);
MPI_Recv(part2,lenght2,MPI_UNSIGNED_CHAR,2,1,MPI_COMM_WORLD,&status);
MPI_Recv(part3,lenght3,MPI_UNSIGNED_CHAR,3,1,MPI_COMM_WORLD,&status);
MPI_Recv(part4,lenght4,MPI_UNSIGNED_CHAR,4,1,MPI_COMM_WORLD,&status);
// Assemble les fragments d'image
image.importVectors(part1,lenght1,part2,lenght2,part3,lenght3,part4,lenght4);
// Informe du nouveau travail
unsigned short exit_process = 0;
MPI_Bcast(&exit_process,1,MPI_UNSIGNED_SHORT,0,MPI_COMM_WORLD);
// Fragmente l'image
image.exportVectors(part1,lenght1,part2,lenght2,part3,lenght3,part4,lenght4);
// Envoi des longeurs des vecteurs qui vont arriver aux 4 processus de calcul
MPI_Send(&lenght1,1,MPI_UNSIGNED,1,1,MPI_COMM_WORLD);
MPI_Send(&lenght2,1,MPI_UNSIGNED,2,1,MPI_COMM_WORLD);
MPI_Send(&lenght3,1,MPI_UNSIGNED,3,1,MPI_COMM_WORLD);
MPI_Send(&lenght4,1,MPI_UNSIGNED,4,1,MPI_COMM_WORLD);
// Envoi des fragments d'image aux 4 processus
MPI_Send(part1,lenght1,MPI_UNSIGNED_CHAR,1,1,MPI_COMM_WORLD);
MPI_Send(part2,lenght2,MPI_UNSIGNED_CHAR,2,1,MPI_COMM_WORLD);
MPI_Send(part3,lenght3,MPI_UNSIGNED_CHAR,3,1,MPI_COMM_WORLD);
MPI_Send(part4,lenght4,MPI_UNSIGNED_CHAR,4,1,MPI_COMM_WORLD);
convolution_used = false;
}
else
convolution_used = true;
}
// Envoi de l'effet à appliquer sauf si on quitte
if(menu_b != '7')
{
MPI_Bcast(&transformation,1,MPI_UNSIGNED_SHORT,0,MPI_COMM_WORLD);
modified = true; // Le fichier a été modifié
// Attend ping de reponse des processus
unsigned short message;
MPI_Status status;
MPI_Recv(&message,1,MPI_UNSIGNED_SHORT,1,1,MPI_COMM_WORLD,&status);
MPI_Recv(&message,1,MPI_UNSIGNED_SHORT,2,1,MPI_COMM_WORLD,&status);
MPI_Recv(&message,1,MPI_UNSIGNED_SHORT,3,1,MPI_COMM_WORLD,&status);
MPI_Recv(&message,1,MPI_UNSIGNED_SHORT,4,1,MPI_COMM_WORLD,&status);
// Confirmation
cout << "Processus de controle >> ";
switch(transformation)
{
case TRANSFORM_BLACK_WHITE: cout << "Filtre noir et blanc appliqué. "; break;
case TRANSFORM_SEPIA: cout << "Filtre Sépia appliqué. "; break;
case TRANSFORM_BLUR: cout << "Effet de flou ajouté. "; break;
case TRANSFORM_SHARPNESS: cout << "Netteté augmentée. "; break;
case TRANSFORM_CONTOURS: cout << "Contours détectés. "; break;
case TRANSFORM_STAMPING: cout << "Effet d'estampage ajouté. "; break;
}
cout << "Appliquer un autre filtre ? (o/n)" << endl;
char menu_c = 'z'; // Variable de menu oui/non
while(menu_c != 'n' && menu_c != 'o')
{
cout << "Utilisateur >> ";
cin >> menu_c;
}
if(menu_c == 'n')
menu_b = '7'; //Quitter
}
} // Quitte le panel d'effets
if(modified)
{
// Rapatrier et recoller
MPI_Status status;
unsigned short message = 0;
MPI_Bcast(&message,1,MPI_UNSIGNED_SHORT,0,MPI_COMM_WORLD);
MPI_Recv(part1,lenght1,MPI_UNSIGNED_CHAR,1,1,MPI_COMM_WORLD,&status);
MPI_Recv(part2,lenght2,MPI_UNSIGNED_CHAR,2,1,MPI_COMM_WORLD,&status);
MPI_Recv(part3,lenght3,MPI_UNSIGNED_CHAR,3,1,MPI_COMM_WORLD,&status);
MPI_Recv(part4,lenght4,MPI_UNSIGNED_CHAR,4,1,MPI_COMM_WORLD,&status);
image.importVectors(part1,lenght1,part2,lenght2,part3,lenght3,part4,lenght4);
// Enregistrement
cout << "Processus de controle >> Entrez le nom du fichier dans lequel l'image sera sauvegardée." << endl
<< "Utilisateur >> ";
char save_file[100];
cin >> save_file;
image.saveAsBMP24(save_file);
}
// Vidange des pointeurs
delete [] part1;
delete [] part2;
delete [] part3;
delete [] part4;
} // Fin du choix ouvrir une image
} // Sortie du menu principal
// Envoi d'un message général que du travail est terminé
unsigned short exit_process = 1;
MPI_Bcast(&exit_process,1,MPI_UNSIGNED_SHORT,0,MPI_COMM_WORLD);
cout << "Processus de controle >> Ciao..." << endl << endl;
}
void Process::computeProcess() const
{
// Test si le processus va avoir du travail
unsigned short exit_process;
MPI_Bcast(&exit_process,1,MPI_UNSIGNED_SHORT,0,MPI_COMM_WORLD);
bool wait_images = (exit_process == 0);
while(wait_images)
{
unsigned char* part = NULL; // Buffer
unsigned int lenght = 0; // Taille du buffer
MPI_Status status;
// En attente d'une longueur de vecteur
MPI_Recv(&lenght,1,MPI_UNSIGNED,0,1,MPI_COMM_WORLD,&status);
//Initialise le buffer de réception et réceptionne
part = new unsigned char[lenght];
MPI_Recv(part,lenght,MPI_UNSIGNED_CHAR,0,1,MPI_COMM_WORLD,&status);
ImgMatrix image;
image.importVector(part,lenght);
// En attente d'une transformation
unsigned short transformation = 0;
do
{
MPI_Bcast(&transformation,1,MPI_UNSIGNED_SHORT,0,MPI_COMM_WORLD);
if(transformation > 0)
{
// Applique la transformation
image.transform(transformation);
// Envoie la confirmation
unsigned short message = 0;
MPI_Send(&message,1,MPI_UNSIGNED_SHORT,0,1,MPI_COMM_WORLD);
}
} while(transformation > 0);
// Exportation de l'image
image.exportVector(part,lenght);
MPI_Send(part,lenght,MPI_UNSIGNED_CHAR,0,1,MPI_COMM_WORLD);
// Vidange du vecteur
delete [] part;
part = NULL;
// Demande s'il y a encore du travail
MPI_Bcast(&exit_process,1,MPI_UNSIGNED_SHORT,0,MPI_COMM_WORLD);
wait_images = (exit_process == 0);
}
} |
Partager