-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImage.c
executable file
·300 lines (259 loc) · 6.78 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
/*====================================================*\
Arash HABIBI, Antoine DUMOULIN
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);
}
// test si deux couleurs sont égales
bool C_eq(Color ca, Color cb)
{
return (ca._blue==cb._blue && ca._green==cb._green && ca._red==cb._red);
}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
//------------------------------------------------------------------------
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;
}
//-----
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_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;
}
//-----
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_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();
}
//------------------------------------------------------------------------
/*
* Fonction du tracé de Bresenham comme vu en TD
*/
void Z2to1octant (int xA, int yA, int xB, int yB,
int* xA10, int* yA10, int* xB10, int* yB10)
{
int xAo, xBo, yAo, yBo;
if (xB > xA) {
xAo = xA;
xBo = xB;
} else {
xAo = -xA;
xBo = -xB;
}
if (yB > yA) {
yAo = yA;
yBo = yB;
} else {
yAo = -yA;
yBo = -yB;
}
if (abs(xB-xA) < abs(yB-yA)) {
*xA10 = yAo;
*xB10 = yBo;
*yA10 = xAo;
*yB10 = xBo;
} else {
*xA10 = xAo;
*xB10 = xBo;
*yA10 = yAo;
*yB10 = yBo;
}
}
void octanttoZ2 (int xA, int yA, int xB, int yB,
int a, int b,
int* xZ, int* yZ)
{
int x, y;
x = abs(a);
y = abs(b);
if (abs(xB-xA) < abs(yB-yA)) {
*xZ = y;
*yZ = x;
} else {
*xZ = x;
*yZ = y;
}
}
void I_bresenham(Image *img, int xA, int yA, int xB, int yB, Color c)
{
int xA10, xB10, yA10, yB10;
Z2to1octant (xA, yA, xB, yB, &xA10, &yA10, &xB10, &yB10);
int dx = xB10-xA10;
int dy = yB10-yA10;
int incr1 = 2 * dy;
int incr2 = 2 * (dy-dx);
int d = 2*dy-dx;
int aZ = 0, bZ = 0;
int b = yA10;
int a;
for (a = xA10; a < xB10; a++)
{
octanttoZ2 (xA, yA, xB, yB, a, b, &aZ, &bZ);
I_plotColor(img, aZ, bZ, c);
if (d < 0) {
d += incr1;
} else {
b++;
d += incr2;
}
}
octanttoZ2 (xA, yA, xB, yB, a, b, &aZ, &bZ);
I_plotColor(img, aZ, bZ, c);
}
/*
* Remplissage scanline
*/
void I_fill_scan_line(Image* img, Polygone* poly, Color cline)
{
Liste* liste = NULL;
bool alterne = true;
bool tangent = false;
// parcours en hauteur de l'écran
for(int y=0; y<img->_height; y++)
{
// parcours en largeur de l'écran
for (int x=0; x<img->_width; x++)
{
if (C_eq(img->_buffer[x][y], cline))
liste = liste_add(liste, x, y);
}
alterne = true;
while (liste!=NULL && liste != liste->last)
{
tangent = false;
// on regarde si le point est tangent, si oui on alterne pas
if (P_is_tangent(poly, liste->x, y))
tangent = true;
// tant que le prochain point de la liste est adjacent en X on parcours la liste
while (liste->last != liste && liste->x == liste->last->x + 1)
{
// on vérifie encore si un des points parcouru est tangent
if (P_is_tangent(poly, liste->x, y))
tangent = true;
Liste* tmp = liste_last(liste);
liste_rm(liste);
liste = tmp;
}
if (tangent) alterne = !alterne; // on réinverse alterne car le un point est tangent
// une fois sur deux on va remplir du point courant jusqu'au prochain de la liste
if (liste != NULL && liste->last != liste && alterne) {
for (int i=liste->x-1; i > liste->last->x; i--)
I_plotColor(img, i, y, cline);
}
liste = liste_rm(liste);
alterne = !alterne;
}
}
liste_destruct(liste);
}