Advertisement
Guest User

Untitled

a guest
Jan 4th, 2018
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 22.18 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "../LIBS/pile.h"
  5.  
  6. // A. G.
  7. //
  8. // 2017/2018
  9.  
  10. typedef struct IMAGE_NB_E{
  11.     char *  nom;
  12.     int     nbligne;
  13.     int     nbcolonne;
  14.     unsigned char ** matrice;
  15. }IMAGE_NB;
  16.  
  17.  
  18. typedef struct IMAGE_RGB_E{
  19.     char *  nom;
  20.     int     nbligne;
  21.     int     nbcolonne;
  22.     unsigned char ** matrice_R;
  23.     unsigned char ** matrice_G;
  24.     unsigned char ** matrice_B;
  25.     unsigned int  ** matrice_Enc;
  26. }IMAGE_RGB;
  27.  
  28.  
  29. typedef struct HISTOGRAMME_E{
  30.     int ** valeurs;
  31.     int nbcolonne;
  32.     int nbligne;
  33.     char type;
  34. }HISTOGRAMME;
  35.  
  36. #define PARAM_BITS_NB 3
  37. #define PARAM_BITS_RGB 3
  38.  
  39. #define FICHIER_DESCRIPTEUR "base_descripteur_image.txt"
  40. #define FICHIER_BASE "liste_base_image.txt"
  41.  
  42. #define CHECK_FICHIER_BASE "wc -l liste_base_image.txt"
  43.  
  44.  
  45.  
  46. /*
  47. *
  48. *
  49. * Lancer l'indexation via la fonction "indexImage()"
  50. *
  51. *
  52. */
  53.  
  54.  
  55. //==================================================================================
  56. //=                                 Commun                                         =
  57. //==================================================================================
  58.  
  59. int lectureFichier(char * s, FILE ** f, int * nbligne, int * nbcolonne, int * type){
  60.     //printf("\n\nOuverture du fichier image");
  61.  
  62.     if((*f=fopen(s,"r")) == NULL)
  63.     {
  64.         printf("\n\nErreur Fatale a l'ouverture du fichier \n");
  65.         return 0;
  66.     }
  67.  
  68.     //Lecture colonne / ligne
  69.     //Enlever les printf pour les débugs
  70.  
  71.     fscanf(*f,"%i ", nbligne);
  72.     //printf("\n\nNombre de lignes: %i", nbligne);
  73.  
  74.     fscanf(*f,"%i ", nbcolonne);
  75.     //printf("\n\nNombre de colonnes: %i", nbcolonne);
  76.  
  77.     fscanf(*f,"%i ", type);
  78.     //printf("\n\nNiveau lu: %i", type);
  79.  
  80.     return 1;
  81. }
  82.  
  83. void creationLien(char * nom, char * type){
  84.  
  85.     FILE * base =  fopen(FICHIER_BASE,"a+");
  86.  
  87.     int ID;
  88.     FILE* check = popen(CHECK_FICHIER_BASE, "r");
  89.     fscanf(check, "%d", &ID);
  90.     fclose(check);
  91.  
  92.     fprintf(base, "<id> %d </id> <fichier> %s </fichier> <type> %s </type>\n", ID, nom, type);
  93.     fclose(base);
  94.  
  95. }
  96.  
  97.  
  98. /*Lis descripteur d'entiers */
  99. int lireBase(char * fichier) {
  100.  
  101.   int check = 0 ;
  102.   FILE* read ;
  103.   read = fopen(FICHIER_BASE, "r");
  104.   char* val = malloc(sizeof(char));
  105.  
  106. if(read != NULL){
  107.     while (fscanf(read,"%s",val) == 1) {
  108.       if(strcmp(val, fichier) == 0){
  109.         check = 1;
  110.       }
  111.       if((strcmp(val, "NB") == 0) && check == 1)
  112.       {
  113.         free(val);
  114.         fclose(read);
  115.         return 1;
  116.       }
  117.       if((strcmp(val, "RGB") == 0) && check == 1)
  118.       {
  119.         free(val);
  120.         fclose(read);
  121.         return 3;
  122.       }
  123.     }
  124. }
  125. return 0;
  126. }
  127.  
  128.  
  129. //==================================================================================
  130. //=                                 IMAGE N & B                                    =
  131. //==================================================================================
  132.  
  133. IMAGE_NB * initImageNB(char * nom_e, int nbligne_e, int nbcolonne_e){
  134.  
  135.     //Malloc structure
  136.     IMAGE_NB * retour = malloc(sizeof(*retour));
  137.  
  138.     //Affectation des differentes valeurs de l'image
  139.     retour->nbligne = nbligne_e;
  140.     retour->nbcolonne = nbcolonne_e;
  141.     retour->nom = nom_e;
  142.    
  143.     //Creation de la matrice image et affectation du pointeur dans la matrice
  144.     int i;
  145.  
  146.     unsigned char ** matrice = malloc(sizeof(unsigned char *) * nbligne_e);
  147.     for(i=0;i<retour->nbligne;i++){
  148.         matrice[i] = malloc(sizeof(unsigned char) * nbcolonne_e);
  149.     }
  150.     retour->matrice = matrice;
  151.  
  152.     return retour;
  153. }
  154.  
  155. void freeImageNB(IMAGE_NB * image){
  156.     //free((*image).nom);
  157.     int i;
  158.     for(i=0;i<image->nbligne;i++){
  159.         free(image->matrice[i]);
  160.     }
  161.     free(image->matrice);
  162.     free(image);
  163.  
  164. }
  165.  
  166.  
  167. void afficherImageNB(IMAGE_NB image){
  168.     int i, j;
  169.     printf("\n\nAffichage de la matrice :\n");
  170.     printf("nbColonne:%d, nbLigne:%d\n", image.nbcolonne, image.nbligne);
  171.     for(i=0;i<image.nbligne;i++)
  172.     {
  173.         for(j=0;j<image.nbcolonne;j++)
  174.         {
  175.             printf("%i ", image.matrice[i][j]);
  176.         }
  177.         printf("\n");
  178.     }
  179.     printf("\nFin Affichage matrice"); 
  180. }
  181.  
  182. int lectureImgageNB(IMAGE_NB image, FILE *f){
  183.     int tempo, i, j, retour;
  184.     for(i=0;i<image.nbligne;i++){
  185.         for(j=0;j<image.nbcolonne;j++){
  186.             retour = fscanf(f,"%i ",&tempo);
  187.             if(retour == EOF)
  188.             {
  189.                     printf("\nErreur fatale : Pas assez de valeurs dans l'image");
  190.                     return 0;
  191.                
  192.             }
  193.             if((tempo > 255) || (tempo < 0))
  194.             {
  195.                 printf("\nErreur fatale : Valeur incorrecte dans l'image");
  196.                 return 0;
  197.  
  198.             }
  199.             image.matrice[i][j] = tempo;
  200.         }
  201.     }
  202.     return 1;
  203. }
  204.  
  205.  
  206. int quantificationImageNB(IMAGE_NB image){
  207.     int i, j;
  208.     for(i=0;i<image.nbligne;i++){
  209.         for(j=0;j<image.nbcolonne;j++){
  210.             image.matrice[i][j] = image.matrice[i][j] >> (8-PARAM_BITS_NB);
  211.             image.matrice[i][j] = image.matrice[i][j] << (8-PARAM_BITS_NB);
  212.         }
  213.     }
  214.     return 1;
  215. }
  216.  
  217. int creationDescripteurNB(IMAGE_NB image){
  218.  
  219.     FILE * descripteur =  fopen(FICHIER_DESCRIPTEUR,"a+");
  220.  
  221.     //Cretion id
  222.  
  223.     int ID = 0;
  224.     FILE* check = popen(CHECK_FICHIER_BASE, "r");
  225.     fscanf(check, "%d", &ID);
  226.     fclose(check);
  227.  
  228.  
  229.     fputs("<id> ", descripteur);
  230.     fprintf(descripteur, "%d", ID);
  231.     fputs(" </id>\n", descripteur);
  232.  
  233.  
  234.  
  235.     fprintf(descripteur, "<type> NB </type>\n<nbcolonne> %d </nbcolonne>\n<nbligne> %d </nbligne>\n", image.nbcolonne,image.nbligne);
  236.  
  237.  
  238.     /*
  239.       Creation de l'histogramme de valeurs:
  240.  
  241.       1. On alloue un espace mémoire d'une matrice 2dim x (nbcol * nbligne)
  242.         => On considére qu'il peut y avoir maximum de (nbcol * nbligne) valeurs différentes
  243.         dans la matrice image
  244.         (en vérité, on pourrait diminuer l'espace mémoire en vérifiant le nb de bits de quantification,
  245.         et en disant que si (nbcol * nbligne) > 2^nbit de quantification, on a alloue 2^nbit de quantification,
  246.         mais c'est "inutile" car on traite ici toujours le pire cas).
  247.       2. On parcours la matrice.
  248.         => La premiere colonne de "histogramme" corresponds aux valeurs
  249.         => La seconde colonne corresponds au nb d'occurences
  250.             => Si on trouve une occurence, on incrémente la seconde variable
  251.       3. Dès qu'on a traiter tous le tableau, on arrête.
  252.  
  253.     */
  254.     int max = image.nbligne * image.nbcolonne;
  255.     int ** histogramme = malloc(sizeof(int*)*2);
  256.     histogramme[0] = malloc(sizeof(int)* max);
  257.     histogramme[1] = malloc(sizeof(int)* max);
  258.  
  259.     int i, j, k;
  260.     int nbvaleur = 0;
  261.  
  262.  
  263.     unsigned char valeur;
  264.     //Init
  265.     for(i=0;i<2;i++)
  266.     {
  267.         for(j=0;j<max;j++)
  268.         {
  269.             histogramme[i][j] = -1;
  270.         }
  271.     }
  272.  
  273.  
  274.     //Parcourir matrice
  275.    
  276.     for(i=0;i<image.nbligne;i++)
  277.     {
  278.         for(j=0;j<image.nbcolonne;j++)
  279.         {
  280.             valeur = image.matrice[i][j];
  281.             //printf("\nvaleur :%d",valeur);
  282.             for(k=0;k<max;k++){
  283.             //On regarde d'abord si on voit valeur, puis ensuite si on voit 0
  284.                 if(histogramme[0][k] == valeur)
  285.                 {
  286.                     histogramme[1][k] += 1;
  287.                     //printf("\nhistogramme:%d:%d, valeur: %d",histogramme[0][k],histogramme[0][k],valeur);
  288.                     break;
  289.                 }
  290.                 if(histogramme[0][k] == -1)
  291.                 {
  292.                     histogramme[0][k] = valeur;
  293.                     histogramme[1][k] = 1;
  294.                     nbvaleur += 1;
  295.                     //printf("\nhistogralmme init :%d",histogramme[1][k]);
  296.                     break;
  297.                 }
  298.             }
  299.         }
  300.  
  301.     }
  302.  
  303.     //Mise en forme des valeurs
  304.     fputs("<valeurs>\n", descripteur);
  305.  
  306.  
  307.     int valmin = 0x7FFFFFFF;
  308.     int indicemin = 0;
  309.     int valprec = -1;
  310.  
  311.     for(i=0;i<nbvaleur;i++)
  312.     {
  313.         valmin = 0x7FFFFFFF;
  314.         for(j=0;j<nbvaleur;j++)
  315.         {
  316.             // On check si
  317.             //  1. Val min (censé être la val min de la plage) est bien minimum
  318.             //  2. On est bien supérieur à la val précédente
  319.             if((valmin>histogramme[0][j]) && (valprec < histogramme[0][j]))
  320.                 {
  321.                     valmin = histogramme[0][j];
  322.                     indicemin = j;
  323.                 }
  324.         }
  325.         valprec = valmin;
  326.         if(histogramme[0][indicemin]<16)
  327.             fprintf(descripteur, "0x0%1x: %i;\n", histogramme[0][indicemin], histogramme[1][indicemin]);
  328.         else
  329.             fprintf(descripteur, "0x%2x: %i;\n", histogramme[0][indicemin], histogramme[1][indicemin]);
  330.     }
  331.  
  332.     fputs("</valeurs>\n\n", descripteur);
  333.     free(histogramme[0]);
  334.     free(histogramme[1]);
  335.     free(histogramme);
  336.     fclose(descripteur);
  337.     return 1;
  338. }
  339.  
  340.  
  341. //==================================================================================
  342. //=                                 IMAGE    RGB                                   =
  343. //==================================================================================
  344.  
  345.  
  346. IMAGE_RGB * initImageRGB(char * nom_e, int nbligne_e, int nbcolonne_e){
  347.  
  348.     //Malloc structure
  349.     IMAGE_RGB * retour = malloc(sizeof(*retour));
  350.  
  351.     //Affectation des differentes valeurs de l'image
  352.     retour->nbligne = nbligne_e;
  353.     retour->nbcolonne = nbcolonne_e;
  354.     retour->nom = nom_e;
  355.    
  356.     //Creation des matrices image et affectation du pointeur dans la matrice
  357.     int i;
  358.    
  359.     unsigned char ** matriceR = malloc(sizeof(unsigned char*) * nbligne_e);
  360.     for(i=0;i<retour->nbligne;i++){
  361.         matriceR[i] = malloc(sizeof(unsigned char) * nbcolonne_e);
  362.     }
  363.     retour->matrice_R = matriceR;
  364.  
  365.         unsigned char ** matriceB = malloc(sizeof(unsigned char*) * nbligne_e);
  366.     for(i=0;i<retour->nbligne;i++){
  367.         matriceB[i] = malloc(sizeof(unsigned char) * nbcolonne_e);
  368.     }
  369.     retour->matrice_B = matriceB;
  370.  
  371.     unsigned char ** matriceG = malloc(sizeof(unsigned char*) * nbligne_e);
  372.     for(i=0;i<retour->nbligne;i++){
  373.         matriceG[i] = malloc(sizeof(unsigned char) * nbcolonne_e);
  374.     }
  375.     retour->matrice_G = matriceG;
  376.  
  377.     unsigned int ** matriceEnc = malloc(sizeof(unsigned int*) * nbligne_e);
  378.     for(i=0;i<retour->nbligne;i++){
  379.         matriceEnc[i] = malloc(sizeof(unsigned int) * nbcolonne_e);
  380.     }
  381.     retour->matrice_Enc = matriceEnc;
  382.  
  383.  
  384.     return retour;
  385. }
  386.  
  387. void freeImageRGB(IMAGE_RGB * image){
  388.     int i;
  389.    
  390.     for(i=0;i<image->nbligne;i++){
  391.         free(image->matrice_R[i]);
  392.     }
  393.     for(i=0;i<image->nbligne;i++){
  394.         free(image->matrice_G[i]);
  395.     }
  396.     for(i=0;i<image->nbligne;i++){
  397.         free(image->matrice_B[i]);
  398.     }
  399.     for(i=0;i<image->nbligne;i++){
  400.         free(image->matrice_Enc[i]);
  401.     }
  402.     free(image->matrice_R);
  403.     free(image->matrice_G);
  404.     free(image->matrice_B);
  405.  
  406.     free(image);
  407.  
  408. }
  409.  
  410.  
  411. void afficherImageRGBEnc(IMAGE_RGB image){
  412.     int i, j;
  413.     printf("nbColonne:%d, nbLigne:%d\n", image.nbcolonne, image.nbligne);
  414.    
  415.     printf("\n\nAffichage de la matrice R :\n");
  416.     for(i=0;i<image.nbligne;i++)
  417.     {
  418.         for(j=0;j<image.nbcolonne;j++)
  419.         {
  420.             printf("%x ", image.matrice_Enc[i][j]);
  421.         }
  422.         printf("\n");
  423.     }
  424.  
  425. }
  426.  
  427. void afficherImageRGB(IMAGE_RGB image){
  428.     int i, j;
  429.     printf("nbColonne:%d, nbLigne:%d\n", image.nbcolonne, image.nbligne);
  430.    
  431.     printf("\n\nAffichage de la matrice R :\n");
  432.     for(i=0;i<image.nbligne;i++)
  433.     {
  434.         for(j=0;j<image.nbcolonne;j++)
  435.         {
  436.             printf("%i ", image.matrice_R[i][j]);
  437.         }
  438.         printf("\n");
  439.     }
  440.    
  441.     printf("\n\nAffichage de la matrice G :\n");
  442.     for(i=0;i<image.nbligne;i++)
  443.     {
  444.         for(j=0;j<image.nbcolonne;j++)
  445.         {
  446.             printf("%i ", image.matrice_G[i][j]);
  447.         }
  448.         printf("\n");
  449.     }
  450.    
  451.     printf("\n\nAffichage de la matrice B :\n");
  452.     for(i=0;i<image.nbligne;i++)
  453.     {
  454.         for(j=0;j<image.nbcolonne;j++)
  455.         {
  456.             printf("%i ", image.matrice_B[i][j]);
  457.         }
  458.         printf("\n");
  459.     }
  460.    
  461.  
  462.     printf("\nFin Affichage matrice"); 
  463. }
  464.  
  465. int lectureImgageRGB(IMAGE_RGB image, FILE *f){
  466.     int tempo, i, j, retour;
  467.     // Lecture image rouge,
  468.     for(i=0;i<image.nbligne;i++){
  469.         for(j=0;j<image.nbcolonne;j++){
  470.             retour = fscanf(f,"%i ",&tempo);
  471.             if(retour == EOF)
  472.             {
  473.                     printf("\nErreur fatale : Pas assez de valeurs dans l'image");
  474.                     return 0;
  475.                
  476.             }
  477.             if((tempo > 255) || (tempo < 0))
  478.             {
  479.                 printf("\nErreur fatale : Valeur incorrecte dans l'image");
  480.                 return 0;
  481.  
  482.             }
  483.             image.matrice_R[i][j] = tempo;
  484.         }
  485.     }
  486.  
  487.     //Lecture image verte
  488.     for(i=0;i<image.nbligne;i++){
  489.         for(j=0;j<image.nbcolonne;j++){
  490.             retour = fscanf(f,"%i ",&tempo);
  491.             if(retour == EOF)
  492.             {
  493.                     printf("\nErreur fatale : Pas assez de valeurs dans l'image");
  494.                     return 0;
  495.                
  496.             }
  497.             if((tempo > 255) || (tempo < 0))
  498.             {
  499.                 printf("\nErreur fatale : Valeur incorrecte dans l'image");
  500.                 return 0;
  501.  
  502.             }
  503.             image.matrice_G[i][j] = tempo;
  504.         }
  505.     }
  506.  
  507.     //lecture image bleue
  508.  
  509.         for(i=0;i<image.nbligne;i++){
  510.         for(j=0;j<image.nbcolonne;j++){
  511.             retour = fscanf(f,"%i ",&tempo);
  512.             if(retour == EOF)
  513.             {
  514.                     printf("\nErreur fatale : Pas assez de valeurs dans l'image");
  515.                     return 0;
  516.                
  517.             }
  518.             if((tempo > 255) || (tempo < 0))
  519.             {
  520.                 printf("\nErreur fatale : Valeur incorrecte dans l'image");
  521.                 return 0;
  522.  
  523.             }
  524.             image.matrice_B[i][j] = tempo;
  525.         }
  526.     }
  527.  
  528.     return 1;
  529. }
  530.  
  531.  
  532. int quantificationImageRGB(IMAGE_RGB image){
  533.     int i, j;
  534.     for(i=0;i<image.nbligne;i++){
  535.         for(j=0;j<image.nbcolonne;j++){
  536.             image.matrice_R[i][j] = image.matrice_R[i][j] >> (8-PARAM_BITS_RGB);
  537.             image.matrice_R[i][j] = image.matrice_R[i][j] << (8-PARAM_BITS_RGB);
  538.             image.matrice_G[i][j] = image.matrice_G[i][j] >> (8-PARAM_BITS_RGB);
  539.             image.matrice_G[i][j] = image.matrice_G[i][j] << (8-PARAM_BITS_RGB);
  540.             image.matrice_B[i][j] = image.matrice_B[i][j] >> (8-PARAM_BITS_RGB);
  541.             image.matrice_B[i][j] = image.matrice_B[i][j] << (8-PARAM_BITS_RGB);
  542.  
  543.             //Mise dans la bonne matrice
  544.             // R, puis G, puis B
  545.             image.matrice_Enc[i][j] = (image.matrice_R[i][j]<<PARAM_BITS_RGB*2) + (image.matrice_G[i][j]<<PARAM_BITS_RGB*1) + image.matrice_B[i][j];
  546.         }
  547.     }
  548.  
  549.  
  550.     return 1;
  551. }
  552.  
  553. int creationDescripteurRGB(IMAGE_RGB image){
  554.  
  555.     FILE * descripteur =  fopen(FICHIER_DESCRIPTEUR,"a+");
  556.  
  557.     //Cretion id
  558.     int ID = 0;
  559.     FILE* check = popen(CHECK_FICHIER_BASE, "r");
  560.     fscanf(check, "%d", &ID);
  561.     fclose(check);
  562.  
  563.     fputs("<id> ", descripteur);
  564.     fprintf(descripteur, "%d", ID);
  565.     fputs(" </id>\n", descripteur);
  566.  
  567.  
  568.  
  569.     fprintf(descripteur, "<type> RGB </type>\n<nbcolonne> %d </nbcolonne>\n<nbligne> %d </nbligne>\n", image.nbcolonne,image.nbligne);
  570.  
  571.     /*
  572.       Creation de l'histogramme de valeurs:
  573.  
  574.       1. On alloue un espace mémoire d'une matrice 2dim x (nbcol * nbligne)
  575.         => On considére qu'il peut y avoir maximum de (nbcol * nbligne) valeurs différentes
  576.         dans la matrice image
  577.         (en vérité, on pourrait diminuer l'espace mémoire en vérifiant le nb de bits de quantification,
  578.         et en disant que si (nbcol * nbligne) > 2^nbit de quantification, on a alloue 2^nbit de quantification,
  579.         mais c'est "inutile" car on traite ici toujours le pire cas).
  580.       2. On parcours la matrice.
  581.         => La premiere colonne de "histogramme" corresponds aux valeurs
  582.         => La seconde colonne corresponds au nb d'occurences
  583.             => Si on trouve une occurence, on incrémente la seconde variable
  584.       3. Dès qu'on a traiter tous le tableau, on arrête.
  585.  
  586.     */
  587.     int max = image.nbligne * image.nbcolonne;
  588.     int ** histogramme = malloc(sizeof(int*)*2);
  589.     histogramme[0] = malloc(sizeof(int)* max);
  590.     histogramme[1] = malloc(sizeof(int)* max);
  591.  
  592.     int i, j, k;
  593.     int nbvaleur = 0;
  594.  
  595.  
  596.     unsigned int valeur;
  597.     //Init
  598.     for(i=0;i<2;i++)
  599.     {
  600.         for(j=0;j<max;j++)
  601.         {
  602.             histogramme[i][j] = -1;
  603.         }
  604.     }
  605.  
  606.  
  607.     //Parcourir matrice
  608.    
  609.     for(i=0;i<image.nbligne;i++)
  610.     {
  611.         for(j=0;j<image.nbcolonne;j++)
  612.         {
  613.             valeur = image.matrice_Enc[i][j];
  614.             //printf("\nvaleur :%d",valeur);
  615.             for(k=0;k<max;k++){
  616.             //On regarde d'abord si on voit valeur, puis ensuite si on voit 0
  617.                 if(histogramme[0][k] == valeur)
  618.                 {
  619.                     histogramme[1][k] += 1;
  620.                     //printf("\nhistogramme:%d:%d, valeur: %d",histogramme[0][k],histogramme[0][k],valeur);
  621.                     break;
  622.                 }
  623.                 if(histogramme[0][k] == -1)
  624.                 {
  625.                     histogramme[0][k] = valeur;
  626.                     histogramme[1][k] = 1;
  627.                     nbvaleur += 1;
  628.                     //printf("\nhistogralmme init :%d",histogramme[1][k]);
  629.                     break;
  630.                 }
  631.             }
  632.         }
  633.  
  634.     }
  635.  
  636.     //Mise en forme des valeurs
  637.     fputs("<valeurs>\n", descripteur);
  638.  
  639.  
  640.     int valmin = 0x7FFFFFFF;
  641.     int indicemin = 0;
  642.     int valprec = -1;
  643.  
  644.     for(i=0;i<nbvaleur;i++)
  645.     {
  646.         valmin = 0x7FFFFFFF;
  647.         for(j=0;j<nbvaleur;j++)
  648.         {
  649.             // On check si
  650.             //  1. Val min (censé être la val min de la plage) est bien minimum
  651.             //  2. On est bien supérieur à la val précédente
  652.             if((valmin>histogramme[0][j]) && (valprec < histogramme[0][j]))
  653.                 {
  654.                     valmin = histogramme[0][j];
  655.                     indicemin = j;
  656.                 }
  657.         }
  658.         valprec = valmin;
  659.         if(histogramme[0][indicemin]<0x10)
  660.             fprintf(descripteur, "0x000%1x: %i;\n", histogramme[0][indicemin], histogramme[1][indicemin]);
  661.         else
  662.         {
  663.             if(histogramme[0][indicemin]<0X100)
  664.                 fprintf(descripteur, "0x00%2x: %i;\n", histogramme[0][indicemin], histogramme[1][indicemin]);  
  665.             else
  666.             {
  667.                 if(histogramme[0][indicemin]<0X1000)
  668.                     fprintf(descripteur, "0x0%3x: %i;\n", histogramme[0][indicemin], histogramme[1][indicemin]);   
  669.                 else
  670.                     fprintf(descripteur, "0x%4x: %i;\n", histogramme[0][indicemin], histogramme[1][indicemin]);
  671.        
  672.             }
  673.         }
  674.     }
  675.  
  676.     fputs("</valeurs>\n\n", descripteur);
  677.     free(histogramme[0]);
  678.     free(histogramme[1]);
  679.     free(histogramme);
  680.     fclose(descripteur);
  681.     return 1;
  682. }
  683.  
  684. //==================================================================================
  685. //=                                 index Image                                    =
  686. //==================================================================================
  687.  
  688.  
  689. int indexImageNB(FILE * f, char * adresse, int nbligne, int nbcolonne){
  690.  
  691.     IMAGE_NB * image;
  692.    
  693.     image = initImageNB(adresse,nbligne,nbcolonne);
  694.     if(!(lectureImgageNB(*image,f)))
  695.         return 0;
  696.     if(!(quantificationImageNB(*image)))
  697.         return 0;
  698.     creationDescripteurNB(*image);
  699.     fclose(f);
  700.     freeImageNB(image);
  701.     return 1;
  702. }
  703.  
  704. int indexImageRGB(FILE * f, char * adresse, int nbligne, int nbcolonne){
  705.  
  706.     IMAGE_RGB * image;
  707.    
  708.     image = initImageRGB(adresse,nbligne,nbcolonne);
  709.     if(!lectureImgageRGB(*image,f))
  710.         return 0;
  711.     if(!quantificationImageRGB(*image))
  712.         return 0;
  713.     //afficherImageRGB(*image);
  714.     //afficherImageRGBEnc(*image);
  715.     creationDescripteurRGB(*image);
  716.     fclose(f);
  717.     freeImageRGB(image);
  718.     return 1;
  719. }
  720.  
  721.  
  722.  
  723. int indexImage()
  724. {
  725.  
  726.     //remove(FICHIER_DESCRIPTEUR);
  727.     //remove(FICHIER_BASE);
  728.     FILE * p;
  729.     FILE * f = NULL;
  730.     char * res = malloc(sizeof(char));
  731.     int nbligne, nbcolonne, type;
  732.     int id = 1;
  733.  
  734.    
  735.  
  736.     //Test RGB
  737.     p = popen("ls TEST_RGB/ | grep .txt ", "r");
  738.     while (fscanf(p,"%s",res) == 1) {
  739.         char fileToRead[100]="TEST_RGB/";
  740.         strcat(fileToRead, res);
  741.         //printf("%s\n",fileToRead );
  742.         lectureFichier(fileToRead,&f,&nbligne,&nbcolonne,&type);
  743.         if(lireBase(res) != type)
  744.         {
  745.         if((type != 3 ) && (type != 1)){
  746.             printf("\nType de l'image inconnu !\nArret de l'indexation");
  747.             return 0;}
  748.         if(type == 1){
  749.             if(indexImageNB(f, fileToRead, nbligne, nbcolonne))
  750.                 creationLien(res,"NB");
  751.         }
  752.         if(type == 3){
  753.             if(indexImageRGB(f, fileToRead, nbligne, nbcolonne))
  754.                 creationLien(res,"RGB");
  755.         }
  756.         }
  757.         id += 1;
  758.         fclose(f);
  759.     }
  760.     fclose(p);
  761.  
  762.     //Test NB
  763.     p = popen("ls TEST_NB/ | grep .txt ", "r");
  764.     while (fscanf(p,"%s",res) == 1) {
  765.  
  766.         char fileToRead[100]="TEST_NB/";
  767.         strcat(fileToRead, res);
  768.         //Descripteur non présent
  769.         lectureFichier(fileToRead,&f,&nbligne,&nbcolonne,&type);
  770.         if(lireBase(res) != type)
  771.         {
  772.             //printf("%s\n",fileToRead );
  773.             if((type != 3 ) && (type != 1)){
  774.                 printf("\nType de l'image inconnu !\nArret de l'indexation");
  775.                 return 0;}
  776.             if(type == 1){
  777.                 if(indexImageNB(f, fileToRead, nbligne, nbcolonne))
  778.                     creationLien(res,"NB");
  779.             }
  780.             if(type == 3){
  781.                 if(indexImageRGB(f, fileToRead, nbligne, nbcolonne))
  782.                     creationLien(res,"RGB");
  783.             }
  784.         }
  785.         id += 1;
  786.         fclose(f);
  787.     }
  788.  
  789.     fclose(p);
  790.    
  791.     return 1;
  792. }
  793.  
  794.  
  795. //==================================================================================
  796. //=                                 Comparaison                                    =
  797. //==================================================================================
  798.  
  799.  
  800.  
  801. HISTOGRAMME * lireDescripteur(FILE * read){
  802.  
  803.     HISTOGRAMME * retour = malloc(sizeof(HISTOGRAMME));
  804.    
  805.  
  806.     int etat = 0;
  807.     int id, type, nbligne, nbcolonne;
  808.     unsigned int max;
  809.     unsigned int cpt = 0;
  810.     int i;
  811.     char canswitch = 1;
  812.     char* val = malloc(sizeof(char));
  813.  
  814.     int ** histoTempo;
  815.  
  816.  
  817.     while (fscanf(read,"%s",val) == 1) {
  818.         // Fonctionnement en MAE
  819.  
  820.         // Actions
  821.         if(etat == 1){
  822.             id = atoi(val);
  823.             etat = 0;
  824.         }
  825.  
  826.         if(etat == 2){
  827.             if(strcmp(val, "RGB"))
  828.                 type = 3;
  829.             else
  830.                 type = 1;
  831.             etat = 0;
  832.         }
  833.  
  834.         if(etat == 3){
  835.             nbcolonne = atoi(val);
  836.             etat = 0;
  837.         }
  838.  
  839.         if(etat == 4){
  840.             nbligne = atoi(val);
  841.             etat = 0;
  842.         }
  843.  
  844.  
  845.  
  846.         // Valeurs
  847.  
  848.         if(etat == 5){
  849.             max = nbligne * nbcolonne;
  850.             histoTempo = malloc(sizeof(int*)*2);
  851.             histoTempo[0] = malloc(sizeof(int)*max);
  852.             histoTempo[1] = malloc(sizeof(int)*max);
  853.             cpt = 0;
  854.             canswitch = 0;
  855.             histoTempo[0][0] = (int)strtol(val, NULL, 0);
  856.             etat = 52;
  857.         }
  858.  
  859.         if(etat == 51 && canswitch){
  860.             histoTempo[0][cpt] = (int)strtol(val, NULL, 0);
  861.             etat = 52;
  862.             canswitch = 0;
  863.         }
  864.        
  865.  
  866.         if(etat == 52  && canswitch){
  867.            
  868.             histoTempo[1][cpt] = atoi(val);
  869.             etat = 51;
  870.             canswitch = 0;
  871.             cpt += 1;
  872.         }
  873.  
  874.  
  875.  
  876.  
  877.  
  878.         // Changement d'états
  879.         if(strcmp(val, "<id>") == 0 && (etat == 0))
  880.             etat = 1;
  881.  
  882.         if(strcmp(val, "<type>") == 0 && (etat == 0))
  883.             etat = 2;
  884.  
  885.         if(strcmp(val, "<nbcolonne>") == 0 && (etat == 0))
  886.             etat = 3;
  887.  
  888.         if(strcmp(val, "<nbligne>") == 0 && (etat == 0))
  889.             etat = 4;
  890.  
  891.         if(strcmp(val, "<valeurs>") == 0 && (etat == 0))
  892.             etat = 5;
  893.  
  894.        
  895.         //if(strcmp(val, "</valeurs>") == 0 && ((etat==51) || (etat == 52)))
  896.         if(strcmp(val, "</valeurs>") == 0)
  897.             {
  898.                 //affichage debug
  899.                 printf("id:%u, type:%u, nbcolonne:%u, nbligne:%u\n", id, type, nbcolonne,nbligne);
  900.                 /*for(i=0;i<cpt;i++){
  901.                     printf("%x : %u \n", histoTempo[0][i], histoTempo[1][i]);
  902.                 }*/
  903.  
  904.                 int ** histogramme = malloc(sizeof(int*)*2);
  905.                 histogramme[0] = malloc(sizeof(int)*cpt);
  906.                 histogramme[1] = malloc(sizeof(int)*cpt);
  907.  
  908.                 for(i=0;i<cpt;i++){
  909.                     histogramme[0][cpt] = histoTempo[0][cpt];
  910.                     histogramme[1][cpt] = histoTempo[1][cpt];
  911.                 }
  912.                 cpt = 0;
  913.                 etat = 0;
  914.                 retour->valeurs = histogramme;
  915.                 retour->nbcolonne = nbcolonne;
  916.                 retour->nbligne = nbligne;
  917.                 retour->type = type;
  918.  
  919.                
  920.                 nbligne = 0;
  921.                 nbcolonne = 0;
  922.                 type = 0;
  923.                 free(histoTempo[0]);
  924.                 free(histoTempo[1]);
  925.                 free(histoTempo);
  926.                 free(val);
  927.                 return retour;
  928.             }
  929.         canswitch =1;
  930.     }
  931.     return retour;
  932. }
  933.  
  934. void test()
  935. {
  936.     FILE * read =  fopen(FICHIER_DESCRIPTEUR,"r");
  937.    
  938.  
  939.     HISTOGRAMME * test;
  940.     int i = 0;
  941.    
  942.     test = lireDescripteur(read);
  943.  
  944.     //printf("%i\n\n\n", test->valeurs[1][1]);
  945.     fclose(read);
  946.     free(test);
  947.  
  948. }
  949.  
  950.  
  951. //==================================================================================
  952. //=                                     Main                                       =
  953. //==================================================================================
  954.  
  955. int main(){
  956.  
  957.  //indexImage();
  958.  test();
  959.  
  960.  
  961. return EXIT_SUCCESS;
  962. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement