aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-07-27 06:41:14 +0200
committerkdx <kikoodx@paranoici.org>2023-07-27 06:41:25 +0200
commitd7b8205596486b60869858c9c46dd63bd1549724 (patch)
treee7fbca4d84685f2fd14e0073cc0817910e81ab2d
parent3acef62b1b142a523c5d1c5bbff37f9a3faac7a7 (diff)
downloadkld-d7b8205596486b60869858c9c46dd63bd1549724.tar.gz
blend
-rw-r--r--main.c23
-rw-r--r--samples/sky.jpgbin0 -> 457962 bytes
-rw-r--r--src/image.c44
-rw-r--r--src/image.h7
4 files changed, 57 insertions, 17 deletions
diff --git a/main.c b/main.c
index 8c5b29e..53e2c30 100644
--- a/main.c
+++ b/main.c
@@ -7,12 +7,21 @@ main(void)
{
mysrand(time(NULL));
- KldImage *img = kldImage_load("samples/cloud.jpg");
- kldImage_vflip(img);
- kldImage_bit3(img);
- kldImage_resize(img, 1024, 512, 0);
- kldImage_resizetiled(img, 2048, 2048);
- kldImage_writePAM(img, stdout);
- kldImage_free(img);
+ KldImage *cloud = kldImage_load("samples/cloud.jpg");
+ KldImage *sky = kldImage_load("samples/sky.jpg");
+
+ kldImage_resize(cloud, sky->width, sky->height, 0);
+
+ kldImage_vflip(cloud);
+ kldImage_chaos3(cloud);
+
+ kldImage_invert(sky);
+ kldImage_chaos3(sky);
+
+ kldImage_blend(cloud, sky, 0.5f);
+ kldImage_writePAM(cloud, stdout);
+
+ kldImage_free(sky);
+ kldImage_free(cloud);
return 0;
}
diff --git a/samples/sky.jpg b/samples/sky.jpg
new file mode 100644
index 0000000..fe10b5a
--- /dev/null
+++ b/samples/sky.jpg
Binary files differ
diff --git a/src/image.c b/src/image.c
index 6fac9b1..46daa23 100644
--- a/src/image.c
+++ b/src/image.c
@@ -171,7 +171,7 @@ kldImage_resize(KldImage *img, size_t w, size_t h, KldColor bg)
return -1;
}
kldImage_clear(new_img, bg);
- kldImage_copy(new_img, img, 0, 0);
+ kldImage_copy(new_img, img, 0, 0, 0, 0);
/* move new_img to img */
free(img->data);
@@ -195,7 +195,7 @@ kldImage_resizetiled(KldImage *img, size_t w, size_t h)
log_error("kldImage_alloc failed");
return -1;
}
- kldImage_copytiled(new_img, img, 0, 0);
+ kldImage_copytiled(new_img, img, 0, 0, 0, 0);
/* move new_img to img */
free(img->data);
@@ -255,7 +255,7 @@ kldImage_clear(KldImage *img, KldColor color)
}
void
-kldImage_copy(KldImage *dest, KldImage *src, int x, int y)
+kldImage_copy(KldImage *dest, KldImage *src, int x, int y, int w, int h)
{
NULL_SAFETY_EX_NO_RV(dest);
NULL_SAFETY_EX_NO_RV(src);
@@ -269,7 +269,7 @@ kldImage_copy(KldImage *dest, KldImage *src, int x, int y)
iy = 0;
dy = y;
}
- while (iy < src->height && dy < dest->height) {
+ while (iy < src->height && dy < dest->height && --w != 0) {
size_t dx;
size_t ix;
if (x < 0) {
@@ -279,7 +279,7 @@ kldImage_copy(KldImage *dest, KldImage *src, int x, int y)
ix = 0;
dx = x;
}
- while (ix < src->width && dx < dest->width) {
+ while (ix < src->width && dx < dest->width && --h != 0) {
kldImage_set(dest, dx, dy, kldImage_get(src, ix, iy));
dx += 1;
ix += 1;
@@ -290,17 +290,17 @@ kldImage_copy(KldImage *dest, KldImage *src, int x, int y)
}
void
-kldImage_copytiled(KldImage *dest, KldImage *src, int x, int y)
+kldImage_copytiled(KldImage *dest, KldImage *src, int x, int y, int w, int h)
{
NULL_SAFETY_EX_NO_RV(dest);
NULL_SAFETY_EX_NO_RV(src);
size_t iy = -y;
size_t dy = 0;
- while (dy < dest->height) {
+ while (dy < dest->height && --h != 0) {
size_t ix = -x;
size_t dx = 0;
- while (dx < dest->width) {
+ while (dx < dest->width && --w != 0) {
kldImage_set(dest, dx, dy, kldImage_get(src, ix, iy));
dx += 1;
ix += 1;
@@ -443,3 +443,31 @@ kldImage_mask(KldImage *img, KldColor mask)
);
}
}
+
+int
+kldImage_blend(KldImage *img, KldImage *other, float ratio)
+{
+ 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;
+ }
+
+ const float iratio = 1.0f - ratio;
+ for (size_t i = 0; i < img->size; i++) {
+ const KldColor c0 = img->data[i];
+ const KldColor c1 = other->data[i];
+ img->data[i] = KLD_COLOR(
+ (int)(KLD_COLOR_R(c0) * ratio + KLD_COLOR_R(c1) * iratio),
+ (int)(KLD_COLOR_G(c0) * ratio + KLD_COLOR_G(c1) * iratio),
+ (int)(KLD_COLOR_B(c0) * ratio + KLD_COLOR_B(c1) * iratio),
+ (int)(KLD_COLOR_A(c0) * ratio + KLD_COLOR_A(c1) * iratio)
+ );
+ }
+
+ return 0;
+}
diff --git a/src/image.h b/src/image.h
index 82e2090..afaaa44 100644
--- a/src/image.h
+++ b/src/image.h
@@ -29,9 +29,9 @@ int kldImage_writePAM(KldImage *img, FILE *fp);
void kldImage_clear(KldImage *img, KldColor color);
-void kldImage_copy(KldImage *dest, KldImage *src, int x, int y);
+void kldImage_copy(KldImage *dest, KldImage *src, int x, int y, int w, int h);
-void kldImage_copytiled(KldImage *dest, KldImage *src, int x, int y);
+void kldImage_copytiled(KldImage *dest, KldImage *src, int x, int y, int w, int h);
/* hflip should be faster than vflip */
void kldImage_hflip(KldImage *img);
@@ -52,3 +52,6 @@ void kldImage_chaos3(KldImage *img);
void kldImage_bit3(KldImage *img);
void kldImage_mask(KldImage *img, KldColor mask);
+
+/* return -1 if sizes are not equal */
+int kldImage_blend(KldImage *img, KldImage *other, float ratio);