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 :

Compression et décompression avec jpeglib dans un fichier


Sujet :

C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Nouveau candidat au Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Février 2015
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 28
    Localisation : France, Somme (Picardie)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2015
    Messages : 2
    Par défaut Compression et décompression avec jpeglib dans un fichier
    Bonjour à tous,
    pour un projet de fac, je dois "charger une image jpeg et de la réécrire dans un autre fichier". J'ai donc codé le programme suivant qui ne fonctionne pas comme il faudrait.
    Après compilation, le contenu de "l'autre" fichier, n'est pas le même que celui du fichier jpeg source. Pouvez vous m'aider ?

    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
    #include <stdio.h>
    #include <jpeglib.h>
    #include <stdlib.h>
     
    /* dimensions of the image we want to write */
    int width = 640; //L'image à charger est bien en 480*640
    int height = 480;
    int bytes_per_pixel = 3;   /* or 1 for GRACYSCALE images */
    J_COLOR_SPACE color_space = JCS_RGB; /* or JCS_GRAYSCALE for grayscale images */
     
    int read_jpeg_file( char *filename )
    {
           /* these are standard libjpeg structures for reading(decompression) */
    	struct jpeg_decompress_struct cinfo;
    	struct jpeg_error_mgr jerr;
    	/* libjpeg data structure for storing one row, that is, scanline of an image */
    	JSAMPROW row_pointer[1];
     
    	FILE *infile = fopen( filename, "rb" );
    	FILE *second = fopen( "test2.jpeg", "wb" );
    	unsigned long location = 0;
    	int i = 0;
     
    	if ( !infile )
    	{
    		printf("Error opening jpeg file %s\n!", filename );
    		return -1;
     
    	}
    	/* here we set up the standard libjpeg error handler */
    	cinfo.err = jpeg_std_error( &jerr );
    	/* setup decompression process and source, then read JPEG header */
    	jpeg_create_decompress( &cinfo );
    	/* this makes the library read from infile */
    	jpeg_stdio_src( &cinfo, infile );
    	/* reading the image header which contains image information */
    	jpeg_read_header( &cinfo, TRUE );
     
    	printf( "JPEG File Information: \n" );
    	printf( "Image width and height: %d pixels and %d pixels.\n", cinfo.image_width, cinfo.image_height );
    	printf( "Color components per pixel: %d.\n", cinfo.num_components );
    	printf( "Color space: %d.\n", cinfo.jpeg_color_space );
     
    	/* Start decompression jpeg here */
    	jpeg_start_decompress( &cinfo );
     
    	/* now actually read the jpeg into the raw buffer */
    	row_pointer[0] = (unsigned char *)malloc( cinfo.output_width*cinfo.num_components );
     
    	/* read one scan line at a time */
    	while( cinfo.output_scanline < cinfo.image_height )
    	{	
    		jpeg_read_scanlines( &cinfo, row_pointer, 1 );
    		for(i=0; i<640;i++) //changer 640 pour w*components
    		{
    		fprintf(second, "%i", row_pointer[0][i]);
     
    		}	
    	}
     
    	/* wrap up decompression, destroy objects, free pointers and close open files */
    	jpeg_finish_decompress( &cinfo );
    	jpeg_destroy_decompress( &cinfo );
    	free( row_pointer[0] );
    	fclose( infile );
    	fclose( second );
    	return 1;
    }
     
    int write_jpeg_file( char *filename )
    {
    	struct jpeg_compress_struct cinfo;
    	struct jpeg_error_mgr jerr;
     
    	/* this is a pointer to one row of image data */
    	//JSAMPROW row_pointer[1];
    	FILE *outfile = fopen( filename, "wb" );
    	FILE *file = fopen("test2.jpeg", "rb" );
    	if ( !outfile )
    	{
    		printf("Error opening output jpeg file %s\n!", filename );
    		return -1;
    	}
    	cinfo.err = jpeg_std_error( &jerr );
    	jpeg_create_compress(&cinfo);
    	jpeg_stdio_dest(&cinfo, outfile);
     
    	/* Setting the parameters of the output file here */
    	cinfo.image_width = width;	
    	printf("%d \n", width);
    	cinfo.image_height = height;
    	printf("%d \n", height);
    	cinfo.input_components = bytes_per_pixel;
    	printf("%d \n", bytes_per_pixel);
    	cinfo.in_color_space = color_space;
    	printf("%d \n", color_space);
     
        /* default compression parameters, we shouldn't be worried about these */
    	jpeg_set_defaults( &cinfo );
    	/* Now do the compression .. */
    	jpeg_start_compress( &cinfo, TRUE );
    	//unsigned char *row_pointer;
    	//row_pointer[0] = (unsigned char *)malloc( cinfo.image_width*cinfo.input_components );
    	char *a;
    	a = (unsigned char*)malloc( cinfo.image_width*cinfo.input_components*cinfo.image_height );
    	int i;
    	unsigned char *line; 
     
    	while( cinfo.next_scanline < cinfo.image_height )
    	{
    		fgets(a, cinfo.image_width*cinfo.input_components, file);
    		jpeg_write_scanlines( &cinfo, &a, 1);
    	}
    	/* similar to read file, clean up after we're done compressing */
    	jpeg_finish_compress( &cinfo );
    	jpeg_destroy_compress( &cinfo );
    	fclose( outfile );
    	/* success code is 1! */
    	return 1;
    }
     
     
    int main()
    {
    	char *infilename = "lecture.jpeg", *outfilename = "ecriture.jpeg";
     
    	/* Try opening a jpeg*/
    	if( read_jpeg_file( infilename ) > 0 ) 
    	{
    		/* then copy it to another file */
    		if( write_jpeg_file( outfilename ) < 0 ) return -1;
    	}
    	else return -1;
    	return 0;
    }

  2. #2
    Membre actif
    Homme Profil pro
    Étudiant
    Inscrit en
    Septembre 2012
    Messages
    63
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2012
    Messages : 63
    Par défaut
    Je pense que tu peux utiliser fread, et donc ne pas utiliser la jpeglib.

    Takago.

  3. #3
    Nouveau candidat au Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Février 2015
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 28
    Localisation : France, Somme (Picardie)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2015
    Messages : 2
    Par défaut
    Le problème c'est que j'ai obligation d'utiliser jpeglib puisque c'est imposé dans la consigne ...

  4. #4
    Membre actif
    Homme Profil pro
    Étudiant
    Inscrit en
    Septembre 2012
    Messages
    63
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2012
    Messages : 63
    Par défaut
    En fouillant un peu j'ai trouvé se petit exemple, je pense que ça peu t'aider.

    https://github.com/LuaDist/libjpeg/b...ster/example.c

Discussions similaires

  1. [CSV] Tableau php avec mysql dans un fichier excel
    Par saraza dans le forum Langage
    Réponses: 3
    Dernier message: 06/01/2009, 18h33
  2. problème avec écriture dans un fichier
    Par haraelendil dans le forum Débuter
    Réponses: 1
    Dernier message: 08/09/2008, 09h36
  3. [Débutant] Popup avec javascript dans un fichier séparé
    Par Marco85 dans le forum Général JavaScript
    Réponses: 6
    Dernier message: 20/12/2006, 17h28
  4. Probleme avec print dans un fichier
    Par goblin dans le forum Langage
    Réponses: 4
    Dernier message: 28/11/2005, 18h15
  5. Réponses: 4
    Dernier message: 03/08/2005, 09h47

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