[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [28236] branches/render25/source/blender: Render Branch: Fix some alpha premul problems with mipmap files by moving

Brecht Van Lommel brecht at blender.org
Fri Apr 16 17:25:43 CEST 2010


Revision: 28236
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=28236
Author:   blendix
Date:     2010-04-16 17:25:43 +0200 (Fri, 16 Apr 2010)

Log Message:
-----------
Render Branch: Fix some alpha premul problems with mipmap files by moving
the premul code to imbuf, still a small difference that needs to be fixed,
but no very bright pixels anymore hopefully.

Modified Paths:
--------------
    branches/render25/source/blender/blenkernel/BKE_image.h
    branches/render25/source/blender/blenkernel/intern/image.c
    branches/render25/source/blender/blenkernel/intern/sequencer.c
    branches/render25/source/blender/imbuf/IMB_imbuf.h
    branches/render25/source/blender/imbuf/IMB_imbuf_types.h
    branches/render25/source/blender/imbuf/intern/filter.c
    branches/render25/source/blender/imbuf/intern/readimage.c
    branches/render25/source/blender/imbuf/intern/tiff.c

Modified: branches/render25/source/blender/blenkernel/BKE_image.h
===================================================================
--- branches/render25/source/blender/blenkernel/BKE_image.h	2010-04-16 15:24:01 UTC (rev 28235)
+++ branches/render25/source/blender/blenkernel/BKE_image.h	2010-04-16 15:25:43 UTC (rev 28236)
@@ -55,7 +55,6 @@
 
 struct anim *openanim(char * name, int flags);
 
-void	converttopremul(struct ImBuf *ibuf);
 void	image_de_interlace(struct Image *ima, int odd);
 	
 void	tag_image_time(struct Image *ima);

Modified: branches/render25/source/blender/blenkernel/intern/image.c
===================================================================
--- branches/render25/source/blender/blenkernel/intern/image.c	2010-04-16 15:24:01 UTC (rev 28235)
+++ branches/render25/source/blender/blenkernel/intern/image.c	2010-04-16 15:25:43 UTC (rev 28236)
@@ -92,58 +92,6 @@
 
 /* ******** IMAGE PROCESSING ************* */
 
-/* used by sequencer and image premul option - IMA_DO_PREMUL */
-void converttopremul(struct ImBuf *ibuf)
-{
-	int x, y;
-	
-	if(ibuf==0) return;
-	if (ibuf->rect) {
-		int val;
-		char *cp;
-		if(ibuf->depth==24) {	/* put alpha at 255 */
-			cp= (char *)(ibuf->rect);
-			for(y=0; y<ibuf->y; y++) {
-				for(x=0; x<ibuf->x; x++, cp+=4) {
-					cp[3]= 255;
-				}
-			}
-		} else {
-			cp= (char *)(ibuf->rect);
-			for(y=0; y<ibuf->y; y++) {
-				for(x=0; x<ibuf->x; x++, cp+=4) {
-					val= cp[3];
-					cp[0]= (cp[0]*val)>>8;
-					cp[1]= (cp[1]*val)>>8;
-					cp[2]= (cp[2]*val)>>8;
-				}
-			}
-		}
-	}
-	if (ibuf->rect_float) {
-		float val;
-		float *cp;
-		if(ibuf->depth==24) {	/* put alpha at 1.0 */
-			cp= ibuf->rect_float;;
-			for(y=0; y<ibuf->y; y++) {
-				for(x=0; x<ibuf->x; x++, cp+=4) {
-					cp[3]= 1.0;
-				}
-			}
-		} else {
-			cp= ibuf->rect_float;
-			for(y=0; y<ibuf->y; y++) {
-				for(x=0; x<ibuf->x; x++, cp+=4) {
-					val= cp[3];
-					cp[0]= cp[0]*val;
-					cp[1]= cp[1]*val;
-					cp[2]= cp[2]*val;
-				}
-			}
-		}
-	}
-}
-
 static void de_interlace_ng(struct ImBuf *ibuf)	/* neogeo fields */
 {
 	struct ImBuf * tbuf1, * tbuf2;
@@ -1588,6 +1536,7 @@
 	struct ImBuf *ibuf;
 	unsigned short numlen;
 	char name[FILE_MAX], head[FILE_MAX], tail[FILE_MAX];
+	int flag;
 	
 	/* XXX temp stuff? */
 	if(ima->lastframe != frame)
@@ -1604,8 +1553,14 @@
 	else
 		BLI_path_abs(name, G.sce);
 	
+	flag= IB_rect|IB_multilayer;
+	if(iuser && iuser->flag & IMA_USECACHE)
+		flag |= IB_usecache;
+	if(ima->flag & IMA_DO_PREMUL)
+		flag |= IB_premul;
+
 	/* read ibuf */
-	ibuf = IMB_loadiffname(name, IB_rect|IB_multilayer);
+	ibuf = IMB_loadiffname(name, flag);
 	if(G.f & G_DEBUG) printf("loaded %s\n", name);
 	
 	if (ibuf) {
@@ -1625,10 +1580,6 @@
 		image_initialize_after_load(ima, ibuf);
 		image_assign_ibuf(ima, ibuf, 0, frame);
 #endif
-		
-		if(ima->flag & IMA_DO_PREMUL)
-			converttopremul(ibuf);
-		
 	}
 	else
 		ima->ok= 0;
@@ -1761,6 +1712,8 @@
 		flag= IB_rect|IB_multilayer|IB_metadata;
 		if(iuser && iuser->flag & IMA_USECACHE)
 			flag |= IB_usecache;
+		if(ima->flag & IMA_DO_PREMUL)
+			flag |= IB_premul;
 			
 		/* get the right string */
 		BLI_strncpy(str, ima->name, sizeof(str));
@@ -1794,9 +1747,6 @@
 			if ((ima->packedfile == NULL) && (G.fileflags & G_AUTOPACK))
 				ima->packedfile = newPackedFile(NULL, str);
 		}
-		
-		if(ima->flag & IMA_DO_PREMUL)
-			converttopremul(ibuf);
 	}
 	else
 		ima->ok= 0;

