rendered paste body#include <iostream>#include <fstream>#include "images_processing.h"using namespace std;// Définition d'une structure Pixel qui nous permet de stocker les valeurs R, G et B dans un seul tableaustruct Pixel { int R, G, B;};// Fonction qui crée un tableau height x width dont chaque élément est de type Pixelstruct Pixel ** create_new_array(int height, int width) { Pixel **array_bis = new Pixel *[height]; for(int i = 0; i < height; i++) { array_bis[i] = new Pixel [width]; } return array_bis;}int main(int n, char *params[]) { int width = 0, height = 0, i = 0, j = 0, R, G, B, max_color, choice; string tmp; // Définition d'une variable "tmp" (temporary) où stocker le résultat de la lecture inutile du fichier bool error = false; /* Initialisation du flux de lecture "i_file" de fichier "params[1]" contient le chemin du fichier à lire */ ifstream i_file(params[1]); // Vérification du succès de l'ouverture du fichier if(i_file) { // Lecture des 2 lignes inutiles du fichier (à modifier) for(i = 0; i < 2; i++) { getline(i_file, tmp); } // Lecture de la hauteur, de la largeur et de la valeur maximale de la couleur i_file >> width >> height >> max_color; // Création d'un pointeur de pointeur d'un élément de type Pixel Pixel **array = NULL; // Création du tableau width x height qui va contenir les couleurs des différents pixels array = create_new_array(height, width); // Lecture des différentes couleurs pixel par pixel et stockage dans le tableau array for(i = 0; i < height && !i_file.eof(); i++) { for(j = 0; j < width && !i_file.eof(); j++) { i_file >> R >> G >> B; array[i][j].R = R; array[i][j].G = G; array[i][j].B = B; } } cout << "Que souhaitez-vous faire ? :" << endl; cout << "1) Appliquer un flou" << endl; cout << "2) Accentuation des lignes horizontales" << endl; cout << "3) Accentuation des lignes verticales" << endl; cout << "4) Detection des bords" << endl; cout << "5) Filtre anti-bruit" << endl; cout << "6) Reduire la taille de l'image" << endl; cout << "-> "; cin >> choice; cout << "-----------------" << endl; float mat_convol[7][7]; for(i = 0; i < 7; i++) { for(j = 0; j < 7; j++) { mat_convol[i][j] = 0.0; } } switch(choice) { case 1: int blur_intensity; cout << "Quelle valeur de flou souhaitez-vous ? (1, 2, 3) :" << endl; cin >> blur_intensity; switch(blur_intensity) { case 1: mat_convol[2][2] = 1.0/16; mat_convol[2][3] = 2.0/16; mat_convol[2][4] = 1.0/16; mat_convol[3][2] = 2.0/16; mat_convol[3][3] = 4.0/16; mat_convol[3][4] = 2.0/16; mat_convol[4][2] = 1.0/16; mat_convol[4][3] = 2.0/16; mat_convol[4][4] = 1.0/16; cout << "Veuillez patienter pendant l'execution du filtre..." << endl; array = apply_linear_filter(array, mat_convol, width, height); break; case 2: mat_convol[1][1] = 1.0/75; mat_convol[1][2] = 2.0/75; mat_convol[1][3] = 3.0/75; mat_convol[1][4] = 2.0/75; mat_convol[1][5] = 1.0/75; mat_convol[2][1] = 2.0/75; mat_convol[2][2] = 4.0/75; mat_convol[2][3] = 5.0/75; mat_convol[2][4] = 4.0/75; mat_convol[2][5] = 2.0/75; mat_convol[3][1] = 3.0/75; mat_convol[3][2] = 5.0/75; mat_convol[3][3] = 7.0/75; mat_convol[3][4] = 5.0/75; mat_convol[3][5] = 3.0/75; mat_convol[4][1] = 2.0/75; mat_convol[4][2] = 4.0/75; mat_convol[4][3] = 5.0/75; mat_convol[4][4] = 4.0/75; mat_convol[4][5] = 2.0/75; mat_convol[5][1] = 1.0/75; mat_convol[5][2] = 2.0/75; mat_convol[5][3] = 3.0/75; mat_convol[5][4] = 2.0/75; mat_convol[5][5] = 1.0/75; cout << "Veuillez patienter pendant l'execution du filtre..." << endl; array = apply_linear_filter(array, mat_convol, width, height); break; default: cerr << "Ce niveau de flou n'est pas proposé" << endl; error = true; break; } break; case 2: mat_convol[2][2] = -1.0; mat_convol[2][3] = -1.0; mat_convol[2][4] = -1.0; mat_convol[3][2] = 0.0; mat_convol[3][3] = 0.0; mat_convol[3][4] = 0.0; mat_convol[4][2] = 1.0; mat_convol[4][3] = 1.0; mat_convol[4][4] = 1.0; cout << "Veuillez patienter pendant l'execution du filtre..." << endl; array = apply_linear_filter(array, mat_convol, width, height); break; case 3: mat_convol[2][2] = -1.0; mat_convol[2][3] = 0.0; mat_convol[2][4] = 1.0; mat_convol[3][2] = -1.0; mat_convol[3][3] = 0.0; mat_convol[3][4] = 1.0; mat_convol[4][2] = -1.0; mat_convol[4][3] = 0.0; mat_convol[4][4] = 1.0; cout << "Veuillez patienter pendant l'execution du filtre..." << endl; array = apply_linear_filter(array, mat_convol, width, height); break; case 4: mat_convol[2][2] = 0.0; mat_convol[2][3] = 1.0; mat_convol[2][4] = 0.0; mat_convol[3][2] = 1.0; mat_convol[3][3] = -4.0; mat_convol[3][4] = 1.0; mat_convol[4][2] = 0.0; mat_convol[4][3] = 1.0; mat_convol[4][4] = 0.0; cout << "Veuillez patienter pendant l'execution du filtre..." << endl; array = apply_linear_filter(array, mat_convol, width, height); break; case 5: cout << "Veuillez patienter pendant l'execution du filtre..." << endl; array = apply_non_linear_filter(array, width, height); break; case 6: float percent; cout << "Quelle pourcentage de reduction souhaitez-vous ?" << endl << "-> "; cin >> percent; cout << "Veuillez patienter pendant l'execution du filtre..." << endl; percent /= 100.0; array = shrink_image(array, width, height, percent); break; default: cerr << "Ce choix n'est pas possible !" << endl; error = true; break; } if(!error) { /* Initialisation du flux d'écriture "o_file" ios::out pour indiquer qu'on ouvre le fichier en écriture ios::trunc pour indiquer que si le fichier existe déjà on l'écrase */ ofstream o_file("panda_2.ppm", ios::out | ios::trunc); // Vérification du succès de la création du fichier if(o_file) { // Ecriture dans le fichier des premières lignes o_file << "P3" << endl; o_file << "# File modified by ixM` && lou" << endl; o_file << width << " " << height << endl; o_file << max_color << endl; // Ecriture dans le fichier des valeurs des nouveaux pixels for(i = 0; i < height; i++) { for(j = 0; j < width; j++) { o_file << array[i][j].R << endl; o_file << array[i][j].G << endl; o_file << array[i][j].B << endl; } } // Fermeture du fichier o_file.close(); } else { cerr << "Error while creating output file" << endl; } } else { cerr << "Erreur lors de l'application du filtre" << endl; } for(i = 0; i < height; i++) { delete[] array[i]; } delete[] array; } else { cerr << "Error while reading source file" << endl; } return 0;}