17 #include "../src/commands.h"
18 #include "../src/imageraster.h"
19 #include "../include/grammar.tab.h"
30 Colour* v = (Colour*)malloc(
sizeof(Colour));
45 Dimension* v = (Dimension*)malloc(
sizeof(Dimension));
60 Point* v = (Point*)malloc(
sizeof(Point));
75 Value* v = (Value*)malloc(
sizeof(Value));
95 Command*
newCommand(
int command,Point *pt,Dimension *dim,Colour *col,Value *val,Value *val2 ,Value *str,Value *str2,Command *child)
97 Command *node = (Command*) malloc (
sizeof(Command));
131 VarList*
updateVar(VarList *lst,
char *varName,
int varValue) {
133 lst = (VarList*) malloc(
sizeof(VarList));
135 lst
->var = strdup(varName);
139 if (strcmp(lst
->var, varName) == 0) {
158 fprintf(stderr,
"Variable %s is undefined\n", varName);
161 if (strcmp(lst
->var, varName) == 0) {
176 if (val
->var == NULL) {
191 Image *image = (Image*) malloc(
sizeof(Image));
226 int from = evalValue(image->vars, lst->val);
227 int to = evalValue(image->vars, lst->val2);
228 for (
int var=from; var<=to ;var++) {
229 image->vars = updateVar(image->vars, lst->str->var, var);
230 runCommands(lst->child , image);
237 image->x_size = evalValue(image->vars, &lst->dimension->x);
238 image->y_size = evalValue(image->vars, &lst->dimension->y);
240 if(image->x_size<=0 || image->y_size<=0) {
241 fprintf(stderr,
"X_SIZE or Y_SIZE incorrect value\n");
246 int r = (lst->color == NULL) ? 255 : evalValue(image->vars, &lst->color->r);
247 int g = (lst->color == NULL) ? 255 : evalValue(image->vars, &lst->color->g);
248 int b = (lst->color == NULL) ? 255 : evalValue(image->vars, &lst->color->b);
251 r = (r>255) ? 255 : r;
252 g = (g>255) ? 255 : g;
253 b = (b>255) ? 255 : b;
255 image->img_data = malloc((3 * image->x_size * image->y_size) *
sizeof(
int));
258 imageFill(image->img_data,image->x_size,image->y_size,r,g,b);
266 image->r = evalValue(image->vars, &lst->color->r);
267 image->g = evalValue(image->vars, &lst->color->g);
268 image->b = evalValue(image->vars, &lst->color->b);
271 image->r = (image->r > 255) ? 255 : image->r;
272 image->g = (image->g > 255) ? 255 : image->g;
273 image->b = (image->b > 255) ? 255 : image->b;
281 FILE *fp = fopen(lst->str->var,
"r");
283 fprintf(stderr,
"\nReaded File %s", lst->str->var);
285 readImageHeader(fp,&image->x_size,&image->y_size);
287 image->img_data = malloc((3 * image->x_size * image->y_size) *
sizeof(
int));
289 readImage(fp,image->img_data);
291 fprintf(stderr,
"[OK]\n");
297 FILE *fp = fopen(lst->str->var,
"w");
299 fprintf(stderr,
"\nSaved File %s", lst->str->var);
302 writeImage(fp,image->img_data,lst->str->var, image->x_size,image->y_size);
303 fprintf(stderr,
"[OK]\n");
311 int r = (lst->color == NULL) ? image->r : evalValue(image->vars, &lst->color->r);
312 int g = (lst->color == NULL) ? image->g : evalValue(image->vars, &lst->color->g);
313 int b = (lst->color == NULL) ? image->b : evalValue(image->vars, &lst->color->b);
315 int x = (lst->point == NULL) ? 1 : evalValue(image->vars, &lst->point->x);
316 int y = (lst->point == NULL) ? 1 : evalValue(image->vars, &lst->point->y);
318 drawPoint(image->img_data,image->x_size,image->y_size,x,y,r,g,b);
326 int r = (lst->color == NULL) ? image->r : evalValue(image->vars, &lst->color->r);
327 int g = (lst->color == NULL) ? image->g : evalValue(image->vars, &lst->color->g);
328 int b = (lst->color == NULL) ? image->b : evalValue(image->vars, &lst->color->b);
330 int x1 = (lst->point == NULL) ? 1 : evalValue(image->vars, &lst->point->x);
331 int y1 = (lst->point == NULL) ? 1 : evalValue(image->vars, &lst->point->y);
333 int x2 = (lst->point->next == NULL) ? 1 : evalValue(image->vars, &lst->point->next->x);
334 int y2 = (lst->point->next == NULL) ? 1 : evalValue(image->vars, &lst->point->next->y);
337 drawLine(image->img_data,image->x_size,image->y_size,x1,y1,x2,y2,r,g,b);
346 int r = (lst->color == NULL) ? image->r : evalValue(image->vars, &lst->color->r);
347 int g = (lst->color == NULL) ? image->g : evalValue(image->vars, &lst->color->g);
348 int b = (lst->color == NULL) ? image->b : evalValue(image->vars, &lst->color->b);
351 int x1 = evalValue(image->vars, &lst->point->x);
352 int y1 = evalValue(image->vars, &lst->point->y);
354 int x2 = (lst->dimension == NULL) ?
355 evalValue(image->vars, &lst->point->next->x)
356 : x1 + evalValue(image->vars, &lst->dimension->x) - 1
358 int y2 = (lst->dimension == NULL) ?
359 evalValue(image->vars, &lst->point->next->y)
360 : y1 + evalValue(image->vars, &lst->dimension->y) - 1
365 drawLine(image->img_data,image->x_size,image->y_size,x2,y1,x1,y1,r,g,b);
368 drawLine(image->img_data,image->x_size,image->y_size,x2,y2,x2,y1,r,g,b);
371 drawLine(image->img_data,image->x_size,image->y_size,x2,y2,x1,y2,r,g,b);
374 drawLine(image->img_data,image->x_size,image->y_size,x1,y2,x1,y1,r,g,b);
382 int r = (lst->color == NULL) ? image->r : evalValue(image->vars, &lst->color->r);
383 int g = (lst->color == NULL) ? image->g : evalValue(image->vars, &lst->color->g);
384 int b = (lst->color == NULL) ? image->b : evalValue(image->vars, &lst->color->b);
386 int x1 = evalValue(image->vars, &lst->point->x);
387 int y1 = evalValue(image->vars, &lst->point->y);
389 int x2 = (lst->dimension == NULL) ?
390 evalValue(image->vars, &lst->point->next->x)
391 : x1 + evalValue(image->vars, &lst->dimension->x) -1
393 int y2 = (lst->dimension == NULL) ?
394 evalValue(image->vars, &lst->point->next->y)
395 : y1 + evalValue(image->vars, &lst->dimension->y)-1
398 for (
int i=y1; i<= y2; i++) {
400 drawLine(image->img_data,image->x_size,image->y_size,x1,i,x2,i,r,g,b);
409 int r = (lst->color == NULL) ? image->r : evalValue(image->vars, &lst->color->r);
410 int g = (lst->color == NULL) ? image->g : evalValue(image->vars, &lst->color->g);
411 int b = (lst->color == NULL) ? image->b : evalValue(image->vars, &lst->color->b);
413 while (lst->point->next != NULL) {
414 int x1 = (lst->point == NULL) ? 1 : evalValue(image->vars, &lst->point->x);
415 int y1 = (lst->point == NULL) ? 1 : evalValue(image->vars, &lst->point->y);
417 int x2 = (lst->point->next == NULL) ? 1 : evalValue(image->vars, &lst->point->next->x);
418 int y2 = (lst->point->next == NULL) ? 1 : evalValue(image->vars, &lst->point->next->y);
420 drawLine(image->img_data,image->x_size,image->y_size,x1,y1,x2,y2,r,g,b);
424 lst->point = lst->point->next;
432 int r = (lst->color == NULL) ? image->r : evalValue(image->vars, &lst->color->r);
433 int g = (lst->color == NULL) ? image->g : evalValue(image->vars, &lst->color->g);
434 int b = (lst->color == NULL) ? image->b : evalValue(image->vars, &lst->color->b);
436 int x = (lst->point == NULL) ? 1 : evalValue(image->vars, &lst->point->x);
437 int y = (lst->point == NULL) ? 1 : evalValue(image->vars, &lst->point->y);
439 int raio = (lst->val == NULL) ? 1 : evalValue(image->vars, lst->val);
441 drawCircle(image->img_data,image->x_size,image->y_size, raio,x,y,r,g,b);
448 for (
int i = 0; i< (image->x_size*image->y_size * 3); i++) {
449 image->img_data[i] = 255 - image->img_data[i];
461 int r = (lst->color == NULL) ? image->r : evalValue(image->vars, &lst->color->r);
462 int g = (lst->color == NULL) ? image->g : evalValue(image->vars, &lst->color->g);
463 int b = (lst->color == NULL) ? image->b : evalValue(image->vars, &lst->color->b);
465 int x = (lst->point == NULL) ? 1 : evalValue(image->vars, &lst->point->x);
466 int y = (lst->point == NULL) ? 1 : evalValue(image->vars, &lst->point->y);
468 int raio = (lst->val == NULL) ? 1 : evalValue(image->vars, lst->val);
470 for (
int i=raio; i>0 ; i--) {
471 drawCircle(image->img_data,image->x_size,image->y_size, i,x,y,r,g,b);
481 int value = evalValue(image->vars, lst->val);
483 image->vars = updateVar(image->vars, lst->str->var, value);
489 int value = evalValue(image->vars, lst->val);
490 image->vars = updateVar(image->vars, lst->str->var, gera_random(0,value));
507 return rand() % max + min;