IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C Discussion :

Utilisation des structures


Sujet :

C

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2013
    Messages
    70
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juillet 2013
    Messages : 70
    Points : 38
    Points
    38
    Par défaut Utilisation des structures
    Je suis en train de rajouter des structures
    à un programme de mise en niveau de gris.

    Le programme principale:

    exemple d'utilisation si image .ppm

    -i baboon.ppm -o bazb.ppm -g
    (fait une mise en gris)

    Comme vous pouvez tester à l'exécution j'ai rajouté des printf
    afin de voir ou le nom de fichier était mal lu et je ne comprends pas
    comment faire pour le régler.

    J'ai ça qui s'affiche:
    ligne 200:baboon.ppm
    ligne 200:baboon.ppm
    ligne 20:baboon.ppm
    ligne 98:�t
    ligne 313:�t
    ligne 251:baboon.ppm


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stddef.h>
    #include <math.h>
    #include <unistd.h>
     
    #define TAILLE_MAX 3
     
    typedef struct{
    char *Itab;
    char *Otab;
    }Image;
     
    #include "Function.h"
    #include "Function.c"
     
    int getInput(Image *Im,char *tab1);//nom du fichier d'entré
    int getOutput(Image *Im,char *tab1);//nom du fichier de sortie
     
    int main(int argc, char *argv[]) {
     
        Image Im;
        int e;
        int max = 3000;
        int count;
        char kernel [20];
     
        int optch;
        extern int opterr;
     
        char format[] = "gb:o:i:";
     
        opterr = 1;
     
        while ((optch = getopt(argc, argv, format)) != -1)
        switch (optch) {
           case 'i':
                printf ("Paramètre i rencontré avec argument %s\n", optarg);
                getInput(&Im,optarg);
               // printf("%s\n",Im.Itab);
                e=saturation(&Im);
                break;
            case 'o':
                printf ("Paramètre o rencontré avec argument %s\n", optarg);
                getOutput(&Im,optarg);
              //  printf("%s\n",Im.Otab);
                break;
            case 'g':
    	    writeFileWithGrayScale(charToIntWidth(heightAndWidth(&Im)),charToIntHeight(heightAndWidth(&Im)),e,&Im,max);
                break;
            case 'b':
                printf ("Paramètre b recontré\n");
                break;
        }
     
        return 0;
     
    }
     
    int getInput(Image *Im,char *tab1){
     
    int i=0;
     
    Im->Itab=malloc( sizeof(tab1)*sizeof(char));
     
    while(tab1[i] != '\0'){
    	Im->Itab[i]=tab1[i];
    	i++;
    	}
    }
     
    int getOutput(Image *Im,char *tab1){
     
    int i=0;
     
    Im->Otab=malloc( sizeof(tab1)*sizeof(char));
     
    while(tab1[i] != '\0'){
    	Im->Otab[i]=tab1[i];
    	i++;
    	}
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stddef.h>
    #define TAILLE_MAX 3
    #include<math.h>
    #include "Function.h"
     
     
     
    /*************GRAYSCALE*********************************************************/
    /*******************************************************************************/
     
    int** grayScale(int width, int height, int saturation, int max,Image *Im)
    {
        int i = 0;
        int j = 0;
        int k = 0;
        int l = 0;
        int **q;
        int summ = 0;
        int counter = 0;
        printf("hello\n");
       int **p = (int **)malloc(sizeof(int*) * height);
     
        if (getFormOfMatrix(width, height,&Im) == 0)
        {
            q = matrixByColumnToMatrixP3(width, height, saturation,Im);
        }
        else
        {
            q = transformArrayOfCharInMatrixP3(width, height, max,Im);
        }
        for (i = 0; i < height; i++)
        {
            p[i] = (int*) malloc(sizeof(int) * width);
            for (j = 0; j < (width * 3); j++)
            {
                summ = summ + q[i][j];
                counter++;
                if ( counter == 3)
                {
                    p[k][l]= (summ / 3);
                    //printf("sum:%d k:%d l:%d \n",(summ/3), k,l);
                    if ( l != (width-1))
                    {
                        l++;
                    }
                    else
                    {
                        l = 0;
                        k++;
                    }
                    summ = 0;
                    counter = 0;
                }
            }
        }  
        return p;
    }
     
    void writeFileWithGrayScale(int width, int height, int saturation, Image *Im,int max)
    {
        int i = 0;
        int j = 0;
        FILE* newFile = NULL;
        int **p = grayScale(width, height, saturation, max,Im);
        newFile = fopen(Im->Otab, "w");
        fputs( "P2\n", newFile);
        fprintf(newFile, "%d %d\n", width,height);
        fprintf(newFile,"%d\n", saturation);
        for ( i = 0; i < height;i++)
        {
            for ( j = 0; j < width; j++)
            {
                if ( j == 0)
                {
                    fprintf(newFile,"%d", p[i][j]);
                }
                else if (p[i][j] <= 9 )
                {
                fprintf(newFile,"  %d", p[i][j]);
                }
                else{fprintf(newFile," %d", p[i][j]);}
            }
            fprintf(newFile, "\n");
        }
        fclose(newFile);
    }
    /////////////////////////////////////////////////////////////////////
    //////////////////////////getFormOfMatrix////////////////////////////
    /////////////////////////////////////////////////////////////////////
     
    int getFormOfMatrix( int width, int height,Image *Im)
    {
        FILE *file = NULL;
        file = fopen(Im->Itab,"r");
        printf("ligne 98:%s\n",Im->Itab);
        if (countlines(Im) >= ((width * height) + 3))
            {
                return 0;
            }
        return 1;
    }
    /////////////////////////////////////////////////////////////////////
    //////////////////////////WHEREISTHESPACE////////////////////////////
    /////////////////////////////////////////////////////////////////////
     
    // This function give you the position of the space in a string between two elements
    // It needs only the line with both of height and width
     
    int whereIsTheSpace(char *lineOfHeightAndWidth)
    {
        int i;
        int n = strlen(lineOfHeightAndWidth); // we catch the length of the line with the width and the height
        for(i = 0; i <= n; i++)
        {
            if (lineOfHeightAndWidth[i] == ' ')
            {
                return i; // we return the position where the space is
            }
        }
    }
     
    /////////////////////////////////////////////////////////////////////
    //////////////////////////////SATURATION/////////////////////////////
    /////////////////////////////////////////////////////////////////////
     
    // function which extract the saturation of a picture and return a number
    // It needs only in parameter the name of the file you what to extract its saturation
     
     
    int saturation(Image *Im)
    {
     
        int i;
        int saturationNumber;
        char saturationString[10] = "";
        FILE *file = NULL;
        file = fopen(Im->Itab,"r");
        printf("ligne 141:%s\n \n",Im->Itab);
        for (i = 1 ; i < 4 ; i++) // counter
            {
                fgets(saturationString, 10, file); // we select only the third line of the file
            }
        fclose(file);
        saturationNumber = atoi(saturationString);
        return saturationNumber;
    }
     
     
    /////////////////////////////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////////////////////////
     
    int charToIntWidth(char *lineOfHeightAndWidth) // Find the width from the line with both height and width
    {
        char widthChar[10] = ""; // The width only
        char aNumber[10]= ""; // A char from the string in parameter
        int width; // The value which we will return
        int i; // counter
        int n = whereIsTheSpace(lineOfHeightAndWidth);
        for (i = 0; i < n; i++) // We will put the value of the main string in our variable until we will found a space
        {
            *aNumber = lineOfHeightAndWidth[i];
            strcat(widthChar, aNumber); // We concatenate our new variable with the number from the main string
            strcpy(aNumber, "");
        }
        width = atoi(widthChar);
        return width;
     
    }
     
    int charToIntHeight(char *lineOfHeightAndWidth)
    {
        char heightChar[10] = "";
        char aNumber[10]= "";
        int n = whereIsTheSpace(lineOfHeightAndWidth);
        int i = n; // counter
        int height;
        while(lineOfHeightAndWidth[i]!='\n') // We stop to looking for the height when we met a '\n'
        {
            *aNumber = lineOfHeightAndWidth[i];
            strcat(heightChar, aNumber);
            i++;
     
        }
        height = atoi(heightChar);
        return height;
    }
     
    /////////////////////////////////////////////////////////////////////
    //////////////////////////HEIGHTANDWIDTH/////////////////////////////
    /////////////////////////////////////////////////////////////////////
     
    char* heightAndWidth(Image *Im) // function which extract the Height and the width of a picture
    {
        int i;
        FILE *file = NULL;
        file = fopen(Im->Itab,"r");
            printf("ligne 200:%s\n",Im->Itab);
        char* valueOfHeightAndWidth = malloc(21*sizeof(char));
        for (i = 1 ; i < 3 ; i++) // counter
        {
            fgets(valueOfHeightAndWidth, 21, file); // we select only the second line of the file
        }
        fclose(file);
        return valueOfHeightAndWidth;
    }
     
     
     
    int** matrixByColumnToMatrixP3(int width, int height, int saturation,Image *Im)
    {
        int i = 0;
        int j = 0;
        int seuil = ((saturation + 1) / 2);
        char pixel[10];
        FILE *file = NULL;
        FILE* newFile = NULL;
        file = fopen(Im->Itab,"r");
            printf("ligne 221:%s\n",Im->Itab);
        newFile = fopen(Im->Otab, "w");
            printf("ligne 223:%s\n",Im->Otab);
        fputs( "P1\n", newFile);
        fprintf(newFile, "%d %d\n", width,height);
        int **q = (int **)malloc(sizeof(int*) * height);
        for ( i = 0; i < 3; i++)
        {
            fgets(pixel,  10, file);
        }
        for ( i = 0; i < height; i++)
        {
            q[i] = (int*) malloc(sizeof(int) * (width * 3));
            for ( j = 0; j < (width * 3); j++)
            {
                q[i][j] = atoi(fgets(pixel, 10, file));
            }
        }
        return q;
     
    }
     
     
    int** transformArrayOfCharInMatrixP3(int width, int height, int stringMax,Image *Im)
    {
        int i = 0;
        int j = 0;
        int k = 0;
        FILE *file = NULL;
        file = fopen(Im->Itab,"r");
                printf("ligne 251:%s\n",Im->Itab);
        char lineOfPixel[stringMax];
        int *line;
        int **p = (int **)malloc(sizeof(int*) * height);
        for(i = 0; i < 3; i++)
        {
            fgets(lineOfPixel, stringMax , file);
        }
        for (i = 0; i < height ; i++)
        {
            p[i] = (int*) malloc(sizeof(int) *(width * 3));
            fgets(lineOfPixel, stringMax , file);
            line = explode(lineOfPixel, ' ', width * 3);
            for (j = 0; j < (width * 3) ; j++)
            {
                p[i][j] = line[j];
            }
            free(line);
        }
        return p;
    }
     
    int* explode(char* stringToExplode, char separator, int width)
    {
        int i = 0;
        int *finalArray = malloc(sizeof(int) * width);
        int indicator = 0; // If several separators follow each other
        int buffer = 0;
        int cursor = 0;
        int length = strlen(stringToExplode);
        for (i = 0;i < length;i++)
        {
            if ( stringToExplode[i] == separator && indicator == -1)
                {
                    finalArray[cursor] = buffer;
                    cursor++;
                    indicator = 0;
                    buffer =  0;
                }
            else if ( stringToExplode[i] != separator && stringToExplode[i] != '\n')
                {
                    buffer = buffer * 10 + (stringToExplode[i] - 48);
                    indicator = -1;
                }
        }
        finalArray[cursor] = buffer;  // Because the last value is not register
        return finalArray;
    }
     
    /////////////////////////////////////////////////////////////////////
    ////////////////////////////COOUNTLINE///////////////////////////////
    /////////////////////////////////////////////////////////////////////
     
     
    // This function counts the number of lines in the file
    // It needs only the name of the file which you want to extract its number of line
     
     
    int countlines(Image *Im)
    {
      FILE *file = NULL;
      file = fopen(Im->Itab,"r");
              printf("ligne 313:%s\n",Im->Itab);
     
      int ch=0;
      int lines=0;
     
      if (file == NULL)  // If the file is unreachable we will return 1
        return 1;
        while(!feof(file)) //each time we will meet a '\n' we will increase the number of lines
        {
            ch = fgetc(file);
            if(ch == '\n') // if we meet a '\n'
            {
            lines++;
        }
        }
        fclose(file);
        return lines;
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
     
    /*********Prototypes*******************/
    /*************************************/
    /*
    int maxint(int first, int second);
    int minint(int first, int second);
     
    char* magicNumber(char *nameOfFile);
    int arrondi(float number);
    */int countlines(Image *Im);/*
    int getTheMaxSizeOfAString(char *nameOfFile);
    */char* heightAndWidth(Image *Im);
    int whereIsTheSpace(char *lineOfHeightAndWidth);
    int charToIntWidth(char *lineOfHeightAndWidth);
    int charToIntHeight(char *lineOfHeightAndWidth);
     
    int saturation(Image *Im);
    int* explode(char* stringToExplode, char separator, int width);
    //int getFormOfMatrix( int width, int height,Image *Im);/*
     
    int** matrixByColumnToMatrixP1( char* nameOfFile, int width, int height, int saturation);
    int** matrixByColumnToMatrixP2( char* nameOfFile, int width, int height, int saturation,char* nameOfOutputFile);
    int** matrixByColumnToMatrixP3(int width, int height, int saturation,Image *Im);/*
     
    int** transformArrayOfCharInMatrixP1( char* nameOfFile, int width, int height, int stringMax);
    int** transformArrayOfCharInMatrixP2( char* nameOfFile, int width, int height, int stringMax);
    */int** transformArrayOfCharInMatrixP3(int width, int height, int stringMax,Image *Im);/*
     
    int** binarisation(int width, int height, int saturation, char* nameOfFile, int max,int temp,char* nameOfOutputFile);
     
    //void fileCopyP1 (char* nameOfFile,int max);
    //void fileCopyP2 (char* nameOfFile, int max);
    //void fileCopyP3 (char* nameOfFile, int max);
     
    void writeFileWithBinarisation(int width, int height, int saturation,char* nameOfFile, int max,char* nameOfOutputFile,int temp);
    */
    void writeFileWithGrayScale(int width, int height, int saturation, int max,Image *Im);
    int** grayScale(int width, int height, int saturation, int max,Image *Im);
     
    //int max(int* table, int saturation);
    /*
    void histogram(int width, int height, int sat,char* nameOfFile, int stringMax,char* nameOfOutputFile);
     
    int** getKernel(char* kernel);
     
    void convolution(int width, int height, char* nameOfFile, char* kernel, int sat, int max,char* nameOfYourOutputFile);
    void convolutionP3(int width, int height, char* nameOfFile, char* kernel, int sat, int max,char* nameOfYourOutputFile);
     
    void dilatation(int width, int height, char* nameOfFile, char* kernel, int sat, int max,char* nameOfOutputFile);
    void dilatationP3(int width, int height, char* nameOfFile, char* kernel, int sat, int max,char* nameOfOutputFile);
     
    void erosion(int width, int height, char* nameOfFile, char* kernel, int sat, int max,char* nameOfOutputFile);
    void erosionP3(int width, int height, char* nameOfFile, char* kernel, int sat, int max,char* nameOfOutputFile);
    */
    Je vous remercie pour votre aide !

  2. #2
    Expert éminent sénior
    Avatar de diogene
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Juin 2005
    Messages
    5 761
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 761
    Points : 13 926
    Points
    13 926
    Par défaut
    fonction getInput() :
    Ceci est incorrect
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Im->Itab=malloc( sizeof(tab1)*sizeof(char));
    tab1 est un pointeur (char*) et donc sizeof(tab1) renvoie la taille d'un ... pointeur et non pas la taille de la chaine pointée.
    Il n'y a donc pas assez de place allouée pour la copie.

    Utiliser plutôt
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Im->Itab=malloc(strlen(tab1)+1);
    (Le +1 est pour la place du '\0' terminal et sizeof(char) vaut toujours 1)

    Idem pour la fonction getOutput()
    Publication : Concepts en C

    Mon avatar : Glenn Gould

    --------------------------------------------------------------------------
    Une réponse vous a été utile ? Remerciez son auteur en cliquant le pouce vert !

  3. #3
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2013
    Messages
    70
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juillet 2013
    Messages : 70
    Points : 38
    Points
    38
    Par défaut
    Merci Diogéne, et pour résumer mon principale problème
    c'est de faire ça (désolé pour le scanf, je sais que c'est à éviter):

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    /* 
     * File:   main.c
     * Author: jean-jacque
     *
     * Created on November 30, 2013, 5:26 PM
     */
     
    #include <stdio.h>
    #include <stdlib.h>
     
    typedef struct{
    char *tab;    
    }Ouga;
     
    /*
     * 
     */
    int main(int argc, char** argv) {
     
        Ouga ou;
     
        ou.tab=malloc(8);
     
        printf("enter variable:\n");
        scanf("%s",ou.tab);
     
        passe1(&ou);
     
        return (EXIT_SUCCESS);
    }
     
    passe1(Ouga *ou){
     
        printf("passe1:%s\n",ou->tab);
     
        passe2(&ou);
    }
     
    passe2(Ouga *ou){
     
            printf("passe2:%s\n",ou->tab);
     
            passe3(&ou);
    }
     
    passe3(Ouga *ou){
     
        printf("passe3:%s\n",ou->tab);
     
    }

  4. #4
    Expert éminent sénior
    Avatar de diogene
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Juin 2005
    Messages
    5 761
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 761
    Points : 13 926
    Points
    13 926
    Par défaut
    - Dans l'appel à passe1() dans main(), tu passes bien l'adresse de la structure (&ou) et dans passe1(), tu accède correctement à son champ tab par ou->tab

    - Dans passe1(), tu appelle passe2() qui attend lui aussi l'adresse de la structure, mais tu passes l'adresse de la variable locale ou à la fonction, autrement dit, l'adresse de l'adresse de la structure (ou contient l'adresse de la structure dans cette fonction).
    Tu devrais avoir dans passe(1)
    - Idem pour l'appel à passe3() dans passe2()
    Publication : Concepts en C

    Mon avatar : Glenn Gould

    --------------------------------------------------------------------------
    Une réponse vous a été utile ? Remerciez son auteur en cliquant le pouce vert !

  5. #5
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2013
    Messages
    70
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juillet 2013
    Messages : 70
    Points : 38
    Points
    38
    Par défaut
    Merci par contre j'ai un nouveau petit problème technique
    très étrange.

    Si j'exécute le programme mon image(.ppm) devient gris comme prévu
    mais dés que le programme s'arrête l'image devient une bouillit
    (mélange de gris et noir)
    alors qu'il n'y a quasiment pas d'instructions qui suit.

    exemple d'exécution:

    ./test -i baboon.ppm -o baz.pgm -g


    On peut trouver un baboon ici:
    http://www.hlevkin.com/TestImages/classic.htm

    le main:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stddef.h>
    #include <math.h>
    #include <unistd.h>
     
    #define TAILLE_MAX 3
     
    typedef struct{
    char *Itab;
    char *Otab;
    }Image;
     
    #include "Function.h"
    #include "Function.c"
     
    int getInput(Image *Im,char *tab1);//nom du fichier d'entré
    int getOutput(Image *Im,char *tab1);//nom du fichier de sortie
     
    int main(int argc, char *argv[]) {
     
        Image Im;
        int e;
        int max = 3000;
        int count;
        char kernel [20];
        char a;
     
        int optch;
        extern int opterr;
     
        char format[] = "gb:o:i:";
     
        opterr = 1;
     
        while ((optch = getopt(argc, argv, format)) != -1)
        switch (optch) {
           case 'i':
                printf ("Paramètre i rencontré avec argument %s\n", optarg);
                getInput(&Im,optarg);
               // printf("%s\n",Im.Itab);
                e=saturation(&Im);
                break;
            case 'o':
                printf ("Paramètre o rencontré avec argument %s\n", optarg);
                getOutput(&Im,optarg);
              //  printf("%s\n",Im.Otab);
                break;
            case 'g':
    	    writeFileWithGrayScale(charToIntWidth(heightAndWidth(&Im)),charToIntHeight(heightAndWidth(&Im)),e,&Im,max);
                break;
            case 'b':
                printf ("Paramètre b recontré\n");
                break;
     
     
        }
     
           printf("voulez vous un chien\n");
                scanf("%c",&a);
     
      return 0;
     
    }
     
    int getInput(Image *Im,char *tab1){
     
    int i=0;
     
    Im->Itab=malloc( ( strlen(tab1)+1));
     
    while(tab1[i] != '\0'){
    	Im->Itab[i]=tab1[i];
    	i++;
    	}
    }
     
    int getOutput(Image *Im,char *tab1){
     
    int i=0;
     
    Im->Otab=malloc( strlen(tab1)+1);
     
    while(tab1[i] != '\0'){
    	Im->Otab[i]=tab1[i];
    	i++;
    	}
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stddef.h>
    #define TAILLE_MAX 3
    #include<math.h>
    #include "Function.h"
     
     
     
    /*************GRAYSCALE*********************************************************/
    /*******************************************************************************/
     
    int** grayScale(int width, int height, int saturation, int max,Image *Im)
    {
        int i = 0;
        int j = 0;
        int k = 0;
        int l = 0;
        printf("ligne 20:%s\n",Im->Itab);
        int **q;
        int summ = 0;
        int counter = 0;
       int **p = (int **)malloc(sizeof(int*) * height);
     
        if (getFormOfMatrix(width, height,Im) == 0)
        {
            q = matrixByColumnToMatrixP3(width, height, saturation,Im);
        }
        else
        {
            q = transformArrayOfCharInMatrixP3(width, height, max,Im);
        }
        for (i = 0; i < height; i++)
        {
            p[i] = (int*) malloc(sizeof(int) * width);
            for (j = 0; j < (width * 3); j++)
            {
                summ = summ + q[i][j];
                counter++;
                if ( counter == 3)
                {
                    p[k][l]= (summ / 3);
                    //printf("sum:%d k:%d l:%d \n",(summ/3), k,l);
                    if ( l != (width-1))
                    {
                        l++;
                    }
                    else
                    {
                        l = 0;
                        k++;
                    }
                    summ = 0;
                    counter = 0;
                }
            }
        }  
        return p;
    }
     
    void writeFileWithGrayScale(int width, int height, int saturation, Image *Im,int max)
    {
        int i = 0;
        int j = 0;
        FILE* newFile = NULL;
        int **p = grayScale(width, height, saturation, max,Im);
        newFile = fopen(Im->Otab, "w");
        fputs( "P2\n", newFile);
        fprintf(newFile, "%d %d\n", width,height);
        fprintf(newFile,"%d\n", saturation);
        for ( i = 0; i < height;i++)
        {
            for ( j = 0; j < width; j++)
            {
                if ( j == 0)
                {
                    fprintf(newFile,"%d", p[i][j]);
                }
                else if (p[i][j] <= 9 )
                {
                fprintf(newFile,"  %d", p[i][j]);
                }
                else{fprintf(newFile," %d", p[i][j]);}
            }
            fprintf(newFile, "\n");
        }
        fclose(newFile);
    }
    /////////////////////////////////////////////////////////////////////
    //////////////////////////getFormOfMatrix////////////////////////////
    /////////////////////////////////////////////////////////////////////
     
    int getFormOfMatrix( int width, int height,Image *Im)
    {
        FILE *file = NULL;
        file = fopen(Im->Itab,"r");
        printf("ligne 98:%s\n",Im->Itab);
        if (countlines(Im) >= ((width * height) + 3))
            {
                return 0;
            }
        return 1;
    }
    /////////////////////////////////////////////////////////////////////
    //////////////////////////WHEREISTHESPACE////////////////////////////
    /////////////////////////////////////////////////////////////////////
     
    // This function give you the position of the space in a string between two elements
    // It needs only the line with both of height and width
     
    int whereIsTheSpace(char *lineOfHeightAndWidth)
    {
        int i;
        int n = strlen(lineOfHeightAndWidth); // we catch the length of the line with the width and the height
        for(i = 0; i <= n; i++)
        {
            if (lineOfHeightAndWidth[i] == ' ')
            {
                return i; // we return the position where the space is
            }
        }
    }
     
    /////////////////////////////////////////////////////////////////////
    //////////////////////////////SATURATION/////////////////////////////
    /////////////////////////////////////////////////////////////////////
     
    // function which extract the saturation of a picture and return a number
    // It needs only in parameter the name of the file you what to extract its saturation
     
     
    int saturation(Image *Im)
    {
        int i;
        int saturationNumber;
        char saturationString[10] = "";
        FILE *file = NULL;
        file = fopen(Im->Itab,"r");
        printf("ligne 141:%s\n \n",Im->Itab);
        for (i = 1 ; i < 4 ; i++) // counter
            {
                fgets(saturationString, 10, file); // we select only the third line of the file
            }
        fclose(file);
        saturationNumber = atoi(saturationString);
        return saturationNumber;
    }
     
     
    /////////////////////////////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////////////////////////
     
    int charToIntWidth(char *lineOfHeightAndWidth) // Find the width from the line with both height and width
    {
        char widthChar[10] = ""; // The width only
        char aNumber[10]= ""; // A char from the string in parameter
        int width; // The value which we will return
        int i; // counter
        int n = whereIsTheSpace(lineOfHeightAndWidth);
        for (i = 0; i < n; i++) // We will put the value of the main string in our variable until we will found a space
        {
            *aNumber = lineOfHeightAndWidth[i];
            strcat(widthChar, aNumber); // We concatenate our new variable with the number from the main string
            strcpy(aNumber, "");
        }
        width = atoi(widthChar);
        return width;
     
    }
     
    int charToIntHeight(char *lineOfHeightAndWidth)
    {
        char heightChar[10] = "";
        char aNumber[10]= "";
        int n = whereIsTheSpace(lineOfHeightAndWidth);
        int i = n; // counter
        int height;
        while(lineOfHeightAndWidth[i]!='\n') // We stop to looking for the height when we met a '\n'
        {
            *aNumber = lineOfHeightAndWidth[i];
            strcat(heightChar, aNumber);
            i++;
     
        }
        height = atoi(heightChar);
        return height;
    }
     
    /////////////////////////////////////////////////////////////////////
    //////////////////////////HEIGHTANDWIDTH/////////////////////////////
    /////////////////////////////////////////////////////////////////////
     
    char* heightAndWidth(Image *Im) // function which extract the Height and the width of a picture
    {
        int i;
        FILE *file = NULL;
        file = fopen(Im->Itab,"r");
            printf("ligne 200:%s\n",Im->Itab);
        char* valueOfHeightAndWidth = malloc(21*sizeof(char));
        for (i = 1 ; i < 3 ; i++) // counter
        {
            fgets(valueOfHeightAndWidth, 21, file); // we select only the second line of the file
        }
        fclose(file);
        return valueOfHeightAndWidth;
    }
     
    int** matrixByColumnToMatrixP3(int width, int height, int saturation,Image *Im)
    {
        int i = 0;
        int j = 0;
        int seuil = ((saturation + 1) / 2);
        char pixel[10];
        FILE *file = NULL;
        FILE* newFile = NULL;
        file = fopen(Im->Itab,"r");
            printf("ligne 221:%s\n",Im->Itab);
        newFile = fopen(Im->Otab, "w");
            printf("ligne 223:%s\n",Im->Otab);
        fputs( "P1\n", newFile);
        fprintf(newFile, "%d %d\n", width,height);
        int **q = (int **)malloc(sizeof(int*) * height);
        for ( i = 0; i < 3; i++)
        {
            fgets(pixel,  10, file);
        }
        for ( i = 0; i < height; i++)
        {
            q[i] = (int*) malloc(sizeof(int) * (width * 3));
            for ( j = 0; j < (width * 3); j++)
            {
                q[i][j] = atoi(fgets(pixel, 10, file));
            }
        }
        return q;
    }
     
    int** transformArrayOfCharInMatrixP3(int width, int height, int stringMax,Image *Im)
    {
        int i = 0;
        int j = 0;
        int k = 0;
        FILE *file = NULL;
        file = fopen(Im->Itab,"r");
                printf("ligne 251:%s\n",Im->Itab);
        char lineOfPixel[stringMax];
        int *line;
        int **p = (int **)malloc(sizeof(int*) * height);
        for(i = 0; i < 3; i++)
        {
            fgets(lineOfPixel, stringMax , file);
        }
        for (i = 0; i < height ; i++)
        {
            p[i] = (int*) malloc(sizeof(int) *(width * 3));
            fgets(lineOfPixel, stringMax , file);
            line = explode(lineOfPixel, ' ', width * 3);
            for (j = 0; j < (width * 3) ; j++)
            {
                p[i][j] = line[j];
            }
            free(line);
        }
        return p;
    }
     
    int* explode(char* stringToExplode, char separator, int width)
    {
        int i = 0;
        int *finalArray = malloc(sizeof(int) * width);
        int indicator = 0; // If several separators follow each other
        int buffer = 0;
        int cursor = 0;
        int length = strlen(stringToExplode);
        for (i = 0;i < length;i++)
        {
            if ( stringToExplode[i] == separator && indicator == -1)
                {
                    finalArray[cursor] = buffer;
                    cursor++;
                    indicator = 0;
                    buffer =  0;
                }
            else if ( stringToExplode[i] != separator && stringToExplode[i] != '\n')
                {
                    buffer = buffer * 10 + (stringToExplode[i] - 48);
                    indicator = -1;
                }
        }
        finalArray[cursor] = buffer;  // Because the last value is not register
        return finalArray;
    }
     
    /////////////////////////////////////////////////////////////////////
    ////////////////////////////COOUNTLINE///////////////////////////////
    /////////////////////////////////////////////////////////////////////
     
     
    // This function counts the number of lines in the file
    // It needs only the name of the file which you want to extract its number of line
     
     
    int countlines(Image *Im)
    {
      FILE *file = NULL;
      file = fopen(Im->Itab,"r");
              printf("ligne 313:%s\n",Im->Itab);
     
      int ch=0;
      int lines=0;
     
      if (file == NULL)  // If the file is unreachable we will return 1
        return 1;
        while(!feof(file)) //each time we will meet a '\n' we will increase the number of lines
        {
            ch = fgetc(file);
            if(ch == '\n') // if we meet a '\n'
            {
            lines++;
        }
        }
        fclose(file);
        return lines;
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
     
    /*********Prototypes*******************/
    /*************************************/
    /*
    int maxint(int first, int second);
    int minint(int first, int second);
     
    char* magicNumber(char *nameOfFile);
    int arrondi(float number);
    */int countlines(Image *Im);/*
    int getTheMaxSizeOfAString(char *nameOfFile);
    */char* heightAndWidth(Image *Im);
    int whereIsTheSpace(char *lineOfHeightAndWidth);
    int charToIntWidth(char *lineOfHeightAndWidth);
    int charToIntHeight(char *lineOfHeightAndWidth);
     
    int saturation(Image *Im);
    int* explode(char* stringToExplode, char separator, int width);
    //int getFormOfMatrix( int width, int height,Image *Im);/*
     
    int** matrixByColumnToMatrixP1( char* nameOfFile, int width, int height, int saturation);
    int** matrixByColumnToMatrixP2( char* nameOfFile, int width, int height, int saturation,char* nameOfOutputFile);
    int** matrixByColumnToMatrixP3(int width, int height, int saturation,Image *Im);/*
     
    int** transformArrayOfCharInMatrixP1( char* nameOfFile, int width, int height, int stringMax);
    int** transformArrayOfCharInMatrixP2( char* nameOfFile, int width, int height, int stringMax);
    */int** transformArrayOfCharInMatrixP3(int width, int height, int stringMax,Image *Im);/*
     
    int** binarisation(int width, int height, int saturation, char* nameOfFile, int max,int temp,char* nameOfOutputFile);
     
    //void fileCopyP1 (char* nameOfFile,int max);
    //void fileCopyP2 (char* nameOfFile, int max);
    //void fileCopyP3 (char* nameOfFile, int max);
     
    void writeFileWithBinarisation(int width, int height, int saturation,char* nameOfFile, int max,char* nameOfOutputFile,int temp);
    */
    void writeFileWithGrayScale(int width, int height, int saturation, Image *Im,int max);
    int** grayScale(int width, int height, int saturation, int max,Image *Im);
     
    //int max(int* table, int saturation);
    /*
    void histogram(int width, int height, int sat,char* nameOfFile, int stringMax,char* nameOfOutputFile);
     
    int** getKernel(char* kernel);
     
    void convolution(int width, int height, char* nameOfFile, char* kernel, int sat, int max,char* nameOfYourOutputFile);
    void convolutionP3(int width, int height, char* nameOfFile, char* kernel, int sat, int max,char* nameOfYourOutputFile);
     
    void dilatation(int width, int height, char* nameOfFile, char* kernel, int sat, int max,char* nameOfOutputFile);
    void dilatationP3(int width, int height, char* nameOfFile, char* kernel, int sat, int max,char* nameOfOutputFile);
     
    void erosion(int width, int height, char* nameOfFile, char* kernel, int sat, int max,char* nameOfOutputFile);
    void erosionP3(int width, int height, char* nameOfFile, char* kernel, int sat, int max,char* nameOfOutputFile);
    */

Discussions similaires

  1. Réponses: 1
    Dernier message: 29/01/2013, 14h44
  2. utilisation des structures dans une interface
    Par ralf91 dans le forum C#
    Réponses: 5
    Dernier message: 22/01/2010, 16h55
  3. Utiliser des structures en type managé
    Par kakrocq dans le forum VC++ .NET
    Réponses: 1
    Dernier message: 11/04/2008, 16h09
  4. Utiliser des Filler dans les structures avec ACCEPT
    Par beegees dans le forum Cobol
    Réponses: 2
    Dernier message: 13/01/2008, 19h09
  5. Débutant: Question sur l'utilisation des structures
    Par pdgnr dans le forum C++Builder
    Réponses: 5
    Dernier message: 26/10/2006, 13h03

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo