aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-07-27 10:14:41 +0200
committerkdx <kikoodx@paranoici.org>2023-07-27 10:14:41 +0200
commit78a3549185557c4970e5397d6d4a4c79a429d350 (patch)
treeb2c881474ab37ed3305fb8e559dc33116042bc00
parentb9bb06da3c4f60aedaa7bf2ce93de0db5b6fbd79 (diff)
downloadkld-78a3549185557c4970e5397d6d4a4c79a429d350.tar.gz
sub & subalpha
-rw-r--r--main.c7
-rw-r--r--src/image.c59
-rw-r--r--src/image.h8
3 files changed, 70 insertions, 4 deletions
diff --git a/main.c b/main.c
index 72c12ac..2cf7fa0 100644
--- a/main.c
+++ b/main.c
@@ -1,9 +1,10 @@
#include "image.h"
#include "myrand.h"
#include <time.h>
+#include <stdlib.h>
int
-main(void)
+main(int argc, char **argv)
{
mysrand(time(NULL));
@@ -15,8 +16,10 @@ main(void)
kldImage_vflip(cloud);
kldImage_invert(sky);
+ const int m = (argc > 1) ? atoi(argv[1]) : 255;
+ kldImage_mod(sky, KLD_COLOR(m, m, m, 255));
- kldImage_add(cloud, sky);
+ kldImage_sub(cloud, sky);
kldImage_writePAM(cloud, stdout);
kldImage_free(sky);
diff --git a/src/image.c b/src/image.c
index 2e57a5f..aa5024a 100644
--- a/src/image.c
+++ b/src/image.c
@@ -429,7 +429,7 @@ kldImage_bit3(KldImage *img)
}
void
-kldImage_mask(KldImage *img, KldColor mask)
+kldImage_mod(KldImage *img, KldColor mask)
{
NULL_SAFETY();
@@ -502,3 +502,60 @@ kldImage_add(KldImage *img, KldImage *other)
return 0;
}
+
+int
+kldImage_sub(KldImage *img, KldImage *other)
+{
+ NULL_SAFETY(-1);
+ NULL_SAFETY_EX(other, -1);
+
+ if (img->width != other->width || img->height != other->height) {
+ log_error("img (%lu x %lu) and other (%lu x %lu) "
+ "dims don't match",
+ img->width, img->height, other->width, other->height);
+ return -1;
+ }
+
+ for (size_t i = 0; i < img->size; i++) {
+ const KldColor c0 = img->data[i];
+ const KldColor c1 = other->data[i];
+ const int r = KLD_COLOR_R(c0) - KLD_COLOR_R(c1);
+ const int g = KLD_COLOR_G(c0) - KLD_COLOR_G(c1);
+ const int b = KLD_COLOR_B(c0) - KLD_COLOR_B(c1);
+ img->data[i] = KLD_COLOR(
+ (r < 0) ? 0 : r,
+ (g < 0) ? 0 : g,
+ (b < 0) ? 0 : b,
+ KLD_COLOR_A(c0)
+ );
+ }
+
+ return 0;
+}
+
+int
+kldImage_subalpha(KldImage *img, KldImage *other)
+{
+ NULL_SAFETY(-1);
+ NULL_SAFETY_EX(other, -1);
+
+ if (img->width != other->width || img->height != other->height) {
+ log_error("img (%lu x %lu) and other (%lu x %lu) "
+ "dims don't match",
+ img->width, img->height, other->width, other->height);
+ return -1;
+ }
+
+ for (size_t i = 0; i < img->size; i++) {
+ const KldColor c0 = img->data[i];
+ const int a = KLD_COLOR_A(c0) - KLD_COLOR_A(other->data[i]);
+ img->data[i] = KLD_COLOR(
+ KLD_COLOR_R(c0),
+ KLD_COLOR_G(c0),
+ KLD_COLOR_B(c0),
+ (a < 0) ? 0 : a
+ );
+ }
+
+ return 0;
+}
diff --git a/src/image.h b/src/image.h
index fbc8442..2c6b453 100644
--- a/src/image.h
+++ b/src/image.h
@@ -51,10 +51,16 @@ void kldImage_chaos3(KldImage *img);
void kldImage_bit3(KldImage *img);
-void kldImage_mask(KldImage *img, KldColor mask);
+void kldImage_mod(KldImage *img, KldColor mask);
/* return -1 if sizes are not equal */
int kldImage_blend(KldImage *img, KldImage *other, float ratio);
/* return -1 if sizes are not equal */
int kldImage_add(KldImage *img, KldImage *other);
+
+/* return -1 if sizes are not equal; ignores alpha */
+int kldImage_sub(KldImage *img, KldImage *other);
+
+/* return -1 if sizes are not equal */
+int kldImage_subalpha(KldImage *img, KldImage *other);