-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image.c
535 lines (475 loc) · 11.1 KB
/
Image.c
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*====================================================*\
Vendredi 8 novembre 2013
Arash HABIBI
Image.c
\*====================================================*/
#include "Image.h"
//------------------------------------------------------------------------
Color C_new(float red, float green, float blue)
{
Color c;
c._red = red;
c._green = green;
c._blue = blue;
return c;
}
//------------------------------------------------------------------------
void C_check(Color c, char *message)
{
fprintf(stderr,"%s : %f %f %f\n",message,c._red,c._green,c._blue);
}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
//------------------------------------------------------------------------
Image* I_new(int width, int height)
{
Image *img_new = (Image*)malloc(sizeof(Image));
img_new->_width = width;
img_new->_height = height;
img_new->_xzoom = 0;
img_new->_yzoom = 0;
img_new->_zoom = 1.0;
img_new->_xoffset=0;
img_new->_yoffset=0;
img_new->_current_color = C_new(255,255,255);
img_new->_buffer = (Color**)calloc(width,sizeof(Color*));
int x;
for(x=0;x<width;x++)
img_new->_buffer[x] = (Color*)calloc(height,sizeof(Color));
return img_new;
}
//------------------------------------------------------------------------
static void _plot(Image *img, int x, int y, Color c)
{
img->_buffer[x][y] = c;
}
//-----
static int _isPpm(char *imagefilename)
{
FILE *imagefile;
imagefile = fopen(imagefilename,"r");
if(imagefile==NULL) {perror(imagefilename); exit(1); }
else
{
int c1 = fgetc(imagefile);
int c2 = fgetc(imagefile);
fclose(imagefile);
if((c1=='P')&&(c2=='6')) return 1;
else return 0;
}
}
//-----
Image* I_read(char *imagefilename)
{
Image *img;
char command[100];
if(_isPpm(imagefilename)) sprintf(command,"cp %s input.ppm",imagefilename);
else sprintf(command,"convert %s input.ppm",imagefilename);
int stat = system(command);
if(stat!=0)
{
fprintf(stderr,"Convert : %s -> input.ppm impossible conversion.\n", imagefilename);
exit(1);
}
else
{
Ppm ppm = PPM_nouv("input.ppm", PPM_LECTURE);
system("rm input.ppm");
fprintf(stderr,"%d x %d\n",PPM_largeur(ppm),PPM_hauteur(ppm));
if(ppm!=NULL)
{
img = I_new(PPM_largeur(ppm),PPM_hauteur(ppm));
int nb_bits=ppm->_nb_bits;
int valmax = ppm->_valmax;
int nb_pixels = img->_width*img->_height;
if(nb_bits <= 8)
{
unsigned char *donnees = (unsigned char*)calloc(3*nb_pixels,sizeof(unsigned char));
PPM_lectureDonneesChar(ppm, donnees);
int x,y;
for(y=0;y<img->_height;y++)
for(x=0;x<img->_width;x++)
{
int indice = (img->_height-y)*img->_width + x;
Color c = C_new((1.0*donnees[3*indice ])/valmax,
(1.0*donnees[3*indice+1])/valmax,
(1.0*donnees[3*indice+2])/valmax);
_plot(img,x,y,c);
}
}
else
{
unsigned short *donnees = (unsigned short*)calloc(3*nb_pixels,sizeof(unsigned short));
PPM_lectureDonneesShort(ppm, donnees);
int x,y;
for(y=0;y<img->_height;y++)
for(x=0;x<img->_width;x++)
{
int indice = (img->_height-y)*img->_width + x;
Color c = C_new((1.0*donnees[3*indice ])/valmax,
(1.0*donnees[3*indice+1])/valmax,
(1.0*donnees[3*indice+2])/valmax);
img->_buffer[x][y] = c;
}
}
PPM_fermeture(ppm);
return(img);
}
else
return(NULL);
}
}
//------------------------------------------------------------------------
void I_fill(Image *img, Color c)
{
int x,y;
for(x=0;x<img->_width;x++)
for(y=0;y<img->_height;y++)
img->_buffer[x][y]=c;
}
//------------------------------------------------------------------------
void I_checker(Image *img, Color c1, Color c2, int step)
{
int x,y;
for(x=0;x<img->_width;x++)
for(y=0;y<img->_height;y++)
{
int n_x = x/step;
int n_y = y/step;
if((n_x+n_y)%2==0) _plot(img,x,y,c1);
else _plot(img,x,y,c2);
}
}
//------------------------------------------------------------------------
void I_changeColor(Image *img, Color c)
{
img->_current_color = c;
}
//------------------------------------------------------------------------
void I_plot(Image *img, int x, int y)
{
if((x>=0)&&(x<img->_width)&&
(y>=0)&&(y<img->_height))
img->_buffer[x][y] = img->_current_color;
else
{
fprintf(stderr,"I_plot : ERROR !!!\n");
fprintf(stderr,"x (=%d) must be in the [%d,%d] range and\n", x, 0, img->_width);
fprintf(stderr,"y (=%d) must be in the [%d,%d] range\n", y, 0, img->_height);
}
}
//------------------------------------------------------------------------
void I_plotColor(Image *img, int x, int y, Color c)
{
if((x>=0)&&(x<img->_width)&&
(y>=0)&&(y<img->_height))
img->_buffer[x][y] = c;
else
{
fprintf(stderr,"I_plotColor : ERROR !!!\n");
fprintf(stderr,"x (=%d) must be in the [%d,%d] range and\n", x, 0, img->_width);
fprintf(stderr,"y (=%d) must be in the [%d,%d] range\n", y, 0, img->_height);
}
}
//------------------------------------------------------------------------
// Changement de repère
static void _windowToImage(Image *img, int xwin, int ywin, int *ximg, int *yimg)
{
*ximg = img->_xoffset + img->_xzoom + (xwin-img->_xzoom) / img->_zoom;
*yimg = img->_yoffset + img->_yzoom + (ywin-img->_yzoom) / img->_zoom;
}
//-----
// Changement de repère inverse
/*
static void _imageToWindow(Image *img, int ximg, int yimg, int *xwin, int *ywin)
{
*xwin = img->_xoffset + img->_xzoom + (ximg-img->_xzoom-img->_xoffset) * img->_zoom;
*ywin = img->_yoffset + img->_yzoom + (yimg-img->_yzoom-img->_yoffset) * img->_zoom;
}
*/
//-----
void I_focusPoint(Image *img, int xwin, int ywin)
{
int dx = xwin - img->_xzoom;
int dy = ywin - img->_yzoom;
img->_xoffset -= dx*(1-1.0/img->_zoom);
img->_yoffset -= dy*(1-1.0/img->_zoom);
img->_xzoom = xwin;
img->_yzoom = ywin;
}
//------------------------------------------------------------------------
void I_zoomInit(Image *img)
{
img->_xoffset = 0;
img->_yoffset = 0;
img->_zoom = 1.0;
}
//------------------------------------------------------------------------
void I_zoom(Image *img, double zoom_coef)
{
img->_zoom = img->_zoom * zoom_coef;
}
//------------------------------------------------------------------------
void I_move(Image *img, int x, int y)
{
img->_xoffset += x;
img->_yoffset += y;
}
//------------------------------------------------------------------------
void I_bresenhamOrigin (Image * img , int x , int y)
{
if ((y > 0) && (x > 0))
{
glColor3f (1 , 1 , 1) ; // Couleur blanche.
int deltaX = x ;
int deltaY = y ;
int incrd1 = 2 * deltaY ;
int incrd2 = 2 * (deltaY - deltaX) ;
int i = 0 , j = 0 ;
int critere = (2 * deltaY) - deltaX ;
while (i < x)
{
I_plot (img , i , j) ; // Afficher.
i ++ ;
if (critere >= 0)
{
j ++ ;
critere += incrd2 ;
}
else
{
critere += incrd1 ;
}
}
I_plot (img , i , j) ;
}
}
void I_bresenham (Image * img , int xA , int yA , int xB , int yB)
{
int deltaX = xB - xA ;
int deltaY = yB - yA ;
int incrd1 = 2 * deltaY ;
int incrd2 = 2 * (deltaY - deltaX) ;
int critere = (2 * deltaY) - deltaX ;
int i = xA , j = yA ;
glColor3f (1 , 1 , 1) ; // Couleur blanche.
if (deltaX > 0) // Moitie droite.
{
if (deltaY > 0) // Quart en haut a droite.
{
if (deltaX >= deltaY) // Premier octant.
{
// printf ("Premier octant.\n") ;
while (i < xB)
{
I_plot (img , i , j) ; // Afficher.
if (critere >= 0)
{
j ++ ;
critere += incrd2 ;
}
else
{
critere += incrd1 ;
}
i ++ ;
}
I_plot (img , i , j) ;
}
else if (deltaX < deltaY) // Deuxieme octant.
{
// printf ("Deuxieme octant.\n") ;
incrd1 = 2 * deltaX ;
incrd2 = 2 * (deltaX - deltaY) ;
critere = (2 * deltaX) - deltaY ;
while (j < yB)
{
I_plot (img , i , j) ; // Afficher.
if (critere >= 0)
{
i ++ ;
critere += incrd2 ;
}
else
{
critere += incrd1 ;
}
j ++ ;
}
I_plot (img , i , j) ;
}
}
else if (deltaY < 0) // Quart en bas a droite.
{
if (deltaX < -deltaY) // Septieme octant.
{
// printf ("Septieme octant.\n") ;
critere = deltaY ;
incrd2 = 2 * deltaX ;
while (j > yB)
{
I_plot (img , i , j) ;
j -- ;
critere += incrd2 ;
if (critere > 0)
{
i ++ ;
critere += incrd1 ;
}
}
}
else if (deltaX >= -deltaY) // Huitieme octant.
{
// printf ("Huitieme octant.\n") ;
critere = deltaX ;
incrd2 = 2 * deltaX ;
while (i < xB)
{
I_plot (img , i , j) ;
i ++ ;
critere += incrd1 ;
if (critere < 0)
{
j -- ;
critere += incrd2 ;
}
}
}
}
}
else if (deltaX < 0) // Moitie gauche.
{
if (deltaY > 0) // Quart en haut a gauche.
{
if (-deltaX < deltaY) // Troisieme octant.
{
// printf ("Troisieme octant.\n") ;
critere = deltaY ;
incrd2 = 2 * deltaX ;
while (j < yB)
{
I_plot (img , i , j) ;
j ++ ;
critere += incrd2 ;
if (critere <= 0)
{
i -- ;
critere += incrd1 ;
}
}
}
else if (-deltaX >= deltaY) // Quatrieme octant.
{
// printf ("Quatrieme octant.\n") ;
critere = deltaX ;
incrd2 = 2 * deltaX ;
while (i > xB)
{
I_plot (img , i , j) ;
i -- ;
critere += incrd1 ;
if (critere >= 0)
{
j ++ ;
critere += incrd2 ;
}
}
}
}
else if (deltaY < 0) // Quart en bas a gauche.
{
if (deltaX <= deltaY) // Cinquime octant.
{
// printf ("Cinquime octant.\n") ;
critere = deltaX ;
incrd2 = 2 * deltaX ;
while (i > xB)
{
I_plot (img , i , j) ;
i -- ;
critere -= incrd1 ;
if (critere >= 0)
{
j -- ;
critere += incrd2 ;
}
}
}
else if (deltaX > deltaY) // Sixieme octant.
{
// printf ("Sixieme octant.\n") ;
critere = deltaY ;
incrd2 = 2 * deltaX ;
while (j > yB)
{
I_plot (img , i , j) ;
j -- ;
critere -= incrd2 ;
if (critere >= 0)
{
i -- ;
critere += incrd1 ;
}
}
}
}
}
else if (deltaY == 0) // A l'horizontale.
{
if (deltaX >= 0) // Vers la droite.
{
while (i < xB)
{
I_plot (img , i , j) ;
i ++ ;
}
}
else if (deltaX < 0) // Vers la gauche.
{
while (i > xB)
{
I_plot (img , i , j) ;
i -- ;
}
}
}
else if (deltaX == 0) // A la verticale.
{
if (deltaY >= 0) // Vers le haut.
{
while (j < yB)
{
I_plot (img , i , j) ;
j ++ ;
}
}
else if (deltaY < 0) // Vers le bas.
{
while (j > yB)
{
I_plot (img , i , j) ;
j -- ;
}
}
}
}
void I_draw(Image *img)
{
glBegin(GL_POINTS);
int xwin, ywin, ximg, yimg;
for(xwin=0;xwin<img->_width;xwin++)
for(ywin=0;ywin<img->_height;ywin++)
{
_windowToImage(img, xwin, ywin, &ximg, &yimg);
Color c;
if((ximg>=0)&&(ximg<img->_width)&&
(yimg>=0)&&(yimg<img->_height))
c = img->_buffer[ximg][yimg];
else
c = C_new(0,0,0);
glColor3f(c._red,c._green,c._blue);
glVertex2i(xwin,ywin);
}
glEnd();
}
//------------------------------------------------------------------------