[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [13759] trunk/blender/source/blender: Further work on the premul option for ton.

Joseph Eagar joeedh at gmail.com
Tue Feb 19 00:50:15 CET 2008


Revision: 13759
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=13759
Author:   joeedh
Date:     2008-02-19 00:50:12 +0100 (Tue, 19 Feb 2008)

Log Message:
-----------
Further work on the premul option for ton.  This option
(which basically tells the renderer and compositor to expect a
key image) is now done at the image user level.

This does have some caveats, as image users don't always work
the way I thought they would/should (for example, the same image user
structure is apparently used in the uv image editor for all images,
which is kindof odd).

The UV image editor also now smartly detects if the premul option is
set and draws the image using key alpha, instead of premul

The subversion level was upped to convert the old premul flag, which was at
the image level, to the new one, which is at the image user level.

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/BKE_blender.h
    trunk/blender/source/blender/blenloader/intern/readfile.c
    trunk/blender/source/blender/makesdna/DNA_image_types.h
    trunk/blender/source/blender/nodes/intern/CMP_nodes/CMP_image.c
    trunk/blender/source/blender/render/intern/source/texture.c
    trunk/blender/source/blender/src/buttons_shading.c
    trunk/blender/source/blender/src/drawimage.c
    trunk/blender/source/blender/src/drawnode.c

Modified: trunk/blender/source/blender/blenkernel/BKE_blender.h
===================================================================
--- trunk/blender/source/blender/blenkernel/BKE_blender.h	2008-02-18 23:00:44 UTC (rev 13758)
+++ trunk/blender/source/blender/blenkernel/BKE_blender.h	2008-02-18 23:50:12 UTC (rev 13759)
@@ -44,7 +44,7 @@
 struct MemFile;
 
 #define BLENDER_VERSION			245
-#define BLENDER_SUBVERSION		14
+#define BLENDER_SUBVERSION		15
 
 #define BLENDER_MINVERSION		240
 #define BLENDER_MINSUBVERSION	0

Modified: trunk/blender/source/blender/blenloader/intern/readfile.c
===================================================================
--- trunk/blender/source/blender/blenloader/intern/readfile.c	2008-02-18 23:00:44 UTC (rev 13758)
+++ trunk/blender/source/blender/blenloader/intern/readfile.c	2008-02-18 23:50:12 UTC (rev 13759)
@@ -2419,6 +2419,7 @@
 		if(tex->id.flag & LIB_NEEDLINK) {
 
 			tex->ima= newlibadr_us(fd, tex->id.lib, tex->ima);
+
 			tex->ipo= newlibadr_us(fd, tex->id.lib, tex->ipo);
 			if(tex->env) tex->env->object= newlibadr(fd, tex->id.lib, tex->env->object);
 
@@ -7450,7 +7451,56 @@
 		}
 	}
 
-	
+	/*version patch to migrate premul flag from images to image users*/
+	if (main->versionfile < 245 || (main->versionfile == 245 && main->subversionfile < 15)) {
+		Tex *tex;
+		Image *image;
+		ImageUser *iuser;
+		Scene *scene;
+		bNode *node;
+		bNodeTree *ntree;
+		
+		/*handle image textures*/
+		for (tex=main->tex.first; tex; tex=tex->id.next) {
+			if (tex->ima) {
+				image = newlibadr(fd, lib, tex->ima);
+				if (image->flag & IMA_OLDFLAG) tex->iuser.flag |= IMA_DO_PREMUL;
+			}
+		}
+
+		/*handle composite node trees*/
+		for (scene=main->scene.first; scene; scene=scene->id.next) {
+			if (scene->nodetree) {
+				for (node=scene->nodetree->nodes.first; node; node=node->next) {
+					ID *nodeid = newlibadr(fd, lib, node->id);
+					if (node->storage && nodeid && *(short*)nodeid->name == ID_IM) {
+						image = (Image*) nodeid;
+						iuser = node->storage;
+						if (image->flag & IMA_OLDFLAG) iuser->flag |= IMA_DO_PREMUL;
+					}
+				}
+			}
+		}
+
+		/*handle node groups*/
+		for (ntree=main->nodetree.first; ntree; ntree=ntree->id.next) {
+			if (ntree->type == NTREE_COMPOSIT) {
+				for (node=ntree->nodes.first; node; node=node->next) {
+					ID *nodeid = newlibadr(fd, lib, node->id);
+					if (node->storage && nodeid && *(short*)nodeid->name == ID_IM) {
+						image = (Image*) nodeid;
+						iuser = node->storage;
+						if (image->flag & IMA_OLDFLAG) iuser->flag |= IMA_DO_PREMUL;
+					}
+				}
+			}
+		}
+
+		/*finally, remove the flag from all images*/
+		for (image=main->image.first; image; image=image->id.next) {
+			if (image->flag & IMA_OLDFLAG) image->flag &= ~IMA_OLDFLAG;
+		}
+	}
 	/* WATCH IT!!!: pointers from libdata have not been converted yet here! */
 	/* WATCH IT 2!: Userdef struct init has to be in src/usiblender.c! */
 

