2008年11月18日 星期二

Microwindows with alpha blending support

Reference

http://hi.baidu.com/chenzhuoyou/blog/item/5ad45a3d9c057ceb3d6d97a2.html

http://hi.baidu.com/chenzhuoyou/blog/item/ee1f57607c7d5fdb8cb10d63.html

1.範例程式參考
src/demos/nanox/nxalpha.c

2.Patch microwindow-0.91

diff -Nur microwindows-0.91.org/src/demos/nanox/Makefile microwindows-0.91/src/demos/nanox/Makefile
--- microwindows-0.91.org/src/demos/nanox/Makefile    2008-11-17 20:36:13.000000000 +0800
+++ microwindows-0.91/src/demos/nanox/Makefile    2008-11-18 22:13:45.000000000 +0800
@@ -57,6 +57,7 @@
     $(MW_DIR_BIN)/ntetris \
     $(MW_DIR_BIN)/getselection \
     $(MW_DIR_BIN)/setselection \
+    $(MW_DIR_BIN)/nxalpha \
     $(MW_DIR_BIN)/dashdemo \
     $(MW_DIR_BIN)/pcfdemo \
     $(MW_DIR_BIN)/fontdemo \
diff -Nur microwindows-0.91.org/src/demos/nanox/nxalpha.c microwindows-0.91/src/demos/nanox/nxalpha.c
--- microwindows-0.91.org/src/demos/nanox/nxalpha.c    1970-01-01 08:00:00.000000000 +0800
+++ microwindows-0.91/src/demos/nanox/nxalpha.c    2008-11-19 13:20:49.000000000 +0800
@@ -0,0 +1,192 @@
+/* Alpha blending demo
+   By Jordan Crouse
+
+   This takes our favorite penguin and alpha blends him
+   against the background.  Hit the + and - keys to adjust the
+   alpha. 
+*/
+
+#include
+#include
+#include
+
+#define BGCOLOR GR_RGB(24, 125, 148)
+//#define BGCOLOR GR_RGB(0, 0, 0)
+
+int sw, sh;
+int iw = 91, ih = 100;
+
+int g_alpha = 128;
+GR_WINDOW_ID g_wid, g_pixmap, g_image;
+
+void do_alpha_blit(int x, int y, int w, int h, unsigned long mode) {
+
+  GR_GC_ID gc = GrNewGC();
+  GrSetGCForeground(gc, BGCOLOR);
+
+  /* Clear the holding buffer */
+  GrFillRect(g_pixmap, gc, 0, 0, w, h); 
+#if 0
+  /* Alpha blend the image in */
+  GrCopyArea(g_pixmap, gc, 0, 0, w, h, g_image, 0, 0, MWROP_SRCCOPY);
+
+  GrFillRect(g_pixmap, gc, 10, 10, 50, 50); 
+
+  GrCopyArea(g_pixmap, gc, 10, 10, 50, 50, g_image, 10, 10, mode);
+#endif
+#if 1
+  /* Alpha blend the image in */
+  GrCopyArea(g_pixmap, gc, 0, 0, w, h, g_image, 0, 0, mode);
+#endif
+  /* Copy the holding buffer to the screen */
+  GrCopyArea(g_wid, gc, x, y, w, h, g_pixmap, 0, 0, MWROP_SRCCOPY);
+  GrDestroyGC(gc);
+}
+
+void draw_window(int alpha) {
+
+  int xpos;
+
+  GR_GC_ID gc = GrNewGC();
+  GrSetGCForeground(gc, GR_RGB(255,255,255));
+  GrSetGCBackground(gc, BGCOLOR);
+  xpos = (sw - ((5 * iw) + 30)) / 2;
+
+  GrText(g_wid, gc, 5, 5, "Press + / - to change the alpha value", -1,
+     GR_TFTOP);
+
+  GrText(g_wid, gc, xpos, ((sh - ih) / 2) - 20, "Original", -1, 0);
+  GrCopyArea(g_wid, gc, xpos, (sh - ih) / 2, iw, ih, g_image,
+         0, 0, MWROP_SRCCOPY);
+
+  xpos += iw + 10;
+  
+  GrText(g_wid, gc, xpos, ((sh - ih) / 2) - 20, "Blend", -1, 0);
+  do_alpha_blit(xpos, (sh - ih) / 2, iw, ih, MWROP_BLEND | (alpha & 0xFF));
+
+  xpos += iw + 10;
+  
+  GrText(g_wid, gc, xpos, (sh - ih) / 2 - 20, "Add", -1, 0);
+  do_alpha_blit(xpos, (sh - ih) / 2, iw, ih, MWROP_BLENDADD | (alpha & 0xFF));
+   
+  xpos += iw + 10;
+  
+  GrText(g_wid, gc, xpos, (sh - ih) / 2 - 20, "Subtract", -1, 0);
+  do_alpha_blit(xpos, (sh - ih) / 2, iw, ih, MWROP_BLENDSUBTRACT | (alpha & 0xFF));
+   
+  xpos += iw + 10;
+
+  GrText(g_wid, gc, xpos, (sh - ih) / 2 - 20, "Reshade", -1, 0);
+  do_alpha_blit(xpos, (sh - ih) / 2, iw, ih, MWROP_BLENDRESHADE | (alpha & 0xFF));
+  
+  GrDestroyGC(gc);
+}
+
+void handle_events(GR_EVENT *event) {
+
+  switch(event->type) {
+  case GR_EVENT_TYPE_EXPOSURE:
+    draw_window(g_alpha);
+    break;
+
+  case GR_EVENT_TYPE_KEY_DOWN:
+    switch(event->keystroke.ch & 0x1) {
+    //case '=':
+    case 0:
+      g_alpha += 5;
+      if (g_alpha > 255) g_alpha = 255;
+      break;
+
+    //case '-':
+    case 1:
+      g_alpha -= 5;
+      if (g_alpha < 0) g_alpha = 0;
+      break;
+    }
+
+    printf("Alpha: %3.3d\r", g_alpha);
+    fflush(stdout);
+
+    draw_window(g_alpha);
+    break;
+
+  case GR_EVENT_TYPE_CLOSE_REQ:
+    exit(0);
+  }
+}
+
+int load_image(char *filename) {
+
+  GR_IMAGE_INFO iinfo;
+
+  GR_GC_ID gc = GrNewGC();
+  GR_IMAGE_ID image = GrLoadImageFromFile(filename, 0);
+
+  if (!image) return(-1);
+
+  GrGetImageInfo(image, &iinfo);
+
+  iw = iinfo.width;
+  ih = iinfo.height;
+
+  /* Make the pixmaps that will buffer the draw */
+  g_pixmap = GrNewPixmap(iw, ih, 0);
+  g_image = GrNewPixmap(iw, ih, 0);
+
+  GrSetGCForeground(gc, BGCOLOR);
+  GrFillRect(g_image, gc, 0, 0, iw, ih);
+
+  GrDrawImageToFit(g_image, gc, 0, 0, -1, -1, image);
+
+  GrDestroyGC(gc);
+  GrFreeImage(image);
+  return(0);
+}
+
+int main(int argc, char **argv) {
+
+  GR_SCREEN_INFO sinfo;
+
+  g_alpha = 128;
+
+  if (GrOpen() == -1) return(-1);

+  if (argc < 2) {
+    if(load_image("tux.gif") == -1) {
+      printf("Unable to open the image [tux.gif]\n");
+      GrClose();
+      return(-1);
+    }
+  }
+  else {
+    if (load_image(argv[1]) == -1) {
+      printf("Unable to open the image [%s]\n", argv[1]);
+      GrClose();
+      return(-1);
+    }
+  }
+
+  GrGetScreenInfo(&sinfo);
+
+  sw = sinfo.cols - 20;
+  sh = sinfo.rows - 20;
+
+  g_wid = GrNewWindow(GR_ROOT_WINDOW_ID,
+              10, 10, sw, sh, 0, BGCOLOR, 0);
+
+  GrSelectEvents(g_wid,
+         GR_EVENT_MASK_KEY_DOWN |
+         GR_EVENT_MASK_EXPOSURE |
+         GR_EVENT_MASK_CLOSE_REQ);
+
+  GrMapWindow(g_wid);

+  fflush(stdout);
+
+  while(1) {
+    GR_EVENT event;
+    GrGetNextEvent(&event);
+   
+    handle_events(&event);
+  }
+}
diff -Nur microwindows-0.91.org/src/drivers/fblin16.c microwindows-0.91/src/drivers/fblin16.c
--- microwindows-0.91.org/src/drivers/fblin16.c    2008-11-17 20:36:13.000000000 +0800
+++ microwindows-0.91/src/drivers/fblin16.c    2008-11-18 22:09:45.000000000 +0800
@@ -166,49 +166,90 @@
     src += srcx + srcy * slinelen;
 
 #if ALPHABLEND
