-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimgen.cpp
229 lines (206 loc) · 5.68 KB
/
imgen.cpp
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
/**
* 下一步计划使用thread进行多线程开发
*/
#define version "v1.3-alpha"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "bmp.h" // bmp文件处理
#include "imgG.h" // 灰度转化
#include "imgH.h" // 直方图均衡
#include "imgS.h" // 平滑化
int main(int argc, char *argv[])
{
// 命令合法性检测
if (argc < 2)
{
printf("Wrong Command.\n");
exit(EXIT_FAILURE);
}
if (strcmp(argv[1], "-v") == 0) // 判断版本号
{
printf("Version: %s\n", version);
exit(EXIT_SUCCESS);
}
else if (strcmp(argv[1], "-g") == 0) // 判断 灰度增强
{
// 命令检测合法性
if (argc != 7)
{
printf("Wrong Command.\n");
exit(EXIT_FAILURE);
}
int a = atoi(argv[3]); // 参数读入
int b = atoi(argv[4]);
char fileIn[1000], fileOut[1000]; // 文件名读入
if (strcpy(fileIn, argv[5]) == NULL || strcpy(fileOut, argv[6]) == NULL)
{
printf("Fail to read the filename.\n");
exit(EXIT_FAILURE);
}
// 文件合法性
if (getValidity(fileIn) == 0)
{
printf("The file format is wrong.\n");
exit(EXIT_FAILURE);
}
else if (getValidity(fileIn) == -1)
{
printf("Fail to open file.\n");
exit(EXIT_FAILURE);
}
// 灰度信息读入
GARYSCALEBIT temp = getGrayscale(fileIn);
if (strcmp(argv[2], "-l") == 0) // 判断 线性变换
{
// 进行线性灰度增强
if (imgGL(temp, a, b) != 0)
{
printf("Fail to process.");
exit(EXIT_FAILURE);
}
}
else if (strcmp(argv[2], "-e") == 0) // 判断 对数变换
{
// 进行对数灰度增强
if (imgGE(temp, a, b) != 0)
{
printf("Fail to process.");
exit(EXIT_FAILURE);
}
}
else
{
// 命令合法性
printf("Wrong command.\n");
exit(EXIT_FAILURE);
}
// 写入文件
if (bmpWrite(temp.garyscale, fileIn, fileOut) != 0)
{
printf("Fail to Write in File.");
exit(EXIT_FAILURE);
}
else
{
printf("Success.\n");
}
// 释放指针
free(getGrayscale(fileIn).garyscale);
}
else if (strcmp(argv[1], "-h") == 0) // 判断 直方图均衡化
{
// 命令检测合法性
if (argc != 4)
{
printf("Wrong Command.\n");
exit(EXIT_FAILURE);
}
// 文件名读入
char fileIn[1000], fileOut[1000];
if (strcpy(fileIn, argv[2]) == NULL || strcpy(fileOut, argv[3]) == NULL)
{
printf("Fail to read the filename.\n");
exit(EXIT_FAILURE);
}
// 文件合法性
if (getValidity(fileIn) == 0)
{
printf("The file format is wrong.\n");
exit(EXIT_FAILURE);
}
else if (getValidity(fileIn) == -1)
{
printf("Fail to open file.\n");
exit(EXIT_FAILURE);
}
// 灰度信息读入
GARYSCALEBIT temp = getGrayscale(fileIn);
// 进行直方图均衡化的处理
if (imgH(temp) != 0)
{
printf("Fail to process.");
exit(EXIT_FAILURE);
}
// 写入文件
if (bmpWrite(temp.garyscale, fileIn, fileOut) != 0)
{
printf("Fail to Write in File.");
exit(EXIT_FAILURE);
}
else
{
printf("Success.\n");
}
// 释放指针
free(getGrayscale(fileIn).garyscale);
}
else if (strcmp(argv[1], "-s") == 0) // 判断 平滑化
{
// 命令检测合法性
if (argc != 5)
{
printf("Wrong Command.\n");
exit(EXIT_FAILURE);
}
// 文件名读入
char fileIn[1000], fileOut[1000];
if (strcpy(fileIn, argv[2]) == NULL || strcpy(fileOut, argv[4]) == NULL)
{
printf("Fail to read the filename.\n");
exit(EXIT_FAILURE);
}
// 文件合法性
if (getValidity(fileIn) == 0)
{
printf("The file format is wrong.\n");
exit(EXIT_FAILURE);
}
else if (getValidity(fileIn) == -1)
{
printf("Fail to open file.\n");
exit(EXIT_FAILURE);
}
// 灰度信息读入
GARYSCALEBIT temp = getGrayscale(fileIn);
if (strcmp(argv[3], "-3") == 0) // 3*3平滑化
{
if (meanFilter_3(temp, fileIn) != 0)
{
printf("Fail to process.");
exit(EXIT_FAILURE);
}
}
else if (strcmp(argv[3], "-5") == 0) // 5*5平滑化
{
if (meanFilter_5(temp, fileIn) != 0)
{
printf("Fail to process.");
exit(EXIT_FAILURE);
}
}
else
{
printf("Wrong Command.\n");
exit(EXIT_FAILURE);
}
// 写入文件
if (bmpWrite(temp.garyscale, fileIn, fileOut) != 0)
{
printf("Fail to Write in File.");
exit(EXIT_FAILURE);
}
else
{
printf("Success.\n");
}
// 释放指针
free(getGrayscale(fileIn).garyscale);
}
else // 错误命令
{
printf("Wrong command.\n");
exit(EXIT_FAILURE);
}
return 0;
}