fixed working strechblt for all dibxx now. do not say it does not take offset of...
[reactos.git] / reactos / subsys / win32k / dib / dib4bpp.c
index ae95374..d3afbd3 100644 (file)
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-/* $Id: dib4bpp.c,v 1.15 2003/07/27 18:37:23 dwelch Exp $ */
-#undef WIN32_LEAN_AND_MEAN
-#include <windows.h>
-#include <stdlib.h>
-#include <win32k/bitmaps.h>
-#include <win32k/debug.h>
+/* $Id$ */
+
+#include <w32k.h>
+
+#define NDEBUG
 #include <debug.h>
-#include <ddk/winddi.h>
-#include "../eng/objects.h"
-#include "dib.h"
 
 VOID
-DIB_4BPP_PutPixel(PSURFOBJ SurfObj, LONG x, LONG y, ULONG c)
+DIB_4BPP_PutPixel(SURFOBJ *SurfObj, LONG x, LONG y, ULONG c)
 {
-  PBYTE addr = SurfObj->pvScan0;
-
-  addr += (x>>1) + y * SurfObj->lDelta;
-  *addr = (*addr & notmask[x&1]) | (c << ((1-(x&1))<<2));
+   PBYTE addr = (PBYTE)SurfObj->pvScan0 + (x>>1) + y * SurfObj->lDelta;
+   *addr = (*addr & notmask[x&1]) | (c << ((1-(x&1))<<2));
 }
 
 ULONG
-DIB_4BPP_GetPixel(PSURFOBJ SurfObj, LONG x, LONG y)
+DIB_4BPP_GetPixel(SURFOBJ *SurfObj, LONG x, LONG y)
 {
-  PBYTE addr = SurfObj->pvScan0;
-
-  return (addr[(x>>1) + y * SurfObj->lDelta] >> ((1-(x&1))<<2) ) & 0x0f;
+   PBYTE addr = (PBYTE)SurfObj->pvScan0 + (x>>1) + y * SurfObj->lDelta;
+   return (*addr >> ((1-(x&1))<<2)) & 0x0f;
 }
 
 VOID
-DIB_4BPP_HLine(PSURFOBJ SurfObj, LONG x1, LONG x2, LONG y, ULONG c)
+DIB_4BPP_HLine(SURFOBJ *SurfObj, LONG x1, LONG x2, LONG y, ULONG c)
 {
-  PBYTE  addr = SurfObj->pvScan0 + (x1>>1) + y * SurfObj->lDelta;
+  PBYTE  addr = (PBYTE)SurfObj->pvScan0 + (x1>>1) + y * SurfObj->lDelta;
   LONG  cx = x1;
 
   while(cx < x2) {
@@ -59,7 +52,7 @@ DIB_4BPP_HLine(PSURFOBJ SurfObj, LONG x1, LONG x2, LONG y, ULONG c)
 }
 
 VOID
-DIB_4BPP_VLine(PSURFOBJ SurfObj, LONG x, LONG y1, LONG y2, ULONG c)
+DIB_4BPP_VLine(SURFOBJ *SurfObj, LONG x, LONG y1, LONG y2, ULONG c)
 {
   PBYTE  addr = SurfObj->pvScan0;
   int  lDelta = SurfObj->lDelta;
@@ -71,172 +64,34 @@ DIB_4BPP_VLine(PSURFOBJ SurfObj, LONG x, LONG y1, LONG y2, ULONG c)
   }
 }
 