Modified: trunk/blender/source/blender/makesdna/DNA_image_types.h
===================================================================
--- trunk/blender/source/blender/makesdna/DNA_image_types.h	2008-02-18 23:00:44 UTC (rev 13758)
+++ trunk/blender/source/blender/makesdna/DNA_image_types.h	2008-02-18 23:50:12 UTC (rev 13759)
@@ -58,6 +58,7 @@
 /* iuser->flag */
 #define	IMA_ANIM_ALWAYS		1
 #define IMA_ANIM_REFRESHED	2
+#define IMA_DO_PREMUL		4
 
 typedef struct Image {
 	ID id;
@@ -108,8 +109,13 @@
 #define	IMA_REFLECT		16
 #define IMA_NOCOLLECT   32
 #define IMA_ANTIALI		64
-#define IMA_DO_PREMUL	128
 
+/*used to be IMA_DO_PREMUL.  Note that
+  in theory, in should be possible
+  to use this flag position if necassary,
+  since this is only used in do_versions.*/
+#define IMA_OLDFLAG		128 
+
 /* tpageflag */
 #define IMA_TILES			1
 #define IMA_TWINANIM		2

Modified: trunk/blender/source/blender/nodes/intern/CMP_nodes/CMP_image.c
===================================================================
--- trunk/blender/source/blender/nodes/intern/CMP_nodes/CMP_image.c	2008-02-18 23:00:44 UTC (rev 13758)
+++ trunk/blender/source/blender/nodes/intern/CMP_nodes/CMP_image.c	2008-02-18 23:50:12 UTC (rev 13759)
@@ -58,7 +58,7 @@
 static CompBuf *node_composit_get_image(RenderData *rd, Image *ima, ImageUser *iuser)
 {
 	ImBuf *ibuf;
-	CompBuf *stackbuf;
+	CompBuf *stackbuf, *old;
 	int type;
 	
 	ibuf= BKE_image_get_ibuf(ima, iuser);
@@ -79,10 +79,11 @@
 		stackbuf->rect= ibuf->rect_float;
 	}
 	
-	/*code to respect the premul flag of images; I'm
-	  not sure if this is a good idea for multilayer images,
-	  since it never worked before for them.
-	if (type==CB_RGBA && ima->flag & IMA_DO_PREMUL) {
+	old = stackbuf;
+	stackbuf = dupalloc_compbuf(stackbuf);
+	free_compbuf(old);	
+
+	if (type==CB_RGBA && iuser && (iuser->flag & IMA_DO_PREMUL)) {
 		//premul the image
 		int i;
 		float *pixel = stackbuf->rect;
@@ -93,7 +94,7 @@
 			pixel[2] *= pixel[3];
 		}
 	}
-	*/
+	
 	return stackbuf;
 };
 
