aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile5
-rw-r--r--src/args.c34
-rw-r--r--src/args.h5
-rw-r--r--src/cli.h3
-rw-r--r--src/main.c75
-rw-r--r--src/ppm.c10
6 files changed, 110 insertions, 22 deletions
diff --git a/Makefile b/Makefile
index 688f08e..c8c4a2d 100644
--- a/Makefile
+++ b/Makefile
@@ -24,11 +24,10 @@ run: build
./${BIN}
test: build
- ./${BIN}
- imv images/house_1.ppm out1.ppm out2.ppm
+ ./${BIN} images/house_1.ppm -f edge | imv images/house_1.ppm -
debug: $(BIN)
- lldb $< --tui
+ gdb $< --tui
clean:
@rm $(OBJS) $(OBJDIR) -rv
diff --git a/src/args.c b/src/args.c
new file mode 100644
index 0000000..6a516da
--- /dev/null
+++ b/src/args.c
@@ -0,0 +1,34 @@
+#include "args.h"
+int args_optind = 1;
+
+int
+args_getopt(int argc, char *argv[], const char *optstring, char **args_optarg)
+{
+ char option;
+ if (args_optind >= argc) return -1;
+
+ if (argv[args_optind][0] != '-'
+ || strlen(argv[args_optind]) > 2
+ || !strcmp(argv[args_optind], "-")) {
+
+ *args_optarg = argv[args_optind];
+ args_optind++;
+ return 0;
+ }
+
+ int i;
+ option = argv[args_optind][1];
+ for (i = 0; i < strlen(optstring); i++) {
+ if (optstring[i + 1] == ':' && optstring[i] == option) {
+ *args_optarg = argv[++args_optind];
+ args_optind++;
+ return option;
+ } else if (optstring[i + 1] != ':' && optstring[i] == option) {
+ args_optind++;
+ return option;
+ }
+ }
+ args_optarg = 0;
+ args_optind++;
+ return -2;
+}
diff --git a/src/args.h b/src/args.h
new file mode 100644
index 0000000..1bbc234
--- /dev/null
+++ b/src/args.h
@@ -0,0 +1,5 @@
+#ifndef __ARGS__
+#define __ARGS__
+#include <string.h>
+int args_getopt(int argc, char *argv[], const char *optstring, char **args_optarg);
+#endif
diff --git a/src/cli.h b/src/cli.h
new file mode 100644
index 0000000..8f6bc67
--- /dev/null
+++ b/src/cli.h
@@ -0,0 +1,3 @@
+#ifndef __CLI__
+#define __CLI__
+#endif
diff --git a/src/main.c b/src/main.c
index ec29a6b..370ece8 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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;
}
diff --git a/src/ppm.c b/src/ppm.c
index a5cf3fe..a868b76 100644
--- a/src/ppm.c
+++ b/src/ppm.c
@@ -9,7 +9,8 @@ ppm_write(const char *file, Image *img)
/*
* Write a ppm file
*/
- FILE *fp = fopen(file, "w");
+ FILE *fp;
+ fp = (file) ? fopen(file, "r") : stdout;
if (fp == NULL) {
perror("ppm_read() Error");
@@ -20,7 +21,7 @@ ppm_write(const char *file, Image *img)
fprintf(fp, "P6\n%d %d\n%d\n", img->width, img->height, img->pixel_bits);
fwrite(img->data, n_pixels, sizeof(uint8_t), fp);
- fclose(fp);
+ if (fp != stdout) fclose(fp);
}
void
@@ -29,7 +30,8 @@ ppm_read(const char *file, Image *img)
* Read a ppm file
*/
{
- FILE *fp = fopen(file, "r");
+ FILE *fp;
+ fp = (file) ? fopen(file, "r") : stdin;
if (fp == NULL) {
perror("ppm_read() Error");
@@ -50,7 +52,7 @@ ppm_read(const char *file, Image *img)
fclose(fp);
exit(1);
}
- fclose(fp);
+ if (fp != stdin) fclose(fp);
}
void
Feel free to download, copy and edit any repo