-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect_types.c
executable file
·127 lines (114 loc) · 2.9 KB
/
select_types.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
#include "skel.h"
// classical swap using auxiliary variable
void conditioned_swap_numbers(int *x, int *y)
{
if (*x <= *y)
return;
int tmp = *x;
*x = *y;
*y = tmp;
}
// verify if the selected coordinates are in bounds
int check_coordinates(int x1, int x2, int y1, int y2, int lim_w, int lim_h)
{
if (x1 < 0 || y1 < 0)
return 0;
if (x2 > lim_w || y2 > lim_h)
return 0;
if (x1 == x2 || y1 == y2)
return 0;
return 1;
}
// verifies if param is an integer
bool valid_param(char *params)
{
if (!params)
return false;
size_t len = strlen(params);
// '0' is valid, so len > 1
if (len > 1 && params[0] == '0')
return false;
for (size_t i = 0; i < len; i++) {
if (params[i] == '-' && i == 0)
continue;
if (params[i] < '0' || params[i] > '9')
return false;
}
return true;
}
// after the function, only [x1, x2) and [y1, y2) pixels will be transformed
void select_types(image *img, int magic_number, int *x1, int *x2, int *y1,
int *y2)
{
// verifying if we have no parameters or less than needed
char *cmd_line = malloc(STRING_MAX);
DIE(!cmd_line, MALLOC_ERROR);
fgets(cmd_line, STRING_MAX, stdin);
if (magic_number == -1) {
fprintf(stdout, "No image loaded\n");
free(cmd_line);
return;
}
if (strcmp(cmd_line, "\n") == 0) {
fprintf(stdout, "Invalid command\n");
free(cmd_line);
return;
}
// fgets adds \n at the final of the string
size_t len = strlen(cmd_line);
cmd_line[len - 1] = '\0';
char *verify_type = malloc(STRING_MAX);
DIE(!verify_type, MALLOC_ERROR);
strcpy(verify_type, cmd_line + 1);
free(cmd_line);
char *params = strtok(verify_type, " ");
if (strcmp(params, "ALL") == 0) {
// open interval for right
*x1 = 0, *x2 = img->width, *y1 = 0, *y2 = img->height;
fprintf(stdout, "Selected ALL\n");
free(verify_type);
return;
}
int cx1 = *x1, cx2 = *x2, cy1 = *y1, cy2 = *y2;
// transforms the string in an integer, neglecting invalid command
if (!valid_param(params)) {
fprintf(stdout, "Invalid command\n");
free(verify_type);
return;
}
*x1 = atoi(verify_type);
params = strtok(NULL, " ");
if (!valid_param(params)) {
*x1 = cx1;
fprintf(stdout, "Invalid command\n");
free(verify_type);
return;
}
*y1 = atoi(params);
params = strtok(NULL, " ");
if (!valid_param(params)) {
*x1 = cx1, *y1 = cy1;
fprintf(stdout, "Invalid command\n");
free(verify_type);
return;
}
*x2 = atoi(params);
params = strtok(NULL, " ");
if (!valid_param(params)) {
*x1 = cx1, *y1 = cy1, *x2 = cx2;
fprintf(stdout, "Invalid command\n");
free(verify_type);
return;
}
*y2 = atoi(params);
conditioned_swap_numbers(&(*x1), &(*x2));
conditioned_swap_numbers(&(*y1), &(*y2));
if (!check_coordinates(*x1, *x2, *y1, *y2, img->width, img->height)) {
fprintf(stdout, "Invalid set of coordinates\n");
*x1 = cx1, *x2 = cx2, *y1 = cy1, *y2 = cy2;
free(verify_type);
return;
}
fprintf(stdout, "Selected %d %d %d %d\n", *x1, *y1, *x2, *y2);
free(verify_type);
}