@@ -202,27 +203,6 @@
 		else {
 			stackbuf= node_composit_get_image(rd, ima, iuser);
 			
-			/*respect image premul option*/
-			if (stackbuf->type==CB_RGBA && ima->flag & IMA_DO_PREMUL) {
-				int i;
-				float *pixel;
-			
-				/*first duplicate stackbuf->rect, since it's just a pointer
-				  to the source imbuf, and we don't want to change that.*/
-				stackbuf->rect = MEM_dupallocN(stackbuf->rect);
-				
-				/*flag that we can free the buffer.*/
-				stackbuf->malloc = 1;
-				
-				/*premul the image*/				
-				pixel = stackbuf->rect;
-				for (i=0; i<stackbuf->x*stackbuf->y; i++, pixel += 4) {
-					pixel[0] *= pixel[3];
-					pixel[1] *= pixel[3];
-					pixel[2] *= pixel[3];
-				}
-			}
-			
 			/* put image on stack */	
 			out[0]->data= stackbuf;
 			

Modified: trunk/blender/source/blender/render/intern/source/texture.c
===================================================================
--- trunk/blender/source/blender/render/intern/source/texture.c	2008-02-18 23:00:44 UTC (rev 13758)
+++ trunk/blender/source/blender/render/intern/source/texture.c	2008-02-18 23:50:12 UTC (rev 13759)
@@ -1140,7 +1140,7 @@
 		tag_image_time(tex->ima); /* tag image as having being used */
 		
 		/*do premul if necassary*/
-		if (tex->ima && tex->ima->flag & IMA_DO_PREMUL) {
+		if (tex->iuser.flag & IMA_DO_PREMUL) {
 			texres->tr *= texres->ta;
 			texres->tg *= texres->ta;
 			texres->tb *= texres->ta;

Modified: trunk/blender/source/blender/src/buttons_shading.c
===================================================================
--- trunk/blender/source/blender/src/buttons_shading.c	2008-02-18 23:00:44 UTC (rev 13758)
+++ trunk/blender/source/blender/src/buttons_shading.c	2008-02-18 23:50:12 UTC (rev 13759)
@@ -1181,7 +1181,7 @@
 		 
 		 uiBlockSetFunc(block, image_reload_cb, ima, iuser);
 		 uiDefButBitS(block, TOG, IMA_ANTIALI, B_NOP, "Anti",			10, 50, 45, 20, &ima->flag, 0, 0, 0, 0, "Toggles Image anti-aliasing, only works with solid colors");
-		 uiDefButBitS(block, TOG, IMA_DO_PREMUL, imagechanged, "Premul",		55, 50, 65, 20, &ima->flag, 0, 0, 0, 0, "Toggles premultiplying alpha");
+		 uiDefButBitS(block, TOG, IMA_DO_PREMUL, imagechanged, "Premul",		55, 50, 65, 20, &iuser->flag, 0, 0, 0, 0, "Toggles whether to premultiply the image at render/composite time.");
 		 uiBlockEndAlign(block);
 		 
 		 if( ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
@@ -2559,7 +2559,7 @@
 				tip= "Irregular buffer produces sharp shadow always, but it doesn't show up for raytracing";
 			else if(la->buftype==LA_SHADBUF_HALFWAY)
 				tip= "Regular buffer, averaging the closest and 2nd closest Z value for reducing biasing";
-			
+				
 			uiDefButC(block, MENU, B_REDR, "Classical %x0|Classic-Halfway %x2|Irregular %x1", 10,140,80,19,&la->buftype, 0, 0, 0, 0, tip);
 		}
 	}
@@ -2655,7 +2655,6 @@
 			}
 		}
 		
-		
 	}
 	else uiDefBut(block, LABEL,0," ",	100,180,200,19,NULL, 0, 0, 0, 0, "");
 

Modified: trunk/blender/source/blender/src/drawimage.c
===================================================================
--- trunk/blender/source/blender/src/drawimage.c	2008-02-18 23:00:44 UTC (rev 13758)
+++ trunk/blender/source/blender/src/drawimage.c	2008-02-18 23:50:12 UTC (rev 13759)
@@ -2060,7 +2060,11 @@
 							if(sima->flag & SI_USE_ALPHA) {
 								sima_draw_alpha_backdrop(sima, x1_rep, y1_rep, (float)ibuf->x, (float)ibuf->y);
 								glEnable(GL_BLEND);
-								glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+								/*use key alpha if the IMA_DO_PREMUL option is set.*/
+								if (sima->iuser.flag & IMA_DO_PREMUL)
+									glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+								else
+									glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
 							}
 							
 							/* detect if we need to redo the curve map. 

Modified: trunk/blender/source/blender/src/drawnode.c
===================================================================
--- trunk/blender/source/blender/src/drawnode.c	2008-02-18 23:00:44 UTC (rev 13758)
+++ trunk/blender/source/blender/src/drawnode.c	2008-02-18 23:50:12 UTC (rev 13759)
@@ -938,7 +938,7 @@
 {
 	
 	ntreeCompositForceHidden(G.scene->nodetree);
-	BKE_image_multilayer_index(ima_v, iuser_v);
+	BKE_image_multilayer_index(BKE_image_get_renderresult(ima_v), iuser_v);
 	allqueue(REDRAWNODE, 0);
 }
 
@@ -995,6 +995,11 @@
 			uiButSetFunc(bt, node_image_type_cb, node, ima);
 			MEM_freeN(strp);
 			
+			if (iuser) {
+				dy -= 19;
+				uiDefButBitS(block, TOG, IMA_DO_PREMUL, B_NODE_EXEC+node->nr, "Premul", xmin, dy, xmax-xmin, 20, &iuser->flag, 0, 0, 0, 0, "Toggles whether to premultiply the image at render/composite time.");
+			}
+
 			if( ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE) ) {
 				width= (xmax-xmin)/2;
 				
@@ -1034,7 +1039,7 @@
 	}	
 	if(node->id) {
 		Image *ima= (Image *)node->id;
-		int retval= 19;
+		int retval= 19*2;
 		
 		/* for each draw we test for anim refresh event */
 		if(iuser->flag & IMA_ANIM_REFRESHED) {





More information about the Bf-blender-cvs mailing list