-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageResize.h
234 lines (204 loc) · 7.18 KB
/
imageResize.h
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
//
// Created by kwoodle on 6/12/20.
//
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <string.h> // strcmpi
#ifndef _WIN64
#include <sys/time.h> // timings
#include <unistd.h>
#endif
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <cuda_runtime_api.h>
#include <nvjpeg.h>
#include <nppi_geometry_transforms.h>
#define CHECK_CUDA(call) \
{ \
cudaError_t _e = (call); \
if (_e != cudaSuccess) \
{ \
std::cout << "CUDA Runtime failure: '#" << _e << "' at " << __FILE__ << ":" << __LINE__ << std::endl;\
exit(1); \
} \
}
#define CHECK_NVJPEG(call) \
{ \
nvjpegStatus_t _e = (call); \
if (_e != NVJPEG_STATUS_SUCCESS) \
{ \
std::cout << "NVJPEG failure: '#" << _e << "' at " << __FILE__ << ":" << __LINE__ << std::endl;\
exit(1); \
} \
}
struct image_resize_params_t {
std::string input_dir;
std::string output_dir;
int quality;
int width;
int height;
int dev;
};
typedef struct {
NppiSize size;
nvjpegImage_t data;
} image_t;
int dev_malloc(void** p, size_t s)
{
return (int)cudaMalloc(p, s);
}
int dev_free(void* p)
{
return (int)cudaFree(p);
}
bool is_interleaved(nvjpegOutputFormat_t format)
{
if (format == NVJPEG_OUTPUT_RGBI || format == NVJPEG_OUTPUT_BGRI)
return true;
else
return false;
}
// *****************************************************************************
// reading input directory to file list
// -----------------------------------------------------------------------------
int readInput(const std::string &sInputPath, std::vector<std::string> &filelist)
{
int error_code = 1;
struct stat s;
if( stat(sInputPath.c_str(), &s) == 0 )
{
if( s.st_mode & S_IFREG )
{
filelist.push_back(sInputPath);
}
else if( s.st_mode & S_IFDIR )
{
// processing each file in directory
DIR *dir_handle;
struct dirent *dir;
dir_handle = opendir(sInputPath.c_str());
std::vector<std::string> filenames;
if (dir_handle)
{
error_code = 0;
while ((dir = readdir(dir_handle)) != NULL)
{
if (dir->d_type == DT_REG)
{
std::string sFileName = sInputPath + dir->d_name;
filelist.push_back(sFileName);
}
else if (dir->d_type == DT_DIR)
{
std::string sname = dir->d_name;
if (sname != "." && sname != "..")
{
readInput(sInputPath + sname + "/", filelist);
}
}
}
closedir(dir_handle);
}
else
{
std::cout << "Cannot open input directory: " << sInputPath << std::endl;
return error_code;
}
}
else
{
std::cout << "Cannot open input: " << sInputPath << std::endl;
return error_code;
}
}
else
{
std::cout << "Cannot find input path " << sInputPath << std::endl;
return error_code;
}
return 0;
}
// *****************************************************************************
// check for inputDirExists
// -----------------------------------------------------------------------------
int inputDirExists(const char *pathname) {
struct stat info;
if (stat(pathname, &info) != 0) {
return 0; // Directory does not exists
} else if (info.st_mode & S_IFDIR) {
// is a directory
return 1;
} else {
// is not a directory
return 0;
}
}
// *****************************************************************************
// check for getInputDir
// -----------------------------------------------------------------------------
int getInputDir(std::string &input_dir, const char *executable_path) {
int found = 0;
if (executable_path != 0) {
std::string executable_name = std::string(executable_path);
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
// Windows path delimiter
size_t delimiter_pos = executable_name.find_last_of('\\');
executable_name.erase(0, delimiter_pos + 1);
if (executable_name.rfind(".exe") != std::string::npos) {
// we strip .exe, only if the .exe is found
executable_name.resize(executable_name.size() - 4);
}
#else
// Linux & OSX path delimiter
size_t delimiter_pos = executable_name.find_last_of('/');
executable_name.erase(0, delimiter_pos + 1);
#endif
// Search in default paths for input images.
std::string pathname = "";
const char *searchPath[] = {
"./images"};
for (unsigned int i = 0; i < sizeof(searchPath) / sizeof(char *); ++i) {
std::string pathname(searchPath[i]);
size_t executable_name_pos = pathname.find("<executable_name>");
// If there is executable_name variable in the searchPath
// replace it with the value
if (executable_name_pos != std::string::npos) {
pathname.replace(executable_name_pos, strlen("<executable_name>"),
executable_name);
}
if (inputDirExists(pathname.c_str())) {
input_dir = pathname + "/";
found = 1;
break;
}
}
}
return found;
}
// *****************************************************************************
// parse parameters
// -----------------------------------------------------------------------------
int findParamIndex(const char **argv, int argc, const char *parm) {
int count = 0;
int index = -1;
for (int i = 0; i < argc; i++) {
if (strncmp(argv[i], parm, 100) == 0) {
index = i;
count++;
}
}
if (count == 0 || count == 1) {
return index;
} else {
std::cout << "Error, parameter " << parm
<< " has been specified more than once, exiting\n"
<< std::endl;
return -1;
}
return -1;
}