Programmation Avancée en C


imageNB.c

00001 #include <stdio.h>
00002 
00003 #define L 6            // nombre de lignes
00004 #define C 5            // nombre de colonnes
00005 #define BLACK_CHAR '*' // caractère "pixel noir"
00006 #define WHITE_CHAR ' ' // caractère "pixel blanc"
00007 
00008 typedef enum { WHITE, BLACK } pixel;
00009 
00010 void miroirVertical(pixel picture[L][C], pixel res[L][C]) {
00011     size_t i,j;
00012     for (i=0; i<L; i++){
00013             for (j=0; j<C; j++) 
00014                 res[i][C-1-j] = picture[i][j];
00015     }
00016 }
00017 void miroirHorizontal(pixel picture[L][C], pixel res[L][C]) {
00018     size_t i,j;
00019     for (i=0; i<L; i++){
00020         for (j=0; j<C; j++) 
00021                 res[L-1-i][j] = picture[i][j];
00022     }
00023 }
00024 void rotationDroite(pixel picture[L][C], pixel res[C][L]) {
00025     size_t i,j;
00026     for (i=0; i<L; i++){
00027             for (j=0; j<C; j++) 
00028                 res[j][C-i] = picture[i][j];
00029     }
00030 }
00031 void rotationGauche(pixel picture[L][C], pixel res[C][L]) {
00032     size_t i,j;
00033     for (i=0; i<L; i++){
00034             for (j=0; j<C; j++) 
00035                 res[C-1-j][i] = picture[i][j];
00036     }
00037 }
00038 size_t compare(pixel picture1[L][C], pixel picture2[L][C]) {
00039     size_t res=0,i,j;
00040     for (i=0; i<L; i++){
00041             for (j=0; j<C; j++) 
00042                 if (picture1[i][j] != picture2[i][j]) res++;
00043     }
00044     return res;
00045 }
00046       /* Affiche une image de L lignes et C colonnes. */
00047 void printImage(pixel picture[L][C]) {
00048     size_t i,j;
00049     if (C > 0) printf("+%*c+\n",C,' ');
00050     for (i=0; i<L; i++){
00051         printf(" ");
00052         for (j=0; j<C; j++)
00053             printf("%c", (picture[i][j] == BLACK)?BLACK_CHAR:WHITE_CHAR);
00054         printf(" \n");
00055     }
00056     if (C > 0) printf("+%*c+\n",C,' ');
00057 
00058 }
00059     /* Affiche une image de C lignes et L colonnes. */
00060 void printImage2(pixel picture[C][L]) {
00061     size_t i,j;
00062     if (L > 0) printf("+%*c+\n",L,' ');
00063     for (i=0; i<C; i++){
00064         printf(" ");
00065         for (j=0; j<L; j++)
00066             printf("%c", (picture[i][j] == BLACK)?BLACK_CHAR:WHITE_CHAR);
00067         printf(" \n");
00068     }
00069     if (L > 0) printf("+%*c+\n",L,' ');
00070 
00071 }
00072 
00073 int main(){
00074     // le F
00075     pixel tab[L][C] = {{BLACK, BLACK, BLACK, BLACK, BLACK},
00076                        {BLACK, WHITE, WHITE, WHITE, WHITE},
00077                        {BLACK, BLACK, BLACK, BLACK, WHITE},
00078                        {BLACK, WHITE, WHITE, WHITE, WHITE},
00079                        {BLACK, WHITE, WHITE, WHITE, WHITE},
00080                        {BLACK, WHITE, WHITE, WHITE, WHITE}};
00081     pixel m_central[L][C], m_horizontal[L][C], r_droite[C][L], r_gauche[C][L];
00082     printf("Image initiale: \n");
00083     printImage(tab);
00084     printf("Miroir Vertical: \n");
00085     miroirVertical(tab, m_central)   ;
00086     printImage(m_central);
00087     printf("Miroir Horizontal: \n");
00088     miroirHorizontal(tab, m_horizontal)   ;
00089     printImage(m_horizontal);
00090     printf("Rotation à 90° a droite: \n");
00091     rotationDroite(tab, r_droite)   ;
00092     printImage2(r_droite);
00093     printf("Rotation à 90° a gauche: \n");
00094     rotationGauche(tab, r_gauche)   ;
00095     printImage2(r_gauche); 
00096     printf("Comparaison entre l'image initiale et elle-même: %lu pixel(s) diffère(nt)\n",
00097            compare(tab,tab));
00098     printf("Comparaison entre l'image initiale et son miroir central: %lu pixel(s) diffère(nt)\n",
00099            compare(tab,m_central));
00100     return 0;
00101 }
00102