diff options
author | jvech <jmvalenciae@unal.edu.co> | 2023-06-21 20:23:29 -0500 |
---|---|---|
committer | jvech <jmvalenciae@unal.edu.co> | 2023-06-21 20:23:29 -0500 |
commit | 290aa13ddd8d05bde86f14a82cd76c18b4b8f082 (patch) | |
tree | bd28aabf58f962721753f5b6e50309d390087148 /src/main.c | |
parent | d579b5e8bf669918908c894304efd66ee79f179e (diff) |
add: CLI support added
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 75 |
1 files changed, 60 insertions, 15 deletions
@@ -1,13 +1,23 @@ #include <stdio.h> #include <stdlib.h> #include <stdint.h> +#include <getopt.h> +#include "args.h" #include "ppm.h" static Image image_convolution(Image input, float kernel[9]); static void split_rgb(Image input, uint8_t *r, uint8_t *g, uint8_t *b); static void merge_rgb(Image *out, uint8_t *r, uint8_t *g, uint8_t *b); static uint8_t kernel_op(uint8_t *data, float kernel[9], int i, int j, int w, int h); +static void usage(void); + +void +usage(void) +{ + fprintf(stderr, "Usage: dsco [INPUT] -f [edge|blur] [-o OUTPUT]\n"); + exit(1); +} Image image_convolution(Image input, float kernel[9]) @@ -112,28 +122,63 @@ split_rgb(Image input, uint8_t *r, uint8_t *g, uint8_t *b) int main(int argc, char *argv[]) { - Image img; + int opt; + char *cli_arg; + char *filter, *out_file, *in_file; + + cli_arg = in_file = filter = out_file = NULL; + while ((opt = args_getopt(argc, argv, "hf:o:", &cli_arg)) != -1) { + switch (opt) { + case 'f': + filter = cli_arg; + break; + case 'o': + out_file = cli_arg; + break; + case 0: + in_file = cli_arg; + break; + default: + usage(); + } + } + + char default_input[] = "/dev/stdin"; + char default_output[] = "/dev/stdout"; + + if (!in_file) in_file = default_input; + if (!in_file) in_file = default_output; + float edge[9] = { -1, -1, -1, -1, 8, -1, -1, -1, -1, }; - float eye[9] = { - 0, 0, 0, - 0, 1, 0, - 0, 0, 0, + float blur[9] = { + 1, 2, 1, + 2, 4, 2, + 1, 2, 1, }; - float gauss[9] = { - 1, 2, 1, - 2, 4, 2, - 1, 2, 1, + + int i; + for (i = 0; i < 9; i++) blur[i] /= 16; + + float sharpen[9] = { + 0, -1, 0, + -1, 5, -1, + 0, -1, 0, }; - for (int i = 0; i < 9; i++) gauss[i] /= 16; - ppm_read("images/house_1.ppm", &img); - Image out_img = image_convolution(img, edge); - ppm_write("out1.ppm", &out_img); - out_img = image_convolution(out_img, gauss); - ppm_write("out2.ppm", &out_img); + + float *kernel = NULL; + if (!strcmp(filter, "edge")) kernel = edge; + else if (!strcmp(filter, "blur")) kernel = blur; + else if (!strcmp(filter, "sharpen")) kernel = sharpen; + else usage(); + + Image img, out; + ppm_read(in_file, &img); + out = image_convolution(img, kernel); + ppm_write(out_file, &out); return 0; } |