[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [47852] trunk/blender: add Anti-Aliasing ( very rough draft algorithm, NOT FINAL version) to raskter lib.

Peter Larabell xgl.asyliax at gmail.com
Wed Jun 13 21:57:24 CEST 2012


Revision: 47852
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=47852
Author:   xglasyliax
Date:     2012-06-13 19:57:23 +0000 (Wed, 13 Jun 2012)
Log Message:
-----------
add Anti-Aliasing (very rough draft algorithm, NOT FINAL version) to raskter lib. Code is still quite messy but will be replaced when final algo comes in anyway.

Modified Paths:
--------------
    trunk/blender/intern/raskter/raskter.c
    trunk/blender/intern/raskter/raskter.h
    trunk/blender/source/blender/blenkernel/BKE_mask.h
    trunk/blender/source/blender/blenkernel/intern/mask.c
    trunk/blender/source/blender/blenkernel/intern/sequencer.c
    trunk/blender/source/blender/blenkernel/intern/tracking.c
    trunk/blender/source/blender/compositor/nodes/COM_MaskNode.cpp
    trunk/blender/source/blender/compositor/operations/COM_MaskOperation.cpp
    trunk/blender/source/blender/compositor/operations/COM_MaskOperation.h
    trunk/blender/source/blender/editors/space_node/drawnode.c
    trunk/blender/source/blender/makesrna/intern/rna_nodetree.c
    trunk/blender/source/blender/nodes/composite/nodes/node_composite_mask.c

Modified: trunk/blender/intern/raskter/raskter.c
===================================================================
--- trunk/blender/intern/raskter/raskter.c	2012-06-13 19:56:13 UTC (rev 47851)
+++ trunk/blender/intern/raskter/raskter.c	2012-06-13 19:57:23 UTC (rev 47852)
@@ -34,8 +34,8 @@
 /* from BLI_utildefines.h */
 #define MIN2(x, y)               ( (x) < (y) ? (x) : (y) )
 #define MAX2(x, y)               ( (x) > (y) ? (x) : (y) )
+#define ABS(a)          ( (a) < 0 ? (-(a)) : (a) )
 
-
 struct e_status {
 	int x;
 	int ybeg;
@@ -67,8 +67,7 @@
  * just the poly. Since the DEM code could end up being coupled with this, we'll keep it separate
  * for now.
  */
-static void preprocess_all_edges(struct r_fill_context *ctx, struct poly_vert *verts, int num_verts, struct e_status *open_edge)
-{
+static void preprocess_all_edges(struct r_fill_context *ctx, struct poly_vert *verts, int num_verts, struct e_status *open_edge) {
 	int i;
 	int xbeg;
 	int ybeg;
@@ -94,8 +93,7 @@
 			/* we're not at the last vert, so end of the edge is the previous vertex */
 			xend = v[i - 1].x;
 			yend = v[i - 1].y;
-		}
-		else {
+		} else {
 			/* we're at the first vertex, so the "end" of this edge is the last vertex */
 			xend = v[num_verts - 1].x;
 			yend = v[num_verts - 1].y;
@@ -124,8 +122,7 @@
 			if (dx > 0) {
 				e_new->xdir = 1;
 				xdist = dx;
-			}
-			else {
+			} else {
 				e_new->xdir = -1;
 				xdist = -dx;
 			}
@@ -138,15 +135,13 @@
 			/* calculate deltas for incremental drawing */
 			if (dx >= 0) {
 				e_new->drift = 0;
-			}
-			else {
+			} else {
 				e_new->drift = -dy + 1;
 			}
 			if (dy >= xdist) {
 				e_new->drift_inc = xdist;
 				e_new->xshift = 0;
-			}
-			else {
+			} else {
 				e_new->drift_inc = xdist % dy;
 				e_new->xshift = (xdist / dy) * e_new->xdir;
 			}
@@ -170,8 +165,7 @@
  * for speed, but waiting on final design choices for curve-data before eliminating data the DEM code will need
  * if it ends up being coupled with this function.
  */
-static int rast_scan_fill(struct r_fill_context *ctx, struct poly_vert *verts, int num_verts)
-{
+static int rast_scan_fill(struct r_fill_context *ctx, struct poly_vert *verts, int num_verts, float intensity) {
 	int x_curr;                 /* current pixel position in X */
 	int y_curr;                 /* current scan line being drawn */
 	int yp;                     /* y-pixel's position in frame buffer */
@@ -260,8 +254,7 @@
 					edgec = &ctx->all_edges->e_next;     /* Set our list to the next edge's location in memory. */
 					ctx->all_edges = e_temp;             /* Skip the NULL or bad X edge, set pointer to next edge. */
 					break;                               /* Stop looping edges (since we ran out or hit empty X span. */
-				}
-				else {
+				} else {
 					edgec = &e_curr->e_next;             /* Set the pointer to the edge list the "next" edge. */
 				}
 			}
@@ -307,7 +300,7 @@
 
 			if ((y_curr >= 0) && (y_curr < ctx->rb.sizey)) {
 				/* draw the pixels. */
-				for (; cpxl <= mpxl; *cpxl++ = 1.0f);
+				for(; cpxl <= mpxl; *cpxl++ += intensity);
 			}
 		}
 
@@ -323,8 +316,7 @@
 		for (edgec = &ctx->possible_edges; (e_curr = *edgec); ) {
 			if (!(--(e_curr->num))) {
 				*edgec = e_curr->e_next;
-			}
-			else {
+			} else {
 				e_curr->x += e_curr->xshift;
 				if ((e_curr->drift += e_curr->drift_inc) > 0) {
 					e_curr->x += e_curr->xdir;
@@ -383,12 +375,17 @@
 }
 
 int PLX_raskterize(float (*base_verts)[2], int num_base_verts,
-                   float *buf, int buf_x, int buf_y)
-{
+				   float *buf, int buf_x, int buf_y, int do_mask_AA) {
+	int subdiv_AA = (do_mask_AA != 0)? 8:0;
 	int i;                                   /* i: Loop counter. */
+	int sAx;
+	int sAy;
 	struct poly_vert *ply;                   /* ply: Pointer to a list of integer buffer-space vertex coordinates. */
 	struct r_fill_context ctx = {0};
-
+	const float buf_x_f = (float)(buf_x);
+	const float buf_y_f = (float)(buf_y);
+	float div_offset=(1.0f / (float)(subdiv_AA));
+	float div_offset_static = 0.5f * (float)(subdiv_AA) * div_offset;
 	/*
 	 * Allocate enough memory for our poly_vert list. It'll be the size of the poly_vert
 	 * data structure multiplied by the number of base_verts.
@@ -400,6 +397,9 @@
 		return(0);
 	}
 
+	ctx.rb.buf = buf;                            /* Set the output buffer pointer. */
+	ctx.rb.sizex = buf_x;                        /* Set the output buffer size in X. (width) */
+	ctx.rb.sizey = buf_y;                        /* Set the output buffer size in Y. (height) */
 	/*
 	 * Loop over all verts passed in to be rasterized. Each vertex's X and Y coordinates are
 	 * then converted from normalized screen space (0.0 <= POS <= 1.0) to integer coordinates
@@ -408,16 +408,25 @@
 	 * It's worth noting that this function ONLY outputs fully white pixels in a mask. Every pixel
 	 * drawn will be 1.0f in value, there is no anti-aliasing.
 	 */
+
+	if(!subdiv_AA) {
 	for (i = 0; i < num_base_verts; i++) {                          /* Loop over all base_verts. */
-		ply[i].x = (base_verts[i][0] * buf_x) + 0.5f;       /* Range expand normalized X to integer buffer-space X. */
-		ply[i].y = (base_verts[i][1] * buf_y) + 0.5f; /* Range expand normalized Y to integer buffer-space Y. */
+			ply[i].x = (int)((base_verts[i][0] * buf_x_f) + 0.5f);       /* Range expand normalized X to integer buffer-space X. */
+			ply[i].y = (int)((base_verts[i][1] * buf_y_f) + 0.5f); /* Range expand normalized Y to integer buffer-space Y. */
 	}
 
-	ctx.rb.buf = buf;                            /* Set the output buffer pointer. */
-	ctx.rb.sizex = buf_x;                        /* Set the output buffer size in X. (width) */
-	ctx.rb.sizey = buf_y;                        /* Set the output buffer size in Y. (height) */
-
-	i = rast_scan_fill(&ctx, ply, num_base_verts);  /* Call our rasterizer, passing in the integer coords for each vert. */
+		i = rast_scan_fill(&ctx, ply, num_base_verts,1.0f);  /* Call our rasterizer, passing in the integer coords for each vert. */
+	} else {
+		for(sAx=0; sAx < subdiv_AA; sAx++) {
+			for(sAy=0; sAy < subdiv_AA; sAy++) {
+				for(i=0; i < num_base_verts; i++) {
+					ply[i].x = (int)((base_verts[i][0]*buf_x_f)+0.5f - div_offset_static + (div_offset*(float)(sAx)));
+					ply[i].y = (int)((base_verts[i][1]*buf_y_f)+0.5f - div_offset_static + (div_offset*(float)(sAy)));
+				}
+				i = rast_scan_fill(&ctx, ply, num_base_verts,(1.0f / (float)(subdiv_AA*subdiv_AA)));
+			}
+		}
+	}
 	free(ply);                                      /* Free the memory allocated for the integer coordinate table. */
 	return(i);                                      /* Return the value returned by the rasterizer. */
 }
@@ -429,8 +438,7 @@
  */
 static int rast_scan_feather(struct r_fill_context *ctx,
                              float (*base_verts_f)[2], int num_base_verts,
-                             struct poly_vert *feather_verts, float (*feather_verts_f)[2], int num_feather_verts)
-{
+							 struct poly_vert *feather_verts, float(*feather_verts_f)[2], int num_feather_verts) {
 	int x_curr;                 /* current pixel position in X */
 	int y_curr;                 /* current scan line being drawn */
 	int yp;                     /* y-pixel's position in frame buffer */
@@ -536,8 +544,7 @@
 					edgec = &ctx->all_edges->e_next;     /* Set our list to the next edge's location in memory. */
 					ctx->all_edges = e_temp;             /* Skip the NULL or bad X edge, set pointer to next edge. */
 					break;                               /* Stop looping edges (since we ran out or hit empty X span. */
-				}
-				else {
+				} else {
 					edgec = &e_curr->e_next;             /* Set the pointer to the edge list the "next" edge. */
 				}
 			}
@@ -647,8 +654,7 @@
 		for (edgec = &ctx->possible_edges; (e_curr = *edgec); ) {
 			if (!(--(e_curr->num))) {
 				*edgec = e_curr->e_next;
-			}
-			else {
+			} else {
 				e_curr->x += e_curr->xshift;
 				if ((e_curr->drift += e_curr->drift_inc) > 0) {
 					e_curr->x += e_curr->xdir;
@@ -708,8 +714,7 @@
 }
 
 int PLX_raskterize_feather(float (*base_verts)[2], int num_base_verts, float (*feather_verts)[2], int num_feather_verts,
-                           float *buf, int buf_x, int buf_y)
-{
+						   float *buf, int buf_x, int buf_y) {
 	int i;                            /* i: Loop counter. */
 	struct poly_vert *fe;             /* fe: Pointer to a list of integer buffer-space feather vertex coords. */
 	struct r_fill_context ctx = {0};
@@ -751,3 +756,569 @@
 	free(fe);
 	return i;                                   /* Return the value returned by the rasterizer. */
 }
+
+int get_range_expanded_pixel_coord(float normalized_value, int max_value) {
+	return (int)((normalized_value * (float)(max_value)) + 0.5f);
+}
+
+float get_pixel_intensity(float *buf, int buf_x, int buf_y, int pos_x, int pos_y) {
+	if(pos_x < 0 || pos_x >= buf_x || pos_y < 0 || pos_y >= buf_y) {
+		return 0.0f;
+	}
+	return buf[(pos_y * buf_y) + buf_x];
+}
+
+float get_pixel_intensity_bilinear(float *buf, int buf_x, int buf_y, float u, float v) {
+	int a;
+	int b;
+	int a_plus_1;
+	int b_plus_1;
+	float prop_u;
+	float prop_v;
+	float inv_prop_u;
+	float inv_prop_v;
+	if(u<0.0f || u>1.0f || v<0.0f || v>1.0f) {
+		return 0.0f;
+	}
+	u = u * (float)(buf_x) - 0.5f;
+	v = v * (float)(buf_y) - 0.5f;
+	a = (int)(u);
+	b = (int)(v);
+	prop_u = u - (float)(a);
+	prop_v = v - (float)(b);
+	inv_prop_u = 1.0f - prop_u;
+	inv_prop_v = 1.0f - prop_v;
+	a_plus_1 = MIN2((buf_x-1),a+1);
+	b_plus_1 = MIN2((buf_y-1),b+1);
+	return (buf[(b * buf_y) + a] * inv_prop_u + buf[(b*buf_y)+(a_plus_1)] * prop_u)*inv_prop_v+(buf[((b_plus_1) * buf_y)+a] * inv_prop_u + buf[((b_plus_1)*buf_y)+(a_plus_1)] * prop_u) * prop_v;
+
+}
+
+void set_pixel_intensity(float *buf, int buf_x, int buf_y, int pos_x, int pos_y, float intensity) {
+	if(pos_x < 0 || pos_x >= buf_x || pos_y < 0 || pos_y >= buf_y) {
+		return;
+	}
+	buf[(pos_y * buf_y) + buf_x] = intensity;
+}
+#define __PLX__FAKE_AA__
+int PLX_antialias_buffer(float *buf, int buf_x, int buf_y) {
+#ifdef __PLX__FAKE_AA__
+#ifdef __PLX_GREY_AA__
+	int i=0;
+	int sz = buf_x * buf_y;
+	for(i=0; i<sz; i++) {
+		buf[i] *= 0.5f;
+	}
+#endif
+	return 1;
+#else
+	/*XXX - TODO: THIS IS NOT FINAL CODE - IT DOES NOT WORK - DO NOT ENABLE IT */
+	const float p0 = 1.0f;
+	const float p1 = 1.0f;
+	const float p2 = 1.0f;
+	const float p3 = 1.0f;
+	const float p4 = 1.0f;
+	const float p5 = 1.5f;
+	const float p6 = 2.0f;

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list