[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [56337] trunk/blender/source/blender: Fix part of #34233: bad alpha blending for 2D image painting.

Brecht Van Lommel brechtvanlommel at pandora.be
Sat Apr 27 14:51:24 CEST 2013


Revision: 56337
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=56337
Author:   blendix
Date:     2013-04-27 12:51:23 +0000 (Sat, 27 Apr 2013)
Log Message:
-----------
Fix part of #34233: bad alpha blending for 2D image painting. This is a very
old issue, the formulas here were never quite right, should all work ok now
with byte and float images.

Some differences:

* Colors with zero alpha from the background will never have an influence, so
  you don't get alpha fringes when painting over such areas. This does give
  hard edges when looking at the RGB channels alone, but there's no way to
  avoid that and fringes at the same time, same behavior as other painting apps.

* Add/Subtract/Multiply/Lighten/Darken now leave the alpha channel unchanged
  and work only the RGB channels, again same behavior as many other apps.

* Erase/Add alpha now compensates for premultiplied float images to keep the
  straight RGB colors the same.

Next: fix projection painting.

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/intern/brush.c
    trunk/blender/source/blender/blenkernel/intern/image_gen.c
    trunk/blender/source/blender/blenkernel/intern/mesh_validate.c
    trunk/blender/source/blender/blenlib/BLI_math_color.h
    trunk/blender/source/blender/blenlib/CMakeLists.txt
    trunk/blender/source/blender/blenlib/intern/math_base_inline.c
    trunk/blender/source/blender/editors/sculpt_paint/paint_image_2d.c
    trunk/blender/source/blender/editors/sculpt_paint/paint_image_proj.c
    trunk/blender/source/blender/imbuf/IMB_imbuf.h
    trunk/blender/source/blender/imbuf/intern/rectop.c
    trunk/blender/source/blender/makesrna/intern/rna_lamp.c
    trunk/blender/source/blender/makesrna/intern/rna_mesh.c

Added Paths:
-----------
    trunk/blender/source/blender/blenlib/BLI_math_color_blend.h
    trunk/blender/source/blender/blenlib/intern/math_color_blend_inline.c

Modified: trunk/blender/source/blender/blenkernel/intern/brush.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/brush.c	2013-04-27 11:56:17 UTC (rev 56336)
+++ trunk/blender/source/blender/blenkernel/intern/brush.c	2013-04-27 12:51:23 UTC (rev 56337)
@@ -518,7 +518,9 @@
 }
 
 /* Generic texture sampler for 3D painting systems. point has to be either in
- * region space mouse coordinates, or 3d world coordinates for 3D mapping */
+ * region space mouse coordinates, or 3d world coordinates for 3D mapping.
+ *
+ * rgba outputs straight alpha. */
 float BKE_brush_sample_tex_3D(const Scene *scene, Brush *br,
                               const float point[3],
                               float rgba[4], const int thread,
@@ -755,7 +757,10 @@
 	return intensity;
 }
 
-/* Brush Sampling for 2D brushes. when we unify the brush systems this will be necessarily a separate function */
+/* Brush Sampling for 2D brushes. when we unify the brush systems this will be
+ * necessarily a separate function.
+ *
+ * rgba outputs straight alpha. */
 float BKE_brush_sample_tex_2D(const Scene *scene, Brush *brush, const float xy[2], float rgba[4])
 {
 	UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
@@ -873,6 +878,11 @@
 					copy_v3_v3(dstf, brush_rgb);
 					dstf[3] = rgba[3] * alpha * BKE_brush_curve_strength_clamp(brush, len_v2(xy), radius);
 				}
+
+				/* output premultiplied alpha image */
+				dstf[0] *= dstf[3];
+				dstf[1] *= dstf[3];
+				dstf[2] *= dstf[3];
 			}
 		}
 	}

Modified: trunk/blender/source/blender/blenkernel/intern/image_gen.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/image_gen.c	2013-04-27 11:56:17 UTC (rev 56336)
+++ trunk/blender/source/blender/blenkernel/intern/image_gen.c	2013-04-27 12:51:23 UTC (rev 56337)
@@ -27,8 +27,9 @@
 #include <math.h>
 #include <stdlib.h>
 
+#include "BLI_math_base.h"
 #include "BLI_math_color.h"
-#include "BLI_math_base.h"
+#include "BLI_math_vector.h"
 
 #include "BKE_image.h"
 

Modified: trunk/blender/source/blender/blenkernel/intern/mesh_validate.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/mesh_validate.c	2013-04-27 11:56:17 UTC (rev 56336)
+++ trunk/blender/source/blender/blenkernel/intern/mesh_validate.c	2013-04-27 12:51:23 UTC (rev 56337)
@@ -40,6 +40,7 @@
 #include "BLI_utildefines.h"
 #include "BLI_edgehash.h"
 #include "BLI_math_base.h"
+#include "BLI_math_vector.h"
 
 #include "BKE_deform.h"
 #include "BKE_depsgraph.h"

Modified: trunk/blender/source/blender/blenlib/BLI_math_color.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_math_color.h	2013-04-27 11:56:17 UTC (rev 56336)
+++ trunk/blender/source/blender/blenlib/BLI_math_color.h	2013-04-27 12:51:23 UTC (rev 56337)
@@ -126,7 +126,7 @@
 
 MINLINE int compare_rgb_uchar(const unsigned char a[3], const unsigned char b[3], const int limit);
 
