HTMLify
main.c
Views: 2 | Author: abh
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "image.h"
#include "filter.h"
#include "gui.h"
char
*image_path_1 = NULL,
*image_path_2 = NULL,
*output_path = NULL
;
Image *image1, *image2;
void help(const char *);
int main(int argc, char *argv[]) {
// arg parsing
char
*filter_name = strdup(""),
*last_arg = strdup(""),
*curr_arg = strdup("")
;
bool show_gui = false;
int i = 1;
if (argc == 1) {
return 0;
}
for (;i<argc; i++) {
free(curr_arg);
curr_arg = strdup(argv[i]);
// printf("current argumante: %s\n", curr_arg);
if (strcmp(last_arg, "-i") == 0) {
goto image_asign;
}
if (strcmp(last_arg, "-f") == 0) {
filter_name = strdup(curr_arg);
goto end;
}
if (strcmp(last_arg, "-o") == 0) {
output_path = strdup(curr_arg);
goto end;
}
if (strcmp(curr_arg, "-i") == 0)
goto end;
if (strcmp(curr_arg, "-f") == 0)
goto end;
if (strcmp(curr_arg, "-o") == 0)
goto end;
if (strcmp(curr_arg, "-h") == 0) {
help(argv[0]);
goto end;
}
if (strcmp(curr_arg, "-ui") == 0) {
show_gui = true;
goto end;
}
if (strcmp(curr_arg, "-gui") == 0) {
show_gui = true;
goto end;
}
image_asign:
if (image_path_1 == NULL) {
image_path_1 = strdup(curr_arg);
goto end;
}
if (image_path_2 == NULL) {
image_path_2 = strdup(curr_arg);
goto end;
}
end:
free(last_arg);
last_arg = strdup(curr_arg);
}
if (image_path_1 == NULL) {
printf("Image path is not providded... exiting...");
exit(0);
}
if (output_path == NULL) {
output_path = (char*)malloc(8+strlen(image_path_1));
output_path[0] = '\0';
strcpy(output_path, "output-");
strcat(output_path, image_path_1);
}
if (strcmp(&output_path[strlen(output_path)-3], "png") != 0) {
int len = strlen(output_path);
char *t = malloc(len + 5);
strcpy(t, output_path);
strcat(t, ".png");
free(output_path);
output_path = t;
}
// filter prosennig
image1 = load_image(image_path_1);
image2 = image_path_2 == NULL ? NULL : load_image(image_path_2);
Filter *filter = filter_by_name(filter_name);
if (show_gui) {
return gui();
}
if (!filter) {
printf("Filter not selected, use -f flag to select filter... exiting...");
exit(0);
}
if (filter->argc > 1 && !image2) {
printf("%s filter require %d args... exiting...", filter->name, filter->argc);
exit(1);
}
Image *filtered;
switch (filter->argc) {
case 1:
filtered = filter->arg_1(image1);
break;
case 2:
filtered = filter->arg_2(image1, image2);
break;
}
bool saved = save_image(filtered, output_path);
if (!saved)
printf("Filtered image didn't saved");
else
printf("Filtered image (%d pixels) saved!!", filtered->width*filtered->height);
}
void help(const char* exe) {
for (int i=0;i<80;i++)printf("-");
printf("\n");
printf("Image Filter Utility\n");
printf("Usage: \n");
printf("\n");
printf("%s [OPTS] [ARGS] ", exe);
printf("\n\n");
printf("OPTS:\n");
printf("\t-i :\tInput Image\n");
printf("\t-f :\tFilter name\n");
printf("\t-o :\tOutput file\n");
printf("\t-h :\tPrit this\n");
printf("\n");
printf("Filters:\n");
for (int i=0; i<filter_count; i++) {
printf("\t%s\n", filters[i].name);
}
for (int i=0;i<80;i++)printf("-");
printf("\n\n");
}