-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
184 lines (166 loc) · 5.27 KB
/
main.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
#include "main.h"
#include "inout.h"
#include "upsampling.h"
int main(int argc, char *argv[]) {
char *pSrcFile = NULL;
char *pDstFile = NULL;
char *pSrcDir = NULL;
char *pDstDir = NULL;
bool bParallel = false;
int i, nCnt;
Pixel *pSrcData = NULL;
Pixel *pDstData = NULL;
char **pSrcFiles = NULL;
char **pDstFiles = NULL;
char **pPictureLists = NULL;
// int nTimes, nRemain, nBufSz;
// int nCores;
/* get input parameters */
if (argc == 1) {
printf("Missing argument, use \"-h\" or \"--help\" to get help\n");
return -1;
} else if (argc == 2) {
if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
printf("Options\t\t\tFunction\n");
printf("-h, --help\t\tget help information\n");
printf("-p\t\t\trun parallelly\n");
printf("-f <source>\t\tspecify the single input picture\n");
printf("-o <result>\t\tspecify the single output picture\n");
printf("-src <source directory>\tspecify the input pictures location\n");
printf("-dst <result directory>\tspecify the output pictures location\n");
printf("Note: \"-f\" and \"-o\" should showup together, \"-src\" and \"-dst\" as well\n");
return 0;
} else {
printf("Wrong argument!\n");
return -1;
}
} else {
for (i = 1; i < argc; ++i) {
if (!strcmp("-p", argv[i])) {
bParallel = true;
} else if (!strcmp("-f", argv[i])) {
pSrcFile = argv[++i];
} else if (!strcmp("-o", argv[i])) {
pDstFile = argv[++i];
} else if (!strcmp("-src", argv[i])) {
pSrcDir = argv[++i];
} else if (!strcmp("-dst", argv[i])) {
pDstDir = argv[++i];
} else {
printf("Wrong argument!\n");
return -1;
}
}
}
/* init weights */
initWeights_540p4k(-0.5);
/* single picture processing */
if (pSrcFile && pDstFile && !pSrcDir && !pDstDir) {
/* allocate sourcee data */
pSrcData = (Pixel *)malloc(sizeof(Pixel) * SRC_WIDTH * SRC_HEIGHT);
if (!pSrcData) {
printf("Allocate source data failed\n");
return -1;
}
memset(pSrcData, 0, sizeof(Pixel) * SRC_WIDTH * SRC_HEIGHT);
/* allocate result data */
pDstData = (Pixel *)malloc(sizeof(Pixel) * DST_WIDTH * DST_HEIGHT);
if (!pDstData) {
printf("Allocate destination data failed\n");
return -1;
}
memset(pDstData, 0, sizeof(Pixel) * DST_WIDTH * DST_HEIGHT);
readSinglePicture_540p(pSrcFile, pSrcData);
/* processing */
if (bParallel) {
upsamplingParallel_540p4k(pSrcData, pDstData);
printf("Single picture parallel processing finished!\n");
} else {
upsampling_540p4k(pSrcData, pDstData);
printf("Single picture processing finished!\n");
}
writeSinglePicture_4k(pDstFile, pDstData);
}
/* multi-pictures processing */
else if (!pSrcFile && !pDstFile && pSrcDir && pDstDir) {
/* get picture names */
pPictureLists = getPictureLists(pSrcDir, &nCnt);
/* allocate source and result filenames */
pSrcFiles = (char **)malloc(sizeof(char *) * nCnt);
assert(pSrcFiles != NULL);
memset(pSrcFiles, 0, sizeof(char *) * nCnt);
pDstFiles = (char **)malloc(sizeof(char *) * nCnt);
assert(pDstFiles != NULL);
memset(pDstFiles, 0, sizeof(char *) * nCnt);
/* join path and filenames together */
for (i = 0; i < nCnt; ++i) {
pSrcFiles[i] = (char *)malloc(sizeof(char) * (strlen(pPictureLists[i]) + strlen(pSrcDir) + 1));
assert(pSrcFiles[i] != NULL);
sprintf(pSrcFiles[i], "%s/%s", pSrcDir, pPictureLists[i]);
pDstFiles[i] = (char *)malloc(sizeof(char) * (strlen(pPictureLists[i] + strlen(pDstDir) + 1)));
assert(pDstFiles != NULL);
sprintf(pDstFiles[i], "%s/%s", pDstDir, pPictureLists[i]);
}
/* allocate source data */
pSrcData = (Pixel *)malloc(sizeof(Pixel) * SRC_WIDTH * SRC_HEIGHT);
if (!pSrcData) {
printf("Allocate source data failed\n");
return -1;
}
memset(pSrcData, 0, sizeof(Pixel) * SRC_WIDTH * SRC_HEIGHT);
/* allocate result data */
pDstData = (Pixel *)malloc(sizeof(Pixel) * DST_WIDTH * DST_HEIGHT);
if (!pDstData) {
printf("Allocate destination data failed\n");
return -1;
}
memset(pDstData, 0, sizeof(Pixel) * DST_WIDTH * DST_HEIGHT);
/* processing */
for (i = 0; i < nCnt; ++i) {
readSinglePicture_540p(pSrcFiles[i], pSrcData);
if (bParallel) {
upsamplingParallel_540p4k(pSrcData, pDstData);
} else {
upsampling_540p4k(pSrcData, pDstData);
}
writeSinglePicture_4k(pDstFiles[i], pDstData);
printf("Pictures %d/%d finished!\n", i + 1, nCnt);
}
for (i = 0; i < nCnt; ++i) {
free(pSrcFiles[i]);
pSrcFiles[i] = NULL;
free(pDstFiles[i]);
pDstFiles[i] = NULL;
}
} else {
printf("Wrong argument!\n");
return -1;
}
// release heap memory
if (pSrcData) {
free(pSrcData);
pSrcData = NULL;
}
if (pSrcFiles) {
free(pSrcFiles);
pSrcFiles = NULL;
}
if (pDstData) {
free(pDstData);
pDstData = NULL;
}
if (pDstFiles) {
free(pDstFiles);
pDstFiles = NULL;
}
if (pPictureLists) {
for (i = 0; i < nCnt; ++i) {
free(pPictureLists[i]);
pPictureLists[i] = NULL;
}
free(pPictureLists);
pPictureLists = NULL;
}
// end of release heap memory
return 0;
}