[gegl/soc-2013-n-point-deformation: 15/28] libs: npd: add alpha blending



commit ac56129db298d1aa46ad43abf3f25dac3f4c795d
Author: Marek Dvoroznak <dvoromar gmail com>
Date:   Thu Aug 15 16:12:51 2013 +0200

    libs: npd: add alpha blending

 libs/npd/graphics.c       |   33 +++++++++++++-
 libs/npd/graphics.h       |  114 ++++++++++++++++++++++++---------------------
 operations/external/npd.c |    2 +-
 3 files changed, 94 insertions(+), 55 deletions(-)
---
diff --git a/libs/npd/graphics.c b/libs/npd/graphics.c
index e4f0878..ef0644d 100644
--- a/libs/npd/graphics.c
+++ b/libs/npd/graphics.c
@@ -183,6 +183,33 @@ npd_bilinear_color_interpolation (NPDColor *I0,
   out->a = npd_bilinear_interpolation (I0->a, I1->a, I2->a, I3->a, dx, dy);
 }
 
+gint
+npd_blend_band (gint   src,
+                gint   dst,
+                gfloat src_alpha,
+                gfloat dst_alpha,
+                gfloat out_alpha_recip)
+{
+  return floor ((src * src_alpha +
+                 dst * dst_alpha * (1 - src_alpha)) * out_alpha_recip);
+}
+
+void
+npd_blend_colors (NPDColor *src,
+                  NPDColor *dst,
+                  NPDColor *out_color)
+{
+  gfloat src_A = src->a / 255.0,
+         dst_A = dst->a / 255.0;
+  gfloat out_alpha = src_A + dst_A * (1 - src_A);
+  gfloat out_alpha_recip = 1 / out_alpha;
+
+  out_color->r = npd_blend_band (src->r, dst->r, src_A, dst_A, out_alpha_recip);
+  out_color->g = npd_blend_band (src->g, dst->g, src_A, dst_A, out_alpha_recip);
+  out_color->b = npd_blend_band (src->b, dst->b, src_A, dst_A, out_alpha_recip);
+  out_color->a = out_alpha * 255;
+}
+
 void
 npd_texture_fill_triangle (gint x1,
                            gint y1,
@@ -333,7 +360,7 @@ npd_draw_texture_line (gint        x1,
   for (x = x1; x <= x2; x++)
     {
       NPDPoint p, q;
-      NPDColor I0, I1, I2, I3, interpolated;
+      NPDColor I0, I1, I2, I3, interpolated, dest;
 
       q.x = x; q.y = y;
       npd_apply_transformation(A, &q, &p);
@@ -350,6 +377,10 @@ npd_draw_texture_line (gint        x1,
       npd_get_pixel_color(input_image, fx+1, fy+1, &I3);
       npd_bilinear_color_interpolation(&I0, &I1, &I2, &I3, dx, dy, &interpolated);
 
+      /* alpha blending */
+      npd_get_pixel_color (output_image, x, y, &dest);
+      npd_blend_colors (&interpolated, &dest, &interpolated);
+
       npd_set_pixel_color (output_image, x, y, &interpolated);
     }
 }
diff --git a/libs/npd/graphics.h b/libs/npd/graphics.h
index 691ec4f..ba1c8f7 100644
--- a/libs/npd/graphics.h
+++ b/libs/npd/graphics.h
@@ -29,64 +29,72 @@ struct _NPDColor {
     unsigned char a;
 };
 
-void        npd_create_model_from_image       (NPDModel *model,
-                                               NPDImage *image,
-                                               gint      square_size);
-void        npd_create_mesh_from_image        (NPDModel *model,
-                                               gint      width,
-                                               gint      height,
-                                               gint      position_x,
-                                               gint      position_y);
-void        npd_draw_model                    (NPDModel *model,
+void        npd_create_model_from_image       (NPDModel   *model,
+                                               NPDImage   *image,
+                                               gint        square_size);
+void        npd_create_mesh_from_image        (NPDModel   *model,
+                                               gint        width,
+                                               gint        height,
+                                               gint        position_x,
+                                               gint        position_y);
+void        npd_draw_model                    (NPDModel   *model,
                                                NPDDisplay *display);
 
-gboolean    npd_load_image                    (NPDImage *image,
+gboolean    npd_load_image                    (NPDImage   *image,
                                                const char *path);
-void        npd_destroy_image                 (NPDImage *image);
-void        npd_draw_image                    (NPDImage *image);
-void        npd_texture_fill_triangle         (gint      x1,
-                                               gint      y1,
-                                               gint      x2,
-                                               gint      y2,
-                                               gint      x3,
-                                               gint      y3,
-                                               NPDMatrix *A,
-                                               NPDImage *input_image,
-                                               NPDImage *output_image);
-void        npd_texture_quadrilateral         (NPDBone  *reference_bone,
-                                               NPDBone  *current_bone,
-                                               NPDImage *input_image,
-                                               NPDImage *output_image);
-void        npd_draw_texture_line             (gint       x1,
-                                               gint       x2,
-                                               gint       y,
-                                               NPDMatrix *A,
+void        npd_destroy_image                 (NPDImage   *image);
+//void        npd_draw_image                    (NPDImage *image);
+void        npd_texture_fill_triangle         (gint        x1,
+                                               gint        y1,
+                                               gint        x2,
+                                               gint        y2,
+                                               gint        x3,
+                                               gint        y3,
+                                               NPDMatrix  *A,
                                                NPDImage   *input_image,
                                                NPDImage   *output_image);
-gint        npd_bilinear_interpolation        (gint      I0,
-                                               gint      I1,
-                                               gint      I2,
-                                               gint      I3,
-                                               gfloat    dx,
-                                               gfloat    dy);
-void        npd_bilinear_color_interpolation  (NPDColor *I0,
-                                               NPDColor *I1,
-                                               NPDColor *I2,
-                                               NPDColor *I3,
-                                               gfloat    dx,
-                                               gfloat    dy,
-                                               NPDColor *out);
-void      (*npd_get_pixel_color)              (NPDImage *image,
-                                               gint      x,
-                                               gint      y,
-                                               NPDColor *color);
-void      (*npd_set_pixel_color)              (NPDImage *image,
-                                               gint      x,
-                                               gint      y,
-                                               NPDColor *color);
-gboolean    npd_compare_colors                (NPDColor *c1,
-                                               NPDColor *c2);
-gboolean    npd_is_color_transparent          (NPDColor *color);
+void        npd_texture_quadrilateral         (NPDBone    *reference_bone,
+                                               NPDBone    *current_bone,
+                                               NPDImage   *input_image,
+                                               NPDImage   *output_image);
+void        npd_draw_texture_line             (gint        x1,
+                                               gint        x2,
+                                               gint        y,
+                                               NPDMatrix  *A,
+                                               NPDImage   *input_image,
+                                               NPDImage   *output_image);
+gint        npd_bilinear_interpolation        (gint        I0,
+                                               gint        I1,
+                                               gint        I2,
+                                               gint        I3,
+                                               gfloat      dx,
+                                               gfloat      dy);
+void        npd_bilinear_color_interpolation  (NPDColor   *I0,
+                                               NPDColor   *I1,
+                                               NPDColor   *I2,
+                                               NPDColor   *I3,
+                                               gfloat      dx,
+                                               gfloat      dy,
+                                               NPDColor   *out);
+gint        npd_blend_band                    (gint        src,
+                                               gint        dst,
+                                               gfloat      src_alpha,
+                                               gfloat      dest_alpha,
+                                               gfloat      out_alpha);
+void        npd_blend_colors                  (NPDColor   *src,
+                                               NPDColor   *dst,
+                                               NPDColor   *out_color);
+void      (*npd_get_pixel_color)              (NPDImage   *image,
+                                               gint        x,
+                                               gint        y,
+                                               NPDColor   *color);
+void      (*npd_set_pixel_color)              (NPDImage   *image,
+                                               gint        x,
+                                               gint        y,
+                                               NPDColor   *color);
+gboolean    npd_compare_colors                (NPDColor   *c1,
+                                               NPDColor   *c2);
+gboolean    npd_is_color_transparent          (NPDColor   *color);
 gboolean    npd_init_display                  (NPDDisplay *display);
 void        npd_destroy_display               (NPDDisplay *display);
 
diff --git a/operations/external/npd.c b/operations/external/npd.c
index d8df11c..47839b1 100644
--- a/operations/external/npd.c
+++ b/operations/external/npd.c
@@ -127,7 +127,7 @@ npd_get_pixel_color_impl (NPDImage *image,
 }
 
 void
-npd_draw_model (NPDModel *model,
+npd_draw_model (NPDModel   *model,
                 NPDDisplay *display)
 {
   NPDHiddenModel *hidden_model = model->hidden_model;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]