-
Notifications
You must be signed in to change notification settings - Fork 0
/
executor.c
700 lines (590 loc) · 14.5 KB
/
executor.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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
#include "executor.h"
int exec_command(char **args){
int nbr_fct_intern = funcs_num();
int i;
for (i=0;i<nbr_fct_intern; i++) {
if(strcmp(args[0], func_names[i]) == 0) {
return (*funcs[i])(args);
}
}
return error(args[0]);
}
int cimp_exit(char **args){
exit(0);
}
int cimp_open(char **args){
//call windows_handler open new window
open_new_window();
printf("New window opened!\n");
return 1;
}
int cimp_load(char **args){
char *filename = strdup(args[1]);
char *imgName=load_image(filename);
if(imgName==NULL){
printf("Error image does not exist!\n");
return 0;
}
//If it's successfull :
printf("Image %s successfully loaded with name : %s!\n",filename,imgName);
return 1;
}
int cimp_transfer(char **args){
char *filename = strdup(args[3]);
//Load /path/to/moh.jpg
int windowId = atoi(args[1]) -1 ;
//Call the related function in the window handler with the two args (filename,Windowid)
int res = transfer_loaded_image(windowId,filename);
//If the image isn't loaded
if(res==1){
printf("The image %s is not loaded in memory! Please use the load command before!\n",filename);
return 0;
}
//If the windowId is wrong
else if(res==2){
printf("Window with ID : %d does not exist!\n",windowId);
return 0;
}
else if(res==3){
printf("An error occured while processing the command!\n");
return 0;
}
//If everything is correct
else{
printf("Image %s transfered with success in Window : %d !\n",filename, windowId);
return 1;
}
}
int cimp_save(char **args){
int windowId = atoi(args[1]) -1 ;
SDL_Surface *image = get_current_window_surface(windowId);
if(image==NULL){
printf("Wrong window ID!\n");
return 0;
}
//Si save fenetre blank
char *filename;
if(!args[2]){
filename = get_image_path_by_window(windowId);
printf("filename : %s\n",filename);
}
else
filename = args[2];
char *format = strrchr(filename,'.');
if(strcmp(format+1,"jpg")==0 || strcmp(format+1,"jpeg")==0){
save_jpg_format(image,filename);
printf("Image saved succesfully!(jpg)\n");
return 1;
}
if(strcmp(format+1,"png")==0){
save_png_format(image,filename);
printf("Image saved succesfully!(png)\n");
return 1;
}
if(strcmp(format+1,"bmp")==0){
save_bmp_format(image,filename);
printf("Image saved succesfully!(bmp)\n");
return 1;
}
return 1;
}
int cimp_select(char **args){
SDL_Point p1,p2;
int windowId = atoi(args[2])-1;
SDL_Surface *image=get_current_window_surface(windowId);
if(image){
if(strcmp(args[1],"-c")==0){
extractCoords(args[3],&p1.x,&p1.y);
extractCoords(args[4],&p2.x,&p2.y);
if(select_rect(windowId,p1,p2)==0){
return 1;
}
else{
printf("Coords out of bound!\n");
return 0;
}
}
if(strcmp(args[1],"-a")==0){
p1.x = 1;
p1.y = 1;
p2.x = image->w-1;
p2.y = image->h-1;
if(select_rect(windowId,p1,p2)==0){
return 1;
}
else{
printf("Coords out of bound!\n");
return 0;
}
}
if(strcmp(args[1],"-f")==0){
select_f(windowId);
return 1;
}
}
printf("Wrong window Id\n");
return 0;
}
int cimp_close(char **args){
int windowId = atoi(args[1]) -1 ;
if(!close_window(windowId)){
printf("Wrong window ID!\n");
return 0;
}
printf("fermé : %d\n",close_window(windowId));
return 1;
}
int cimp_grayscale(char **args){
int windowId = atoi(args[1]) -1 ;
SDL_Surface *surface = get_current_window_surface(windowId);
SDL_Point p1,p2;
if(surface){
if(args[2]){
extractCoords(args[2],&p1.x,&p1.y);//
extractCoords(args[3],&p2.x,&p2.y);
}
else{
p1.x = 1;
p1.y = 1;
p2.x = surface->w-1;
p2.y=surface->h-1;
}
if(select_rect(windowId,p1,p2)==0){
GrayScale(surface);
refresh_window(windowId);
return 1;
}
else {
printf("Coords Out of bound!\n");
return 0;
}
}
printf("Wrong window ID");
return 0;
}
int cimp_blackwhite(char **args){
int windowId = atoi(args[1]) -1 ;
SDL_Surface *surface = get_current_window_surface(windowId);
SDL_Point p1,p2;
if(surface){
if(args[2]){
extractCoords(args[2],&p1.x,&p1.y);//
extractCoords(args[3],&p2.x,&p2.y);
}
else{
p1.x = 1;
p1.y = 1;
p2.x = surface->w-1;
p2.y=surface->h-1;
}
if(select_rect(windowId,p1,p2)==0){
BlackWhite(surface);
refresh_window(windowId);
return 1;
}
else {
printf("Coords Out of bound!\n");
return 0;
}
}
printf("Wrong window ID");
return 0;
}
int cimp_negative(char **args){
int windowId = atoi(args[1]) -1 ;
SDL_Surface *surface = get_current_window_surface(windowId);
SDL_Point p1,p2;
if(surface){
if(args[2]){
extractCoords(args[2],&p1.x,&p1.y);//
extractCoords(args[3],&p2.x,&p2.y);
}
else{
p1.x = 1;
p1.y = 1;
p2.x = surface->w-1;
p2.y=surface->h-1;
}
if(select_rect(windowId,p1,p2)==0){
Negative(surface);
refresh_window(windowId);
return 1;
}
else {
printf("Coords Out of bound!\n");
return 0;
}
}
printf("Wrong window ID");
return 0;
}
int cimp_alpha(char **args){
int windowId = atoi(args[1]) -1 ;
SDL_Surface *surface = get_current_window_surface(windowId);
SDL_Point p1,p2;
int alphaValue=0;
if(surface){
if(args[2] && args[3]){
extractCoords(args[2],&p1.x,&p1.y);
extractCoords(args[3],&p2.x,&p2.y);
alphaValue = atoi(args[4]);
}
else if (args[2] && ! args[3]){
p1.x = 1;
p1.y = 1;
p2.x = surface->w-1;
p2.y=surface->h-1;
alphaValue = atoi(args[2]);
}
if(select_rect(windowId,p1,p2)==0){
alpha(surface,alphaValue);
refresh_window(windowId);
return 1;
}
else {
printf("Coords Out of bound!\n");
return 0;
}
}
printf("Wrong ID");
return 0;
}
int cimp_crop(char **args){
int windowId = atoi(args[1]) -1 ;
SDL_Surface *image = get_current_window_surface(windowId);
if(image==NULL){
printf("Wrong window ID!\n");
return 0;
}
int height = atoi(args[2]);
int width = atoi(args[3]);
SDL_Rect rect = {0,0,height,width};
image=crop_image(image,rect);
edit(windowId,image);
refresh_window(windowId);
return 1;
}
int cimp_zoom(char **args){
int windowId = atoi(args[1]) -1 ;
SDL_Surface *image = get_current_window_surface(windowId);
if(image==NULL){
printf("Wrong window ID!\n");
return 0;
}
int height = atoi(args[2]);
int width = atoi(args[3]);
SDL_Rect rect = {0,0,height,width};
image=zoom_image(image,rect);
edit(windowId,image);
refresh_window(windowId);
return 1;
}
int cimp_brightness(char **args){
int windowId = atoi(args[1]) -1 ;
SDL_Surface *surface = get_current_window_surface(windowId);
SDL_Point p1,p2;
int brightnessValue=0;
if(surface){
if(args[2] && args[3]){
extractCoords(args[2],&p1.x,&p1.y);
extractCoords(args[3],&p2.x,&p2.y);
brightnessValue = atoi(args[4]);
}
else if (args[2] && ! args[3]){
p1.x = 1;
p1.y = 1;
p2.x = surface->w-1;
p2.y=surface->h-1;
brightnessValue = atoi(args[2]);
}
if(select_rect(windowId,p1,p2)==0){
brightness(surface,brightnessValue);
refresh_window(windowId);
return 1;
}
else {
printf("Coords Out of bound!\n");
return 0;
}
}
printf("Wrong ID");
return 0;
}
int cimp_fillrect(char **args){
int windowId = atoi(args[1]) -1 ;
SDL_Color color;
SDL_Point p1,p2;
extractRGB(args[2], &color.r, &color.g, &color.b);
SDL_Surface *surface = get_current_window_surface(windowId);
if(surface){
if(args[3]){
extractCoords(args[3],&p1.x,&p1.y);
extractCoords(args[4],&p2.x,&p2.y);
}
else{
p1.x = 1;
p1.y = 1;
p2.x = surface->w -1;
p2.y = surface->h -1 ;
}
select_rect(windowId,p1,p2);
FillRect(surface,color);
refresh_window(windowId);
return 1;
}
printf("Wrong ID");
return 0;
}
int cimp_rotate(char **args){
// int windowId = atoi(args[1]) -1 ;
// int angle = atoi(args[2]);
//SDL_Renderer *ren = getRenderer(windowId);
//SDL_Texture *tex = getTex(windowId);
// rotation(ren,tex,angle)
return 1;
}
int cimp_vertical(char **args){
int windowId = atoi(args[1]) -1;
SDL_Surface *surface = get_current_window_surface(windowId);
if(surface){
Flip_Vertical(surface);
refresh_window(windowId);
return 1;
}
printf("Wrong ID");
return 0;
}
int cimp_horizontal(char **args){
int windowId = atoi(args[1]) -1 ;
SDL_Surface *surface= get_current_window_surface(windowId);
if(surface){
Flip_Horizontal(surface);
refresh_window(windowId);
return 1;
}
printf("Wrong ID");
return 0;
}
int cimp_resize(char **args){
int windowId = atoi(args[1]) -1 ;
SDL_Surface *image = get_current_window_surface(windowId);
if(image==NULL){
printf("Wrong window ID!\n");
return 0;
}
int height = atoi(args[2]);
int width = atoi(args[3]);
SDL_Rect rect = {0,0,height,width};
image=resize_image(image,rect);
edit(windowId,image);
refresh_window(windowId);
return 1;
}
int cimp_replace(char **args){
//replace {specify window} p1 p2 (r,g,b) (r,g,b) d
int windowId = atoi(args[1]) -1 ;
SDL_Surface *surface = get_current_window_surface(windowId);
SDL_Color color1;
SDL_Color color2;
SDL_Point p1,p2;
int difference;
if(surface){
if(args[5] && args[6]){
extractCoords(args[2],&p1.x,&p1.y);
extractCoords(args[3],&p2.x,&p2.y);
extractRGB(args[4], &color1.r, &color1.g, &color1.b);
extractRGB(args[5], &color2.r, &color2.g, &color2.b);
difference= atoi(args[6]);
}
else if(!args[5]){
p1.x = 1;
p1.y = 1;
p2.x = surface->w-1;
p2.y=surface->h-1;
extractRGB(args[2], &color1.r, &color1.g, &color1.b);
extractRGB(args[3], &color2.r, &color2.g, &color2.b);
difference = atoi(args[4]);
}
if(select_rect(windowId,p1,p2)==0){
replace_color(surface,color1,color2,difference);
refresh_window(windowId);
return 1;
}
else{
printf("Coords Out of bound!\n");
return 0;
}
}
printf("Wrong window id\n!");
return 0;
}
int cimp_copy(char **args){
if(!args[1]){
copy(NULL);
return 1;
}
SDL_Point p1,p2;
int windowId = atoi(args[2])-1;
SDL_Surface *image=get_current_window_surface(windowId);
if(image){
if(strcmp(args[1],"-c")==0){
extractCoords(args[3],&p1.x,&p1.y);
extractCoords(args[4],&p2.x,&p2.y);
if(select_rect(windowId,p1,p2)==0){
copy(image);
return 1;
}
else{
printf("Coords out of bound!\n");
return 0;
}
}
if(strcmp(args[1],"-a")==0){
p1.x = 1;
p1.y = 1;
p2.x = image->w-1;
p2.y = image->h-1;
if(select_rect(windowId,p1,p2)==0){
copy(image);
return 1;
}
else{
printf("Coords out of bound!\n");
return 0;
}
}
if(strcmp(args[1],"-f")==0){
//Free Select
return 1;
}
}
printf("Wrong window Id\n");
return 0;
}
int cimp_cut(char **args){
if(!args[1]){
cut(NULL);
refresh_window(selected_win_id);
return 1;
}
SDL_Point p1,p2;
int windowId = atoi(args[2])-1;
SDL_Surface *image=get_current_window_surface(windowId);
if(image){
if(strcmp(args[1],"-c")==0){
extractCoords(args[3],&p1.x,&p1.y);
extractCoords(args[4],&p2.x,&p2.y);
if(select_rect(windowId,p1,p2)==0){
cut(image);
refresh_window(windowId);
return 1;
}
else{
printf("Coords out of bound!\n");
return 0;
}
}
if(strcmp(args[1],"-a")==0){
p1.x = 1;
p1.y = 1;
p2.x = image->w-1;
p2.y = image->h-1;
if(select_rect(windowId,p1,p2)==0){
cut(image);
refresh_window(windowId);
return 1;
}
else{
printf("Coords out of bound!\n");
return 0;
}
}
if(strcmp(args[1],"-f")==0){
//Free Select
return 1;
}
}
printf("Wrong window Id\n");
return 0;
}
int cimp_paste(char **args){
int windowId = atoi(args[1]) -1;
SDL_Point p;
if(args[2])
extractCoords(args[2],&p.x,&p.y);
else {
p.x=0;
p.y=0;
}
if(get_current_window_surface(windowId)){
paste(windowId,p);
refresh_window(windowId);
return 1;
}
printf("Wrong id!\n");
return 0;
}
int cimp_delete(char **args){
if(!args[1]){
delete(NULL);
refresh_window(selected_win_id);
return 1;
}
else {
int windowId = atoi(args[1]) -1;
SDL_Surface *image = get_current_window_surface(windowId);
SDL_Point p1,p2;
if(image){
extractCoords(args[2],&p1.x,&p1.y);
extractCoords(args[3],&p2.x,&p2.y);
if(select_rect(windowId,p1,p2)==0){
delete(image);
refresh_window(windowId);
return 1;
}
else{
printf("Coords out of bound!\n");
return 0;
}
}
printf("Wrong window ID!\n");
return 0;
}
}
int cimp_help(char **args){
printf("\n\nCIMP EDITON v1.0, all these commands a defined internally.\n\n");
printf("load /path/to/Image \t\t Loads the Image in the buffer\n");
printf("open \t\t Opens a new empty Window.\n");
printf("transfer WindowNumber -img ImageName \t\t Transfers an image from the buffer into the window with index : WindowNumber, the imageName is given by load command upon loading\n");
printf("save WindowNumber [path/toImage.(jpg,jpeg,png,bmp)] \t\t Saves the image in the WindowNumber'th window, ");
printf("if path is specified, it creates a new image with the specified name and format. If not, it modifies the original image\n");
printf("close windowNumber \t\t Closes the window with id : windownumber\n");
printf("exit \t\t closes the program\n");
printf("grayscale/negative/blackwhite windowNumber [(x1,y1) (x2,y2)] \t\t ");
printf("Applies grayscale,negative or blackwhite effect on the image in window : Windownumber, if no coords are specified, it's applied on the whole image ");
printf("Otherwise,it applies on the rectangle between the 2 specified points\n");
printf("fillrect windowNumber (R,G,B) [(x1,y1) (x2,y2)] \t\t Fills the whole image with the (R,G,B) color, if points are specified, fills the rectangle between those points\n");
printf("resize/crop/zoom windowNumber height width \t\t Resizes, crop or zoom the image in the window : WindowNumber according to the specified height and width\n");
printf("select -c/-a/-f windowNumber \t\t -c windowNumber (x1,y1) (x2,y2) selects the rectanble between the two specified points\n");
printf("\t\t\t\t\t-a select the whole image\n");
printf("\t\t\t\t\t-f free selection with mouse\n");
printf("copy/cut [-[c/a/f] windowNumber] \t\t Copies/Cuts the current selection. -c/-a/-f options acts the same as select\n");
printf("horizontal/vertical windowNumber \t\t Applies a horizontal/vertical flip on the image\n");
printf("alpha/brightness windowNumber value \t\t Changes the alpha/brightness to value\n");
printf("delete [windowNumber (x1,y1) (x2,y2)]\t\t Deletes the current selection. If coords are specified, deletes the rectangle between those points\n");
return 1;
}
void extractCoords(char *coords, int *x, int *y){
char *token = strtok(coords,",");
*x = atoi(token+1);
token = strtok(NULL, ",");
*y = atoi(token);
}
void extractRGB(char *coords, Uint8 *r, Uint8 *g, Uint8 *b){
char *token = strtok(coords,",");
*r = atoi(token+1);
token = strtok(NULL, ",");
*g = atoi(token);
token = strtok(NULL,",");
*b = atoi(token);
}