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;
}