forked from Dwayne-Phillips/CIPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflip.c
executable file
·267 lines (217 loc) · 6.74 KB
/
flip.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
/*********************************************
*
* file flip.c
*
* Functions: This file contains
* main
* flip_image
*
* Purpose:
* The program rotates or flips an
* image in 90 degree increments.
*
*
* The rotation_type parameter specifies
* the operation. When rotation_type is
* 1, 2, or 3 you rotate. When it is
* 4 or 5 you flip.
*
* I define rotation as this: Pin down the
* lower left hand corner of the image array
* and rotate the image 90 degrees clockwise.
* 1 rotation is 90 degrees, 2 rotations are
* 180 degrees, and 3 rotations are 270 degrees.
* 4 rotations bring you back to where you
* started.
*
* The cases are:
*
* If the input image array is:
* 1 2 3
* 4 5 6
* 7 8 9
*
* Rotate # 1 - the result is:
* 7 4 1
* 8 5 2
* 9 6 3
*
* Rotate # 2 - the result is:
* 9 8 7
* 6 5 4
* 3 2 1
*
* Rotate # 3 - the result is:
* 3 6 9
* 2 5 8
* 1 4 7
*
* Flip # 4 - horizontal the result is:
* 3 2 1
* 6 5 4
* 9 8 7
*
* Flip # 5 - vertical the result is:
* 7 8 9
* 4 5 6
* 1 2 3
*
*
* External Calls:
* imageio.c - create_image_file
* read_image_array
* write_image_array
* get_image_size
* allocate_image_array
* free_image_array
*
* Modifications:
* 1 July 1999 - created
*
***********************************************/
#include "cips.h"
int flip_image();
int does_not_exist();
int create_image_file();
int get_image_size();
int read_image_array();
int free_image_array();
int write_image_array();
short **the_image;
short **out_image;
int main(argc, argv)
int argc;
char *argv[];
{
char in_name[MAX_NAME_LENGTH], out_name[MAX_NAME_LENGTH];
int type;
long length1, width1;
if(argc != 4){
printf("\n\nusage: flip in-file out-file type");
printf("\n type is 1 2 3 4 (90 degree increments)\n");
exit(0);
}
strcpy(in_name, argv[1]);
strcpy(out_name, argv[2]);
type = atoi(argv[3]);
/* check input file */
if(does_not_exist(in_name)){
printf("\nERROR input file %s does not exist",
in_name);
exit(0);
} /* ends if does_not_exist */
create_image_file(in_name, out_name);
/* allocate the image arrays */
get_image_size(in_name, &length1, &width1);
the_image = allocate_image_array(length1, width1);
out_image = allocate_image_array(length1, width1);
read_image_array(in_name, the_image);
flip_image(the_image, out_image, type, length1, width1);
write_image_array(out_name, out_image);
free_image_array(the_image, length1);
free_image_array(out_image, length1);
} /* ends main */
int flip_image(the_image, out_image,
type,
rows, cols)
int type;
long cols, rows;
short **the_image,
**out_image;
{
int cd2, i, j, rd2;
/*******************************************
*
* Check the rotation_type. If it is not
* a valid value, set it to 1.
*
*******************************************/
if(type != 1 &&
type != 2 &&
type != 3 &&
type != 4 &&
type != 5) type = 1;
/*******************************************
*
* Rotate the image array as desired.
*
*******************************************/
/*******************************************
*
* 1 90 degree rotation
*
*******************************************/
if(type == 1 || type == 2 || type == 3){
for(i=0; i<rows; i++){
for(j=0; j<cols; j++)
out_image[j][cols-1-i] = the_image[i][j];
} /* ends loop over i */
} /* ends if type == 1 or 2 or 3 */
/*******************************************
*
* a second 90 degree rotation
*
*******************************************/
if(type == 2 || type == 3){
for(i=0; i<rows; i++)
for(j=0; j<cols; j++)
the_image[i][j] = out_image[i][j];
for(i=0; i<rows; i++){
for(j=0; j<cols; j++)
out_image[j][cols-1-i] = the_image[i][j];
} /* ends loop over i */
} /* ends if type == 2 or 3 */
/*******************************************
*
* a third 90 degree rotation
*
*******************************************/
if(type == 3){
for(i=0; i<rows; i++)
for(j=0; j<cols; j++)
the_image[i][j] = out_image[i][j];
for(i=0; i<rows; i++){
for(j=0; j<cols; j++)
out_image[j][cols-1-i] = the_image[i][j];
} /* ends loop over i */
} /* ends if type == 3 */
/*******************************************
*
* Flip the image array horizontally
* about the center vertical axis.
*
*******************************************/
if(type == 4){
cd2 = cols/2;
for(j=0; j<cd2; j++){
for(i=0; i<rows; i++){
out_image[i][cols-1-j] = the_image[i][j];
} /* ends loop over i */
} /* ends loop over j */
for(j=cd2; j<cols; j++){
for(i=0; i<rows; i++){
out_image[i][cols-1-j] = the_image[i][j];
} /* ends loop over i */
} /* ends loop over j */
} /* ends if type == 4 */
/*******************************************
*
* Flip the image array vertically
* about the center horizontal axis.
*
*******************************************/
if(type == 5){
rd2 = rows/2;
for(i=0; i<rd2; i++){
for(j=0; j<cols; j++){
out_image[rows-1-i][j] = the_image[i][j];
} /* ends loop over j */
} /* ends loop over i */
for(i=rd2; i<rows; i++){
for(j=0; j<cols; j++){
out_image[rows-1-i][j] = the_image[i][j];
} /* ends loop over j */
} /* ends loop over i */
} /* ends if type == 5 */
return(1);
} /* ends flip_image */