diff options
-rw-r--r-- | src/blender/blender.c | 29 | ||||
-rw-r--r-- | test/blender/blender.c | 29 |
2 files changed, 55 insertions, 3 deletions
diff --git a/src/blender/blender.c b/src/blender/blender.c index 0e0835d..738811d 100644 --- a/src/blender/blender.c +++ b/src/blender/blender.c | |||
@@ -1,6 +1,7 @@ | |||
1 | #include "blender/blender.h" | 1 | #include "blender/blender.h" |
2 | #include <assert.h> | 2 | #include <assert.h> |
3 | #include <math.h> | 3 | #include <math.h> |
4 | #include "morpher/morpher.h" | ||
4 | 5 | ||
5 | static ColorComponent blend_components(ColorComponent origin, ColorComponent target, TimeVector frame) { | 6 | static ColorComponent blend_components(ColorComponent origin, ColorComponent target, TimeVector frame) { |
6 | return (ColorComponent) sqrt((TIME_UNIT - frame) * pow(origin, 2) + frame * pow(target, 2)); | 7 | return (ColorComponent) sqrt((TIME_UNIT - frame) * pow(origin, 2) + frame * pow(target, 2)); |
@@ -13,3 +14,31 @@ Color blender_blend_colors(Color origin, Color target, TimeVector frame) { | |||
13 | blend_components(origin.rgba.b, target.rgba.b, frame), | 14 | blend_components(origin.rgba.b, target.rgba.b, frame), |
14 | blend_components(origin.rgba.a, target.rgba.a, frame)}}; | 15 | blend_components(origin.rgba.a, target.rgba.a, frame)}}; |
15 | } | 16 | } |
17 | |||
18 | void blender_blend_canvas(Canvas *canvas, Canvas *source, Canvas *target, Morphing *morphing, TimeVector frame) { | ||
19 | IntVector flat_dim; | ||
20 | CartesianVector dim, point; | ||
21 | CartesianMapping mapping; | ||
22 | Color pixel; | ||
23 | |||
24 | dim = morpher_get_dim(morphing); | ||
25 | |||
26 | assert(dim.x > 0 && dim.y > 0); | ||
27 | assert(vector_equals(dim, canvas_get_dim(canvas))); | ||
28 | assert(vector_equals(dim, canvas_get_dim(source))); | ||
29 | assert(vector_equals(dim, canvas_get_dim(target))); | ||
30 | assert(frame >= TIME_ORIGIN && frame <= TIME_UNIT); | ||
31 | |||
32 | for (flat_dim = (dim.x - 1) * (dim.y - 1); flat_dim >= 0; --flat_dim) { | ||
33 | point.x = flat_dim % dim.y; | ||
34 | point.y = flat_dim / dim.y; | ||
35 | |||
36 | mapping = morpher_get_point_mapping(morphing, point, frame); | ||
37 | |||
38 | pixel = blender_blend_colors(canvas_get_pixel(source, mapping.origin), | ||
39 | canvas_get_pixel(target, mapping.target), | ||
40 | frame); | ||
41 | |||
42 | canvas_set_pixel(canvas, point, pixel); | ||
43 | } | ||
44 | } | ||
diff --git a/test/blender/blender.c b/test/blender/blender.c index 344ce04..9a43db0 100644 --- a/test/blender/blender.c +++ b/test/blender/blender.c | |||
@@ -2,14 +2,37 @@ | |||
2 | #include <assert.h> | 2 | #include <assert.h> |
3 | 3 | ||
4 | static void test_color_blending() { | 4 | static void test_color_blending() { |
5 | Color a = {{0xFF, 0xED, 0x00, 0x00}}; | 5 | Color origin = {{0xFF, 0xED, 0x00, 0x00}}; |
6 | Color b = {{0x00, 0x47, 0xAB, 0x00}}; | 6 | Color target = {{0x00, 0x47, 0xAB, 0x00}}; |
7 | Color result = blender_blend_colors(a, b, 0.125); | 7 | Color result = blender_blend_colors(origin, target, 0.125); |
8 | 8 | ||
9 | assert(color_equals(result, (Color) {{0xEE, 0xDF, 0x3C, 0x00}})); | 9 | assert(color_equals(result, (Color) {{0xEE, 0xDF, 0x3C, 0x00}})); |
10 | } | 10 | } |
11 | 11 | ||
12 | static void test_canvas_blending() { | ||
13 | Morphing morphing; | ||
14 | Canvas origin, target, result; | ||
15 | CartesianVector sample_point = {13, 17}; | ||
16 | |||
17 | morpher_init(&morphing, 64, 64); | ||
18 | canvas_init(&origin, 64, 64); | ||
19 | canvas_init(&target, 64, 64); | ||
20 | canvas_init(&result, 64, 64); | ||
21 | |||
22 | canvas_set_pixel(&origin, sample_point, (Color) {{0xFF, 0xED, 0x00, 0x00}}); | ||
23 | canvas_set_pixel(&target, sample_point, (Color) {{0x00, 0x47, 0xAB, 0x00}}); | ||
24 | |||
25 | blender_blend_canvas(&result, &origin, &target, &morphing, 0.125); | ||
26 | assert(color_equals(canvas_get_pixel(&result, sample_point), (Color) {{0xEE, 0xDF, 0x3C, 0x00}})); | ||
27 | |||
28 | canvas_free(&result); | ||
29 | canvas_free(&target); | ||
30 | canvas_free(&origin); | ||
31 | morpher_free(&morphing); | ||
32 | } | ||
33 | |||
12 | int main(int argc, char **argv) { | 34 | int main(int argc, char **argv) { |
13 | test_color_blending(); | 35 | test_color_blending(); |
36 | test_canvas_blending(); | ||
14 | return 0; | 37 | return 0; |
15 | } | 38 | } |