-ULONG
-DIB_DoRop(ULONG Rop, ULONG Dest, ULONG Source, ULONG Pattern)
-{
-  ULONG ResultNibble;
-  ULONG Result;
-  ULONG i;
-  static const ULONG ExpandDest[16] = 
-    {
-      0x55555555 /* 0000 */,
-      0x555555AA /* 0001 */,
-      0x5555AA55 /* 0010 */,
-      0x5555AAAA /* 0011 */,
-      0x55AA5555 /* 0100 */,
-      0x55AA55AA /* 0101 */,
-      0x55AAAA55 /* 0110 */,
-      0x55AAAAAA /* 0111 */,
-      0xAA555555 /* 1000 */,
-      0xAA5555AA /* 1001 */,
-      0xAA55AA55 /* 1010 */,
-      0xAA55AAAA /* 1011 */,
-      0xAAAA5555 /* 1100 */,
-      0xAAAA55AA /* 1101 */,
-      0xAAAAAA55 /* 1110 */,
-      0xAAAAAAAA /* 1111 */,
-    };
-  static const ULONG ExpandSource[16] = 
-    {
-      0x33333333 /* 0000 */,
-      0x333333CC /* 0001 */,
-      0x3333CC33 /* 0010 */,
-      0x3333CCCC /* 0011 */,
-      0x33CC3333 /* 0100 */,
-      0x33CC33CC /* 0101 */,
-      0x33CCCC33 /* 0110 */,
-      0x33CCCCCC /* 0111 */,
-      0xCC333333 /* 1000 */,
-      0xCC3333CC /* 1001 */,
-      0xCC33CC33 /* 1010 */,
-      0xCC33CCCC /* 1011 */,
-      0xCCCC3333 /* 1100 */,
-      0xCCCC33CC /* 1101 */,
-      0xCCCCCC33 /* 1110 */,
-      0xCCCCCCCC /* 1111 */,
-    };
-  static const ULONG ExpandPattern[16] = 
-    {
-      0x0F0F0F0F /* 0000 */,
-      0x0F0F0FF0 /* 0001 */,
-      0x0F0FF00F /* 0010 */,
-      0x0F0FF0F0 /* 0011 */,
-      0x0FF00F0F /* 0100 */,
-      0x0FF00FF0 /* 0101 */,
-      0x0FF0F00F /* 0110 */,
-      0x0FF0F0F0 /* 0111 */,
-      0xF00F0F0F /* 1000 */,
-      0xF00F0FF0 /* 1001 */,
-      0xF00FF00F /* 1010 */,
-      0xF00FF0F0 /* 1011 */,
-      0xF0F00F0F /* 1100 */,
-      0xF0F00FF0 /* 1101 */,
-      0xF0F0F00F /* 1110 */,
-      0xF0F0F0F0 /* 1111 */,
-    };
-
-  /* Optimized code for the various named rop codes. */
-  switch (Rop)
-    {
-    case BLACKNESS:   return(0);
-    case NOTSRCERASE: return(~(Dest | Source));
-    case NOTSRCCOPY:  return(~Source);
-    case SRCERASE:    return((~Dest) & Source);
-    case DSTINVERT:   return(~Dest);
-    case PATINVERT:   return(Dest ^ Pattern);
-    case SRCINVERT:   return(Dest ^ Source);
-    case SRCAND:      return(Dest & Source);
-    case MERGEPAINT:  return(Dest & (~Source));
-    case SRCPAINT:    return(Dest | Source);
-    case MERGECOPY:   return(Source & Pattern);
-    case SRCCOPY:     return(Source);
-    case PATCOPY:     return(Pattern);
-    case PATPAINT:    return(Dest | (~Source) | Pattern);
-    case WHITENESS:   return(0xFFFFFFFF);
-    }
-  /* Expand the ROP operation to all four bytes */
-  Rop &= 0x00FF0000;
-  Rop = (Rop << 8) | (Rop) | (Rop >> 8) | (Rop >> 16);
-  /* Do the operation on four bits simultaneously. */
-  Result = 0;
-  for (i = 0; i < 8; i++)
-    {
-      ResultNibble = Rop & ExpandDest[Dest & 0xF] & ExpandSource[Source & 0xF] & ExpandPattern[Pattern & 0xF];
-      Result |= (((ResultNibble & 0xFF000000) ? 0x8 : 0x0) | ((ResultNibble & 0x00FF0000) ? 0x4 : 0x0) | 
-       ((ResultNibble & 0x0000FF00) ? 0x2 : 0x0) | ((ResultNibble & 0x000000FF) ? 0x1 : 0x0)) << (i * 4);
-      Dest >>= 4;
-      Source >>= 4;
-      Pattern >>= 4;
-    }
-  return(Result);
-}
-
-ULONG
-DIB_4BPP_GetSource(SURFOBJ* SourceSurf, SURFGDI* SourceGDI, ULONG sx, ULONG sy, XLATEOBJ* ColorTranslation)
-{
-  switch (SourceGDI->BitsPerPixel)
-    {
-    case 1:
-      if (DIB_1BPP_GetPixel(SourceSurf, sx, sy))
-       {
-         return(XLATEOBJ_iXlate(ColorTranslation, 0));
-       }
-      else
-       {
-         return(XLATEOBJ_iXlate(ColorTranslation, 1));
-       }
-    case 4:
-      if (ColorTranslation != NULL)
-       {
-         return(XLATEOBJ_iXlate(ColorTranslation, DIB_4BPP_GetPixel(SourceSurf, sx, sy)));
-       }
-      else
-       {
-         return(DIB_4BPP_GetPixel(SourceSurf, sx, sy));
-       }
-    case 8:
-      return(XLATEOBJ_iXlate(ColorTranslation, DIB_8BPP_GetPixel(SourceSurf, sx, sy)));
-    case 16:
-      return(XLATEOBJ_iXlate(ColorTranslation, DIB_16BPP_GetPixel(SourceSurf, sx, sy)));
-    case 24:
-      return(XLATEOBJ_iXlate(ColorTranslation, DIB_24BPP_GetPixel(SourceSurf, sx, sy)));
-    case 32:
-      return(XLATEOBJ_iXlate(ColorTranslation, DIB_8BPP_GetPixel(SourceSurf, sx, sy)));
-    default:
-      DbgPrint("DIB_4BPP_ExpandSource: Unhandled number of bits per pixel in source.\n");
-      return(0);
-    }
-}
-
-BOOLEAN STATIC
-DIB_4BPP_BitBltSrcCopy(SURFOBJ *DestSurf, SURFOBJ *SourceSurf,
-                      SURFGDI *DestGDI,  SURFGDI *SourceGDI,
-                      PRECTL  DestRect,  POINTL  *SourcePoint,
-                      XLATEOBJ* ColorTranslation)
+BOOLEAN
+DIB_4BPP_BitBltSrcCopy(PBLTINFO BltInfo)
 {
   LONG     i, j, sx, sy, f2, xColor;
   PBYTE    SourceBits_24BPP, SourceLine_24BPP;
   PBYTE    DestBits, DestLine, SourceBits_8BPP, SourceLine_8BPP;
   PBYTE    SourceBits, SourceLine;
 
-  DestBits = DestSurf->pvScan0 + (DestRect->left>>1) + DestRect->top * DestSurf->lDelta;
+  DestBits = (PBYTE)BltInfo->DestSurface->pvScan0 +
+             (BltInfo->DestRect.left >> 1) +
+             BltInfo->DestRect.top * BltInfo->DestSurface->lDelta;
 
-  switch(SourceGDI->BitsPerPixel)
+  switch (BltInfo->SourceSurface->iBitmapFormat)
   {
-    case 1:
-      sx = SourcePoint->x;
-      sy = SourcePoint->y;
+    case BMF_1BPP:
+      sx = BltInfo->SourcePoint.x;
+      sy = BltInfo->SourcePoint.y;
 
-      for (j=DestRect->top; j<DestRect->bottom; j++)
+      for (j=BltInfo->DestRect.top; j<BltInfo->DestRect.bottom; j++)
       {
-        sx = SourcePoint->x;
-        for (i=DestRect->left; i<DestRect->right; i++)
+        sx = BltInfo->SourcePoint.x;
+        for (i=BltInfo->DestRect.left; i<BltInfo->DestRect.right; i++)
         {
-          if(DIB_1BPP_GetPixel(SourceSurf, sx, sy) == 0)
+          if(DIB_1BPP_GetPixel(BltInfo->SourceSurface, sx, sy) == 0)
           {
-            DIB_4BPP_PutPixel(DestSurf, i, j, XLATEOBJ_iXlate(ColorTranslation, 0));
+            DIB_4BPP_PutPixel(BltInfo->DestSurface, i, j, XLATEOBJ_iXlate(BltInfo->XlateSourceToDest, 0));
           } else {
-            DIB_4BPP_PutPixel(DestSurf, i, j, XLATEOBJ_iXlate(ColorTranslation, 1));
+            DIB_4BPP_PutPixel(BltInfo->DestSurface, i, j, XLATEOBJ_iXlate(BltInfo->XlateSourceToDest, 1));
           }
           sx++;
         }
@@ -244,22 +99,22 @@ DIB_4BPP_BitBltSrcCopy(SURFOBJ *DestSurf, SURFOBJ *SourceSurf,
       }
       break;
 
-    case 4:
-      sy = SourcePoint->y;
+    case BMF_4BPP:
+      sy = BltInfo->SourcePoint.y;
 
-      for (j=DestRect->top; j<DestRect->bottom; j++)
+      for (j=BltInfo->DestRect.top; j<BltInfo->DestRect.bottom; j++)
       {
-        sx = SourcePoint->x;
+        sx = BltInfo->SourcePoint.x;
 
-        for (i=DestRect->left; i<DestRect->right; i++)
+        for (i=BltInfo->DestRect.left; i<BltInfo->DestRect.right; i++)
         {
-         if (NULL != ColorTranslation)
+         if (NULL != BltInfo->XlateSourceToDest)
          {
-           DIB_4BPP_PutPixel(DestSurf, i, j, XLATEOBJ_iXlate(ColorTranslation, DIB_4BPP_GetPixel(SourceSurf, sx, sy)));
+           DIB_4BPP_PutPixel(BltInfo->DestSurface, i, j, XLATEOBJ_iXlate(BltInfo->XlateSourceToDest, DIB_4BPP_GetPixel(BltInfo->SourceSurface, sx, sy)));
          }
          else
          {
-           DIB_4BPP_PutPixel(DestSurf, i, j, DIB_4BPP_GetPixel(SourceSurf, sx, sy));
+           DIB_4BPP_PutPixel(BltInfo->DestSurface, i, j, DIB_4BPP_GetPixel(BltInfo->SourceSurface, sx, sy));
          }
           sx++;
         }
@@ -267,123 +122,121 @@ DIB_4BPP_BitBltSrcCopy(SURFOBJ *DestSurf, SURFOBJ *SourceSurf,
       }
       break;
 
-    case 8:
-      SourceBits_8BPP = SourceSurf->pvScan0 + (SourcePoint->y * SourceSurf->lDelta) + SourcePoint->x;
+    case BMF_8BPP:
+      SourceBits_8BPP = (PBYTE)BltInfo->SourceSurface->pvScan0 + (BltInfo->SourcePoint.y * BltInfo->SourceSurface->lDelta) + BltInfo->SourcePoint.x;
 
-      for (j=DestRect->top; j<DestRect->bottom; j++)
+      for (j=BltInfo->DestRect.top; j<BltInfo->DestRect.bottom; j++)
       {
         SourceLine_8BPP = SourceBits_8BPP;
         DestLine = DestBits;
-        f2 = DestRect->left & 1;
+        f2 = BltInfo->DestRect.left & 1;
 
-        for (i=DestRect->left; i<DestRect->right; i++)
+        for (i=BltInfo->DestRect.left; i<BltInfo->DestRect.right; i++)
         {
-          *DestLine = (*DestLine & notmask[i&1]) |
-            ((XLATEOBJ_iXlate(ColorTranslation, *SourceLine_8BPP)) << ((4 * (1-(i & 1)))));
+          *DestLine = (*DestLine & notmask[f2]) |
+            ((XLATEOBJ_iXlate(BltInfo->XlateSourceToDest, *SourceLine_8BPP)) << ((4 * (1 - f2))));
           if(f2 == 1) { DestLine++; f2 = 0; } else { f2 = 1; }
           SourceLine_8BPP++;
         }
 
-        SourceBits_8BPP += SourceSurf->lDelta;
-        DestBits += DestSurf->lDelta;
+        SourceBits_8BPP += BltInfo->SourceSurface->lDelta;
+        DestBits += BltInfo->DestSurface->lDelta;
       }
       break;
 
-    case 16:
-      SourceLine = SourceSurf->pvScan0 + (SourcePoint->y * SourceSurf->lDelta) + 2 * SourcePoint->x;
+    case BMF_16BPP:
+      SourceLine = (PBYTE)BltInfo->SourceSurface->pvScan0 + (BltInfo->SourcePoint.y * BltInfo->SourceSurface->lDelta) + 2 * BltInfo->SourcePoint.x;
       DestLine = DestBits;
 
-      for (j = DestRect->top; j < DestRect->bottom; j++)
+      for (j = BltInfo->DestRect.top; j < BltInfo->DestRect.bottom; j++)
       {
         SourceBits = SourceLine;
         DestBits = DestLine;
-        f2 = DestRect->left & 1;
+        f2 = BltInfo->DestRect.left & 1;
 
-        for (i = DestRect->left; i < DestRect->right; i++)
+        for (i = BltInfo->DestRect.left; i < BltInfo->DestRect.right; i++)
         {
           xColor = *((PWORD) SourceBits);
-          *DestBits = (*DestBits & notmask[i&1]) |
-            ((XLATEOBJ_iXlate(ColorTranslation, xColor)) << ((4 * (1-(i & 1)))));
+          *DestBits = (*DestBits & notmask[f2]) |
+            ((XLATEOBJ_iXlate(BltInfo->XlateSourceToDest, xColor)) << ((4 * (1 - f2))));
           if(f2 == 1) { DestBits++; f2 = 0; } else { f2 = 1; }
           SourceBits += 2;
         }
 
-        SourceLine += SourceSurf->lDelta;
-        DestLine += DestSurf->lDelta;
+        SourceLine += BltInfo->SourceSurface->lDelta;
+        DestLine += BltInfo->DestSurface->lDelta;
       }
       break;
 
-    case 24:
-      SourceBits_24BPP = SourceSurf->pvScan0 + (SourcePoint->y * SourceSurf->lDelta) + SourcePoint->x * 3;
+    case BMF_24BPP:
+      SourceBits_24BPP = (PBYTE)BltInfo->SourceSurface->pvScan0 + (BltInfo->SourcePoint.y * BltInfo->SourceSurface->lDelta) + BltInfo->SourcePoint.x * 3;
 
-      for (j=DestRect->top; j<DestRect->bottom; j++)
+      for (j=BltInfo->DestRect.top; j<BltInfo->DestRect.bottom; j++)
       {
         SourceLine_24BPP = SourceBits_24BPP;
         DestLine = DestBits;
-        f2 = DestRect->left & 1;
+        f2 = BltInfo->DestRect.left & 1;
 
-        for (i=DestRect->left; i<DestRect->right; i++)
+        for (i=BltInfo->DestRect.left; i<BltInfo->DestRect.right; i++)
         {
           xColor = (*(SourceLine_24BPP + 2) << 0x10) +
              (*(SourceLine_24BPP + 1) << 0x08) +
              (*(SourceLine_24BPP));
-          *DestLine = (*DestLine & notmask[i&1]) |
-            ((XLATEOBJ_iXlate(ColorTranslation, xColor)) << ((4 * (1-(i & 1)))));
+          *DestLine = (*DestLine & notmask[f2]) |
+            ((XLATEOBJ_iXlate(BltInfo->XlateSourceToDest, xColor)) << ((4 * (1 - f2))));
           if(f2 == 1) { DestLine++; f2 = 0; } else { f2 = 1; }
           SourceLine_24BPP+=3;
         }
 
-        SourceBits_24BPP += SourceSurf->lDelta;
-        DestBits += DestSurf->lDelta;
+        SourceBits_24BPP += BltInfo->SourceSurface->lDelta;
+        DestBits += BltInfo->DestSurface->lDelta;
       }
       break;
 
-    case 32:
-      SourceLine = SourceSurf->pvScan0 + (SourcePoint->y * SourceSurf->lDelta) + 4 * SourcePoint->x;
+    case BMF_32BPP:
+      SourceLine = (PBYTE)BltInfo->SourceSurface->pvScan0 + (BltInfo->SourcePoint.y * BltInfo->SourceSurface->lDelta) + 4 * BltInfo->SourcePoint.x;
       DestLine = DestBits;
 
-      for (j = DestRect->top; j < DestRect->bottom; j++)
+      for (j = BltInfo->DestRect.top; j < BltInfo->DestRect.bottom; j++)
       {
         SourceBits = SourceLine;
         DestBits = DestLine;
-        f2 = DestRect->left & 1;
+        f2 = BltInfo->DestRect.left & 1;
 
-        for (i = DestRect->left; i < DestRect->right; i++)
+        for (i = BltInfo->DestRect.left; i < BltInfo->DestRect.right; i++)
         {
           xColor = *((PDWORD) SourceBits);
-          *DestBits = (*DestBits & notmask[i&1]) |
-            ((XLATEOBJ_iXlate(ColorTranslation, xColor)) << ((4 * (1-(i & 1)))));
+          *DestBits = (*DestBits & notmask[f2]) |
+            ((XLATEOBJ_iXlate(BltInfo->XlateSourceToDest, xColor)) << ((4 * (1 - f2))));
           if(f2 == 1) { DestBits++; f2 = 0; } else { f2 = 1; }
           SourceBits += 4;
         }
 
-        SourceLine += SourceSurf->lDelta;
-        DestLine += DestSurf->lDelta;
+        SourceLine += BltInfo->SourceSurface->lDelta;
+        DestLine += BltInfo->DestSurface->lDelta;
       }
       break;
 
     default:
-      DbgPrint("DIB_4BPP_Bitblt: Unhandled Source BPP: %u\n", SourceGDI->BitsPerPixel);
+      DbgPrint("DIB_4BPP_Bitblt: Unhandled Source BPP: %u\n", BitsPerFormat(BltInfo->SourceSurface->iBitmapFormat));
       return FALSE;
   }
   return(TRUE);
 }
 
 BOOLEAN
-DIB_4BPP_BitBlt(SURFOBJ *DestSurf, SURFOBJ *SourceSurf,
-               SURFGDI *DestGDI,  SURFGDI *SourceGDI,
-               PRECTL  DestRect,  POINTL  *SourcePoint,
-               PBRUSHOBJ Brush, PPOINTL BrushOrigin,
-               XLATEOBJ *ColorTranslation, ULONG Rop4)
+DIB_4BPP_BitBlt(PBLTINFO BltInfo)
 {
-  LONG     i, j, k, sx, sy;
-  ULONG    Dest, Source, Pattern;
-  PULONG   DestBits;
-  BOOL     UsesSource = ((Rop4 & 0xCC0000) >> 2) != (Rop4 & 0x330000);
-  BOOL     UsesPattern = ((Rop4 & 0xF00000) >> 4) != (Rop4 & 0x0F0000);  
-  ULONG    RoundedRight = ((DestRect->right + 0x7) & ~0x7);
-  static const ULONG ExpandSolidColor[16] = 
-    {
+   ULONG DestX, DestY;
+   ULONG SourceX, SourceY;
+   ULONG PatternY = 0;
+   ULONG Dest, Source = 0, Pattern = 0;
+   BOOLEAN UsesSource;
+   BOOLEAN UsesPattern;
+   PULONG DestBits;
+   LONG RoundedRight;
+   static const ULONG ExpandSolidColor[16] =
+   {
       0x00000000 /* 0 */,
       0x11111111 /* 1 */,
       0x22222222 /* 2 */,
@@ -400,58 +253,285 @@ DIB_4BPP_BitBlt(SURFOBJ *DestSurf, SURFOBJ *SourceSurf,
       0xDDDDDDDD /* 13 */,
       0xEEEEEEEE /* 14 */,
       0xFFFFFFFF /* 15 */,
-    };
+   };
+
+   UsesSource = ROP4_USES_SOURCE(BltInfo->Rop4);
+   UsesPattern = ROP4_USES_PATTERN(BltInfo->Rop4);
 
-  if (Rop4 == SRCCOPY)
-    {
-      return(DIB_4BPP_BitBltSrcCopy(DestSurf, SourceSurf, DestGDI, SourceGDI, DestRect, SourcePoint, ColorTranslation));
-    }
-  else
-    {
-      sy = SourcePoint->y;
+   SourceY = BltInfo->SourcePoint.y;
+   RoundedRight = BltInfo->DestRect.right -
+                  ((BltInfo->DestRect.right - BltInfo->DestRect.left) & 0x7);
 
-      for (j=DestRect->top; j<DestRect->bottom; j++)
+   if (UsesPattern)
+   {
+      if (BltInfo->PatternSurface)
       {
-        sx = SourcePoint->x;
-       DestBits = (PULONG)(DestSurf->pvScan0 + (DestRect->left>>1) + j * DestSurf->lDelta);
-        for (i=DestRect->left; i<RoundedRight; i+=8, DestBits++)
-         {
-           Dest = *DestBits;
-           if (UsesSource)
-             {
-               Source = 0;
-               for (k = 0; k < 8; k++)
-                 {
-                   Source |= (DIB_4BPP_GetSource(SourceSurf, SourceGDI, sx + i + k, sy, ColorTranslation) << (k * 4));
-                 }
-             }
-           if (UsesPattern)
-             {
-               /* FIXME: No support for pattern brushes. */
-               Pattern = ExpandSolidColor[Brush->iSolidColor & 0xF];
-             }
-           *DestBits = DIB_DoRop(Rop4, Dest, Source, Pattern);     
-         }
-       if (i < DestRect->right)
-         {
-           Dest = *DestBits;
-           for (; i < DestRect->right; i++)
-             {
-               if (UsesSource)
-                 {
-                   Source = DIB_4BPP_GetSource(SourceSurf, SourceGDI, sx + i, sy, ColorTranslation);
-                 }
-               if (UsesPattern)
-                 {
-                   /* FIXME: No support for pattern brushes. */
-                   Pattern = ExpandSolidColor[Brush->iSolidColor & 0xF];
-                 }                             
-               DIB_4BPP_PutPixel(DestSurf, i, j, DIB_DoRop(Rop4, Dest, Source, Pattern) & 0xF);
-               Dest >>= 4;
-             }  
-         }
+         PatternY = (BltInfo->DestRect.top + BltInfo->BrushOrigin.y) %
+                    BltInfo->PatternSurface->sizlBitmap.cy;
+      }
+      else
+      {
+         Pattern = ExpandSolidColor[BltInfo->Brush->iSolidColor];
+      }
+   }
+
+   for (DestY = BltInfo->DestRect.top; DestY < BltInfo->DestRect.bottom; DestY++)
+   {
+      DestBits = (PULONG)(
+         (PBYTE)BltInfo->DestSurface->pvScan0 +
+         (BltInfo->DestRect.left >> 1) +
+         DestY * BltInfo->DestSurface->lDelta);
+      SourceX = BltInfo->SourcePoint.x;
+      DestX = BltInfo->DestRect.left;
+
+      if (DestX & 0x1)
+      {
+         Dest = DIB_4BPP_GetPixel(BltInfo->DestSurface, DestX, DestY);
+
+         if (UsesSource)
+         {
+            Source = DIB_GetSource(BltInfo->SourceSurface, SourceX, SourceY, BltInfo->XlateSourceToDest);
+         }
+
+         if (BltInfo->PatternSurface)
+         {
+            Pattern = DIB_GetSource(BltInfo->PatternSurface, (DestX + BltInfo->BrushOrigin.x) % BltInfo->PatternSurface->sizlBitmap.cx, PatternY, BltInfo->XlatePatternToDest);
+         }
+
+         DIB_4BPP_PutPixel(BltInfo->DestSurface, DestX, DestY, DIB_DoRop(BltInfo->Rop4, Dest, Source, Pattern) & 0xF);
+
+         DestX++;
+         SourceX++;
+         DestBits = (PULONG)((ULONG_PTR)DestBits + 1);
+      }
+
+      for (; DestX < RoundedRight; DestX += 8, SourceX += 8, DestBits++)
+      {
+         Dest = *DestBits;
+         if (UsesSource)
+         {
+            Source =
+               (DIB_GetSource(BltInfo->SourceSurface, SourceX + 1, SourceY, BltInfo->XlateSourceToDest)) |
+               (DIB_GetSource(BltInfo->SourceSurface, SourceX + 0, SourceY, BltInfo->XlateSourceToDest) << 4) |
+               (DIB_GetSource(BltInfo->SourceSurface, SourceX + 3, SourceY, BltInfo->XlateSourceToDest) << 8) |
+               (DIB_GetSource(BltInfo->SourceSurface, SourceX + 2, SourceY, BltInfo->XlateSourceToDest) << 12) |
+               (DIB_GetSource(BltInfo->SourceSurface, SourceX + 5, SourceY, BltInfo->XlateSourceToDest) << 16) |
+               (DIB_GetSource(BltInfo->SourceSurface, SourceX + 4, SourceY, BltInfo->XlateSourceToDest) << 20) |
+               (DIB_GetSource(BltInfo->SourceSurface, SourceX + 7, SourceY, BltInfo->XlateSourceToDest) << 24) |
+               (DIB_GetSource(BltInfo->SourceSurface, SourceX + 6, SourceY, BltInfo->XlateSourceToDest) << 28);
+         }
+         if (BltInfo->PatternSurface)
+         {
+            Pattern = DIB_GetSource(BltInfo->PatternSurface, (DestX + BltInfo->BrushOrigin.x + 1) % BltInfo->PatternSurface->sizlBitmap.cx, PatternY, BltInfo->XlatePatternToDest);
+            Pattern |= DIB_GetSource(BltInfo->PatternSurface, (DestX + BltInfo->BrushOrigin.x + 0) % BltInfo->PatternSurface->sizlBitmap.cx, PatternY, BltInfo->XlatePatternToDest) << 4;
+            Pattern |= DIB_GetSource(BltInfo->PatternSurface, (DestX + BltInfo->BrushOrigin.x + 3) % BltInfo->PatternSurface->sizlBitmap.cx, PatternY, BltInfo->XlatePatternToDest) << 8;
+            Pattern |= DIB_GetSource(BltInfo->PatternSurface, (DestX + BltInfo->BrushOrigin.x + 2) % BltInfo->PatternSurface->sizlBitmap.cx, PatternY, BltInfo->XlatePatternToDest) << 12;
+            Pattern |= DIB_GetSource(BltInfo->PatternSurface, (DestX + BltInfo->BrushOrigin.x + 5) % BltInfo->PatternSurface->sizlBitmap.cx, PatternY, BltInfo->XlatePatternToDest) << 16;
+            Pattern |= DIB_GetSource(BltInfo->PatternSurface, (DestX + BltInfo->BrushOrigin.x + 4) % BltInfo->PatternSurface->sizlBitmap.cx, PatternY, BltInfo->XlatePatternToDest) << 20;
+            Pattern |= DIB_GetSource(BltInfo->PatternSurface, (DestX + BltInfo->BrushOrigin.x + 7) % BltInfo->PatternSurface->sizlBitmap.cx, PatternY, BltInfo->XlatePatternToDest) << 24;
+            Pattern |= DIB_GetSource(BltInfo->PatternSurface, (DestX + BltInfo->BrushOrigin.x + 6) % BltInfo->PatternSurface->sizlBitmap.cx, PatternY, BltInfo->XlatePatternToDest) << 28;
+         }
+         *DestBits = DIB_DoRop(BltInfo->Rop4, Dest, Source, Pattern);
       }
-    }
+
+      /* Process the rest of pixel on the line */
+      for (; DestX < BltInfo->DestRect.right; DestX++, SourceX++)
+      {
+         Dest = DIB_4BPP_GetPixel(BltInfo->DestSurface, DestX, DestY);
+         if (UsesSource)
+         {
+            Source = DIB_GetSource(BltInfo->SourceSurface, SourceX, SourceY, BltInfo->XlateSourceToDest);
+         }
+         if (BltInfo->PatternSurface)
+         {
+            Pattern = DIB_GetSource(BltInfo->PatternSurface, (DestX + BltInfo->BrushOrigin.x) % BltInfo->PatternSurface->sizlBitmap.cx, PatternY, BltInfo->XlatePatternToDest);
+         }
+         DIB_4BPP_PutPixel(BltInfo->DestSurface, DestX, DestY, DIB_DoRop(BltInfo->Rop4, Dest, Source, Pattern) & 0xF);
+      }
+
+      SourceY++;
+      if (BltInfo->PatternSurface)
+      {
+         PatternY++;
+         PatternY %= BltInfo->PatternSurface->sizlBitmap.cy;
+      }
+   }
+
+   return TRUE;
+}
+
+/* BitBlt Optimize */
+BOOLEAN 
+DIB_4BPP_ColorFill(SURFOBJ* DestSurface, RECTL* DestRect, ULONG color)
+{
+  ULONG DestY; 
+
+        for (DestY = DestRect->top; DestY< DestRect->bottom; DestY++)
+       {                                                       
+               DIB_4BPP_HLine(DestSurface, DestRect->left, DestRect->right, DestY, color);                                                     
+       }
+
+return TRUE;
+}
+
+//NOTE: If you change something here, please do the same in other dibXXbpp.c files!
+BOOLEAN DIB_4BPP_StretchBlt(SURFOBJ *DestSurf, SURFOBJ *SourceSurf,
+                            RECTL* DestRect, RECTL *SourceRect,
+                            POINTL* MaskOrigin, POINTL BrushOrigin,
+                            CLIPOBJ *ClipRegion, XLATEOBJ *ColorTranslation,
+                            ULONG Mode)
+{
+   LONG SrcSizeY;
+   LONG SrcSizeX;
+   LONG DesSizeY;
+   LONG DesSizeX;      
+   LONG sx;
+   LONG sy;
+   LONG DesX;
+   LONG DesY;
+   LONG color;
+  
+   SrcSizeY = SourceRect->bottom - SourceRect->top;
+   SrcSizeX = SourceRect->right - SourceRect->left;
+  
+   DesSizeY = DestRect->bottom - DestRect->top;
+   DesSizeX = DestRect->right - DestRect->left;
+
+   switch(SourceSurf->iBitmapFormat)
+   {
+      case BMF_1BPP:
+         /* FIXME :  MaskOrigin, BrushOrigin, ClipRegion, Mode ? */
+      /* This is a reference implementation, it hasn't been optimized for speed */
+                      
+       for (DesY=DestRect->top; DesY<DestRect->bottom; DesY++)
+       {                        
+           sy = (((DesY - DestRect->top) * SrcSizeY) / DesSizeY) + SourceRect->top;
+                     
+            for (DesX=DestRect->left; DesX<DestRect->right; DesX++)
+            {                  
+                  sx = (((DesX - DestRect->left) * SrcSizeX) / DesSizeX) + SourceRect->left;
+                               
+                  if(DIB_1BPP_GetPixel(SourceSurf, sx, sy) == 0)
+                                 {
+                                       DIB_4BPP_PutPixel(DestSurf, DesX, DesY, XLATEOBJ_iXlate(ColorTranslation, 0));
+                  } 
+                                 else 
+                                 {
+                    DIB_4BPP_PutPixel(DestSurf, DesX, DesY, XLATEOBJ_iXlate(ColorTranslation, 1));
+                  }
+            }
+       }               
+
+         break;
+
+      case BMF_4BPP:           
+      /* FIXME :  MaskOrigin, BrushOrigin, ClipRegion, Mode ? */
+      /* This is a reference implementation, it hasn't been optimized for speed */
+                      
+       for (DesY=DestRect->top; DesY<DestRect->bottom; DesY++)
+       {                        
+           sy = (((DesY - DestRect->top) * SrcSizeY) / DesSizeY) + SourceRect->top;
+                     
+            for (DesX=DestRect->left; DesX<DestRect->right; DesX++)
+            {                  
+                 sx = (((DesX - DestRect->left) * SrcSizeX) / DesSizeX) + SourceRect->left;            
+                 color = DIB_4BPP_GetPixel(SourceSurf, sx, sy);
+                 DIB_4BPP_PutPixel(DestSurf, DesX, DesY, XLATEOBJ_iXlate(ColorTranslation, color));
+            }
+       }                  
+      break;
+
+      case BMF_8BPP:           
+      /* FIXME :  MaskOrigin, BrushOrigin, ClipRegion, Mode ? */
+      /* This is a reference implementation, it hasn't been optimized for speed */
+                      
+       for (DesY=DestRect->top; DesY<DestRect->bottom; DesY++)
+       {                        
+           sy = (((DesY - DestRect->top) * SrcSizeY) / DesSizeY) + SourceRect->top;
+                     
+            for (DesX=DestRect->left; DesX<DestRect->right; DesX++)
+            {                  
+                 sx = (((DesX - DestRect->left) * SrcSizeX) / DesSizeX) + SourceRect->left;            
+                 color = DIB_8BPP_GetPixel(SourceSurf, sx, sy);
+                 DIB_4BPP_PutPixel(DestSurf, DesX, DesY, XLATEOBJ_iXlate(ColorTranslation, color));
+            }
+       }                  
+      break;
+
+      case BMF_16BPP:          
+      /* FIXME :  MaskOrigin, BrushOrigin, ClipRegion, Mode ? */
+      /* This is a reference implementation, it hasn't been optimized for speed */
+                      
+       for (DesY=DestRect->top; DesY<DestRect->bottom; DesY++)
+       {                        
+           sy = (((DesY - DestRect->top) * SrcSizeY) / DesSizeY) + SourceRect->top;
+                     
+            for (DesX=DestRect->left; DesX<DestRect->right; DesX++)
+            {                  
+                 sx = (((DesX - DestRect->left) * SrcSizeX) / DesSizeX) + SourceRect->left;            
+                 color = DIB_16BPP_GetPixel(SourceSurf, sx, sy);
+                 DIB_4BPP_PutPixel(DestSurf, DesX, DesY, XLATEOBJ_iXlate(ColorTranslation, color));
+            }
+       }                  
+      break;
+
+      case BMF_24BPP:          
+      /* FIXME :  MaskOrigin, BrushOrigin, ClipRegion, Mode ? */
+      /* This is a reference implementation, it hasn't been optimized for speed */
+                      
+       for (DesY=DestRect->top; DesY<DestRect->bottom; DesY++)
+       {                        
+           sy = (((DesY - DestRect->top) * SrcSizeY) / DesSizeY) + SourceRect->top;
+                     
+            for (DesX=DestRect->left; DesX<DestRect->right; DesX++)
+            {                  
+                 sx = (((DesX - DestRect->left) * SrcSizeX) / DesSizeX) + SourceRect->left;            
+                 color = DIB_24BPP_GetPixel(SourceSurf, sx, sy);
+                 DIB_4BPP_PutPixel(DestSurf, DesX, DesY, XLATEOBJ_iXlate(ColorTranslation, color));
+            }
+       }                  
+      break;
+
+      case BMF_32BPP:          
+      /* FIXME :  MaskOrigin, BrushOrigin, ClipRegion, Mode ? */
+      /* This is a reference implementation, it hasn't been optimized for speed */
+                      
+       for (DesY=DestRect->top; DesY<DestRect->bottom; DesY++)
+       {                        
+           sy = (((DesY - DestRect->top) * SrcSizeY) / DesSizeY) + SourceRect->top;
+                     
+            for (DesX=DestRect->left; DesX<DestRect->right; DesX++)
+            {                  
+                 sx = (((DesX - DestRect->left) * SrcSizeX) / DesSizeX) + SourceRect->left;            
+                 color = DIB_32BPP_GetPixel(SourceSurf, sx, sy);
+                 DIB_4BPP_PutPixel(DestSurf, DesX, DesY, XLATEOBJ_iXlate(ColorTranslation, color));
+            }
+       }                  
+      break;
+
+      default:
+      //DPRINT1("DIB_4BPP_StretchBlt: Unhandled Source BPP: %u\n", BitsPerFormat(SourceSurf->iBitmapFormat));
+      return FALSE;
+   }
+  
   return TRUE;
 }
+
+BOOLEAN
+DIB_4BPP_TransparentBlt(SURFOBJ *DestSurf, SURFOBJ *SourceSurf,
+                        RECTL*  DestRect,  POINTL  *SourcePoint,
+                        XLATEOBJ *ColorTranslation, ULONG iTransColor)
+{
+  return FALSE;
+}
+
+BOOLEAN
+DIB_4BPP_AlphaBlend(SURFOBJ* Dest, SURFOBJ* Source, RECTL* DestRect,
+                    RECTL* SourceRect, CLIPOBJ* ClipRegion,
+                    XLATEOBJ* ColorTranslation, BLENDOBJ* BlendObj)
+{
+  UNIMPLEMENTED;
+  return FALSE;
+}
+
 /* EOF */