Modified: branches/render25/source/blender/blenkernel/intern/sequencer.c
===================================================================
--- branches/render25/source/blender/blenkernel/intern/sequencer.c	2010-04-16 15:24:01 UTC (rev 28235)
+++ branches/render25/source/blender/blenkernel/intern/sequencer.c	2010-04-16 15:25:43 UTC (rev 28236)
@@ -1772,7 +1772,7 @@
 
 	if(seq->flag & SEQ_MAKE_PREMUL) {
 		if(se->ibuf->depth == 32 && se->ibuf->zbuf == 0) {
-			converttopremul(se->ibuf);
+			IMB_premultiply_alpha(se->ibuf);
 		}
 	}
 

Modified: branches/render25/source/blender/imbuf/IMB_imbuf.h
===================================================================
--- branches/render25/source/blender/imbuf/IMB_imbuf.h	2010-04-16 15:24:01 UTC (rev 28235)
+++ branches/render25/source/blender/imbuf/IMB_imbuf.h	2010-04-16 15:25:43 UTC (rev 28236)
@@ -402,6 +402,10 @@
 void IMB_flipx(struct ImBuf *ibuf);
 void IMB_flipy(struct ImBuf * ibuf);
 
+/* Premultiply alpha */
+
+void IMB_premultiply_alpha(struct ImBuf *ibuf);
+
 /**
  *
  * @attention Defined in allocimbuf.c

Modified: branches/render25/source/blender/imbuf/IMB_imbuf_types.h
===================================================================
--- branches/render25/source/blender/imbuf/IMB_imbuf_types.h	2010-04-16 15:24:01 UTC (rev 28235)
+++ branches/render25/source/blender/imbuf/IMB_imbuf_types.h	2010-04-16 15:25:43 UTC (rev 28236)
@@ -141,6 +141,7 @@
 #define IB_metadata			(1 << 8)
 #define IB_animdeinterlace	(1 << 9)
 #define IB_usecache			(1 << 10)
+#define IB_premul			(1 << 11)
 
 /*
  * The bit flag is stored in the ImBuf.ftype variable.

Modified: branches/render25/source/blender/imbuf/intern/filter.c
===================================================================
--- branches/render25/source/blender/imbuf/intern/filter.c	2010-04-16 15:24:01 UTC (rev 28235)
+++ branches/render25/source/blender/imbuf/intern/filter.c	2010-04-16 15:25:43 UTC (rev 28236)
@@ -451,3 +451,59 @@
 	return levels;
 }
 
+void IMB_premultiply_alpha(ImBuf *ibuf)
+{
+	int x, y;
+	
+	if(ibuf==NULL)
+		return;
+
+	if(ibuf->rect) {
+		int val;
+		char *cp;
+
+		if(ibuf->depth==24) {	/* put alpha at 255 */
+			cp= (char *)(ibuf->rect);
+
+			for(y=0; y<ibuf->y; y++)
+				for(x=0; x<ibuf->x; x++, cp+=4)
+					cp[3]= 255;
+		}
+		else {
+			cp= (char *)(ibuf->rect);
+			for(y=0; y<ibuf->y; y++) {
+				for(x=0; x<ibuf->x; x++, cp+=4) {
+					val= cp[3];
+					cp[0]= (cp[0]*val)>>8;
+					cp[1]= (cp[1]*val)>>8;
+					cp[2]= (cp[2]*val)>>8;
+				}
+			}
+		}
+	}
+
+	if(ibuf->rect_float) {
+		float val;
+		float *cp;
+
+		if(ibuf->depth==24) {	/* put alpha at 1.0 */
+			cp= ibuf->rect_float;
+
+			for(y=0; y<ibuf->y; y++)
+				for(x=0; x<ibuf->x; x++, cp+=4)
+					cp[3]= 1.0;
+		}
+		else {
+			cp= ibuf->rect_float;
+			for(y=0; y<ibuf->y; y++) {
+				for(x=0; x<ibuf->x; x++, cp+=4) {
+					val= cp[3];
+					cp[0]= cp[0]*val;
+					cp[1]= cp[1]*val;
+					cp[2]= cp[2]*val;
+				}
+			}
+		}
+	}
+}
+

