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 :

Intégrer une source.c (ou avancer sans réinventer la roue) Cjpeg


Sujet :

C

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    210
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Avril 2007
    Messages : 210
    Points : 91
    Points
    91
    Par défaut Intégrer une source.c (ou avancer sans réinventer la roue) Cjpeg
    Bonjour à tous,

    Je voudrais intégrer cjpeg (un programme de conversion d'image) dans mon programme. J'ai donc la source.c mais je n'ai pas envie d'intégrer tout les lignes de la source mais uniquement un "lien" qui permettrait au compilateur de l'intégrer.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    #include <stdio.h>
    #include <string.h>
     
     
    int main()
    {
     
    char fichier_entree[] ="test.ppm";
    char fichier_sortie[] ="test.jpg";
     
    //voici le problème, comment remplacer la fonction ci dessous
    cjpeg -outfile fichier_sortie fichier_entree  
    }
    Merci

  2. #2
    Membre averti

    Homme Profil pro
    Enseignant
    Inscrit en
    Septembre 2012
    Messages
    313
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Septembre 2012
    Messages : 313
    Points : 354
    Points
    354
    Par défaut
    Si cjpeg est une librairie il y a des options du compilateur qui te permettent d'ajouter la librairie à ta fonction main().

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    gcc tonmain.c -I chemindesheaders (.h) -lcjpeg -o tonprogrammeintegrantcjpeg
    exemple avec la librairie opengl

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    gcc testopenglc.c -lGL -lGLU -lglut -I/usr/include/libdrm -o openglc
    J'intègre les librairies GL, GLU, glut et j'explique au compilateur où trouver les définitions des fonctions utilisées dans testopengl.c puis le compilateur va créer l'exécutable openglc.

  3. #3
    Membre averti

    Homme Profil pro
    Enseignant
    Inscrit en
    Septembre 2012
    Messages
    313
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Septembre 2012
    Messages : 313
    Points : 354
    Points
    354
    Par défaut
    si c'est un programme tu devrais alors utiliser les "redirections"... sous Unix/Linux tu peux utiliser '<' pour rediriger le contenu d'un fichier vers un programme ou '|' (pipe) pour rediriger la sortie de ton programme sur cjpeg...

    ou
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    cjpeg < fichierdentree > fichierdesortie
    "<" va rediriger "fichierentree" probablement une image à traiter puis cjpeg va faire ce qu'il est censé faire et le résultat va être redirigé dans "fichierdesortie" probablement une image aussi.

  4. #4
    Membre averti

    Homme Profil pro
    Enseignant
    Inscrit en
    Septembre 2012
    Messages
    313
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Septembre 2012
    Messages : 313
    Points : 354
    Points
    354
    Par défaut
    D'après le code source il semblerait que ce soit un programme et non une librairie, vu qu'il y a une fonction main() dedans... il faudra donc probablement utiliser le système d'exploitation pour obtenir des résultats.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    cjpeg -outfile test.jpg test.ppm
    devrait suffire...

  5. #5
    Expert éminent sénior
    Avatar de Sve@r
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Février 2006
    Messages
    12 684
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Oise (Picardie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Février 2006
    Messages : 12 684
    Points : 30 973
    Points
    30 973
    Billets dans le blog
    1
    Par défaut
    Bonjour
    Citation Envoyé par sebaaas Voir le message
    Code c : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    #include <stdio.h>
    #include <string.h>
     
     
    int main()
    {
     
    char fichier_entree[] ="test.ppm";
    char fichier_sortie[] ="test.jpg";
     
    //voici le problème, comment remplacer la fonction ci dessous
    cjpeg -outfile fichier_sortie fichier_entree  
    }
    Code c : 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
    #include <stdio.h>
    #include <string.h>
     
     
    int main() {
    	char fichier_entree[] ="test.ppm";
    	char fichier_sortie[] ="test.jpg";
    	char cmd[1024];
    	int s;
     
    	// cjpeg -outfile fichier_sortie fichier_entree  
    	sprintf(cmd, "cjpeg -outfile %s %s", fichier_sortie, fichier_entree);
    	s=system(cmd);
    	if (s == 0) printf("[%s] terminée ok\n", cmd);
    	else printf("[%s] terminée avec l'erreur %d\n", cmd, s);
    }
    Mon Tutoriel sur la programmation «Python»
    Mon Tutoriel sur la programmation «Shell»
    Sinon il y en a pleins d'autres. N'oubliez pas non plus les différentes faq disponibles sur ce site
    Et on poste ses codes entre balises [code] et [/code]

  6. #6
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    210
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Avril 2007
    Messages : 210
    Points : 91
    Points
    91
    Par défaut
    Parfait pour moi ! Merci

  7. #7
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    210
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Avril 2007
    Messages : 210
    Points : 91
    Points
    91
    Par défaut
    Bon, je prolonge me sujet. La solution proposée est bonne mais "lente".

    Je pense, sauf erreur de me part, que si j'intègre le programme dans le mien plutôt que de le lancer, accelererai les choses.

    Donc, j'ai renommé le fichier .c en .h et changé int main(int argc, char **argv) par int turbojpeg(int argc, char **argv)
    mais j'ai l'erreur :
    Mon raisonnement est-il trop simpliste?

    voici le .c
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    #include </home/seb/c/jpeg.h>
     
    int main()
    {
    char *argument = "test.ppm test2.jpg";
    	turbojpeg(95,&(argument));
    return 0;
    }
    et le jpeg.h
    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
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    #include <turbojpeg.h>
     
     
    #ifdef _WIN32
    #define strcasecmp  stricmp
    #define strncasecmp  strnicmp
    #endif
     
    #define THROW(action, message) { \
      printf("ERROR in line %d while %s:\n%s\n", __LINE__, action, message); \
      retval = -1;  goto bailout; \
    }
     
    #define THROW_TJ(action)  THROW(action, tjGetErrorStr2(tjInstance))
     
    #define THROW_UNIX(action)  THROW(action, strerror(errno))
     
    #define DEFAULT_SUBSAMP  TJSAMP_444
    #define DEFAULT_QUALITY  95
     
     
    const char *subsampName[TJ_NUMSAMP] = {
      "4:4:4", "4:2:2", "4:2:0", "Grayscale", "4:4:0", "4:1:1"
    };
     
    const char *colorspaceName[TJ_NUMCS] = {
      "RGB", "YCbCr", "GRAY", "CMYK", "YCCK"
    };
     
    tjscalingfactor *scalingFactors = NULL;
    int numScalingFactors = 0;
     
     
    /* DCT filter example.  This produces a negative of the image. */
     
    static int customFilter(short *coeffs, tjregion arrayRegion,
                            tjregion planeRegion, int componentIndex,
                            int transformIndex, tjtransform *transform)
    {
      int i;
     
      for (i = 0; i < arrayRegion.w * arrayRegion.h; i++)
        coeffs[i] = -coeffs[i];
     
      return 0;
    }
     
     
    static void usage(char *programName)
    {
      int i;
     
      printf("\nUSAGE: %s <Input image> <Output image> [options]\n\n",
             programName);
     
      printf("Input and output images can be in Windows BMP or PBMPLUS (PPM/PGM) format.  If\n");
      printf("either filename ends in a .jpg extension, then the TurboJPEG API will be used\n");
      printf("to compress or decompress the image.\n\n");
     
      printf("Compression Options (used if the output image is a JPEG image)\n");
      printf("--------------------------------------------------------------\n\n");
     
      printf("-subsamp <444|422|420|gray> = Apply this level of chrominance subsampling when\n");
      printf("     compressing the output image.  The default is to use the same level of\n");
      printf("     subsampling as in the input image, if the input image is also a JPEG\n");
      printf("     image, or to use grayscale if the input image is a grayscale non-JPEG\n");
      printf("     image, or to use %s subsampling otherwise.\n\n",
             subsampName[DEFAULT_SUBSAMP]);
     
      printf("-q <1-100> = Compress the output image with this JPEG quality level\n");
      printf("     (default = %d).\n\n", DEFAULT_QUALITY);
     
      printf("Decompression Options (used if the input image is a JPEG image)\n");
      printf("---------------------------------------------------------------\n\n");
     
      printf("-scale M/N = Scale the input image by a factor of M/N when decompressing it.\n");
      printf("(M/N = ");
      for (i = 0; i < numScalingFactors; i++) {
        printf("%d/%d", scalingFactors[i].num, scalingFactors[i].denom);
        if (numScalingFactors == 2 && i != numScalingFactors - 1)
          printf(" or ");
        else if (numScalingFactors > 2) {
          if (i != numScalingFactors - 1)
            printf(", ");
          if (i == numScalingFactors - 2)
            printf("or ");
        }
      }
      printf(")\n\n");
     
      printf("-hflip, -vflip, -transpose, -transverse, -rot90, -rot180, -rot270 =\n");
      printf("     Perform one of these lossless transform operations on the input image\n");
      printf("     prior to decompressing it (these options are mutually exclusive.)\n\n");
     
      printf("-grayscale = Perform lossless grayscale conversion on the input image prior\n");
      printf("     to decompressing it (can be combined with the other transform operations\n");
      printf("     above.)\n\n");
     
      printf("-crop WxH+X+Y = Perform lossless cropping on the input image prior to\n");
      printf("     decompressing it.  X and Y specify the upper left corner of the cropping\n");
      printf("     region, and W and H specify the width and height of the cropping region.\n");
      printf("     X and Y must be evenly divible by the MCU block size (8x8 if the input\n");
      printf("     image was compressed using no subsampling or grayscale, 16x8 if it was\n");
      printf("     compressed using 4:2:2 subsampling, or 16x16 if it was compressed using\n");
      printf("     4:2:0 subsampling.)\n\n");
     
      printf("General Options\n");
      printf("---------------\n\n");
     
      printf("-fastupsample = Use the fastest chrominance upsampling algorithm available in\n");
      printf("     the underlying codec.\n\n");
     
      printf("-fastdct = Use the fastest DCT/IDCT algorithms available in the underlying\n");
      printf("     codec.\n\n");
     
      printf("-accuratedct = Use the most accurate DCT/IDCT algorithms available in the\n");
      printf("     underlying codec.\n\n");
     
      exit(1);
    }
     
     
    int turbojpeg(int argc, char **argv)
    {
      tjscalingfactor scalingFactor = { 1, 1 };
      int outSubsamp = -1, outQual = -1;
      tjtransform xform;
      int flags = 0;
      int width, height;
      char *inFormat, *outFormat;
      FILE *jpegFile = NULL;
      unsigned char *imgBuf = NULL, *jpegBuf = NULL;
      int retval = 0, i, pixelFormat = TJPF_UNKNOWN;
      tjhandle tjInstance = NULL;
     
      if ((scalingFactors = tjGetScalingFactors(&numScalingFactors)) == NULL)
        THROW_TJ("getting scaling factors");
      memset(&xform, 0, sizeof(tjtransform));
     
      if (argc < 3)
        usage(argv[0]);
     
      /* Parse arguments. */
      for (i = 3; i < argc; i++) {
        if (!strncasecmp(argv[i], "-sc", 3) && i < argc - 1) {
          int match = 0, temp1 = 0, temp2 = 0, j;
     
          if (sscanf(argv[++i], "%d/%d", &temp1, &temp2) < 2)
            usage(argv[0]);
          for (j = 0; j < numScalingFactors; j++) {
            if ((double)temp1 / (double)temp2 == (double)scalingFactors[j].num /
                                                 (double)scalingFactors[j].denom) {
              scalingFactor = scalingFactors[j];
              match = 1;
              break;
            }
          }
          if (match != 1)
            usage(argv[0]);
        } else if (!strncasecmp(argv[i], "-su", 3) && i < argc - 1) {
          i++;
          if (!strncasecmp(argv[i], "g", 1))
            outSubsamp = TJSAMP_GRAY;
          else if (!strcasecmp(argv[i], "444"))
            outSubsamp = TJSAMP_444;
          else if (!strcasecmp(argv[i], "422"))
            outSubsamp = TJSAMP_422;
          else if (!strcasecmp(argv[i], "420"))
            outSubsamp = TJSAMP_420;
          else
            usage(argv[0]);
        } else if (!strncasecmp(argv[i], "-q", 2) && i < argc - 1) {
          outQual = atoi(argv[++i]);
          if (outQual < 1 || outQual > 100)
            usage(argv[0]);
        } else if (!strncasecmp(argv[i], "-g", 2))
          xform.options |= TJXOPT_GRAY;
        else if (!strcasecmp(argv[i], "-hflip"))
          xform.op = TJXOP_HFLIP;
        else if (!strcasecmp(argv[i], "-vflip"))
          xform.op = TJXOP_VFLIP;
        else if (!strcasecmp(argv[i], "-transpose"))
          xform.op = TJXOP_TRANSPOSE;
        else if (!strcasecmp(argv[i], "-transverse"))
          xform.op = TJXOP_TRANSVERSE;
        else if (!strcasecmp(argv[i], "-rot90"))
          xform.op = TJXOP_ROT90;
        else if (!strcasecmp(argv[i], "-rot180"))
          xform.op = TJXOP_ROT180;
        else if (!strcasecmp(argv[i], "-rot270"))
          xform.op = TJXOP_ROT270;
        else if (!strcasecmp(argv[i], "-custom"))
          xform.customFilter = customFilter;
        else if (!strncasecmp(argv[i], "-c", 2) && i < argc - 1) {
          if (sscanf(argv[++i], "%dx%d+%d+%d", &xform.r.w, &xform.r.h, &xform.r.x,
                     &xform.r.y) < 4 ||
              xform.r.x < 0 || xform.r.y < 0 || xform.r.w < 1 || xform.r.h < 1)
            usage(argv[0]);
          xform.options |= TJXOPT_CROP;
        } else if (!strcasecmp(argv[i], "-fastupsample")) {
          printf("Using fast upsampling code\n");
          flags |= TJFLAG_FASTUPSAMPLE;
        } else if (!strcasecmp(argv[i], "-fastdct")) {
          printf("Using fastest DCT/IDCT algorithm\n");
          flags |= TJFLAG_FASTDCT;
        } else if (!strcasecmp(argv[i], "-accuratedct")) {
          printf("Using most accurate DCT/IDCT algorithm\n");
          flags |= TJFLAG_ACCURATEDCT;
        } else usage(argv[0]);
      }
     
      /* Determine input and output image formats based on file extensions. */
      inFormat = strrchr(argv[1], '.');
      outFormat = strrchr(argv[2], '.');
      if (inFormat == NULL || outFormat == NULL || strlen(inFormat) < 2 ||
          strlen(outFormat) < 2)
        usage(argv[0]);
      inFormat = &inFormat[1];
      outFormat = &outFormat[1];
     
      if (!strcasecmp(inFormat, "jpg")) {
        /* Input image is a JPEG image.  Decompress and/or transform it. */
        long size;
        int inSubsamp, inColorspace;
        int doTransform = (xform.op != TJXOP_NONE || xform.options != 0 ||
                           xform.customFilter != NULL);
        unsigned long jpegSize;
     
        /* Read the JPEG file into memory. */
        if ((jpegFile = fopen(argv[1], "rb")) == NULL)
          THROW_UNIX("opening input file");
        if (fseek(jpegFile, 0, SEEK_END) < 0 || ((size = ftell(jpegFile)) < 0) ||
            fseek(jpegFile, 0, SEEK_SET) < 0)
          THROW_UNIX("determining input file size");
        if (size == 0)
          THROW("determining input file size", "Input file contains no data");
        jpegSize = (unsigned long)size;
        if ((jpegBuf = (unsigned char *)tjAlloc(jpegSize)) == NULL)
          THROW_UNIX("allocating JPEG buffer");
        if (fread(jpegBuf, jpegSize, 1, jpegFile) < 1)
          THROW_UNIX("reading input file");
        fclose(jpegFile);  jpegFile = NULL;
     
        if (doTransform) {
          /* Transform it. */
          unsigned char *dstBuf = NULL;  /* Dynamically allocate the JPEG buffer */
          unsigned long dstSize = 0;
     
          if ((tjInstance = tjInitTransform()) == NULL)
            THROW_TJ("initializing transformer");
          xform.options |= TJXOPT_TRIM;
          if (tjTransform(tjInstance, jpegBuf, jpegSize, 1, &dstBuf, &dstSize,
                          &xform, flags) < 0)
            THROW_TJ("transforming input image");
          tjFree(jpegBuf);
          jpegBuf = dstBuf;
          jpegSize = dstSize;
        } else {
          if ((tjInstance = tjInitDecompress()) == NULL)
            THROW_TJ("initializing decompressor");
        }
     
        if (tjDecompressHeader3(tjInstance, jpegBuf, jpegSize, &width, &height,
                                &inSubsamp, &inColorspace) < 0)
          THROW_TJ("reading JPEG header");
     
        printf("%s Image:  %d x %d pixels, %s subsampling, %s colorspace\n",
               (doTransform ? "Transformed" : "Input"), width, height,
               subsampName[inSubsamp], colorspaceName[inColorspace]);
     
        if (!strcasecmp(outFormat, "jpg") && doTransform &&
            scalingFactor.num == 1 && scalingFactor.denom == 1 && outSubsamp < 0 &&
            outQual < 0) {
          /* Input image has been transformed, and no re-compression options
             have been selected.  Write the transformed image to disk and exit. */
          if ((jpegFile = fopen(argv[2], "wb")) == NULL)
            THROW_UNIX("opening output file");
          if (fwrite(jpegBuf, jpegSize, 1, jpegFile) < 1)
            THROW_UNIX("writing output file");
          fclose(jpegFile);  jpegFile = NULL;
          goto bailout;
        }
     
        /* Scaling and/or a non-JPEG output image format and/or compression options
           have been selected, so we need to decompress the input/transformed
           image. */
        width = TJSCALED(width, scalingFactor);
        height = TJSCALED(height, scalingFactor);
        if (outSubsamp < 0)
          outSubsamp = inSubsamp;
     
        pixelFormat = TJPF_BGRX;
        if ((imgBuf = (unsigned char *)tjAlloc(width * height *
                                               tjPixelSize[pixelFormat])) == NULL)
          THROW_UNIX("allocating uncompressed image buffer");
     
        if (tjDecompress2(tjInstance, jpegBuf, jpegSize, imgBuf, width, 0, height,
                          pixelFormat, flags) < 0)
          THROW_TJ("decompressing JPEG image");
        tjFree(jpegBuf);  jpegBuf = NULL;
        tjDestroy(tjInstance);  tjInstance = NULL;
      } else {
        /* Input image is not a JPEG image.  Load it into memory. */
        if ((imgBuf = tjLoadImage(argv[1], &width, 1, &height, &pixelFormat,
                                  0)) == NULL)
          THROW_TJ("loading input image");
        if (outSubsamp < 0) {
          if (pixelFormat == TJPF_GRAY)
            outSubsamp = TJSAMP_GRAY;
          else
            outSubsamp = TJSAMP_444;
        }
        printf("Input Image:  %d x %d pixels\n", width, height);
      }
     
      printf("Output Image (%s):  %d x %d pixels", outFormat, width, height);
     
      if (!strcasecmp(outFormat, "jpg")) {
        /* Output image format is JPEG.  Compress the uncompressed image. */
        unsigned long jpegSize = 0;
     
        jpegBuf = NULL;  /* Dynamically allocate the JPEG buffer */
     
        if (outQual < 0)
          outQual = DEFAULT_QUALITY;
        printf(", %s subsampling, quality = %d\n", subsampName[outSubsamp],
               outQual);
     
        if ((tjInstance = tjInitCompress()) == NULL)
          THROW_TJ("initializing compressor");
        if (tjCompress2(tjInstance, imgBuf, width, 0, height, pixelFormat,
                        &jpegBuf, &jpegSize, outSubsamp, outQual, flags) < 0)
          THROW_TJ("compressing image");
        tjDestroy(tjInstance);  tjInstance = NULL;
     
        /* Write the JPEG image to disk. */
        if ((jpegFile = fopen("test0.jpg", "wb")) == NULL)
          THROW_UNIX("opening output file");
        if (fwrite(jpegBuf, jpegSize, 1, jpegFile) < 1)
          THROW_UNIX("writing output file");
        tjDestroy(tjInstance);  tjInstance = NULL;
        fclose(jpegFile);  jpegFile = NULL;
        tjFree(jpegBuf);  jpegBuf = NULL;
      } else {
        /* Output image format is not JPEG.  Save the uncompressed image
           directly to disk. */
        printf("\n");
        if (tjSaveImage(argv[2], imgBuf, width, 0, height, pixelFormat, 0) < 0)
          THROW_TJ("saving output image");
      }
     
    bailout:
      tjFree(imgBuf);
      if (tjInstance) tjDestroy(tjInstance);
      tjFree(jpegBuf);
      if (jpegFile) fclose(jpegFile);
      return retval;
    }

  8. #8
    Expert éminent sénior
    Avatar de Sve@r
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Février 2006
    Messages
    12 684
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Oise (Picardie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Février 2006
    Messages : 12 684
    Points : 30 973
    Points
    30 973
    Billets dans le blog
    1
    Par défaut
    Citation Envoyé par sebaaas Voir le message
    Bon, je prolonge me sujet. La solution proposée est bonne mais "lente".
    Heu... C'est vrai que system() crée un sous-process pour exécuter la commande... mais je pense toutefois que la "lenteur" est plus due à l'exécution totale de la commande "cpeg" elle-même qu'au temps mis pour générer le sous-processus et récupérer son état.
    Pas compliqué à vérifier avec un petit time cjpeg -outfile fichier_sortie fichier_entree comparé à un petit time le_programme_qui_appelle_cpeg.

    Citation Envoyé par sebaaas Voir le message
    Je pense, sauf erreur de me part, que si j'intègre le programme dans le mien plutôt que de le lancer, accelererai les choses.
    J'ai un avis mitigé à ce propos... mais rien ne vaut ses propres expériences

    Citation Envoyé par sebaaas Voir le message
    Donc, j'ai renommé le fichier .c en .h et changé int main(int argc, char **argv) par int turbojpeg(int argc, char **argv)
    mais j'ai l'erreur :
    Mon raisonnement est-il trop simpliste?
    Non, ça peut marcher... à condition de passer à la fonction turbojpeg() ce qu'elle attend. Et qu'attend-elle ? Un int et un char**, ok. Mais ce n'est pas parce qu'elle attend un nombre qu'il faut lui passer un bête nombre pris au hasard. Et ce n'est pas parce qu'elle attend un char** qu'il faut lui passer l'adresse d'une string en espérant qu'elle saura s'en contenter. Ok les types correspondent mais pas ce qu'ils représentent. Pour que ça marche, il faut la forme mais aussi le fond !!!
    Le premier paramètre int argc contient le nombre d'arguments passés au programme. C'est à dire que si j'appelle prog toto titi, argc contient la valeur "3" (le nom du programme compte aussi).
    Donc si tu appelles la fonction pour qu'elle traite 2 fichiers, il faut lui mettre "3" en premier paramètre.

    Ensuite le second paramètre contient un tableau de strings (un tableau de pointeurs donc) où chaque string correspond aux arguments passés (ici "prog", "toto" et "titi") plus un dernier pointeur qui vaut NULL. C'est impératif. Le programme (ou ici la fonction) se basera sur ces caractéristiques pour travailler. Certes dans char *argument = "test.ppm test2.jpg", &argument est bien un char** mais le contenu de ce char** ne correspond absolument pas à cette description.

    Donc si tu veux que ça marche, il te faut créer la structure équivalente
    Code c : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    char *argument[]={
    	"cjpeg",
    	"test.ppm",
    	"test2.jpg",
    	NULL,
    };
    turbojpeg(3, argument);
    Là ça devrait aller mieux...
    Mon Tutoriel sur la programmation «Python»
    Mon Tutoriel sur la programmation «Shell»
    Sinon il y en a pleins d'autres. N'oubliez pas non plus les différentes faq disponibles sur ce site
    Et on poste ses codes entre balises [code] et [/code]

  9. #9
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    210
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Avril 2007
    Messages : 210
    Points : 91
    Points
    91
    Par défaut
    Merciiiiiiiiiiiiiiiiiii

    Je viens de tester et c'est plus rapide (car je faisais 25 appels/secondes) .... mais pas aussi rapide que voulu :-)

  10. #10
    Expert éminent sénior
    Avatar de Sve@r
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Février 2006
    Messages
    12 684
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Oise (Picardie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Février 2006
    Messages : 12 684
    Points : 30 973
    Points
    30 973
    Billets dans le blog
    1
    Par défaut
    Citation Envoyé par sebaaas Voir le message
    .... mais pas aussi rapide que voulu :-)
    Ben oui, il faut quand-même laisser au programme (ou à la fonction) cjpeg le temps qu'elle fasse le job.
    Ensuite tu peux partir dans le système de jobs en parallèle via fork(). C'est plus chaud à gérer mais si ça vaut le coup pourquoi pas...
    Mon Tutoriel sur la programmation «Python»
    Mon Tutoriel sur la programmation «Shell»
    Sinon il y en a pleins d'autres. N'oubliez pas non plus les différentes faq disponibles sur ce site
    Et on poste ses codes entre balises [code] et [/code]

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 0
    Dernier message: 22/01/2013, 10h55
  2. Réponses: 11
    Dernier message: 24/01/2012, 15h09
  3. Peut on encore innover en informatique « sans réinventer la roue » ?
    Par Idelways dans le forum Humour Informatique
    Réponses: 35
    Dernier message: 09/10/2011, 00h00
  4. Réponses: 0
    Dernier message: 01/03/2011, 10h39
  5. Réponses: 3
    Dernier message: 10/09/2010, 19h07

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