-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotate.c
164 lines (153 loc) · 4.48 KB
/
rotate.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
// Copyright Mantu Matei-Cristian 312CA 2022-2023
#include "utils.h"
#include "load_select.h"
#include "image.h"
void reverse_col_colour(image_t image)
{
int i, j1, j2, k;
for (i = image.p1.y ; i < image.p2.y; ++i)
for (j1 = image.p1.x, j2 = image.p2.x - 1; j1 < j2; ++j1, --j2)
for (k = 0; k < 3; ++k)
swap_F(&image.pic.colour[i][j1][k]
, &image.pic.colour[i][j2][k]);
}
void reverse_rows_colour(image_t image)
{
int i1, i2, j, k;
for (i1 = image.p1.y, i2 = image.p2.y - 1; i1 < i2; ++i1, --i2)
for (j = image.p1.x; j < image.p2.x; ++j)
for (k = 0; k < 3; ++k)
swap_F(&image.pic.colour[i1][j][k]
, &image.pic.colour[i2][j][k]);
}
void transpose_colour(image_t *image)
{
int i, j, k;
if (image->p2.y - image->p1.y != image->p2.x - image->p1.x) {
// copying the elements transposed into another matrix
double ***new = allocate_colour(image->height, image->width);
for (i = 0; i < image->height; ++i)
for (j = 0; j < image->width; ++j)
for (k = 0; k < 3; ++k)
new[j][i][k] = image->pic.colour[i][j][k];
// replacing the old matrix
free_colour(image->pic.colour, image->width, image->height);
image->pic.colour = new;
// updating size
my_swap_int(&image->height, &image->width);
my_swap_int(&image->p2.y, &image->p2.x);
return;
}
// transposing selection by swapping along the diagonal
for (i = 0; i < image->p2.y - image->p1.y; ++i)
for (j = i + 1; j < image->p2.x - image->p1.x; ++j) {
int iy = i + image->p1.y;
int jx = j + image->p1.x;
int jy = j + image->p1.y;
int ix = i + image->p1.x;
for (k = 0; k < 3; ++k)
swap_F(&image->pic.colour[iy][jx][k]
, &image->pic.colour[jy][ix][k]);
}
}
void reverse_col_gs(image_t image)
{
int i, j1, j2;
for (i = image.p1.y; i < image.p2.y; ++i)
for (j1 = image.p1.x, j2 = image.p2.x - 1; j1 < j2; ++j1, --j2)
swap_F(&image.pic.gs[i][j1], &image.pic.gs[i][j2]);
}
void reverse_rows_gs(image_t image)
{
int i1, i2, j;
for (i1 = image.p1.y, i2 = image.p2.y - 1; i1 < i2; ++i1, --i2)
for (j = image.p1.x; j < image.p2.x; ++j)
swap_F(&image.pic.gs[i1][j], &image.pic.gs[i2][j]);
}
void transpose_gs(image_t *image)
{
int i, j;
if (image->p2.y - image->p1.y != image->p2.x - image->p1.x) {
// copying the elements transposed into another matrix
double **new = allocate_gs(image->height, image->width);
for (i = 0; i < image->height; ++i)
for (j = 0; j < image->width; ++j)
new[j][i] = image->pic.gs[i][j];
// replacing the old matrix
free_gs(image->pic.gs, image->height);
image->pic.gs = new;
// updating size
my_swap_int(&image->height, &image->width);
my_swap_int(&image->p2.y, &image->p2.x);
return;
}
for (i = 0; i < image->p2.y - image->p1.y; ++i)
for (j = i + 1; j < image->p2.x - image->p1.x; ++j) {
int iy = i + image->p1.y;
int jx = j + image->p1.x;
int jy = j + image->p1.y;
int ix = i + image->p1.x;
swap_F(&image->pic.gs[iy][jx], &image->pic.gs[jy][ix]);
}
}
int rotate(image_t *image, char *parameters)
{
if (image->magic == 1) {
printf("No image loaded\n");
return -1;
}
if (!parameters) {
printf("Invalid command\n");
return -1;
}
if (image->p1.x != 0 || image->p1.y != 0)
if (image->p2.x != image->width || image->p2.y != image->height)
if (image->p2.y - image->p1.y != image->p2.x - image->p1.x) {
printf("The selection must be square\n");
return -1;
}
char *arg = strtok(parameters, " ");
int angle = atoi(arg);
if (angle % 90 != 0) {
printf("Unsupported rotation angle\n");
return -1;
}
angle %= 360;
if (image->magic % 3 == 2) {
if (angle == 90 || angle == -270) {
// transpose the matrix
transpose_gs(image);
// reverse matrix columns
reverse_col_gs(*image);
} else if (angle == 180 || angle == -180) {
// reverse matrix columns
reverse_col_gs(*image);
// reverse matrix rows
reverse_rows_gs(*image);
} else if (angle == 270 || angle == -90) {
// transpose the matrix
transpose_gs(image);
// reverse matrix rows
reverse_rows_gs(*image);
}
} else {
if (angle == 90 || angle == -270) {
// transpose the matrix
transpose_colour(image);
// reverse matrix columns
reverse_col_colour(*image);
} else if (angle == 180 || angle == -180) {
// reverse matrix columns
reverse_col_colour(*image);
// reverse matrix rows
reverse_rows_colour(*image);
} else if (angle == 270 || angle == -90) {
// transpose the matrix
transpose_colour(image);
// reverse matrix rows
reverse_rows_colour(*image);
}
}
printf("Rotated %d\n", atoi(arg));
return 0;
}