From d477d24d39c538bef36762d22ed2bd1978071370 Mon Sep 17 00:00:00 2001 From: yanenshuo Date: Mon, 2 Sep 2024 20:29:10 +0800 Subject: [PATCH] Add a pixel inversion program --- make.sh | 1 + samples/reverse.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 make.sh create mode 100644 samples/reverse.c diff --git a/make.sh b/make.sh new file mode 100644 index 0000000..5a71772 --- /dev/null +++ b/make.sh @@ -0,0 +1 @@ +gcc samples/resize_image.c sod.c -I./ -lm -o resver \ No newline at end of file diff --git a/samples/reverse.c b/samples/reverse.c new file mode 100644 index 0000000..bbf3065 --- /dev/null +++ b/samples/reverse.c @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include +#include +#include "sod.h" + +#define MAX_IMG_SZ 1024*1024 + +int main() +{ + unsigned char *zInpbuf = NULL; + + zInpbuf = malloc(MAX_IMG_SZ); + if (!zInpbuf) + { + perror("malloc"); + return -1; + } + + ssize_t imgSz = read(0, zInpbuf, MAX_IMG_SZ); + if (imgSz <= 0) + { + perror("read"); + free(zInpbuf); + return -1; + } + + sod_img imgIn = sod_img_load_from_mem(zInpbuf, imgSz, SOD_IMG_COLOR /* full color channels */); + if (imgIn.data == 0) { + /* Invalid path, unsupported format, memory failure, etc. */ + //puts("Cannot load input image..exiting"); + printf("Error loading input\n"); + free(zInpbuf); + return -1; + } + + for (int i = 0; i < imgIn.w * imgIn.h * imgIn.c; i++) + { + imgIn.data[i] = 255.0f - imgIn.data[i]; + } + sod_img_save_as_png(imgIn, NULL); + sod_free_image(imgIn); + free(zInpbuf); + return 0; +}