-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.c
310 lines (260 loc) · 9.61 KB
/
cli.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
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Author: Makzzzimus
* Date: 2024
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bmp.h"
#include "cli.h"
#include "conio.h"
#define MEGABYTE_SIZE 1048576
#define HEADER "========================[ Welcome to Bincture v0.1a! ]=========================\n"
#define PATH_TIP "Drag & drop the file into the terminal window to quickly insert the path to it.\n"
#define BPP_TIP \
"The bytes per pixel value specifies the color depth and affects the number of pixels in visualization. \nNote: " \
"Most modern images use 3 Bpp or 4 Bpp. Depending on whether the image can have transparent pixels or not. If the " \
"visualization looks strange with a value of 3, try swapping it to 4\n"
#define SIZE_TIP "The visualization will contain "
#define DIRECTION_TIP "Keep in mind that visualization starts at the bottom of the image."
#define ASK_PATH_PROMPT "Enter the path to the destination file: "
#define ASK_BPP_PROMPT "Enter the number of bytes per pixel [1, 2, 3, 4]: "
#define ASK_SIZE_PROMPT \
"Enter the size of visualization in following format {Width} {Height} (Both must be <100000). Both variables must " \
"be dividable by 4: "
#define ASK_EXIT_PROMPT "\nPress any key to exit the application..."
#define WAIT_WHILE_VISUALIZATION "The process of visualization may take a while to complete..."
#define SUCCESS_VISUALIZATION \
"The process of visualization has successfully finished. The file was saved in the current directory in the folder " \
"\"Bincture visualizations\""
// Positive error codes refer to errors in CLI input, while negative errors in runtime
#define INVALID_FILE_ERROR "Invalid file path provided!\n" // Error code 1
#define LARGE_FILE_ERROR "The file is too large or empty! The file must be smaller than 4 GB\n" // Error code 2
#define INVALID_BPP_ERROR "Enter only numbers ranging from 1 to 4!\n" // Error code 3
#define NEGATIVE_SIZE_ERROR "Enter only numbers greater than 0!\n" // Error code 4
#define UNDIVIDABLE_SIZE_ERROR "Both width and height values must be dividable by 4!\n" // Error code 5
#define LARGE_SIZE_ERROR "Visualization can't handle more than 4,294,967,295 bytes!\n" // Error code 6
#define NO_PALLETTE_ERROR \
"The pallette.txt file doesn't exist. This file is required to generate 8-bit images. It contains all colors of " \
"the image. Download it from project's repo github.com/Makzzzimus/bincture/blob/main/pallette.txt or create your " \
"own." // Error code 7
#define NO_LONGER_EXISTS_ERROR "\nThe given file doesn't exist anymore or can't be accessed\n" // Error code -1
#define CANT_MODIFY_ERROR \
"\nBincture doesn't have enough privilege to create and modify files in this directory.\n" // Error code -2
#define MOVED_PALLETTE_ERROR "\nThe pallette.txt file was moved or deleted and can't be accessed" // Error code -3
static int8_t lastError = 0;
void fgetw(char* buffer, uint8_t maxLength, FILE* file) {
char temp;
for (uint8_t i = 0; i < maxLength; i++) {
temp = fgetc(file);
if (temp == ' ' || temp == '\n' || temp == '\0') {
return;
} else {
buffer[i] = temp;
}
}
fgetc(file);
}
void printError(int8_t errorCode) {
c_textcolor(RED);
printf("Error: ");
switch (errorCode) {
case 1:
puts(INVALID_FILE_ERROR);
break;
case 2:
puts(LARGE_FILE_ERROR);
break;
case 3:
puts(INVALID_BPP_ERROR);
break;
case 4:
puts(NEGATIVE_SIZE_ERROR);
break;
case 5:
puts(UNDIVIDABLE_SIZE_ERROR);
break;
case 6:
puts(LARGE_SIZE_ERROR);
break;
case 7:
puts(NO_PALLETTE_ERROR);
break;
case -1:
puts(NO_LONGER_EXISTS_ERROR);
break;
case -2:
puts(CANT_MODIFY_ERROR);
break;
case -3:
puts(MOVED_PALLETTE_ERROR);
break;
}
c_textcolor(WHITE);
return;
}
void printTip(char* string) {
c_textcolor(YELLOW);
printf("Tip: ");
puts(string);
c_textcolor(WHITE);
return;
}
void printHead() {
c_clrscr();
puts(HEADER);
if (lastError != 0) {
printError(lastError);
lastError = 0;
}
}
unsigned int askPath(char* path) {
unsigned int userFileSize = 0;
printHead();
printTip(PATH_TIP);
printf(ASK_PATH_PROMPT);
fgets(path, 255, stdin);
// Delete quote signs if user dragged n dropped a file into terminal window
if (path[0] == '"' || path[0] == '\'') {
memmove(path, path + 1, strlen(path));
}
for (uint8_t i = 0; i < 3; i++) {
if (path[strlen(path) - 1] == '\n' || path[strlen(path) - 1] == '"' || path[strlen(path) - 1] == '\'' ||
path[strlen(path) - 1] == ' ') {
path[strlen(path) - 1] = '\0';
}
}
FILE* userFile = fopen(path, "r");
if (userFile == NULL) {
lastError = 1;
path[0] = '\0';
userFileSize = askPath(path);
return userFileSize;
}
userFileSize = getFileSize(userFile);
if (userFileSize > UINT32_MAX || userFileSize == 0) {
lastError = 2;
path[0] = '\0';
fclose(userFile);
userFileSize = askPath(path);
return userFileSize;
}
fclose(userFile);
return userFileSize;
}
int8_t askBytesPerPixel() {
printHead();
printTip(BPP_TIP);
printf(ASK_BPP_PROMPT);
uint8_t bytesPerPixel = c_getch() - 48;
if (bytesPerPixel == 0 || bytesPerPixel > 4) {
lastError = 3;
bytesPerPixel = askBytesPerPixel();
}
if (bytesPerPixel == 1) {
FILE* pallette = fopen("./pallette.txt", "r");
if (pallette == NULL) {
lastError = 7;
bytesPerPixel = askBytesPerPixel();
}
fclose(pallette);
}
return bytesPerPixel;
}
void askSize(unsigned int* width, unsigned int* height, int fileSize, int* lostPixels, int8_t bytesPerPixel) {
int totalPixels = fileSize / bytesPerPixel;
char widthBuffer[8] = "\0", heightBuffer[8] = "\0";
unsigned int recWidth1 = 128, recHeight1 = 0, recWidth2 = 256, recHeight2 = 0; // rec - Recommended
int recLostPixels = 0;
recHeight1 = totalPixels / recWidth1;
if (recHeight1 % 4) {
recHeight1 += 4 - (recHeight1 % 4);
}
recLostPixels = totalPixels - recWidth1 * recHeight1;
recHeight2 = totalPixels / recWidth2;
if (recHeight2 % 4) {
recHeight2 += 4 - (recHeight2 % 4);
}
// recLostPixels2 = totalPixels - recWidth2 * recHeight2;
printHead();
char finalTip[512] = "\0", tipBody[325] = "\0";
strcat(finalTip, SIZE_TIP);
sprintf(tipBody,
"%d pixels. The recommended visualization sizes for this file are %dx%d or %dx%d. In both cases, %d pixels "
"are lost \nNote: If the value of lost pixels is negative, additional white pixels are added to the start "
"visualization.)\n\n",
totalPixels, recWidth1, recHeight1, recWidth2, recHeight2, recLostPixels);
strcat(finalTip, tipBody);
printTip(finalTip);
printf(ASK_SIZE_PROMPT);
fgetw(widthBuffer, 7, stdin);
fgetw(heightBuffer, 7, stdin);
*width = strtol(widthBuffer, NULL, 0);
*height = strtol(heightBuffer, NULL, 0);
if (*width <= 0 || *height <= 0) {
lastError = 4;
fflush(stdin);
askSize(width, height, fileSize, lostPixels, bytesPerPixel);
return;
}
if (*width % 4 || *height % 4) {
lastError = 5;
fflush(stdin);
askSize(width, height, fileSize, lostPixels, bytesPerPixel);
return;
}
uint64_t bytes = (uint64_t)(*width) * (*height) * bytesPerPixel;
if (bytes > UINT32_MAX) {
lastError = 6;
fflush(stdin);
askSize(width, height, fileSize, lostPixels, bytesPerPixel);
return;
}
*lostPixels = totalPixels - *width * *height;
printf(
"Selected visualization size is %dx%d. In this case, %d pixels will be lost \nContinue with this size? [Y/n]: ",
*width, *height, *lostPixels);
char action = c_getch();
if (action == 'n') {
fflush(stdin);
askSize(width, height, fileSize, lostPixels, bytesPerPixel);
}
return;
}
void askExit() {
printHead();
c_textcolor(GREEN);
puts(SUCCESS_VISUALIZATION);
c_textcolor(WHITE);
printTip(DIRECTION_TIP);
puts(ASK_EXIT_PROMPT);
c_getch();
exit(EXIT_SUCCESS);
}
void printProgress(int totalBytes, int processedBytes) {
printHead();
char progressBarFill[26] = ".........................";
float totalMBytes = (float)totalBytes / (float)MEGABYTE_SIZE,
processedMBytes = (float)processedBytes / (float)MEGABYTE_SIZE,
percents = processedMBytes / (totalMBytes / 100);
for (int i = 0; i < percents / 4; i++) {
progressBarFill[i] = '#';
}
puts(WAIT_WHILE_VISUALIZATION);
printf("Visualizing [%s] %.1f MB / %.1f MB | %.1f%%\n", progressBarFill, processedMBytes, totalMBytes, percents);
}