-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
357 lines (266 loc) · 10.6 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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include "PNG_generator.h"
#include "dir_creator.h"
#include "JPEG_generator.h"
#include "my_utils.h" // my_utils.c is replaced
#include "progressbar.h"
#include "version.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>
const int MS = 1000;
char format[4];
int main(int argc, char* argv[])
{
// Get the current time in UTC
if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
perror("clock_gettime");
return 1;
}
*pStartTime = (double)ts.tv_sec + (double)ts.tv_nsec / 1.0e9;
printf("Welcome to RIG %s!\n", RIG_VERSION);
// Declaring vars
unsigned int width = 0;
unsigned int height = 0;
bool alpha = false;
unsigned int count;
int n;
bool help = false;
bool termuxExternal = false;
char outDir[] = "out";
char androidInternalPath[] = "/storage/emulated/0/";
int quality = 100;
// Terminal sizes:
getTerminalSize(&terminalHeight, &terminalWidth);
/*for (int i = 0; i < terminalHeight - 3; i++) {
* printf("\n");
}*/
// Number of the same arguments
short unsigned int sCount = 0; // -s, --size
short unsigned int cCount = 0; // -c --count
short unsigned int aCount = 0; // -a --alpha
short unsigned int hCount = 0; // -h --help
short unsigned int tCount = 0; // --termux_external
short unsigned int dCount = 0; // -d --debug
short unsigned int fCount = 0; // -f --format
short unsigned int qCount = 0; // -f --format
// Print the arguments
// for (int i = 0; i <= argc+1; i++) {
// printf("Argv%d = %s\n", i, argv[i]);
//}
// Handle the arguments.
for (n = 1; n < argc; n++) {
if (strcmp(argv[n], "-s") == 0 || strcmp(argv[n], "--size") == 0) { // -s,
sCount++;
if (argv[n + 1] != NULL && argv[n + 1] != NULL) {
width = atoi(argv[n + 1]);
height = atoi(argv[n + 2]);
n += 2;
} else {
printf("Size is not specified!\n");
return 3;
}
} else if (strcmp(argv[n], "-a") == 0 || strcmp(argv[n], "--alpha") == 0) { // If n-th arg -a,
alpha = true;
aCount++;
} else if (strcmp(argv[n], "-c") == 0 || strcmp(argv[n], "--count") == 0) { // If n-th arg -c,
if (argv[n + 1] != NULL) {
count = atoi(argv[n + 1]);
cCount++;
n++;
} else {
printf("Count is not specified!\n");
return 3;
}
} else if (strcmp(argv[n], "-h") == 0 || strcmp(argv[n], "--help") == 0) { // If n-th arg -h,
help = true;
hCount++;
} else if (strcmp(argv[n], "--termux-external") == 0) { // If n-th arg -a,
termuxExternal = true; // to implement
tCount++; // to implement
} else if (strcmp(argv[n], "-d") == 0 || strcmp(argv[n], "--debug") == 0) { // If n-th arg -a,
allowDebugInfo = true; // to implement
dCount++; // to implement
} else if (strcmp(argv[n], "-f") == 0 || strcmp(argv[n], "--format") == 0) { // If n-th arg -f
fCount++;
if ((argv[n + 1]) != NULL) {
if (strcmp(argv[n + 1], "png") == 0) {
strcpy(format, "png");
n++;
} else if (strcmp(argv[n + 1], "jpeg") == 0 || strcmp(argv[n + 1], "jpg") == 0) {
strcpy(format, "jpg");
n++;
} else {
printf("RIG currently only supports png (default) and jpeg as a format option!");
return 3;
}
} else {
printf("--format/-f is not set correctly or left empty, write image format after -f or --format!\n");
return 3;
}
} else if (strcmp(argv[n], "-q") == 0 || strcmp(argv[n], "--quality") == 0) { // If n-th arg -q
qCount++;
if ((argv[n + 1]) != NULL) {
quality = atoi(argv[n + 1]);
if (quality > 0 && quality <= 100) {
n++;
} else {
printf("Quality isn't set correctly!\n");
return 3;
}
} else {
printf("Missing quality value after -q or --quality.\n");
return 3;
}
} else { // If there is no known argument at a given argc location.
printf("Unknown option \"%s\" at the %d. argument. Use -h for help.\n", argv[n], n);
return 3;
}
}
// Additional checks
if ((!fCount) && (!hCount) && argc > 1 ) {
printDebugPlusInt("fCount: %d", fCount);
printDebugPlusInt("hCount: %d", hCount);
printf("Format is not set, defaulting to png.\n");
strcpy(format, "png");
} else if (aCount && (strcmp(format, "jpg") == 0) ) {
printf("--alpha (transparency) option will be ignored when using jpeg.\n");
} else if ((!qCount) && (strcmp(format, "jpg") == 0)) {
printf("Quality is not set, defaulting to 100.\n");
}
// Depends on allowDebugInfo == true
errorFileOpener();
// All printDebug depends on errorFileOpener
printDebugPlusInt("sCount = ", sCount);
printDebugPlusInt("aCount = ", aCount);
printDebugPlusInt("cCount = ", cCount);
printDebugPlusInt("hCount = ", hCount);
printDebugPlusInt("tCount = ", tCount);
printDebugPlusInt("dCount = ", dCount);
printDebugPlusInt("fCount = ", fCount);
if (argc == 1) {
printf("Use -h to display help message.\n");
return 0;
}
// Too few arguments warning
if ((width == 0 || height == 0 || count == 0) && !help) {
printf("Too few arguments. Width, height or count is 0. Unexpected "
"behaviour may occur! (Argc = %d)\n", argc);
return 1;
}
// Too many arguments
if (sCount > 1 || cCount > 1 || aCount > 1 || hCount > 1 || tCount > 1) {
printf("Too many arguments in the same type.");
return 1;
}
// Helpmsg
if (help == true) {
printHelp();
return 0;
}
// New:
dirCreatorLinux(outDir, termuxExternal);
// Generating images
srand((unsigned int)time(NULL)); // Seed the random number generator
int i = 0;
int errorCount = 0;
printDebugPlusInt("Terminal height = ", terminalHeight);
printDebugPlusInt("Terminal width = ", terminalWidth);
// Multithread stuff
pthread_mutex_init(&mutex, NULL); // init the mutex.
pthread_t progressThread;
struct ProgressBarArgs* args = (struct ProgressBarArgs*)malloc(sizeof(struct ProgressBarArgs));
pthread_create(&progressThread, NULL, multiThreadedProgressbar, (void*)args);
double genTime = 0;
double genTime1 = 0;
double genTime2 = (double)ts.tv_sec + (double)ts.tv_nsec / 1.0e9;
char fileExtension[5];
// Start of the image loop
for (i = 1; i <= count; i++) {
char imagename[30];
printDebugPlusFloat("gentime:", genTime);
getTerminalSize(&terminalHeight, &terminalWidth);
if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
perror("clock_gettime");
return 1;
}
args->progress = i;
args->total = count;
args->length = terminalWidth - 35;
args->time = genTime * (args->total - args->progress); // To modify
printDebugPlusFloat("time:", genTime * (args->total - args->progress));
//progressbar(i, count, terminalWidth - 30, genTime); -----------------------------
// Create file for image
sprintf(imagename, "%s/random_image%d.%s", outDir, i, format);
printDebugPlusStr("Image name:", imagename);
if (strcmp(format, "png") == 0) {
// Write PNG file
errorCount = errorCount + generatePNG(imagename, width, height, alpha, allowDebugInfo);
} else {
// Write JPEG file
generateJPEG(imagename, width, height, quality);
}
if (i == 1) {
printDebug("First iteration of image gen loop.");
}
// Time for the progressbar
genTime1 = genTime2;
genTime2 = (double)ts.tv_sec + (double)ts.tv_nsec / 1.0e9;
genTime = genTime2 - genTime1;
printDebugPlusFloat("genTime1:", genTime1);
printDebugPlusFloat("genTime2:", genTime2);
}
pthread_join(progressThread, NULL);
pthread_mutex_destroy(&mutex);
free(args);
printf("\n");
if (termuxExternal) {
printf("Moving images to internal storage...\n");
int shellCommandLenght = strlen("mv ") + strlen(outDir) + strlen(" ") + strlen(androidInternalPath) + 1;
char shellCommand[shellCommandLenght];
printf(" Removing previously generated folder\n");
shellCommand[0] = '\0'; // Initialize it as an empty string
strcat(shellCommand, "rm -rf ");
strcat(shellCommand, androidInternalPath);
strcat(shellCommand, outDir);
printDebugPlusStr("Shell command:", shellCommand);
system(shellCommand); // Removing dirs to avoid write error
printf(" Moving files\n");
// TODO: Make a progressbar
// To do that, I need to manually move the files.
// The directory will be made with dirCreatorLinux, so the files should be only moved to it.
shellCommand[0] = '\0'; // Initialize it as an empty string
strcat(shellCommand, "mv ");
strcat(shellCommand, outDir);
strcat(shellCommand, " ");
strcat(shellCommand, androidInternalPath);
printDebugPlusStr("Shell command:", shellCommand);
system(shellCommand); // Moving dirs to external
}
// Get the current time in UTC
if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
perror("clock_gettime");
return 1;
}
double endTime = (double)ts.tv_sec + (double)ts.tv_nsec / 1.0e9;
double fullTime = endTime - startTime;
// Error number counting
if (errorCount == 0) {
if (count > 1) {
printf("%d random images generated successfully in %lf seconds.\n", i - 1, fullTime);
} else if (count == 1) {
printf("1 random image generated successfully in %lf seconds.\n", fullTime);
} else if (count == 0) {
printf("0 random image generated in %lf seconds.\n", fullTime);
}
} else {
printf("%d random image(s) generated with %d error(s) in %lf seconds.\n", i, errorCount, fullTime);
return 1;
}
errorFileCloser();
return 0;
}