Tk Source Code

Artifact [7728bf77]
Login

Artifact 7728bf777bb09ae30a52fd71649a2296563d5c72:

Attachment "alpha-hobbs2.patch" to ticket [809157ff] added by hobbs 2003-10-29 05:22:56.
Index: generic/tkImgPhoto.c
===================================================================
RCS file: /cvsroot/tktoolkit/tk/generic/tkImgPhoto.c,v
retrieving revision 1.36.2.2
diff -u -u -r1.36.2.2 tkImgPhoto.c
--- generic/tkImgPhoto.c	17 Jul 2003 13:05:53 -0000	1.36.2.2
+++ generic/tkImgPhoto.c	28 Oct 2003 22:20:37 -0000
@@ -178,10 +178,13 @@
  *				components.
  * IMAGE_CHANGED:		1 means that the instances of this image
  *				need to be redithered.
+ * COMPLEX_ALPHA:		1 means that the instances of this image
+ *				have alpha values that aren't 0 or 255.
  */
 
 #define COLOR_IMAGE		1
 #define IMAGE_CHANGED		2
+#define COMPLEX_ALPHA		4
 
 /*
  * The following data structure represents all of the instances of
@@ -374,6 +377,14 @@
  * Forward declarations
  */
 
+static int              ToggleComplexAlphaIfNeeded _ANSI_ARGS_((
+                            PhotoMaster *mPtr));
+
+static void             ImgBlendComplexAlphaPhoto _ANSI_ARGS_((XImage *bgImg,
+				PhotoInstance *iPtr,
+				int xOffset, int yOffset,
+				int width, int height));
+
 static void		PhotoFormatThreadExitProc _ANSI_ARGS_((
 			    ClientData clientData));
 static int		ImgPhotoCmd _ANSI_ARGS_((ClientData clientData,
@@ -430,6 +441,97 @@
 #undef MAX
 #define MAX(a, b)	((a) > (b)? (a): (b))
 
+
+static int
+ToggleComplexAlphaIfNeeded(PhotoMaster *mPtr)
+{
+    size_t len = MAX(mPtr->userWidth, mPtr->width) *
+	MAX(mPtr->userHeight, mPtr->height) * 4;
+    unsigned char *c   = mPtr->pix32;
+    unsigned char *end = c + len;
+
+    /*
+     * Set the COMPLEX_ALPHA flag if we have an image with partially
+     * transparent bits.
+     */
+    mPtr->flags &= ~COMPLEX_ALPHA;
+    c += 3; /* start at first alpha byte */
+    for (; c < end; c += 4) {
+	if (*c && *c != 255) {
+     	    mPtr->flags |= COMPLEX_ALPHA;
+	    break;
+	}
+    }
+    return (mPtr->flags & COMPLEX_ALPHA);
+}
+
+#ifndef GetRValue
+/*
+ * This part is a hack that must be corrected.  This should work on all
+ * little-endian machines, but it's not 100% correct across the board.  See
+ * similar code in tkCanvPs.c:TkImageGetColor.  For Unix, we should probably
+ * access the mask and shift data from the visualInfo. -- hobbs
+ */
+#define GetRValue(rgb)	(UCHAR(rgb))
+#define GetGValue(rgb)	(UCHAR(((unsigned short)(rgb)) >> 8))
+#define GetBValue(rgb)	(UCHAR((rgb)>>16))
+#define RGB(r,g,b)      ((unsigned)((UCHAR(r))|(UCHAR(g)<<8)|(UCHAR(b)<<16)))
+#endif
+
+static void ImgBlendComplexAlphaPhoto (
+    XImage *bgImg,
+    PhotoInstance *iPtr,
+    int xOffset, int yOffset,
+    int width, int height
+    )
+{
+    int x, y, line, r, g, b;
+    unsigned long pixel;
+    unsigned char alpha;
+    unsigned char *alphaAr = iPtr->masterPtr->pix32;
+    unsigned char *masterPtr;
+
+#if 0
+    fprintf(stdout, "depth %d xOff %d yOff %d w %d h %d\n",
+	    bgImg->depth, xOffset, yOffset, width, height);
+    fflush(stdout);
+#endif
+
+#define ALPHA_BLEND(bgPix, imgPix, alpha) \
+		((bgPix + (int) ((imgPix - bgPix) * (((double) alpha)/255.0))))
+
+    for (y = 0; y < height; y++) {
+	line = (y + yOffset) * iPtr->masterPtr->width;
+	for (x = 0; x < width; x++) {
+	    masterPtr = alphaAr + ((line + x + xOffset) * 4);
+	    alpha     = masterPtr[3];
+	    /*
+	     * Ignore pixels that are fully transparent
+	     */
+	    if (alpha) {
+		/*
+		 * We could perhaps be more efficient than XGetPixel for
+		 * 24 and 32 bit displays, but this seems "fast enough".
+		 */
+		pixel = XGetPixel(bgImg, x, y);
+		r = masterPtr[0];
+		g = masterPtr[1];
+		b = masterPtr[2];
+		if (alpha != 255) {
+		    /*
+		     * Only blend pixels that have some transparency
+		     */
+		    r = ALPHA_BLEND(GetRValue(pixel), r, alpha);
+		    g = ALPHA_BLEND(GetGValue(pixel), g, alpha);
+		    b = ALPHA_BLEND(GetBValue(pixel), b, alpha);
+		}
+		XPutPixel(bgImg, x, y, RGB(r, g, b));
+	    }
+	}
+    }
+#undef ALPHA_BLEND
+}
+
 /*
  *----------------------------------------------------------------------
  *
@@ -2082,6 +2184,9 @@
     if (oldFormat != NULL) {
 	Tcl_DecrRefCount(oldFormat);
     }
+
+    ToggleComplexAlphaIfNeeded(masterPtr);
+
     return TCL_OK;
 
   errorExit:
@@ -2234,7 +2339,6 @@
 		    validBox.width, validBox.height);
 	}
     }
-
 }
 
 /*
@@ -2471,21 +2575,47 @@
 	return;
     }
 
-    /*
-     * masterPtr->region describes which parts of the image contain
-     * valid data.  We set this region as the clip mask for the gc,
-     * setting its origin appropriately, and use it when drawing the
-     * image.
-     */
+    if ((instancePtr->masterPtr->flags & COMPLEX_ALPHA)
+	    && instancePtr->visualInfo.depth >= 16) {
+	XImage *bgImg = NULL;
 
-    TkSetRegion(display, instancePtr->gc, instancePtr->masterPtr->validRegion);
-    XSetClipOrigin(display, instancePtr->gc, drawableX - imageX,
-	    drawableY - imageY);
-    XCopyArea(display, instancePtr->pixels, drawable, instancePtr->gc,
-	    imageX, imageY, (unsigned) width, (unsigned) height,
-	    drawableX, drawableY);
-    XSetClipMask(display, instancePtr->gc, None);
-    XSetClipOrigin(display, instancePtr->gc, 0, 0);
+	/*
+	 * Pull the current background from the display to blend with
+	 */
+	bgImg = XGetImage(display, drawable, drawableX, drawableY,
+		(unsigned int)width, (unsigned int)height, AllPlanes, ZPixmap);
+	if (bgImg == NULL) {
+	    return;
+	}
+
+	ImgBlendComplexAlphaPhoto(bgImg, instancePtr,
+		imageX, imageY, width, height);
+
+	/*
+	 * Color info is unimportant as we only do this operation for
+	 * depth >= 16.
+	 */
+	TkPutImage(NULL, 0, display, drawable, instancePtr->gc,
+		bgImg, 0, 0, drawableX, drawableY,
+		(unsigned int) width, (unsigned int) height);
+	XDestroyImage(bgImg);
+    } else {
+	/*
+	 * masterPtr->region describes which parts of the image contain
+	 * valid data.  We set this region as the clip mask for the gc,
+	 * setting its origin appropriately, and use it when drawing the
+	 * image.
+	 */
+	TkSetRegion(display, instancePtr->gc, instancePtr->masterPtr->validRegion);
+	XSetClipOrigin(display, instancePtr->gc, drawableX - imageX,
+	               drawableY - imageY);
+	XCopyArea(display, instancePtr->pixels, drawable, instancePtr->gc,
+	          imageX, imageY, (unsigned) width, (unsigned) height,
+	          drawableX, drawableY);
+	XSetClipMask(display, instancePtr->gc, None);
+	XSetClipOrigin(display, instancePtr->gc, 0, 0);
+    }
+    XFlush (display);
 }
 
 /*
@@ -2789,6 +2919,8 @@
 	    masterPtr->ditherY = 0;
 	}
     }
+
+    ToggleComplexAlphaIfNeeded(masterPtr);
 
     /*
      * Now adjust the sizes of the pixmaps for all of the instances.
Index: win/tkWinImage.c
===================================================================
RCS file: /cvsroot/tktoolkit/tk/win/tkWinImage.c,v
retrieving revision 1.6.2.1
diff -u -u -r1.6.2.1 tkWinImage.c
--- win/tkWinImage.c	17 Jul 2003 01:02:47 -0000	1.6.2.1
+++ win/tkWinImage.c	28 Oct 2003 22:20:37 -0000
@@ -425,6 +425,29 @@
 	for (i = 0; i < bmInfo->bmiHeader.biSizeImage; i++, p++) {
 	    *p = (unsigned char) palette[*p];
 	}
+    } else if (depth == 16) {
+	GetDIBits(hdcMem, hbmp, 0, height, NULL, bmInfo, DIB_RGB_COLORS);
+	data = ckalloc(bmInfo->bmiHeader.biSizeImage);
+	if (!data) {
+	    /* printf("Failed to allocate data area for XImage.\n"); */
+	    ret_image = NULL;
+	    goto cleanup;
+	}
+	ret_image = XCreateImage(display, NULL, 16, ZPixmap, 0, data,
+		width, height, 16, 0 /* will be calc'ed from bitmap_pad */);
+	if (ret_image == NULL) {
+	    ckfree((char *) data);
+	    goto cleanup;
+	}
+
+	/* Get the BITMAP info directly into the Image. */
+	if (GetDIBits(hdcMem, hbmp, 0, height, ret_image->data, bmInfo,
+		    DIB_RGB_COLORS) == 0) {
+	    ckfree((char *) ret_image->data);
+	    ckfree((char *) ret_image);
+	    ret_image = NULL;
+	    goto cleanup;
+	}
     } else {
 	GetDIBits(hdcMem, hbmp, 0, height, NULL, bmInfo, DIB_RGB_COLORS);
 	data = ckalloc(width * height * 4);
@@ -456,19 +479,18 @@
 
 	    /* Get the BITMAP info into the Image. */
 	    if (GetDIBits(hdcMem, hbmp, 0, height, smallBitData, bmInfo,
-		    DIB_RGB_COLORS) == 0) {
+			DIB_RGB_COLORS) == 0) {
 		ckfree((char *) ret_image->data);
 		ckfree((char *) ret_image);
 		ckfree((char *) smallBitBase);
 		ret_image = NULL;
 		goto cleanup;
 	    }
-
 	    /* Copy the 24 Bit Pixmap to a 32-Bit one. */
 	    for (h = 0; h < height; h++) {
 		bigBitData   = ret_image->data + h * ret_image->bytes_per_line;
 		smallBitData = smallBitBase + h * byte_width;
-		
+
 		for (w = 0; w < width; w++) {
 		    *bigBitData++ = ((*smallBitData++));
 		    *bigBitData++ = ((*smallBitData++));