Bonjour, je suis un assidu des cours Youtube de Jacques Olivier Lapeyre. A ce titre, j'utilise ses modules pour apprendre le c. Je recommande vivement de regarder ses videos qui sont uniques sur le net pour qui veut apprendre le c. J'ai écrit un programme qui charge une image BMP et qui l'enregistre sur le disque dur. Seulement voilà, l'image du disque dur est plus petite que l'image chargée. Je vous livre mon code, constitué ici de deux fonctions : image_load_BMP24 et image_saveBMP24 + le main.

merci d'avance pour vos conseils éclairés.


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
void image_save_BMP24 (char* filename, imageBMP I)
{
 
fprintf(stderr, "\nFONCTION SAVE\n");
  jfile F = standard_jfile_open (filename, JFILE_MODE_WRITE);
   int i;
  unsigned char IdentifierB = I->IdentifierB;
  unsigned char IdentifierM = I->IdentifierM;
  unsigned int FileSize = I->FileSize;
  unsigned int Reserved =  I->Reserved;
  unsigned int DataOffset = I->DataOffset;
  unsigned int HeaderSize = I->HeaderSize;
 
  unsigned int width = I->w;
  unsigned int height = I->h;
 
  short int Planes = I->Planes;
  short int BitsPerPixel = I->BitsPerPixel;
  unsigned int Compression = I->Compression;
  unsigned int BitmapDataSize = I->BitmapDataSize;
  unsigned int HResolution = I->HResolution;
  unsigned int VResolution = I->VResolution;
  unsigned int Colours = I->Colours;
  unsigned int ImportantColours = I->ImportantColours;
 
  jfile_write_char (F, (unsigned char) I->IdentifierB);
    fprintf(stderr, "IdentifierB = %c\n", IdentifierB);
 
  jfile_write_char (F, (char) I->IdentifierM);
   fprintf(stderr, "IdentifierM = %c\n", IdentifierM);
 
 
  jfile_write_char (F, ((unsigned char) ((FileSize) & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((FileSize >> 8) & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((FileSize >> 16) & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((FileSize >> 24) & 0xFF))); 
  fprintf(stderr, "FileSize = %d\n", FileSize);
 
  jfile_write_char (F, ((unsigned char) (Reserved & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((Reserved >> 8) & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((Reserved >> 16) & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((Reserved >> 24) & 0xFF)));
  fprintf(stderr, "Reserved = %d\n", Reserved);
 
 
  jfile_write_char (F, ((unsigned char) (DataOffset & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((DataOffset >> 8) & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((DataOffset >> 16) & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((DataOffset >> 24) & 0xFF))); 
  fprintf(stderr, "DataOffset = %d\n", DataOffset);
 
 
  jfile_write_char (F, ((unsigned char) (HeaderSize & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((HeaderSize >> 8) & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((HeaderSize >> 16) & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((HeaderSize >> 24) & 0xFF)));   
  fprintf(stderr, "HeaderSize = %d\n", HeaderSize);
 
 
  jfile_write_char (F, ((unsigned char) (width & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((width >> 8) & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((width >> 16) & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((width >> 24) & 0xFF)));   
  fprintf(stderr, "width = %d\n", width);  
 
 
  jfile_write_char (F, ((unsigned char) (height & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((height >> 8) & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((height >> 16) & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((height >> 24) & 0xFF)));   
  fprintf(stderr, "height = %d\n", height); 
 
 
  jfile_write_char (F, ((unsigned char) (Planes & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((Planes >> 8) & 0xFF))); 
  fprintf(stderr, "Planes = %d\n", Planes);
 
 
 
  jfile_write_char (F, ((unsigned char) (BitsPerPixel & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((BitsPerPixel >> 8) & 0xFF))); 
  fprintf(stderr, "BitesPerPixel = %d\n", BitsPerPixel); 
 
 
  jfile_write_char (F, ((unsigned char) (Compression & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((Compression >> 8) & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((Compression >> 16) & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((Compression >> 24) & 0xFF)));   
  fprintf(stderr, "Compression = %d\n", Compression); 
 
 
  jfile_write_char (F, ((unsigned char) (BitmapDataSize & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((BitmapDataSize >> 8) & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((BitmapDataSize >> 16) & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((BitmapDataSize >> 24) & 0xFF)));  
  fprintf(stderr, "BitmapDataSize = %d\n", BitmapDataSize); 
 
 
 
  jfile_write_char (F, ((unsigned char) (HResolution & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((HResolution >> 8) & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((HResolution >> 16) & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((HResolution >> 24) & 0xFF)));   
  fprintf(stderr, "HResolution = %d\n", HResolution); 
 
 
  jfile_write_char (F, ((unsigned char) (VResolution & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((VResolution >> 8) & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((VResolution >> 16) & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((VResolution >> 24) & 0xFF)));   
  fprintf(stderr, "VResolution = %d\n", VResolution); 
 
 
  jfile_write_char (F, ((unsigned char) (Colours & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((Colours >> 8) & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((Colours >> 16) & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((Colours >> 24) & 0xFF)));   
  fprintf(stderr, "Nombre de couleurs dans l'image = %d\n", Colours); 
 
 
  jfile_write_char (F, ((unsigned char) (ImportantColours & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((ImportantColours >> 8) & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((ImportantColours >> 16) & 0xFF))); 
  jfile_write_char (F, ((unsigned char) ((ImportantColours >> 24) & 0xFF)));   
  fprintf(stderr, "Nombre de couleurs importantes dans l'image = %d\n", ImportantColours); 
 
 
 
 
 
 
  for (i = 0; i < width * height; i++)
    {
      byte r, g, b;
      colour_get_rgb (I->pixel[i], &r, &g, &b);
      jfile_write_char (F, (unsigned char) b);
      jfile_write_char (F, (unsigned char) g);
      jfile_write_char (F, (unsigned char) r);
 
    }  
 
  jfile_close (&F);
}
 
 
 
 
image image_load_BMP24 (char* filename)
{
fprintf(stderr, "\nFONCTION LOAD\n");
  jfile F = standard_jfile_open (filename, JFILE_MODE_READ);
  imageBMP I;
  unsigned char c;
  int i;
   /*int w, h, i;*/
  unsigned char IdentifierB, IdentifierM;
  unsigned int FileSize; 
  unsigned int Reserved, DataOffset, HeaderSize, width, height;
  short int Planes, BitsPerPixel;
  unsigned int Compression, BitmapDataSize, HResolution, VResolution, Colours, ImportantColours;
 
  /*Identifier = 2 octects*/
  IdentifierB = (byte) jfile_read_char (F);
  fprintf(stderr, "IdentifierB = %c\n", IdentifierB);
 
  IdentifierM =  (byte) jfile_read_char (F);
  fprintf(stderr, "IdentifierM = %c\n", IdentifierM);
 
 
  /*FileSize = 4 octects*/ 
  FileSize = (byte) jfile_read_char (F);
  FileSize |=  (((byte) jfile_read_char (F)) << 8);
  FileSize |=  (((byte) jfile_read_char (F)) << 16);
  FileSize |=  (((byte) jfile_read_char (F)) << 24); 
  fprintf(stderr, "FileSize = %d\n", FileSize);
 
  /*Reserved = 4 octects*/ 
  Reserved = (byte) jfile_read_char (F);
  Reserved |=  (((byte) jfile_read_char (F)) << 8);
  Reserved |=  (((byte) jfile_read_char (F)) << 16);
  Reserved |=  (((byte) jfile_read_char (F)) << 24); 
  fprintf(stderr, "Reserved = %d\n", Reserved);
 
  /*DataOffset = 4 octets*/ 
  DataOffset = (byte) jfile_read_char (F);
  DataOffset |=  (((byte) jfile_read_char (F)) << 8);
  DataOffset |=  (((byte) jfile_read_char (F)) << 16);
  DataOffset |=  (((byte) jfile_read_char (F)) << 24); 
  fprintf(stderr, "DataOffset = %d\n", DataOffset);
 
  /*HeaderSize = 4 octects*/
  HeaderSize = (byte) jfile_read_char (F);
  HeaderSize |=  (((byte) jfile_read_char (F)) << 8);
  HeaderSize |=  (((byte) jfile_read_char (F)) << 16);
  HeaderSize |=  (((byte) jfile_read_char (F)) << 24); 
  fprintf(stderr, "HeaderSize = %d\n", HeaderSize);
 
  /*Width = 4 octets*/
  width = (byte) jfile_read_char (F);
  width |=  (((byte) jfile_read_char (F)) << 8);
  width |=  (((byte) jfile_read_char (F)) << 16);
  width |=  (((byte) jfile_read_char (F)) << 24); 
  fprintf(stderr, "width = %d\n", width);  
 
  /*height = 4 octets*/
  height = (byte) jfile_read_char (F);
  height |=  (((byte) jfile_read_char (F)) << 8);
  height |=  (((byte) jfile_read_char (F)) << 16);
  height |=  (((byte) jfile_read_char (F)) << 24); 
  fprintf(stderr, "height = %d\n", height); 
 
  /*Planes = 2 octets*/  
  Planes = (byte) jfile_read_char (F);
  Planes |=  (((byte) jfile_read_char (F)) << 8);
  fprintf(stderr, "Planes = %d\n", Planes);
 
  /*BitsPerPixel = 2 octets*/ 
  BitsPerPixel = (byte) jfile_read_char (F);
  BitsPerPixel |= ((byte) jfile_read_char (F) << 8);
  fprintf(stderr, "BitesPerPixel = %d\n", BitsPerPixel); 
 
  /*Compression (type de compression) = 4 octets*/
  Compression = (byte) jfile_read_char (F);
  Compression |=  (((byte) jfile_read_char (F)) << 8);
  Compression |=  (((byte) jfile_read_char (F)) << 16);
  Compression |=  (((byte) jfile_read_char (F)) << 24); 
  fprintf(stderr, "Compression = %d\n", Compression); 
 
  /*BitmapDataSize (Taille en octets des données de l'image) = 4 octets*/
  BitmapDataSize = (byte) jfile_read_char (F);
  BitmapDataSize |=  (((byte) jfile_read_char (F)) << 8);
  BitmapDataSize |=  (((byte) jfile_read_char (F)) << 16);
  BitmapDataSize |=  (((byte) jfile_read_char (F)) << 24); 
  fprintf(stderr, "BitmapDataSize = %d\n", BitmapDataSize); 
 
  /*HResolution (Résolution horizontale de l'image en pixels par mètre) = 4 octets*/ 
  HResolution = (byte) jfile_read_char (F);
  HResolution |=  (((byte) jfile_read_char (F)) << 8);
  HResolution |=  (((byte) jfile_read_char (F)) << 16);
  HResolution |=  (((byte) jfile_read_char (F)) << 24); 
  fprintf(stderr, "HResolution = %d\n", HResolution); 
 
 
  /*VResolution (Résolution verticale de l'image en pixels par mètre) = 4 octets*/ 
  VResolution = (byte) jfile_read_char (F);
  VResolution |=  (((byte) jfile_read_char (F)) << 8);
  VResolution |=  (((byte) jfile_read_char (F)) << 16);
  VResolution |=  (((byte) jfile_read_char (F)) << 24); 
  fprintf(stderr, "VResolution = %d\n", VResolution); 
 
 
  /*Colours (nombre de couleurs dans l'image) = 4 octets*/
  Colours = (byte) jfile_read_char (F);
  Colours |=  (((byte) jfile_read_char (F)) << 8);
  Colours |=  (((byte) jfile_read_char (F)) << 16);
  Colours |=  (((byte) jfile_read_char (F)) << 24); 
  fprintf(stderr, "Nombre de couleurs dans l'image = %d\n", Colours); 
 
  /*ImportantColours (nombre de couleurs importantes dans l'image) = 4 octets*/
  ImportantColours = (byte) jfile_read_char (F);
  ImportantColours |=  (((byte) jfile_read_char (F)) << 8);
  ImportantColours |=  (((byte) jfile_read_char (F)) << 16);
  ImportantColours |=  (((byte) jfile_read_char (F)) << 24); 
  fprintf(stderr, "Nombre de couleurs importantes dans l'image = %d\n", ImportantColours); 
  fprintf(stderr, "1\n");
 
   I = imageBMP_malloc (width, height); 
   I->w = width;
   I->h = height;
   I->IdentifierB = IdentifierB;
   I->IdentifierM = IdentifierM;
   I->FileSize = FileSize;
   I->Reserved = Reserved;
   I->DataOffset = DataOffset;
   I->HeaderSize = HeaderSize;
   I->Planes = Planes;
   I->BitsPerPixel = BitsPerPixel;
   I->Compression = Compression;
   I->BitmapDataSize = BitmapDataSize;
   I->HResolution = HResolution;
   I->VResolution = VResolution;
   I->Colours = Colours;
   I->ImportantColours = ImportantColours;
 
 
 
    for (i = 0; i < I->w * I->h; i++)
      {
	byte r, g, b;
	b = (byte) jfile_read_char (F);
	g = (byte) jfile_read_char (F);
	r = (byte) jfile_read_char (F);
	I->pixel[i] = make_colour (r, g, b, 0xFF);
      }
        fprintf(stderr, "2\n");
 
  jfile_close (&F);
 
  return I;
}
 
 
void dessiner_disque_pleinBMP (imageBMP I)
{
   int cx, cy, radius;
   colour a;
   printf("\nEntrez le cx du disque : ");
   scanf("%d", &cx);
   printf("\nEntrez le cy du disque : ");
 
   scanf("%d", &cy);
   printf("\nEntrez le rayon du disque : ");
   scanf("%d", &radius);
   printf("\nQuelle couleur désirez-vous pour ce disque (en hexadecimal svp ) : ");
   scanf("%X", &a);
   /*image_draw_fullcircle (image I, int cx, int cy, int radius, colour c)*/
   image_draw_fullcircle (I, cx, cy, radius, a);
 
}
 
 
 
 
int main (int argc, char **argv)
{
 
imageBMP A = image_load_BMP24 ("vague.bmp");
image_save_BMP24 ("vagueBis.bmp", A);
imageBMP_destroy (&A);
 
if (argc < 0)
 
    fprintf (stderr, "%s",argv[-1]);
  return EXIT_SUCCESS;
}