-/***************** lift/gamma/gain / ASC-CDL conversion *****************/
+/********* lift/gamma/gain / ASC-CDL conversion ***********/
 
 void lift_gamma_gain_to_asc_cdl(float *lift, float *gamma, float *gain, float *offset, float *slope, float *power);
 

Added: trunk/blender/source/blender/blenlib/BLI_math_color_blend.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_math_color_blend.h	                        (rev 0)
+++ trunk/blender/source/blender/blenlib/BLI_math_color_blend.h	2013-04-27 12:51:23 UTC (rev 56337)
@@ -0,0 +1,71 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * The Original Code is: some of this file.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ * */
+
+#ifndef __BLI_MATH_COLOR_BLEND_H__
+#define __BLI_MATH_COLOR_BLEND_H__
+
+/** \file BLI_math_color.h
+ *  \ingroup bli
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "BLI_math_inline.h"
+
+/******************** Blending Modes **********************
+ * - byte function assume straight alpha
+ * - float functions assume premultiplied alpha
+ */
+
+MINLINE void blend_color_mix_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4]);
+MINLINE void blend_color_add_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4]);
+MINLINE void blend_color_sub_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4]);
+MINLINE void blend_color_mul_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4]);
+MINLINE void blend_color_lighten_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4]);
+MINLINE void blend_color_darken_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4]);
+MINLINE void blend_color_erase_alpha_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4]);
+MINLINE void blend_color_add_alpha_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4]);
+
+MINLINE void blend_color_mix_float(float dst[4], const float src1[4], const float src2[4]);
+MINLINE void blend_color_add_float(float dst[4], const float src1[4], const float src2[4]);
+MINLINE void blend_color_sub_float(float dst[4], const float src1[4], const float src2[4]);
+MINLINE void blend_color_mul_float(float dst[4], const float src1[4], const float src2[4]);
+MINLINE void blend_color_lighten_float(float dst[4], const float src1[4], const float src2[4]);
+MINLINE void blend_color_darken_float(float dst[4], const float src1[4], const float src2[4]);
+MINLINE void blend_color_erase_alpha_float(float dst[4], const float src1[4], const float src2[4]);
+MINLINE void blend_color_add_alpha_float(float dst[4], const float src1[4], const float src2[4]);
+
+#if BLI_MATH_DO_INLINE
+#include "intern/math_color_blend_inline.c"
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __BLI_MATH_COLOR_BLEND_H__ */
+

Modified: trunk/blender/source/blender/blenlib/CMakeLists.txt
===================================================================
--- trunk/blender/source/blender/blenlib/CMakeLists.txt	2013-04-27 11:56:17 UTC (rev 56336)
+++ trunk/blender/source/blender/blenlib/CMakeLists.txt	2013-04-27 12:51:23 UTC (rev 56337)
@@ -70,6 +70,7 @@
 	intern/math_base.c
 	intern/math_base_inline.c
 	intern/math_color.c
+	intern/math_color_blend_inline.c
 	intern/math_color_inline.c
 	intern/math_geom.c
 	intern/math_geom_inline.c
@@ -128,6 +129,7 @@
 	BLI_math.h
 	BLI_math_base.h
 	BLI_math_color.h
+	BLI_math_color_blend.h
 	BLI_math_geom.h
 	BLI_math_inline.h
 	BLI_math_interp.h

Modified: trunk/blender/source/blender/blenlib/intern/math_base_inline.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/math_base_inline.c	2013-04-27 11:56:17 UTC (rev 56336)
+++ trunk/blender/source/blender/blenlib/intern/math_base_inline.c	2013-04-27 12:51:23 UTC (rev 56337)
@@ -35,7 +35,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include "BLI_math.h"
+#include "BLI_math_base.h"
 
 /* copied from BLI_utildefines.h */
 #ifdef __GNUC__

Added: trunk/blender/source/blender/blenlib/intern/math_color_blend_inline.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/math_color_blend_inline.c	                        (rev 0)
+++ trunk/blender/source/blender/blenlib/intern/math_color_blend_inline.c	2013-04-27 12:51:23 UTC (rev 56337)
@@ -0,0 +1,413 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * The Original Code is: some of this file.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ * */
+
+/** \file blender/blenlib/intern/math_color_inline.c
+ *  \ingroup bli
+ */
+
+
+#include "BLI_math_base.h"
+#include "BLI_math_color.h"
+#include "BLI_utildefines.h"
+
+#ifndef __MATH_COLOR_BLEND_INLINE_C__
+#define __MATH_COLOR_BLEND_INLINE_C__
+
+/***************************** Color Blending ********************************
+ *
+ * - byte colors are assumed to be straight alpha
+ * - byte colors uses to do >>8 (same as /256) but actually should do /255,
+ *   otherwise get quick darkening due to rounding
+ * - divide_round_i is also used to avoid darkening due to integers always
+ *   rounding down
+ * - float colors are assumed to be premultiplied alpha
+ */
+
+/* straight alpha byte blending modes */
+
+MINLINE void blend_color_mix_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4])
+{
+	if (src2[3] != 0) {
+		/* straight over operation */
+		const int t = src2[3];
+		const int mt = 255 - t;
+		int tmp[4];
+
+		tmp[0] = (mt * src1[3] * src1[0]) + (t * 255 * src2[0]);

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list