-    if((op & MWROP_EXTENSION) != MWROP_BLENDCONSTANT)
-        goto stdblit;
+
+#define R_VAL(p) ((unsigned char *)(p))[2]
+#define G_VAL(p) ((unsigned char *)(p))[1]
+#define B_VAL(p) ((unsigned char *)(p))[0]
+
+#include
+
+    if((op & MWROP_EXTENSION) < MWROP_BLEND) goto stdblit;
     alpha = op & 0xff;
 
-    if (dstpsd->pixtype == MWPF_TRUECOLOR565) {
-        while(--h >= 0) {
-            for(i=0; i
-                unsigned int s = *src++;
-                unsigned int d = *dst;
-                unsigned int t = d & 0xf800;
-                unsigned int m1, m2, m3;
-                m1 = (((((s & 0xf800) - t)*alpha)>>8) & 0xf800) + t;
-                t = d & 0x07e0;
-                m2 = (((((s & 0x07e0) - t)*alpha)>>8) & 0x07e0) + t;
-                t = d & 0x001f;
-                m3 = (((((s & 0x001f) - t)*alpha)>>8) & 0x001f) + t;
-                *dst++ = m1 | m2 | m3;
-            }
-            dst += dlinelen - w;
-            src += slinelen - w;
-        }
+    if (dstpsd->pixtype == MWPF_TRUECOLOR565) {       /* 565 format */
+      while(--h >= 0) {
+        for(i=0; i
+          unsigned long tmp;
+
+          unsigned long in = (*src & 0xf800) << 8 | (*src & 0x07e0) << 5 | (*src & 0x001f) << 3;
+          unsigned long out = (*dst & 0xf800) << 8 | (*dst & 0x07e0) << 5 | (*dst & 0x001f) << 3;
+
+          unsigned long *ptr = &out;
+
+          switch (op & MWROP_EXTENSION) {
+          case MWROP_BLENDADD:
+        BLEND_ADD(R_VAL(&in), G_VAL(&in), B_VAL(&in), alpha, ptr);
+        break;
+       
+          case MWROP_BLENDSUBTRACT:
+        BLEND_SUB(R_VAL(&in), G_VAL(&in), B_VAL(&in), alpha, ptr);
+        break;
+       
+          case MWROP_BLENDRESHADE:
+        BLEND_RE(R_VAL(&in), G_VAL(&in), B_VAL(&in), alpha, ptr);
+        break;
+       
+          default:
+        BLEND(R_VAL(&in), G_VAL(&in), B_VAL(&in), alpha, ptr);
+        break;
+          }
+       
+          src++;
+          *dst++ = ((R_VAL(ptr) & 0xF8) << 8) | ((G_VAL(ptr) & 0xFC) << 3) | ((B_VAL(ptr) & 0xF8) >> 3);
+        }
+       
+        dst += dlinelen - w;
+        src += slinelen - w;
+      }
     } else {
-        /* 5/5/5 format*/
-        while(--h >= 0) {
-            for(i=0; i
-                unsigned int s = *src++;
-                unsigned int d = *dst;
-                unsigned int t = d & 0x7c00;
-                unsigned int m1, m2, m3;
-                m1 = (((((s & 0x7c00) - t)*alpha)>>8) & 0x7c00) + t;
-                t = d & 0x03e0;
-                m2 = (((((s & 0x03e0) - t)*alpha)>>8) & 0x03e0) + t;
-                t = d & 0x001f;
-                m3 = (((((s & 0x001f) - t)*alpha)>>8) & 0x001f) + t;
-                *dst++ = m1 | m2 | m3;
-            }
-            dst += dlinelen - w;
-            src += slinelen - w;
-        }
+     
+      while(--h >= 0) {
+        for(i=0; i
+          unsigned long tmp;
+
+          unsigned long in = (*src & 0x7c00) << 9 | (*src & 0x03e0) << 6 | (*src & 0x001f) << 3;
+          unsigned long out = (*dst & 0x7c00) << 9 | (*dst & 0x03e0) << 6 | (*dst & 0x001f) << 3;
+   
+          unsigned long *ptr = &out;
+
+          switch (op & MWROP_EXTENSION) {
+          case MWROP_BLENDADD:
+        BLEND_ADD(R_VAL(&in), G_VAL(&in), B_VAL(&in), alpha, ptr);
+        break;
+       
+          case MWROP_BLENDSUBTRACT:
+        BLEND_SUB(R_VAL(&in), G_VAL(&in), B_VAL(&in), alpha, ptr);
+        break;
+       
+          case MWROP_BLENDRESHADE:
+        BLEND_RE(R_VAL(&in), G_VAL(&in), B_VAL(&in), alpha, ptr);
+        break;
+       
+          default:
+        BLEND(R_VAL(&in), G_VAL(&in), B_VAL(&in), alpha, ptr);
+        break;
+          }
+          src++;
+          *dst++ = ((R_VAL(ptr) & 0xF8) << 7) | ((G_VAL(ptr) & 0xF8) << 2) | ((B_VAL(ptr) & 0xF8) >> 3);
+        }
+       
+        dst += dlinelen - w;
+        src += slinelen - w;
+      }
     }
     DRAWOFF;
     return;
-stdblit:
+ stdblit:
 #endif
 
     if (op == MWROP_COPY) {
diff -Nur microwindows-0.91.org/src/drivers/fblin24.c microwindows-0.91/src/drivers/fblin24.c
--- microwindows-0.91.org/src/drivers/fblin24.c    2008-11-17 20:36:13.000000000 +0800
+++ microwindows-0.91/src/drivers/fblin24.c    2008-11-18 22:09:45.000000000 +0800
@@ -172,25 +172,45 @@
     src += (srcx + srcy * srcpsd->linelen) * 3;
 
 #if ALPHABLEND
-    if((op & MWROP_EXTENSION) != MWROP_BLENDCONSTANT)
+#define R_VAL(p) ((unsigned char *)(p))[2]
+#define G_VAL(p) ((unsigned char *)(p))[1]
+#define B_VAL(p) ((unsigned char *)(p))[0]
+
+#include
+
+    if((op & MWROP_EXTENSION) < MWROP_BLEND)
         goto stdblit;
     alpha = op & 0xff;
-
+   
     while(--h >= 0) {
-        for(i=0; i
-            unsigned long s = *src++;
-            unsigned long d = *dst;
-            *dst++ = (unsigned char)(((s - d)*alpha)>>8) + d;
-            s = *src++;
-            d = *dst;
-            *dst++ = (unsigned char)(((s - d)*alpha)>>8) + d;
-            s = *src++;
-            d = *dst;
-            *dst++ = (unsigned char)(((s - d)*alpha)>>8) + d;
-        }
-        dst += dlinelen_minus_w;
-        src += slinelen_minus_w;
+      for(i=0; i
+        unsigned long tmp;
+
+        switch (op & MWROP_EXTENSION) {
+        case MWROP_BLENDADD:
+          BLEND_ADD(R_VAL(src), G_VAL(src), B_VAL(src), alpha, dst);
+          break;
+        case MWROP_BLENDSUBTRACT:
+          BLEND_SUB(R_VAL(src), G_VAL(src), B_VAL(src), alpha, dst);
+          break;
+         
+        case MWROP_BLENDRESHADE:
+          BLEND_RE(R_VAL(src), G_VAL(src), B_VAL(src), alpha, dst);
+          break;
+           
+        default:
+          BLEND(R_VAL(src), G_VAL(src), B_VAL(src), alpha, dst);
+          break;
+        }
+       
+        src++;
+        dst++;
+      }
+     
+      dst += dlinelen_minus_w;
+      src += slinelen_minus_w;
     }
+   
     DRAWOFF;
     return;
 stdblit:
diff -Nur microwindows-0.91.org/src/drivers/fblin32.c microwindows-0.91/src/drivers/fblin32.c
--- microwindows-0.91.org/src/drivers/fblin32.c    2008-11-17 20:36:13.000000000 +0800
+++ microwindows-0.91/src/drivers/fblin32.c    2008-11-19 13:36:20.000000000 +0800
@@ -119,14 +119,12 @@
 linear32_blit(PSD dstpsd, MWCOORD dstx, MWCOORD dsty, MWCOORD w, MWCOORD h,
     PSD srcpsd, MWCOORD srcx, MWCOORD srcy, long op)
 {
-    ADDR8    dst8, src8;
     ADDR32    dst = dstpsd->addr;
     ADDR32    src = srcpsd->addr;
     int    i;
     int    dlinelen = dstpsd->linelen;
     int    slinelen = srcpsd->linelen;
-    int    dlinelen_minus_w4;
-    int    slinelen_minus_w4;
+
 #if ALPHABLEND
     unsigned int alpha;
 #endif
@@ -149,35 +147,44 @@
     src += srcx + srcy * slinelen;
 
 #if ALPHABLEND
-    if((op & MWROP_EXTENSION) != MWROP_BLENDCONSTANT)
-        goto stdblit;
-    alpha = op & 0xff;
-
-    src8 = (ADDR8)src;
-    dst8 = (ADDR8)dst;
-    dlinelen_minus_w4 = (dlinelen - w) * 4;
-    slinelen_minus_w4 = (slinelen - w) * 4;
-    while(--h >= 0) {
-        for(i=0; i
-            /* FIXME: This doesn't look endian-neutral.
-             * I don't think this'll work on PowerPC,
-             * but I don't have one to test it.
-             */
-            register unsigned long s = *src8++;
-            register unsigned long d = *dst8;
-            *dst8++ = (unsigned char)(((s - d)*alpha)>>8) + d;
-            s = *src8++;
-            d = *dst8;
-            *dst8++ = (unsigned char)(((s - d)*alpha)>>8) + d;
-            s = *src8;
-            d = *dst8;
-            *dst8 = (unsigned char)(((s - d)*alpha)>>8) + d;
-            dst8 += 2;
-            src8 += 2;
-        }
-        dst8 += dlinelen_minus_w4;
-        src8 += slinelen_minus_w4;
-    }
+
+  #define R_VAL(p) ((unsigned char *)(p))[2]
+  #define G_VAL(p) ((unsigned char *)(p))[1]
+  #define B_VAL(p) ((unsigned char *)(p))[0]

+  #include

+        if((op & MWROP_EXTENSION) < MWROP_BLEND)goto stdblit;
+        alpha = op & 0xff;

+        while(--h >= 0) {
+          for(i=0; i
+            unsigned long tmp;

+            switch (op & MWROP_EXTENSION) {
+            case MWROP_BLENDADD:
+              BLEND_ADD(R_VAL(src), G_VAL(src), B_VAL(src), alpha, dst);
+              break;
+            case MWROP_BLENDSUBTRACT:
+              BLEND_SUB(R_VAL(src), G_VAL(src), B_VAL(src), alpha, dst);
+              break;

+            case MWROP_BLENDRESHADE:
+              BLEND_RE(R_VAL(src), G_VAL(src), B_VAL(src), alpha, dst);
+              break;

+            default:
+              BLEND(R_VAL(src), G_VAL(src), B_VAL(src), alpha, dst);
+              break;
+            }

+            src++;
+            dst++;
+          }

+          dst += dlinelen - w;
+          src += slinelen - w;
+        }
     DRAWOFF;
     return;
 stdblit:
diff -Nur microwindows-0.91.org/src/drivers/fblin8.c microwindows-0.91/src/drivers/fblin8.c
--- microwindows-0.91.org/src/drivers/fblin8.c    2008-11-17 20:36:13.000000000 +0800
+++ microwindows-0.91/src/drivers/fblin8.c    2008-11-18 22:09:45.000000000 +0800
@@ -19,6 +19,11 @@
 #include "device.h"
 #include "fb.h"
 
+/* FIXME:  This table lookup does not scale well
+   We now have 4 alpha blending modes, which would result in 256 * 256 * 4 entries
+   which IMHO is not acceptable.  This needs to be re thought
+*/
+
 #if ALPHABLEND
 /*
  * Alpha lookup tables for 256 color palette systems
@@ -164,37 +169,86 @@
     src = ((ADDR8)srcpsd->addr) + srcx + srcy * slinelen;
 
 #if ALPHABLEND
-    if((op & MWROP_EXTENSION) != MWROP_BLENDCONSTANT)
+
+#define R_VAL(p) ((unsigned char *)(p))[2]
+#define G_VAL(p) ((unsigned char *)(p))[1]
+#define B_VAL(p) ((unsigned char *)(p))[0]
+   
+#include
+
+    if((op & MWROP_EXTENSION) < MWROP_BLEND)
         goto stdblit;
     srcalpha = op & 0xff;
 
     /* FIXME create lookup table after palette is stabilized...*/
-    if(!rgb_to_palindex || !alpha_to_rgb) {
-        init_alpha_lookup();
-        if(!rgb_to_palindex || !alpha_to_rgb)
-            goto stdblit;
-    }
+    /* We should only do this for palette mode - truecolor 332
+       can compute it on the fly */
 
-    /* Create 5 bit alpha value index for 256 color indexing*/
-
-    /* destination alpha is (1 - source) alpha*/
-    dstalpha = ((srcalpha>>3) ^ 31) << 8;
-    srcalpha = (srcalpha>>3) << 8;
-
-    while(--h >= 0) {
+    if (dstpsd->pixtype == MWPF_PALETTE) {
+      if(!rgb_to_palindex || !alpha_to_rgb) {
+        init_alpha_lookup();
+        if(!rgb_to_palindex || !alpha_to_rgb)
+          goto stdblit;
+      }
+
+      /* Create 5 bit alpha value index for 256 color indexing*/
+     
+      /* destination alpha is (1 - source) alpha*/
+      dstalpha = ((srcalpha>>3) ^ 31) << 8;
+      srcalpha = (srcalpha>>3) << 8;
+     
+      while(--h >= 0) {
         int    i;
         for(i=0; i
-        /* Get source RGB555 value for source alpha value*/
-        unsigned short s = alpha_to_rgb[srcalpha + *src++];
+          /* Get source RGB555 value for source alpha value*/
+          unsigned short s = alpha_to_rgb[srcalpha + *src++];
+         
+          /* Get destination RGB555 value for dest alpha value*/
+          unsigned short d = alpha_to_rgb[dstalpha + *dst];
+         
+          /* Add RGB values together and get closest palette index to it*/
+          *dst++ = rgb_to_palindex[s + d];
+        }
+        dst += dlinelen - w;
+        src += slinelen - w;
+      }
+    }
+    else { /* Truecolor 332 mode */
+      unsigned long tmp;
+
+      while(--h >= 0) {
+        int i;
+        for(i=0; i
+          unsigned long in = (*src & 0xE0) << 16 | (*src & 0x16) << 11 | (*src & 0x03) << 6;
+          unsigned long out = (*dst & 0xE0) << 16 | (*dst & 0x16) << 11 | (*dst & 0x03) << 6;
 
-        /* Get destination RGB555 value for dest alpha value*/
-        unsigned short d = alpha_to_rgb[dstalpha + *dst];
+          unsigned long *ptr = &out;
 
-        /* Add RGB values together and get closest palette index to it*/
-        *dst++ = rgb_to_palindex[s + d];
+          switch (op & MWROP_EXTENSION) {
+          case MWROP_BLENDADD:
+        BLEND_ADD(R_VAL(&in), G_VAL(&in), B_VAL(&in), srcalpha, ptr);
+        break;
+       
+          case MWROP_BLENDSUBTRACT:
+        BLEND_SUB(R_VAL(&in), G_VAL(&in), B_VAL(&in), srcalpha, ptr);
+        break;
+       
+          case MWROP_BLENDRESHADE:
+        BLEND_RE(R_VAL(&in), G_VAL(&in), B_VAL(&in), srcalpha, ptr);
+        break;
+       
+          default:
+        BLEND(R_VAL(&in), G_VAL(&in), B_VAL(&in), srcalpha, ptr);
+        break;
+          }
+         
+          src++;
+          *dst++ = ((R_VAL(ptr) & 0x07) << 5) | ((G_VAL(ptr) & 0x07) << 2) | (B_VAL(ptr) & 0x03);
         }
+       
         dst += dlinelen - w;
         src += slinelen - w;
+      }
     }
     DRAWOFF;
     return;
diff -Nur microwindows-0.91.org/src/include/alpha.h microwindows-0.91/src/include/alpha.h
--- microwindows-0.91.org/src/include/alpha.h    1970-01-01 08:00:00.000000000 +0800
+++ microwindows-0.91/src/include/alpha.h    2008-11-18 22:09:45.000000000 +0800
@@ -0,0 +1,296 @@
+/*
+ * Copied and inspired by imlib2/src/blend.h
+ * most routines by Raster (raster@rasterman.com).
+ *
+ * IMBLIB2 Copyright (C) 1999 Carsten Haitzler and various contributors
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies of the Software, its documentation and marketing & publicity
+ * materials, and acknowledgment shall be given in the documentation, materials
+ * and software packages that this Software was used.
+ */
+
+/* I have included Rasters excellent comments at the bottom of this file.  These
+   routines and alogrithms are his.  Thank you so much Raster (and special props to Chris Ross,
+   who pointed these out to me).  The only changes I made are to refect the fact that we don't
+   currently have a alpha channel, so we don't need to worry about merging / saturating the
+   destination alpha channel.
+*/
+
+/* Note:  The macros R_VAL, G_VAL and B_VAL need to be defined for the appropriate depth */
+
+#if !defined(R_VAL) || !defined(G_VAL) || !defined(B_VAL)
+#error You have to define R_VAL, G_VAL and B_VAL for your depth
+#endif
+
+/* Saturate values in the range [0, 512) */
+#define SATURATE_UPPER(nc, v) nc = ((v) | (-((v) >> 8)))
+
+/* Saturate values in the range (-256, 256) */
+#define SATURATE_LOWER(nc, v) nc = (v) & (~((v) >> 8))
+
+/* Saturate values in the range (-256, 512) */
+#define SATURATE_BOTH(nc, v) nc = ((v) | (-((v) >> 8))) & (~((v) >> 9))
+
+#define BLEND_COLOR(a, nc, c, cc) \
+tmp = ((c) - (cc)) * (a); \
+nc = (cc) + ((tmp + (tmp >> 8) + 0x80) >> 8);
+
+#define ADD_COLOR_WITH_ALPHA(a, nc, c, cc) \
+tmp = (cc) + (((c) * (a)) >> 8); \
+SATURATE_UPPER(nc, tmp);
+
+#define ADD_COLOR(nc, c, cc) \
+tmp = (cc) + (c); \
+SATURATE_UPPER(nc, tmp);
+
+#define SUB_COLOR_WITH_ALPHA(a, nc, c, cc) \
+tmp = (cc) - (((c) * (a)) >> 8); \
+SATURATE_LOWER((nc), (tmp));
+
+#define SUB_COLOR(nc, c, cc) \
+tmp = (cc) - (c); \
+SATURATE_LOWER(nc, tmp);
+
+#define RESHADE_COLOR_WITH_ALPHA(a, nc, c, cc) \
+tmp = (cc) + ((((c) - 127) * (a)) >> 7); \
+SATURATE_BOTH(nc, tmp);
+
+#define RESHADE_COLOR(nc, c, cc) \
+tmp = (cc) + (((c) - 127) << 1); \
+SATURATE_BOTH(nc, tmp);
+
+#define BLEND(r1, g1, b1, a1, dest) \
+BLEND_COLOR(a1, R_VAL(dest), r1, R_VAL(dest)); \
+BLEND_COLOR(a1, G_VAL(dest), g1, G_VAL(dest)); \
+BLEND_COLOR(a1, B_VAL(dest), b1, B_VAL(dest));
+
+#define BLEND_ADD(r1, g1, b1, a1, dest) \
+ADD_COLOR_WITH_ALPHA(a1, R_VAL(dest), r1, R_VAL(dest)); \
+ADD_COLOR_WITH_ALPHA(a1, G_VAL(dest), g1, G_VAL(dest)); \
+ADD_COLOR_WITH_ALPHA(a1, B_VAL(dest), b1, B_VAL(dest));
+
+#define BLEND_SUB(r1, g1, b1, a1, dest) \
+SUB_COLOR_WITH_ALPHA(a1, R_VAL(dest), r1, R_VAL(dest)); \
+SUB_COLOR_WITH_ALPHA(a1, G_VAL(dest), g1, G_VAL(dest)); \
+SUB_COLOR_WITH_ALPHA(a1, B_VAL(dest), b1, B_VAL(dest));
+
+#define BLEND_RE(r1, g1, b1, a1, dest) \
+RESHADE_COLOR_WITH_ALPHA(a1, R_VAL(dest), r1, R_VAL(dest)); \
+RESHADE_COLOR_WITH_ALPHA(a1, G_VAL(dest), g1, G_VAL(dest)); \
+RESHADE_COLOR_WITH_ALPHA(a1, B_VAL(dest), b1, B_VAL(dest));
+
+/*
+ * 1) Basic Saturation - 8 bit unsigned
+ *
+ * The add, subtract, and reshade operations generate new color values that may
+ * be out of range for an unsigned 8 bit quantity.  Therefore, we will want to
+ * saturate the values into the range [0, 255].  Any value < 0 will become 0,
+ * and any value > 255 will become 255.  Or simply:
+ *
+ *   saturated = (value < 0) ? 0 : ((value > 255) ? 255 : value)
+ *
+ * Of course the above isn't the most efficient means of saturating.  Sometimes
+ * due to the nature of a calculation, we know we only need to saturate from
+ * above (> 255) or just from below (< 0).  Or simply:
+ *
+ *   saturated = (value < 0)   ?   0 : value
+ *   saturated = (value > 255) ? 255 : value
+ *
+ * 2) Alternate Forms of Saturation
+ *
+ * The methods of saturation described above use testing/branching operations,
+ * which are not necessarily efficient on all platforms.  There are other means
+ * of performing saturation using just simple arithmetic operations
+ * (+, -, >>, <<, ~).  A discussion of these saturation techniques follows.
+ *
+ * A) Saturation in the range [0, 512), or "from above".
+ *
+ * Assuming we have an integral value in the range [0, 512), the following
+ * formula evaluates to either 0, or 255:
+ *
+ *    (value & 255) - ((value & 256) >> 8)
+ *
+ * This is easy to show.  Notice that if the value is in the range [0, 256)
+ * the 9th bit is 0, and we get (0 - 0), which is 0.   And if the value is in
+ * the range [256, 512) the 9th bit is 1, and we get (256 - 1), which is 255.
+ *
+ * Now, using the above information and the fact that assigning an integer to
+ * an 8 bit unsigned value will truncate to the lower 8 bits of the integer,
+ * the following properly saturates:
+ *
+ *    8bit_value = value | (value & 256) - ((value & 256) >> 8)
+ *
+ * To prove this to yourself, just think about what the lower 8 bits look like
+ * in the ranges [0, 256) and [256, 512).  In particular, notice that the value
+ * in the range [0, 256) are unchanged, and values in the range [256, 512)
+ * always give you 255.  Just what we want!
+ *
+ * B) Saturation in the range (-256, 256), or "from below".
+ *
+ * Assuming we have an integral value in the range (-256, 256), the following
+ * formula evaluates to either 0, or -1:
+ *
+ *   ~(value >> 8)
+ *
+ * Here's why.  If the value is in the range [0, 256), then shifting right by
+ * 8 bits gives us all 0 bits, or 0.  And thus inverting the bits gives all
+ * 1 bits, which is -1.  If the value is in the range (-256, 0), then the 9th
+ * bit and higher bits are all 1.  So, when we shift right by 8 bits (with
+ * signed extension), we get a value with all 1 bits.  Which when inverted is
+ * all 0 bits, or 0.
+ *
+ * Now, using the above information the following properly saturates:
+ *
+ *    8bit_value = value & (~(value >> 8))
+ *
+ * To prove this to yourself, noticed that values in the range (-256, 0) will
+ * always be AND'd with 0, and thus map to 0.   Further, values in the range
+ * [0, 256) will always be AND'd with a value that is all 1 bits, and thus
+ * be unchanged.  Just what we want!
+ *
+ * C) Saturation in the range (-256, 512), or "from above and below".
+ *
+ * The short of it is the following works:
+ *
+ *    8bit_value = (tmp | ((tmp & 256) - ((tmp & 256) >> 8))) & (~(tmp >> 9))
+ *
+ * We leave it to the reader to prove.  Looks very similar to the techniques
+ * used above, eh? :)
+ */
+  
+/*
+ * 1) Operations
+ *
+ * There are 4 operations supported:
+ *
+ *    Copy, Add, Subtract, and Reshade
+ *
+ * For each operation there are 3 different variations that can be made:
+ *
+ *   a) Use "blend" or "copy" in the calculations?  A "blend" uses the alpha
+ *      value of the source pixel to lighten the source pixel values.  Where
+ *      as "copy" ignores the alpha value and uses the raw source pixel values.
+ *   b) Include source alpha in the calculation for new destination alpha?
+ *      If source alpha is not used, then destination alpha is preserved.
+ *      If source alpha is used, a "copy" sets the new alpha to the source
+ *      alpha, and a "blend" adds them together (with saturation).
+ *   c) Should the source pixels be passed through a color modifier before the
+ *      calculations are performed?
+ *
+ * All together we have 4*2*2*2 = 32 combinations.
+ *
+ * 2) Copy operation
+ *
+ * The "copy" version of this operation copies the source image onto the
+ * destination image.
+ *
+ * The "blend" version of this operation blends the source image color 'c' with
+ * the destination image color 'cc' using 'a' (in the range [0, 1]) according
+ * to the following formula.  Also notice that saturation is not needed for
+ * this calculation, the output is in the range [0, 255]:
+ *
+ *    nc = c * alpha + (1 - alpha) * cc
+ *       = c * alpha - cc * alpha + cc
+ *       = (c - cc) * alpha + cc;
+ *
+ * A discussion of how we're calculating this value follows:
+ *
+ * We're using 'a', an integer, in the range [0, 255] for alpha (and for 'c'
+ * and 'cc', BTW).  Therefore, we need to slightly modify the equation to take
+ * that into account.  To get into the range [0, 255] we need to divide 'a'
+ * by 255:
+ *
+ *    nc = ((c - cc) * a) / 255 + cc
+ *
+ * Notice that it is faster to divide by 256 (bit shifting), however without a
+ * fudge factor 'x' to balance things this isn't horribly accurate.  So, let's
+ * solve for 'x'.  The equality is:
+ *
+ *    ((c - cc) * a) / 256 + cc + x = ((c - cc) * a) / 255 + cc
+ *
+ * The 'cc' terms cancel, and multiply both sides by 255*256 to remove the
+ * fractions:
+ *
+ *    ((c - cc) * a) * 255 + 255 * 256 * x = ((c - cc) * a) * 256
+ *
+ * Get the 'x' term alone:
+ *
+ *    255 * 256 * x = ((c - cc) * a)
+ *
+ * Divide both sides by 255 * 256 to solve for 'x':
+ *
+ *    x = ((c - cc) * a) / (255 * 256)
+ *
+ * And putting 'x' back into the equation we get:
+ *
+ *    nc = ((c - cc) * a) / 256 + cc + ((c - cc) * a) / (255 * 256)
+ *
+ * And if we let 'tmp' represent the value '(c - cc) * a', and do a little
+ * regrouping we get:
+ *
+ *    nc = tmp / 256 + tmp / (255 * 256) + cc
+ *       = (tmp + tmp / 255) / 256 + cc
+ *
+ * We'll be using integer arithmetic, and over the range of values tmp takes
+ * (in [-255*255, 255*255]) the term tmp/(255*256) is pretty much the same as
+ * tmp/(256*256).  So we get:
+ *
+ *    nc = (tmp + tmp / 256) / 256 + cc
+ *
+ * And because the division of the sum uses integer arithmetic, it always
+ * rounds up/down even if that isn't the "best" choice.  If we add .5 to the
+ * sum, we can get standard rounding:  Like so:
+ *
+ *    nc = (tmp + tmp / 256 + 128) / 256 + cc
+ *
+ * 3) Add operation
+ *
+ * The "copy" version of this operation sums the source image pixel values
+ * with the destination image pixel values, saturating at 255 (from above).
+ *
+ * The "blend" version of this operation sums the source image pixel values,
+ * after taking into account alpha transparency (e.g. a percentage), with the
+ * destination image pixel values, saturating at 255 (from above).
+ *
+ * 4) Subtract operation
+ *
+ * This operation is the same as the Add operation, except the source values
+ * are subtracted from the destination values (instead of added).  Further,
+ * the result must be saturated at 0 (from below).
+ *
+ * 5) Reshade operation
+ *
+ * This operation uses the source image color values to lighten/darken color
+ * values in the destination image using the following formula:
+ *
+ *    nc = cc + ((c - middle_value) * 2 * alpha)
+ *
+ * Recall our pixel color and alpha values are in the range [0, 255].  So, the
+ * "blend" version of this operation can be calculated as:
+ *
+ *    nc = cc + ((c - 127) * 2 * (a / 255))
+ *
+ * And in an integer arithmetic friendly form is:
+ *
+ *    nc = cc + (((c - 127) * a) >> 7)
+ *
+ * The "copy" version of this operation treats alpha as 1.0 (or a/255), and in
+ * integer arithmetic friendly form is:
+ *
+ *    nc = cc + ((c - 127) << 1)
+ *
+ * Notice the color values created by this operation are in the range
+ * (-256, 512), and thus must be saturated at 0 and 255 (from above and below).
+ */
+
+
+
+
diff -Nur microwindows-0.91.org/src/include/mwtypes.h microwindows-0.91/src/include/mwtypes.h
--- microwindows-0.91.org/src/include/mwtypes.h    2008-11-17 20:36:14.000000000 +0800
+++ microwindows-0.91/src/include/mwtypes.h    2008-11-18 22:28:44.000000000 +0800
@@ -132,7 +132,7 @@
 #define MWROP_SRCTRANSCOPY    0x21000000L
 
 /* alpha blend src -> dst with constant alpha, alpha value in low 8 bits*/
-#define MWROP_BLENDCONSTANT    0x22000000L
+/*#define MWROP_BLENDCONSTANT    0x22000000L*/
 
 /* alpha blend fg/bg color -> dst with src as alpha channel*/
 #define MWROP_BLENDFGBG        0x23000000L
@@ -143,6 +143,18 @@
 /* stretch src -> dst*/
 #define MWROP_STRETCH        0x25000000L
 
+ /* alpha blend src & dst with constant alpha, alpha value in low 8 bits*/
+
+#define MWROP_BLEND         0x26000000L
+#define MWROP_BLENDCONSTANT     MWROP_BLEND
+
+/* alpha blend src + dst with constant alpha, alpha value in low 8 bits */
+#define MWROP_BLENDADD          0x27000000L
+/* alpha blend src - dst with constant alpha, alpha value in low 8 bits */
+#define MWROP_BLENDSUBTRACT     0x28000000L
+/* Use src to lighten / darken dst value, alpha value in low 8 bits */
+#define MWROP_BLENDRESHADE      0x29000000L
+
 /* Use the MWMODE value in the graphics context
  * to choose the appropriate MWROP value.
  * (This is only valid in calls to the Nano-X API,


沒有留言:

張貼留言