Modified: branches/render25/source/blender/imbuf/intern/readimage.c
===================================================================
--- branches/render25/source/blender/imbuf/intern/readimage.c	2010-04-16 15:24:01 UTC (rev 28235)
+++ branches/render25/source/blender/imbuf/intern/readimage.c	2010-04-16 15:25:43 UTC (rev 28236)
@@ -62,6 +62,12 @@
 			if(ibuf) {
 				if(!(ibuf->flags & IB_usecache))
 					ibuf->miplevels= IMB_getmipmaplevel_num(ibuf);
+
+				if(flags & IB_premul) {
+					IMB_premultiply_alpha(ibuf);
+					ibuf->flags |= IB_premul;
+				}
+
 				return ibuf;
 			}
 		}

Modified: branches/render25/source/blender/imbuf/intern/tiff.c
===================================================================
--- branches/render25/source/blender/imbuf/intern/tiff.c	2010-04-16 15:24:01 UTC (rev 28235)
+++ branches/render25/source/blender/imbuf/intern/tiff.c	2010-04-16 15:25:43 UTC (rev 28236)
@@ -282,7 +282,7 @@
 		 (memcmp(lil_endian, mem, IMB_TIFF_NCB) == 0) );
 }
 
-static int imb_read_tiff_pixels(ImBuf *ibuf, TIFF *image)
+static int imb_read_tiff_pixels(ImBuf *ibuf, TIFF *image, int premul)
 {
 	ImBuf *tmpibuf;
 	int success;
@@ -290,7 +290,12 @@
 	tmpibuf= IMB_allocImBuf(ibuf->x, ibuf->y, 32, IB_rect, 0);
 	success = libtiff_TIFFReadRGBAImage(image, ibuf->x, ibuf->y, tmpibuf->rect, 0);
 
-	if (ENDIAN_ORDER == B_ENDIAN) IMB_convert_rgba_to_abgr(tmpibuf);
+	if(ENDIAN_ORDER == B_ENDIAN)
+		IMB_convert_rgba_to_abgr(tmpibuf);
+	if(premul) {
+		IMB_premultiply_alpha(tmpibuf);
+		ibuf->flags |= IB_premul;
+	}
 
 	/* assign rect last */
 	ibuf->rect= tmpibuf->rect;
@@ -386,7 +391,7 @@
 	}
 
 	/* read pixels */
-	if(!(ibuf->flags & IB_usecache) && !imb_read_tiff_pixels(ibuf, image)) {
+	if(!(ibuf->flags & IB_usecache) && !imb_read_tiff_pixels(ibuf, image, 0)) {
 		fprintf(stderr, "imb_loadtiff: Failed to read tiff image.\n");
 		libtiff_TIFFClose(image);
 		return NULL;
@@ -431,11 +436,11 @@
 
 		if(width == mipx && height == mipy) {
 			if(level == 0) {
-				imb_read_tiff_pixels(ibuf, image);
+				imb_read_tiff_pixels(ibuf, image, (ibuf->flags & IB_premul));
 			}
 			else {
 				ibuf->mipmap[level-1] = IMB_allocImBuf(width, height, 32, 0, 0);
-				imb_read_tiff_pixels(ibuf->mipmap[level-1], image);
+				imb_read_tiff_pixels(ibuf->mipmap[level-1], image, (ibuf->flags & IB_premul));
 			}
 		}
 		else





More information about the Bf-blender-cvs mailing list