Transform correctly the coordinates.
[reactos.git] / reactos / subsys / win32k / objects / bitmaps.c
index b7359cb..1800c5f 100644 (file)
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-/* $Id: bitmaps.c,v 1.69 2004/04/09 20:03:20 navaraf Exp $ */
-#undef WIN32_LEAN_AND_MEAN
-#include <windows.h>
-#include <stdlib.h>
-#include <win32k/color.h>
-#include <win32k/gdiobj.h>
-#include <win32k/bitmaps.h>
-#include <win32k/brush.h>
-#include <win32k/region.h>
-//#include <win32k/debug.h>
-#include "../eng/handle.h"
-#include <include/inteng.h>
-#include <include/intgdi.h>
-#include <include/eng.h>
-#include <include/error.h>
-#include <include/surface.h>
-#include <include/palette.h>
-#include <include/tags.h>
-#include <rosrtl/gdimacro.h>
+/* $Id$ */
+
+#include <w32k.h>
 
 #define NDEBUG
-#include <win32k/debug1.h>
+#include <debug.h>
+
+#define IN_RECT(r,x,y) \
+( \
+ (x) >= (r).left && \
+ (y) >= (r).top && \
+ (x) < (r).right && \
+ (y) < (r).bottom \
+)
 
 BOOL STDCALL
 NtGdiBitBlt(
@@ -49,23 +41,22 @@ NtGdiBitBlt(
        HDC  hDCSrc,
        INT  XSrc,
        INT  YSrc,
-       DWORD  ROP)
+       DWORD  ROP,
+       IN DWORD crBackColor,
+       IN FLONG fl)
 {
        PDC DCDest = NULL;
        PDC DCSrc  = NULL;
-       SURFOBJ *SurfDest, *SurfSrc;
-       PSURFGDI SurfGDIDest, SurfGDISrc;
+       BITMAPOBJ *BitmapDest, *BitmapSrc;
        RECTL DestRect;
-       POINTL SourcePoint;
+       POINTL SourcePoint, BrushOrigin;
        BOOL Status;
-       PPALGDI PalDestGDI, PalSourceGDI;
        XLATEOBJ *XlateObj = NULL;
-       HPALETTE SourcePalette, DestPalette;
-       ULONG SourceMode, DestMode;
+       HPALETTE SourcePalette = 0, DestPalette = 0;
        PGDIBRUSHOBJ BrushObj;
-       BOOL UsesSource = ((ROP & 0xCC0000) >> 2) != (ROP & 0x330000);
-       BOOL UsesPattern = TRUE;//((ROP & 0xF00000) >> 4) != (ROP & 0x0F0000);
-       HPALETTE Mono = NULL;
+       GDIBRUSHINST BrushInst;
+       BOOL UsesSource = ROP3_USES_SOURCE(ROP);
+       BOOL UsesPattern = ROP3_USES_PATTERN(ROP);
 
        DCDest = DC_LockDc(hDCDest);
        if (NULL == DCDest)
@@ -74,6 +65,12 @@ NtGdiBitBlt(
                SetLastWin32Error(ERROR_INVALID_HANDLE);
                return FALSE;
        }
+       if (DCDest->IsIC)
+       {
+               DC_UnlockDc(DCDest);
+               /* Yes, Windows really returns TRUE in this case */
+               return TRUE;
+       }
 
        if (UsesSource)
        {
@@ -82,11 +79,18 @@ NtGdiBitBlt(
                        DCSrc = DC_LockDc(hDCSrc);
                        if (NULL == DCSrc)
                        {
-                               DC_UnlockDc(hDCDest);
+                               DC_UnlockDc(DCDest);
                                DPRINT1("Invalid source dc handle (0x%08x) passed to NtGdiBitBlt\n", hDCSrc);
                                SetLastWin32Error(ERROR_INVALID_HANDLE);
                                return FALSE;
                        }
+                       if (DCSrc->IsIC)
+                       {
+                               DC_UnlockDc(DCSrc);
+                               DC_UnlockDc(DCDest);
+                               /* Yes, Windows really returns TRUE in this case */
+                               return TRUE;
+                       }
                }
                else
                {
@@ -112,21 +116,26 @@ NtGdiBitBlt(
        DestRect.right  = XDest+Width;
        DestRect.bottom = YDest+Height;
 
+        IntLPtoDP(DCDest, (LPPOINT)&DestRect, 2);
+
        SourcePoint.x = XSrc;
        SourcePoint.y = YSrc;
 
+       BrushOrigin.x = 0;
+       BrushOrigin.y = 0;
+
        /* Determine surfaces to be used in the bitblt */
-       SurfDest = (SURFOBJ*)AccessUserObject((ULONG)DCDest->Surface);
-       SurfGDIDest = (PSURFGDI)AccessInternalObjectFromUserObject(SurfDest);
+       BitmapDest = BITMAPOBJ_LockBitmap(DCDest->w.hBitmap);
        if (UsesSource)
        {
-               SurfSrc  = (SURFOBJ*)AccessUserObject((ULONG)DCSrc->Surface);
-               SurfGDISrc  = (PSURFGDI)AccessInternalObjectFromUserObject(SurfSrc);
+               if (DCSrc->w.hBitmap == DCDest->w.hBitmap)
+                       BitmapSrc = BitmapDest;
+               else
+                       BitmapSrc = BITMAPOBJ_LockBitmap(DCSrc->w.hBitmap);
        }
        else
        {
-               SurfSrc  = NULL;
-               SurfGDISrc  = NULL;
+               BitmapSrc = NULL;
        }
 
        if (UsesPattern)
@@ -136,130 +145,108 @@ NtGdiBitBlt(
                {
                        if (UsesSource && hDCSrc != hDCDest)
                        {
-                               DC_UnlockDc(hDCSrc);
+                               DC_UnlockDc(DCSrc);
                        }
-                       DC_UnlockDc(hDCDest);
+                       if(BitmapDest != NULL)
+                       {
+                                BITMAPOBJ_UnlockBitmap(BitmapDest);
+                       }
+                       if(BitmapSrc != NULL && BitmapSrc != BitmapDest)
+                       {
+                                BITMAPOBJ_UnlockBitmap(BitmapSrc);
+                       }
+                       DC_UnlockDc(DCDest);
                        SetLastWin32Error(ERROR_INVALID_HANDLE);
                        return FALSE;
                }
+               BrushOrigin = BrushObj->ptOrigin;
+               IntGdiInitBrushInstance(&BrushInst, BrushObj, DCDest->XlateBrush);
        }
        else
        {
                BrushObj = NULL;
        }
 
-       if (DCDest->w.hPalette != 0)
-       {
-               DestPalette = DCDest->w.hPalette;
-       }
-       else
+       /* Create the XLATEOBJ. */
+       if (UsesSource)
        {
-               DestPalette = NtGdiGetStockObject(DEFAULT_PALETTE);
-       }
+               if (DCDest->w.hPalette != 0)
+                       DestPalette = DCDest->w.hPalette;
 
-       if (UsesSource && DCSrc->w.hPalette != 0)
-       {
-               SourcePalette = DCSrc->w.hPalette;
-       }
-       else
-       {
-               SourcePalette = NtGdiGetStockObject(DEFAULT_PALETTE);
-       }
+               if (DCSrc->w.hPalette != 0)
+                       SourcePalette = DCSrc->w.hPalette;
 
-       PalSourceGDI = PALETTE_LockPalette(SourcePalette);
-       if (NULL == PalSourceGDI)
-       {
-               if (UsesSource && hDCSrc != hDCDest)
+               /* KB41464 details how to convert between mono and color */
+               if (DCDest->w.bitsPerPixel == 1 && DCSrc->w.bitsPerPixel == 1)
                {
-                       DC_UnlockDc(hDCSrc);
+                       XlateObj = NULL;
                }
-               DC_UnlockDc(hDCDest);
-               SetLastWin32Error(ERROR_INVALID_HANDLE);
-               return FALSE;
-       }
-       SourceMode = PalSourceGDI->Mode;
-       PALETTE_UnlockPalette(SourcePalette);
-
-       if (DestPalette == SourcePalette)
-       {
-               DestMode = SourceMode;
-       }
-       else
-       {
-               PalDestGDI = PALETTE_LockPalette(DestPalette);
-               if (NULL == PalDestGDI)
+               else
                {
-                       if (UsesSource && hDCSrc != hDCDest)
+                       if (DCDest->w.bitsPerPixel == 1)
                        {
-                               DC_UnlockDc(hDCSrc);
+                               XlateObj = IntEngCreateMonoXlate(0, DestPalette, SourcePalette, DCSrc->w.backgroundColor);
+                       }
+                       else if (DCSrc->w.bitsPerPixel == 1)
+                       {
+                               XlateObj = IntEngCreateSrcMonoXlate(DestPalette, DCSrc->w.backgroundColor, DCSrc->w.textColor);
+                       }
+                       else
+                       {
+                               XlateObj = IntEngCreateXlate(0, 0, DestPalette, SourcePalette);
+                       }
+                       if (NULL == XlateObj)
+                       {
+                               if (UsesSource && hDCSrc != hDCDest)
+                               {
+                                       DC_UnlockDc(DCSrc);
+                               }
+                               DC_UnlockDc(DCDest);
+                               if(BitmapDest != NULL)
+                               {
+                                       BITMAPOBJ_UnlockBitmap(BitmapDest);
+                               }
+                               if(BitmapSrc != NULL && BitmapSrc != BitmapDest)
+                               {
+                                       BITMAPOBJ_UnlockBitmap(BitmapSrc);
+                               }
+                               if(BrushObj != NULL)
+                               {
+                                        BRUSHOBJ_UnlockBrush(BrushObj);
+                               }
+                               SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES);
+                               return FALSE;
                        }
-                       DC_UnlockDc(hDCDest);
-                       SetLastWin32Error(ERROR_INVALID_HANDLE);
-                       return FALSE;
-               }
-               DestMode = PalDestGDI->Mode;
-               PALETTE_UnlockPalette(DestPalette);
-       }
-
-       /* KB41464 details how to convert between mono and color */
-       if (DCDest->w.bitsPerPixel == 1)
-       {
-               XlateObj = (XLATEOBJ*)IntEngCreateMonoXlate(SourceMode, DestPalette,
-                       SourcePalette, DCSrc->w.backgroundColor);
-       }
-       else if (UsesSource && 1 == DCSrc->w.bitsPerPixel)
-       {
-               ULONG Colors[2];
-
-               Colors[0] = DCSrc->w.textColor;
-               Colors[1] = DCSrc->w.backgroundColor;
-               Mono = EngCreatePalette(PAL_INDEXED, 2, Colors, 0, 0, 0);
-               if (NULL != Mono)
-               {
-                       XlateObj = (XLATEOBJ*)IntEngCreateXlate(DestMode, PAL_INDEXED, DestPalette, Mono);
-               }
-               else
-               {
-                       XlateObj = NULL;
-               }
-       }
-       else
-       {
-               XlateObj = (XLATEOBJ*)IntEngCreateXlate(DestMode, SourceMode, DestPalette, SourcePalette);
-       }
-       if (NULL == XlateObj)
-       {
-               if (NULL != Mono)
-               {
-                       EngDeletePalette(Mono);
-               }
-               if (UsesSource && hDCSrc != hDCDest)
-               {
-                       DC_UnlockDc(hDCSrc);
                }
-               DC_UnlockDc(hDCDest);
-               SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES);
-               return FALSE;
        }
 
        /* Perform the bitblt operation */
-       Status = IntEngBitBlt(SurfDest, SurfSrc, NULL, DCDest->CombinedClip, XlateObj,
-               &DestRect, &SourcePoint, NULL, &BrushObj->BrushObject, NULL, ROP);
+       Status = IntEngBitBlt(&BitmapDest->SurfObj, &BitmapSrc->SurfObj, NULL,
+                              DCDest->CombinedClip, XlateObj, &DestRect,
+                              &SourcePoint, NULL,
+                              BrushObj ? &BrushInst.BrushObject : NULL,
+                             &BrushOrigin, ROP3_TO_ROP4(ROP));
+
+       if (UsesSource && XlateObj != NULL)
+               EngDeleteXlate(XlateObj);
 
-       EngDeleteXlate(XlateObj);
-       if (NULL != Mono)
+        if(BitmapDest != NULL)
+        {
+                BITMAPOBJ_UnlockBitmap(BitmapDest);
+        }
+       if (UsesSource && BitmapSrc != BitmapDest)
        {
-               EngDeletePalette(Mono);
+               BITMAPOBJ_UnlockBitmap(BitmapSrc);
        }
-       if (UsesPattern)
+       if (BrushObj != NULL)
        {
-               BRUSHOBJ_UnlockBrush(DCDest->w.hBrush);
+               BRUSHOBJ_UnlockBrush(BrushObj);
        }
        if (UsesSource && hDCSrc != hDCDest)
        {
-               DC_UnlockDc(hDCSrc);
+               DC_UnlockDc(DCSrc);
        }
-       DC_UnlockDc(hDCDest);
+       DC_UnlockDc(DCDest);
 
        return Status;
 }
@@ -280,24 +267,30 @@ NtGdiTransparentBlt(
 {
   PDC DCDest, DCSrc;
   RECTL rcDest, rcSrc;
-  SURFOBJ *SurfDest, *SurfSrc;
+  BITMAPOBJ *BitmapDest, *BitmapSrc;
   XLATEOBJ *XlateObj;
-  HPALETTE SourcePalette, DestPalette;
+  HPALETTE SourcePalette = 0, DestPalette = 0;
   PPALGDI PalDestGDI, PalSourceGDI;
   USHORT PalDestMode, PalSrcMode;
-  ULONG TransparentColor;
-  BOOL Ret;
-  
+  ULONG TransparentColor = 0;
+  BOOL Ret = FALSE;
+
   if(!(DCDest = DC_LockDc(hdcDst)))
   {
     DPRINT1("Invalid destination dc handle (0x%08x) passed to NtGdiTransparentBlt\n", hdcDst);
     SetLastWin32Error(ERROR_INVALID_HANDLE);
     return FALSE;
   }
-  
+  if (DCDest->IsIC)
+  {
+    DC_UnlockDc(DCDest);
+    /* Yes, Windows really returns TRUE in this case */
+    return TRUE;
+  }
+
   if((hdcDst != hdcSrc) && !(DCSrc = DC_LockDc(hdcSrc)))
   {
-    DC_UnlockDc(hdcDst);
+    DC_UnlockDc(DCDest);
     DPRINT1("Invalid source dc handle (0x%08x) passed to NtGdiTransparentBlt\n", hdcSrc);
     SetLastWin32Error(ERROR_INVALID_HANDLE);
     return FALSE;
@@ -306,35 +299,41 @@ NtGdiTransparentBlt(
   {
     DCSrc = DCDest;
   }
-  
+  if (DCSrc->IsIC)
+  {
+    DC_UnlockDc(DCSrc);
+    if(hdcDst != hdcSrc)
+    {
+      DC_UnlockDc(DCDest);
+    }
+    /* Yes, Windows really returns TRUE in this case */
+    return TRUE;
+  }
+
   /* Offset positions */
   xDst += DCDest->w.DCOrgX;
   yDst += DCDest->w.DCOrgY;
   xSrc += DCSrc->w.DCOrgX;
   ySrc += DCSrc->w.DCOrgY;
-  
+
   if(DCDest->w.hPalette)
     DestPalette = DCDest->w.hPalette;
-  else
-    DestPalette = NtGdiGetStockObject(DEFAULT_PALETTE);
-  
+
   if(DCSrc->w.hPalette)
     SourcePalette = DCSrc->w.hPalette;
-  else
-    SourcePalette = NtGdiGetStockObject(DEFAULT_PALETTE);
-  
+
   if(!(PalSourceGDI = PALETTE_LockPalette(SourcePalette)))
   {
-    DC_UnlockDc(hdcSrc);
-    DC_UnlockDc(hdcDst);
+    DC_UnlockDc(DCSrc);
+    DC_UnlockDc(DCDest);
     SetLastWin32Error(ERROR_INVALID_HANDLE);
     return FALSE;
   }
   if((DestPalette != SourcePalette) && !(PalDestGDI = PALETTE_LockPalette(DestPalette)))
   {
-    PALETTE_UnlockPalette(SourcePalette);
-    DC_UnlockDc(hdcSrc);
-    DC_UnlockDc(hdcDst);
+    PALETTE_UnlockPalette(PalSourceGDI);
+    DC_UnlockDc(DCSrc);
+    DC_UnlockDc(DCDest);
     SetLastWin32Error(ERROR_INVALID_HANDLE);
     return FALSE;
   }
@@ -342,29 +341,31 @@ NtGdiTransparentBlt(
   {
     PalDestMode = PalDestGDI->Mode;
     PalSrcMode = PalSourceGDI->Mode;
-    PALETTE_UnlockPalette(DestPalette);
+    PALETTE_UnlockPalette(PalDestGDI);
   }
   else
   {
     PalDestMode = PalSrcMode = PalSourceGDI->Mode;
   }
-  PALETTE_UnlockPalette(SourcePalette);
-  
+  PALETTE_UnlockPalette(PalSourceGDI);
+
   /* Translate Transparent (RGB) Color to the source palette */
   if((XlateObj = (XLATEOBJ*)IntEngCreateXlate(PalSrcMode, PAL_RGB, SourcePalette, NULL)))
   {
     TransparentColor = XLATEOBJ_iXlate(XlateObj, (ULONG)TransColor);
     EngDeleteXlate(XlateObj);
   }
-  
+
   /* Create the XLATE object to convert colors between source and destination */
   XlateObj = (XLATEOBJ*)IntEngCreateXlate(PalDestMode, PalSrcMode, DestPalette, SourcePalette);
-  
-  SurfDest = (SURFOBJ*)AccessUserObject((ULONG)DCDest->Surface);
-  ASSERT(SurfDest);
-  SurfSrc = (SURFOBJ*)AccessUserObject((ULONG)DCSrc->Surface);
-  ASSERT(SurfSrc);
-  
+
+  BitmapDest = BITMAPOBJ_LockBitmap(DCDest->w.hBitmap);
+  /* FIXME - BitmapDest can be NULL!!!! Don't assert here! */
+  ASSERT(BitmapDest);
+  BitmapSrc = BITMAPOBJ_LockBitmap(DCSrc->w.hBitmap);
+  /* FIXME - BitmapSrc can be NULL!!!! Don't assert here! */
+  ASSERT(BitmapSrc);
+
   rcDest.left = xDst;
   rcDest.top = yDst;
   rcDest.right = rcDest.left + cxDst;
@@ -373,21 +374,24 @@ NtGdiTransparentBlt(
   rcSrc.top = ySrc;
   rcSrc.right = rcSrc.left + cxSrc;
   rcSrc.bottom = rcSrc.top + cySrc;
-  
+
   if((cxDst != cxSrc) || (cyDst != cySrc))
   {
     DPRINT1("TransparentBlt() does not support stretching at the moment!\n");
     goto done;
   }
-  
-  Ret = IntEngTransparentBlt(SurfDest, SurfSrc, DCDest->CombinedClip, XlateObj, &rcDest, &rcSrc, 
+
+  Ret = IntEngTransparentBlt(&BitmapDest->SurfObj, &BitmapSrc->SurfObj,
+                             DCDest->CombinedClip, XlateObj, &rcDest, &rcSrc,
                              TransparentColor, 0);
-  
+
 done:
-  DC_UnlockDc(hdcSrc);
+  BITMAPOBJ_UnlockBitmap(BitmapDest);
+  BITMAPOBJ_UnlockBitmap(BitmapSrc);
+  DC_UnlockDc(DCSrc);
   if(hdcDst != hdcSrc)
   {
-    DC_UnlockDc(hdcDst);
+    DC_UnlockDc(DCDest);
   }
   if(XlateObj)
   {
@@ -396,108 +400,109 @@ done:
   return Ret;
 }
 
+static FASTCALL HBITMAP
+IntCreateBitmapIndirect(CONST BITMAP *BM)
+{
+   PBITMAPOBJ bmp;
+   HBITMAP hBitmap;
+   SIZEL Size;
+   UINT BitsPixel;
+
+   /* NOTE: Windows also doesn't store nr. of planes separately! */
+   BitsPixel = BM->bmBitsPixel * BM->bmPlanes;
+
+   /* Check parameters */
+   if (0 == BM->bmHeight || 0 == BM->bmWidth)
+   {
+      Size.cx = Size.cy = 1;
+   }
+   else
+   {
+      Size.cx = abs(BM->bmWidth);
+      Size.cy = abs(BM->bmHeight);
+   }
+
+   /* Create the bitmap object. */
+   hBitmap = IntCreateBitmap(Size, BM->bmWidthBytes,
+                             BitmapFormat(BitsPixel, BI_RGB),
+                             (BM->bmHeight < 0 ? BMF_TOPDOWN : 0) |
+                             (NULL == BM->bmBits ? 0 : BMF_NOZEROINIT), NULL);
+   if (!hBitmap)
+   {
+      DPRINT("NtGdiCreateBitmap: IntCreateBitmap returned 0\n");
+      return 0;
+   }
+
+   DPRINT("NtGdiCreateBitmap:%dx%d, %d BPP colors returning %08x\n",
+          Size.cx, Size.cy, BitsPixel, hBitmap);
+
+   bmp = BITMAPOBJ_LockBitmap( hBitmap );
+   /* FIXME - bmp can be NULL!!!!!! */
+   bmp->flFlags = BITMAPOBJ_IS_APIBITMAP;
+   BITMAPOBJ_UnlockBitmap( bmp );
+
+   /*
+    * NOTE: It's ugly practice, but we are using the object even
+    * after unlocking. Since the handle is currently known only
+    * to us it should be safe.
+    */
+
+   if (NULL != BM->bmBits)
+   {
+      NtGdiSetBitmapBits(hBitmap, bmp->SurfObj.cjBits, BM->bmBits);
+   }
+
+   return hBitmap;
+}
+
 HBITMAP STDCALL
 NtGdiCreateBitmap(
        INT  Width,
        INT  Height,
        UINT  Planes,
-       UINT  BitsPerPel,
-       CONST VOID *Bits)
+       UINT  BitsPixel,
+       IN OPTIONAL LPBYTE Bits)
 {
-       PBITMAPOBJ  bmp;
-       HBITMAP  hBitmap;
+   BITMAP BM;
 
-       Planes = (BYTE) Planes;
-       BitsPerPel = (BYTE) BitsPerPel;
+   BM.bmType = 0;
+   BM.bmWidth = Width;
+   BM.bmHeight = Height;
+   BM.bmWidthBytes = BITMAPOBJ_GetWidthBytes(Width, Planes * BitsPixel);
+   BM.bmPlanes = Planes;
+   BM.bmBitsPixel = BitsPixel;
+   BM.bmBits = Bits;
 
-       /* Check parameters */
-       if (!Height || !Width)
-       {
-               Width = 1;
-               Height = 1;
-       }
-       if (Planes != 1)
-       {
-               DPRINT("NtGdiCreateBitmap - UNIMPLEMENTED\n");
-               return  0;
-       }
-       if (Height < 0)
-       {
-               Height = -Height;
-       }
-       if (Width < 0)
-       {
-               Width = -Width;
-       }
-
-       /* Create the BITMAPOBJ */
-       hBitmap = BITMAPOBJ_AllocBitmap ();
-       if (!hBitmap)
-       {
-               DPRINT("NtGdiCreateBitmap: BITMAPOBJ_AllocBitmap returned 0\n");
-               return 0;
-       }
-
-       bmp = BITMAPOBJ_LockBitmap( hBitmap );
-
-       DPRINT("NtGdiCreateBitmap:%dx%d, %d (%d BPP) colors returning %08x\n", Width, Height,
-               1 << (Planes * BitsPerPel), BitsPerPel, bmp);
-
-       bmp->dimension.cx = 0;
-       bmp->dimension.cy = 0;
-       bmp->bitmap.bmType = 0;
-       bmp->bitmap.bmWidth = Width;
-       bmp->bitmap.bmHeight = Height;
-       bmp->bitmap.bmPlanes = Planes;
-       bmp->bitmap.bmBitsPixel = BitsPerPel;
-       bmp->bitmap.bmWidthBytes = BITMAPOBJ_GetWidthBytes (Width, BitsPerPel);
-       bmp->bitmap.bmBits = NULL;
-       bmp->DDBitmap = NULL;
-       bmp->dib = NULL;
-
-       // Allocate memory for bitmap bits
-       bmp->bitmap.bmBits = ExAllocatePoolWithTag(PagedPool, bmp->bitmap.bmWidthBytes * bmp->bitmap.bmHeight, TAG_BITMAP);
-
-       BITMAPOBJ_UnlockBitmap( hBitmap );
-
-       if (Bits) /* Set bitmap bits */
-       {
-               NtGdiSetBitmapBits(hBitmap, Height * bmp->bitmap.bmWidthBytes, Bits);
-       }
-       else
-       {
-               // Initialize the bitmap (fixes bug 244?)
-               RtlZeroMemory(bmp->bitmap.bmBits, Height * bmp->bitmap.bmWidthBytes);
-       }
-
-       return  hBitmap;
+   return IntCreateBitmapIndirect(&BM);
 }
 
-BOOL FASTCALL
-Bitmap_InternalDelete( PBITMAPOBJ pBmp )
+BOOL INTERNAL_CALL
+BITMAP_Cleanup(PVOID ObjectBody)
 {
-       ASSERT( pBmp );
-
-       if (NULL != pBmp->bitmap.bmBits)
+        PBITMAPOBJ pBmp = (PBITMAPOBJ)ObjectBody;
+       if (pBmp->SurfObj.pvBits != NULL &&
+           (pBmp->flFlags & BITMAPOBJ_IS_APIBITMAP))
        {
-               if (NULL != pBmp->dib)
+               if (pBmp->dib == NULL)
                {
-                       if (NULL == pBmp->dib->dshSection)
-                       {
-                               EngFreeUserMem(pBmp->bitmap.bmBits);
-                       }
-                       else
-                       {
-                               /* This is a file-mapped section */
-                               UNIMPLEMENTED;
-                       }
+                       ExFreePool(pBmp->SurfObj.pvBits);
                }
                else
                {
-                       ExFreePool(pBmp->bitmap.bmBits);
+                       EngFreeUserMem(pBmp->SurfObj.pvBits);
+               }
+               if (pBmp->hDIBPalette)
+               {
+                       NtGdiDeleteObject(pBmp->hDIBPalette);
                }
        }
 
+       if (NULL != pBmp->BitsLock)
+       {
+               ExFreePoolWithTag(pBmp->BitsLock, TAG_BITMAPOBJ);
+               pBmp->BitsLock = NULL;
+       }
+
        return TRUE;
 }
 
@@ -542,7 +547,7 @@ NtGdiCreateCompatibleBitmap(
 
        Dc = DC_LockDc(hDC);
 
-       DPRINT("NtGdiCreateCompatibleBitmap(%04x,%d,%d, bpp:%d) = \n", hDC, Width, Height, dc->w.bitsPerPixel);
+       DPRINT("NtGdiCreateCompatibleBitmap(%04x,%d,%d, bpp:%d) = \n", hDC, Width, Height, Dc->w.bitsPerPixel);
 
        if (NULL == Dc)
        {
@@ -553,18 +558,37 @@ NtGdiCreateCompatibleBitmap(
        Bmp = IntCreateCompatibleBitmap(Dc, Width, Height);
 
        DPRINT ("\t\t%04x\n", Bmp);
-       DC_UnlockDc(hDC);
+       DC_UnlockDc(Dc);
        return Bmp;
 }
 
 HBITMAP STDCALL
-NtGdiCreateBitmapIndirect(CONST BITMAP  *BM)
+NtGdiCreateBitmapIndirect(CONST BITMAP *UnsafeBM)
 {
-       return NtGdiCreateBitmap (BM->bmWidth,
-               BM->bmHeight,
-               BM->bmPlanes,
-               BM->bmBitsPixel,
-               BM->bmBits);
+  BITMAP BM;
+  NTSTATUS Status = STATUS_SUCCESS;
+
+  _SEH_TRY
+  {
+    ProbeForRead(UnsafeBM, sizeof(BITMAP), 1);
+    BM = *UnsafeBM;
+    if (NULL != BM.bmBits)
+      {
+      ProbeForRead(BM.bmBits, BM.bmWidthBytes * abs(BM.bmHeight), 2);
+      }
+  }
+  _SEH_HANDLE
+  {
+    Status = _SEH_GetExceptionCode();
+  }
+  _SEH_END;
+  if(!NT_SUCCESS(Status))
+  {
+    SetLastNtError(Status);
+    return NULL;
+  }
+
+  return IntCreateBitmapIndirect(&BM);
 }
 
 HBITMAP STDCALL
@@ -585,7 +609,10 @@ NtGdiExtFloodFill(
        COLORREF  Color,
        UINT  FillType)
 {
-       UNIMPLEMENTED;
+   DPRINT1("FIXME: NtGdiExtFloodFill is UNIMPLEMENTED\n");
+
+   /* lie and say we succeded */
+       return TRUE;
 }
 
 BOOL STDCALL
@@ -613,7 +640,7 @@ NtGdiGetBitmapDimensionEx(
 
        *Dimension = bmp->dimension;
 
-       BITMAPOBJ_UnlockBitmap(hBitmap);
+       BITMAPOBJ_UnlockBitmap(bmp);
 
        return  TRUE;
 }
@@ -624,11 +651,9 @@ NtGdiGetPixel(HDC hDC, INT XPos, INT YPos)
        PDC dc = NULL;
        COLORREF Result = (COLORREF)CLR_INVALID; // default to failure
        BOOL bInRect = FALSE;
-       PSURFGDI Surface;
+       BITMAPOBJ *BitmapObject;
        SURFOBJ *SurfaceObject;
-       HPALETTE Pal;
-       PPALGDI PalGDI;
-       USHORT PalMode;
+       HPALETTE Pal = 0;
        XLATEOBJ *XlateObj;
 
        dc = DC_LockDc (hDC);
@@ -638,45 +663,45 @@ NtGdiGetPixel(HDC hDC, INT XPos, INT YPos)
                SetLastWin32Error(ERROR_INVALID_HANDLE);
                return Result;
        }
+       if (dc->IsIC)
+       {
+               DC_UnlockDc(dc);
+               return Result;
+       }
+       XPos += dc->w.DCOrgX;
+       YPos += dc->w.DCOrgY;
        if ( IN_RECT(dc->CombinedClip->rclBounds,XPos,YPos) )
        {
                bInRect = TRUE;
-               SurfaceObject = (SURFOBJ*)AccessUserObject((ULONG)dc->Surface);
-               ASSERT(SurfaceObject);
-               Surface = (PSURFGDI)AccessInternalObjectFromUserObject(SurfaceObject);
-               if ( Surface )
+               BitmapObject = BITMAPOBJ_LockBitmap(dc->w.hBitmap);
+               SurfaceObject = &BitmapObject->SurfObj;
+               if ( BitmapObject )
                {
                        if ( dc->w.hPalette != 0 )
                                Pal = dc->w.hPalette;
-                       else
-                               Pal = NtGdiGetStockObject(DEFAULT_PALETTE);
-                       PalGDI = PALETTE_LockPalette(Pal);
-                       if ( PalGDI )
+                       /* FIXME: Verify if it shouldn't be PAL_BGR! */
+                       XlateObj = (XLATEOBJ*)IntEngCreateXlate ( PAL_RGB, 0, NULL, Pal );
+                       if ( XlateObj )
                        {
-                               PalMode = PalGDI->Mode;
-                               PALETTE_UnlockPalette(Pal);
-
-                               XlateObj = (XLATEOBJ*)IntEngCreateXlate ( PAL_RGB, PalMode, NULL, Pal );
-                               if ( XlateObj )
+                               // check if this DC has a DIB behind it...
+                               if ( SurfaceObject->pvScan0 ) // STYPE_BITMAP == SurfaceObject->iType
                                {
-                                       // check if this DC has a DIB behind it...
-                                       if ( SurfaceObject->pvScan0 ) // STYPE_BITMAP == SurfaceObject->iType
-                                       {
-                                               ASSERT ( SurfaceObject->lDelta && Surface->DIB_GetPixel );
-                                               Result = XLATEOBJ_iXlate(XlateObj, Surface->DIB_GetPixel ( SurfaceObject, XPos, YPos ) );
-                                       }
-                                       EngDeleteXlate(XlateObj);
+                                       ASSERT ( SurfaceObject->lDelta );
+                                       Result = XLATEOBJ_iXlate(XlateObj,
+                                               DibFunctionsForBitmapFormat[SurfaceObject->iBitmapFormat].DIB_GetPixel ( SurfaceObject, XPos, YPos ) );
                                }
+                               EngDeleteXlate(XlateObj);
                        }
+                       BITMAPOBJ_UnlockBitmap(BitmapObject);
                }
        }
-       DC_UnlockDc(hDC);
+       DC_UnlockDc(dc);
 
        // if Result is still CLR_INVALID, then the "quick" method above didn't work
        if ( bInRect && Result == CLR_INVALID )
        {
                // FIXME: create a 1x1 32BPP DIB, and blit to it
-               HDC hDCTmp = NtGdiCreateCompatableDC(hDC);
+               HDC hDCTmp = NtGdiCreateCompatibleDC(hDC);
                if ( hDCTmp )
                {
                        static const BITMAPINFOHEADER bih = { sizeof(BITMAPINFOHEADER), 1, 1, 1, 32, BI_RGB, 0, 0, 0, 0, 0 };
@@ -691,15 +716,15 @@ NtGdiGetPixel(HDC hDC, INT XPos, INT YPos)
                                {
                                        PBITMAPOBJ bmpobj;
 
-                                       NtGdiBitBlt ( hDCTmp, 0, 0, 1, 1, hDC, XPos, YPos, SRCCOPY );
+                                       NtGdiBitBlt ( hDCTmp, 0, 0, 1, 1, hDC, XPos, YPos, SRCCOPY, 0, 0 );
                                        NtGdiSelectObject ( hDCTmp, hBmpOld );
 
                                        // our bitmap is no longer selected, so we can access it's stuff...
                                        bmpobj = BITMAPOBJ_LockBitmap ( hBmpTmp );
                                        if ( bmpobj )
                                        {
-                                               Result = *(COLORREF*)bmpobj->bitmap.bmBits;
-                                               BITMAPOBJ_UnlockBitmap ( hBmpTmp );
+                                               Result = *(COLORREF*)bmpobj->SurfObj.pvScan0;
+                                               BITMAPOBJ_UnlockBitmap ( bmpobj );
                                        }
                                }
                                NtGdiDeleteObject ( hBmpTmp );
@@ -719,7 +744,7 @@ NtGdiGetPixel(HDC hDC, INT XPos, INT YPos)
  * to GDI32. I dunno. Write a test and let me know.
  */
 
-static inline BYTE
+static __inline BYTE
 SwapROP3_SrcDst(BYTE bRop3)
 {
        return (bRop3 & 0x99) | ((bRop3 & 0x22) << 1) | ((bRop3 & 0x44) >> 1);
@@ -735,7 +760,8 @@ NtGdiMaskBlt (
        HDC hdcDest, INT nXDest, INT nYDest,
        INT nWidth, INT nHeight, HDC hdcSrc,
        INT nXSrc, INT nYSrc, HBITMAP hbmMask,
-       INT xMask, INT yMask, DWORD dwRop)
+       INT xMask, INT yMask, DWORD dwRop,
+       IN DWORD crBackColor)
 {
        HBITMAP hOldMaskBitmap, hBitmap2, hOldBitmap2, hBitmap3, hOldBitmap3;
        HDC hDCMask, hDC1, hDC2;
@@ -872,39 +898,39 @@ NtGdiMaskBlt (
        };
 
        if (!hbmMask)
-               return NtGdiBitBlt(hdcDest, nXDest, nYDest, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, FRGND_ROP3(dwRop));
+               return NtGdiBitBlt(hdcDest, nXDest, nYDest, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, FRGND_ROP3(dwRop), 0, 0);
 
        /* 1. make mask bitmap's dc */
-       hDCMask = NtGdiCreateCompatableDC(hdcDest);
+       hDCMask = NtGdiCreateCompatibleDC(hdcDest);
        hOldMaskBitmap = (HBITMAP)NtGdiSelectObject(hDCMask, hbmMask);
 
        /* 2. make masked Background bitmap */
 
        /* 2.1 make bitmap */
-       hDC1 = NtGdiCreateCompatableDC(hdcDest);
+       hDC1 = NtGdiCreateCompatibleDC(hdcDest);
        hBitmap2 = NtGdiCreateCompatibleBitmap(hdcDest, nWidth, nHeight);
        hOldBitmap2 = (HBITMAP)NtGdiSelectObject(hDC1, hBitmap2);
 
        /* 2.2 draw dest bitmap and mask */
-       NtGdiBitBlt(hDC1, 0, 0, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, SRCCOPY);
-       NtGdiBitBlt(hDC1, 0, 0, nWidth, nHeight, hdcDest, nXDest, nYDest, BKGND_ROP3(dwRop));
-       NtGdiBitBlt(hDC1, 0, 0, nWidth, nHeight, hDCMask, xMask, yMask, DSTERASE);
+       NtGdiBitBlt(hDC1, 0, 0, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, SRCCOPY, 0, 0);
+       NtGdiBitBlt(hDC1, 0, 0, nWidth, nHeight, hdcDest, nXDest, nYDest, BKGND_ROP3(dwRop), 0, 0);
+       NtGdiBitBlt(hDC1, 0, 0, nWidth, nHeight, hDCMask, xMask, yMask, DSTERASE, 0, 0);
 
        /* 3. make masked Foreground bitmap */
 
        /* 3.1 make bitmap */
-       hDC2 = NtGdiCreateCompatableDC(hdcDest);
+       hDC2 = NtGdiCreateCompatibleDC(hdcDest);
        hBitmap3 = NtGdiCreateCompatibleBitmap(hdcDest, nWidth, nHeight);
        hOldBitmap3 = (HBITMAP)NtGdiSelectObject(hDC2, hBitmap3);
 
        /* 3.2 draw src bitmap and mask */
-       NtGdiBitBlt(hDC2, 0, 0, nWidth, nHeight, hdcDest, nXDest, nYDest, SRCCOPY);
-       NtGdiBitBlt(hDC2, 0, 0, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, FRGND_ROP3(dwRop));
-       NtGdiBitBlt(hDC2, 0, 0, nWidth, nHeight, hDCMask, xMask, yMask, SRCAND);
+       NtGdiBitBlt(hDC2, 0, 0, nWidth, nHeight, hdcDest, nXDest, nYDest, SRCCOPY, 0, 0);
+       NtGdiBitBlt(hDC2, 0, 0, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, FRGND_ROP3(dwRop), 0,0);
+       NtGdiBitBlt(hDC2, 0, 0, nWidth, nHeight, hDCMask, xMask, yMask, SRCAND, 0, 0);
 
        /* 4. combine two bitmap and copy it to hdcDest */
-       NtGdiBitBlt(hDC1, 0, 0, nWidth, nHeight, hDC2, 0, 0, SRCPAINT);
-       NtGdiBitBlt(hdcDest, nXDest, nYDest, nWidth, nHeight, hDC1, 0, 0, SRCCOPY);
+       NtGdiBitBlt(hDC1, 0, 0, nWidth, nHeight, hDC2, 0, 0, SRCPAINT, 0, 0);
+       NtGdiBitBlt(hdcDest, nXDest, nYDest, nWidth, nHeight, hDC1, 0, 0, SRCCOPY, 0, 0);
 
        /* 5. restore all object */
        NtGdiSelectObject(hDCMask, hOldMaskBitmap);
@@ -922,27 +948,30 @@ NtGdiMaskBlt (
        return TRUE;
 }
 
-BOOL STDCALL
+BOOL
+APIENTRY
 NtGdiPlgBlt(
-       HDC  hDCDest,
-       CONST POINT  *Point,
-       HDC  hDCSrc,
-       INT  XSrc,
-       INT  YSrc,
-       INT  Width,
-       INT  Height,
-       HBITMAP  hMaskBitmap,
-       INT  xMask,
-       INT  yMask)
+    IN HDC hdcTrg,
+    IN LPPOINT pptlTrg,
+    IN HDC hdcSrc,
+    IN INT xSrc,
+    IN INT ySrc,
+    IN INT cxSrc,
+    IN INT cySrc,
+    IN HBITMAP hbmMask,
+    IN INT xMask,
+    IN INT yMask,
+    IN DWORD crBackColor)
 {
        UNIMPLEMENTED;
+       return FALSE;
 }
 
 LONG STDCALL
 NtGdiSetBitmapBits(
        HBITMAP  hBitmap,
        DWORD  Bytes,
-       CONST VOID *Bits)
+       IN PBYTE Bits)
 {
        LONG height, ret;
        PBITMAPOBJ bmp;
@@ -960,19 +989,19 @@ NtGdiSetBitmapBits(
        }
 
        /* Only get entire lines */
-       height = Bytes / bmp->bitmap.bmWidthBytes;
-       if (height > bmp->bitmap.bmHeight)
+       height = Bytes / abs(bmp->SurfObj.lDelta);
+       if (height > bmp->SurfObj.sizlBitmap.cy)
        {
-               height = bmp->bitmap.bmHeight;
+               height = bmp->SurfObj.sizlBitmap.cy;
        }
-       Bytes = height * bmp->bitmap.bmWidthBytes;
+       Bytes = height * abs(bmp->SurfObj.lDelta);
        DPRINT ("(%08x, bytes:%ld, bits:%p) %dx%d %d colors fetched height: %ld\n",
                hBitmap,
                Bytes,
                Bits,
-               bmp->bitmap.bmWidth,
-               bmp->bitmap.bmHeight,
-               1 << bmp->bitmap.bmBitsPixel,
+               bmp->SurfObj.sizlBitmap.cx,
+               bmp->SurfObj.sizlBitmap.cy,
+               1 << BitsPerFormat(bmp->SurfObj.iBitmapFormat),
                height);
 
 #if 0
@@ -993,24 +1022,11 @@ NtGdiSetBitmapBits(
        else
 #endif
        {
-               /* FIXME: Alloc enough for entire bitmap */
-               if (bmp->bitmap.bmBits == NULL)
-               {
-                       bmp->bitmap.bmBits = ExAllocatePoolWithTag(PagedPool, Bytes, TAG_BITMAP);
-               }
-               if(!bmp->bitmap.bmBits)
-               {
-                       DPRINT ("Unable to allocate bit buffer\n");
-                       ret = 0;
-               }
-               else
-               {
-                       memcpy(bmp->bitmap.bmBits, Bits, Bytes);
-                       ret = Bytes;
-               }
+               memcpy(bmp->SurfObj.pvBits, Bits, Bytes);
+               ret = Bytes;
        }
 
-       BITMAPOBJ_UnlockBitmap(hBitmap);
+       BITMAPOBJ_UnlockBitmap(bmp);
 
        return ret;
 }
@@ -1037,7 +1053,7 @@ NtGdiSetBitmapDimensionEx(
        bmp->dimension.cx = Width;
        bmp->dimension.cy = Height;
 
-       BITMAPOBJ_UnlockBitmap (hBitmap);
+       BITMAPOBJ_UnlockBitmap (bmp);
 
        return TRUE;
 }
@@ -1049,10 +1065,9 @@ NtGdiSetPixel(
        INT  Y,
        COLORREF  Color)
 {
-       COLORREF cr = NtGdiGetPixel(hDC,X,Y);
-       if(cr != CLR_INVALID && NtGdiSetPixelV(hDC,X,Y,Color))
+       if (NtGdiSetPixelV(hDC,X,Y,Color))
        {
-               return(cr);
+               return NtGdiGetPixel(hDC,X,Y);
        }
        return ((COLORREF) -1);
 }
@@ -1064,14 +1079,17 @@ NtGdiSetPixelV(
        INT  Y,
        COLORREF  Color)
 {
-       HBRUSH NewBrush = NtGdiCreateSolidBrush(Color);
+       HBRUSH NewBrush = NtGdiCreateSolidBrush(Color, NULL);
        HGDIOBJ OldBrush;
 
        if (NewBrush == NULL)
                return(FALSE);
        OldBrush = NtGdiSelectObject(hDC, NewBrush);
        if (OldBrush == NULL)
+       {
+               NtGdiDeleteObject(NewBrush);
                return(FALSE);
+       }
        NtGdiPatBlt(hDC, X, Y, 1, 1, PATCOPY);
        NtGdiSelectObject(hDC, OldBrush);
        NtGdiDeleteObject(NewBrush);
@@ -1090,19 +1108,17 @@ NtGdiStretchBlt(
        INT  YOriginSrc,
        INT  WidthSrc,
        INT  HeightSrc,
-       DWORD  ROP)
+       DWORD  ROP,
+       IN DWORD dwBackColor)
 {
        PDC DCDest = NULL;
        PDC DCSrc  = NULL;
-       SURFOBJ *SurfDest, *SurfSrc;
-       PSURFGDI SurfGDIDest, SurfGDISrc;
+       BITMAPOBJ *BitmapDest, *BitmapSrc;
        RECTL DestRect;
        RECTL SourceRect;
        BOOL Status;
-       PPALGDI PalDestGDI, PalSourceGDI;
        XLATEOBJ *XlateObj = NULL;
-       HPALETTE SourcePalette, DestPalette;
-       ULONG SourceMode, DestMode;
+       HPALETTE SourcePalette = 0, DestPalette = 0;
        PGDIBRUSHOBJ BrushObj;
        BOOL UsesSource = ((ROP & 0xCC0000) >> 2) != (ROP & 0x330000);
        BOOL UsesPattern = ((ROP & 0xF00000) >> 4) != (ROP & 0x0F0000);
@@ -1119,6 +1135,12 @@ NtGdiStretchBlt(
                SetLastWin32Error(ERROR_INVALID_HANDLE);
                return FALSE;
        }
+       if (DCDest->IsIC)
+       {
+               DC_UnlockDc(DCDest);
+               /* Yes, Windows really returns TRUE in this case */
+               return TRUE;
+       }
 
        if (UsesSource)
        {
@@ -1127,11 +1149,18 @@ NtGdiStretchBlt(
                        DCSrc = DC_LockDc(hDCSrc);
                        if (NULL == DCSrc)
                        {
-                               DC_UnlockDc(hDCDest);
+                               DC_UnlockDc(DCDest);
                                DPRINT1("Invalid source dc handle (0x%08x) passed to NtGdiStretchBlt\n", hDCSrc);
                                SetLastWin32Error(ERROR_INVALID_HANDLE);
                                return FALSE;
                        }
+                       if (DCSrc->IsIC)
+                       {
+                               DC_UnlockDc(DCSrc);
+                               DC_UnlockDc(DCDest);
+                               /* Yes, Windows really returns TRUE in this case */
+                               return TRUE;
+                       }
                }
                else
                {
@@ -1163,17 +1192,71 @@ NtGdiStretchBlt(
        SourceRect.bottom = YOriginSrc+HeightSrc;
 
        /* Determine surfaces to be used in the bitblt */
-       SurfDest = (SURFOBJ*)AccessUserObject((ULONG)DCDest->Surface);
-       SurfGDIDest = (PSURFGDI)AccessInternalObjectFromUserObject(SurfDest);
+       BitmapDest = BITMAPOBJ_LockBitmap(DCDest->w.hBitmap);
        if (UsesSource)
        {
-               SurfSrc  = (SURFOBJ*)AccessUserObject((ULONG)DCSrc->Surface);
-               SurfGDISrc  = (PSURFGDI)AccessInternalObjectFromUserObject(SurfSrc);
+               if (DCSrc->w.hBitmap == DCDest->w.hBitmap)
+                       BitmapSrc = BitmapDest;
+               else
+                       BitmapSrc = BITMAPOBJ_LockBitmap(DCSrc->w.hBitmap);
        }
        else
        {
-               SurfSrc  = NULL;
-               SurfGDISrc  = NULL;
+               BitmapSrc = NULL;
+       }
+
+       if ( UsesSource )
+       {
+               int sw = BitmapSrc->SurfObj.sizlBitmap.cx;
+               int sh = BitmapSrc->SurfObj.sizlBitmap.cy;
+               if ( SourceRect.left < 0 )
+               {
+                       DestRect.left = DestRect.right - (DestRect.right-DestRect.left) * (SourceRect.right)/abs(SourceRect.right-SourceRect.left);
+                       SourceRect.left = 0;
+               }
+               if ( SourceRect.top < 0 )
+               {
+                       DestRect.top = DestRect.bottom - (DestRect.bottom-DestRect.top) * (SourceRect.bottom)/abs(SourceRect.bottom-SourceRect.top);
+                       SourceRect.top = 0;
+               }
+               if ( SourceRect.right < -1 )
+               {
+                       DestRect.right = DestRect.left + (DestRect.right-DestRect.left) * (-1-SourceRect.left)/abs(SourceRect.right-SourceRect.left);
+                       SourceRect.right = -1;
+               }
+               if ( SourceRect.bottom < -1 )
+               {
+                       DestRect.bottom = DestRect.top + (DestRect.bottom-DestRect.top) * (-1-SourceRect.top)/abs(SourceRect.bottom-SourceRect.top);
+                       SourceRect.bottom = -1;
+               }
+               if ( SourceRect.right > sw )
+               {
+                       DestRect.right = DestRect.left + (DestRect.right-DestRect.left) * abs(sw-SourceRect.left) / abs(SourceRect.right-SourceRect.left);
+                       SourceRect.right = sw;
+               }
+               if ( SourceRect.bottom > sh )
+               {
+                       DestRect.bottom = DestRect.top + (DestRect.bottom-DestRect.top) * abs(sh-SourceRect.top) / abs(SourceRect.bottom-SourceRect.top);
+                       SourceRect.bottom = sh;
+               }
+               sw--;
+               sh--;
+               if ( SourceRect.left > sw )
+               {
+                       DestRect.left = DestRect.right - (DestRect.right-DestRect.left) * (SourceRect.right-sw) / abs(SourceRect.right-SourceRect.left);
+                       SourceRect.left = 0;
+               }
+               if ( SourceRect.top > sh )
+               {
+                       DestRect.top = DestRect.bottom - (DestRect.bottom-DestRect.top) * (SourceRect.bottom-sh) / abs(SourceRect.bottom-SourceRect.top);
+                       SourceRect.top = 0;
+               }
+               if (0 == (DestRect.right-DestRect.left) || 0 == (DestRect.bottom-DestRect.top) || 0 == (SourceRect.right-SourceRect.left) || 0 == (SourceRect.bottom-SourceRect.top))
+               {
+                       SetLastWin32Error(ERROR_INVALID_PARAMETER);
+                       Status = FALSE;
+                       goto failed;
+               }
        }
 
        if (UsesPattern)
@@ -1183,9 +1266,9 @@ NtGdiStretchBlt(
                {
                        if (UsesSource && hDCSrc != hDCDest)
                        {
-                               DC_UnlockDc(hDCSrc);
+                               DC_UnlockDc(DCSrc);
                        }
-                       DC_UnlockDc(hDCDest);
+                       DC_UnlockDc(DCDest);
                        SetLastWin32Error(ERROR_INVALID_HANDLE);
                        return FALSE;
                }
@@ -1195,85 +1278,192 @@ NtGdiStretchBlt(
                BrushObj = NULL;
        }
 
-       if (DCDest->w.hPalette != 0)
+       /* Create the XLATEOBJ. */
+       if (UsesSource)
        {
-               DestPalette = DCDest->w.hPalette;
+               if (DCDest->w.hPalette != 0)
+                       DestPalette = DCDest->w.hPalette;
+
+               if (DCSrc->w.hPalette != 0)
+                       SourcePalette = DCSrc->w.hPalette;
+
+               /* FIXME: Use the same logic for create XLATEOBJ as in NtGdiBitBlt. */
+               XlateObj = (XLATEOBJ*)IntEngCreateXlate(0, 0, DestPalette, SourcePalette);
+               if (NULL == XlateObj)
+               {
+                       if (UsesSource && hDCSrc != hDCDest)
+                       {
+                               DC_UnlockDc(DCSrc);
+                       }
+                       DC_UnlockDc(DCDest);
+                       SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES);
+                       return FALSE;
+               }
        }
-       else
+
+       /* Perform the bitblt operation */
+       Status = IntEngStretchBlt(&BitmapDest->SurfObj, &BitmapSrc->SurfObj,
+                                  NULL, DCDest->CombinedClip, XlateObj,
+                                  &DestRect, &SourceRect, NULL, NULL, NULL,
+                                  COLORONCOLOR);
+
+       if (UsesSource)
+               EngDeleteXlate(XlateObj);
+       if (UsesPattern)
        {
-               DestPalette = NtGdiGetStockObject(DEFAULT_PALETTE);
+               BRUSHOBJ_UnlockBrush(BrushObj);
        }
-
-       if (UsesSource && DCSrc->w.hPalette != 0)
+failed:
+       if (UsesSource && DCSrc->w.hBitmap != DCDest->w.hBitmap)
        {
-               SourcePalette = DCSrc->w.hPalette;
+               BITMAPOBJ_UnlockBitmap(BitmapSrc);
        }
-       else
+       BITMAPOBJ_UnlockBitmap(BitmapDest);
+       if (UsesSource && hDCSrc != hDCDest)
        {
-               SourcePalette = NtGdiGetStockObject(DEFAULT_PALETTE);
+               DC_UnlockDc(DCSrc);
        }
+       DC_UnlockDc(DCDest);
 
-       PalSourceGDI = PALETTE_LockPalette(SourcePalette);
-       if (NULL == PalSourceGDI)
+       return Status;
+}
+
+BOOL STDCALL
+NtGdiAlphaBlend(
+       HDC  hDCDest,
+       LONG  XOriginDest,
+       LONG  YOriginDest,
+       LONG  WidthDest,
+       LONG  HeightDest,
+       HDC  hDCSrc,
+       LONG  XOriginSrc,
+       LONG  YOriginSrc,
+       LONG  WidthSrc,
+       LONG  HeightSrc,
+       BLENDFUNCTION  BlendFunc,
+       IN HANDLE hcmXform)
+{
+       PDC DCDest = NULL;
+       PDC DCSrc  = NULL;
+       BITMAPOBJ *BitmapDest, *BitmapSrc;
+       RECTL DestRect, SourceRect;
+       BOOL Status;
+       XLATEOBJ *XlateObj;
+       BLENDOBJ BlendObj = {BlendFunc};
+       HPALETTE SourcePalette = 0, DestPalette = 0;
+
+       DCDest = DC_LockDc(hDCDest);
+       if (NULL == DCDest)
        {
-               if (UsesSource && hDCSrc != hDCDest)
-               {
-                       DC_UnlockDc(hDCSrc);
-               }
-               DC_UnlockDc(hDCDest);
+               DPRINT1("Invalid destination dc handle (0x%08x) passed to NtGdiAlphaBlend\n", hDCDest);
                SetLastWin32Error(ERROR_INVALID_HANDLE);
                return FALSE;
        }
-       SourceMode = PalSourceGDI->Mode;
-       PALETTE_UnlockPalette(SourcePalette);
-
-       if (DestPalette == SourcePalette)
+       if (DCDest->IsIC)
        {
-               DestMode = SourceMode;
+               DC_UnlockDc(DCDest);
+               /* Yes, Windows really returns TRUE in this case */
+               return TRUE;
        }
-       else
+
+       if (hDCSrc != hDCDest)
        {
-               PalDestGDI = PALETTE_LockPalette(DestPalette);
-               if (NULL == PalDestGDI)
+               DCSrc = DC_LockDc(hDCSrc);
+               if (NULL == DCSrc)
                {
-                       if (UsesSource && hDCSrc != hDCDest)
-                       {
-                               DC_UnlockDc(hDCSrc);
-                       }
-                       DC_UnlockDc(hDCDest);
+                       DC_UnlockDc(DCDest);
+                       DPRINT1("Invalid source dc handle (0x%08x) passed to NtGdiAlphaBlend\n", hDCSrc);
                        SetLastWin32Error(ERROR_INVALID_HANDLE);
                        return FALSE;
                }
-               DestMode = PalDestGDI->Mode;
-               PALETTE_UnlockPalette(DestPalette);
-       }
-
-       XlateObj = (XLATEOBJ*)IntEngCreateXlate(DestMode, SourceMode, DestPalette, SourcePalette);
-       if (NULL == XlateObj)
-       {
-               if (UsesSource && hDCSrc != hDCDest)
+               if (DCSrc->IsIC)
                {
-                       DC_UnlockDc(hDCSrc);
+                       DC_UnlockDc(DCSrc);
+                       DC_UnlockDc(DCDest);
+                       /* Yes, Windows really returns TRUE in this case */
+                       return TRUE;
                }
-               DC_UnlockDc(hDCDest);
-               SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES);
-               return FALSE;
+       }
+       else
+       {
+               DCSrc = DCDest;
        }
 
-       /* Perform the bitblt operation */
-       Status = IntEngStretchBlt(SurfDest, SurfSrc, NULL, DCDest->CombinedClip,
-               XlateObj, &DestRect, &SourceRect, NULL, NULL, NULL, COLORONCOLOR);
+       /* Offset the destination and source by the origin of their DCs. */
+       XOriginDest += DCDest->w.DCOrgX;
+       YOriginDest += DCDest->w.DCOrgY;
+       XOriginSrc += DCSrc->w.DCOrgX;
+       YOriginSrc += DCSrc->w.DCOrgY;
 
-       EngDeleteXlate(XlateObj);
-       if (UsesPattern)
+       DestRect.left   = XOriginDest;
+       DestRect.top    = YOriginDest;
+       DestRect.right  = XOriginDest + WidthDest;
+       DestRect.bottom = YOriginDest + HeightDest;
+
+       SourceRect.left   = XOriginSrc;
+       SourceRect.top    = YOriginSrc;
+       SourceRect.right  = XOriginSrc + WidthSrc;
+       SourceRect.bottom = YOriginSrc + HeightSrc;
+
+       /* Determine surfaces to be used in the bitblt */
+       BitmapDest = BITMAPOBJ_LockBitmap(DCDest->w.hBitmap);
+       if (DCSrc->w.hBitmap == DCDest->w.hBitmap)
+               BitmapSrc = BitmapDest;
+       else
+               BitmapSrc = BITMAPOBJ_LockBitmap(DCSrc->w.hBitmap);
+
+       /* Create the XLATEOBJ. */
+       if (DCDest->w.hPalette != 0)
+               DestPalette = DCDest->w.hPalette;
+       if (DCSrc->w.hPalette != 0)
+               SourcePalette = DCSrc->w.hPalette;
+
+       /* KB41464 details how to convert between mono and color */
+       if (DCDest->w.bitsPerPixel == 1 && DCSrc->w.bitsPerPixel == 1)
        {
-               BRUSHOBJ_UnlockBrush(DCDest->w.hBrush);
+               XlateObj = NULL;
        }
-       if (UsesSource && hDCSrc != hDCDest)
+       else
        {
-               DC_UnlockDc(hDCSrc);
+               if (DCDest->w.bitsPerPixel == 1)
+               {
+                       XlateObj = IntEngCreateMonoXlate(0, DestPalette, SourcePalette, DCSrc->w.backgroundColor);
+               }
+               else if (DCSrc->w.bitsPerPixel == 1)
+               {
+                       XlateObj = IntEngCreateSrcMonoXlate(DestPalette, DCSrc->w.backgroundColor, DCSrc->w.textColor);
+               }
+               else
+               {
+                       XlateObj = IntEngCreateXlate(0, 0, DestPalette, SourcePalette);
+               }
+               if (NULL == XlateObj)
+               {
+                       BITMAPOBJ_UnlockBitmap(BitmapDest);
+                       if (BitmapSrc != BitmapDest)
+                               BITMAPOBJ_UnlockBitmap(BitmapSrc);
+                       DC_UnlockDc(DCDest);
+                       if (hDCSrc != hDCDest)
+                               DC_UnlockDc(DCSrc);
+                       SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES);
+                       return FALSE;
+               }
        }
-       DC_UnlockDc(hDCDest);
+
+       /* Perform the alpha blend operation */
+       Status = IntEngAlphaBlend(&BitmapDest->SurfObj, &BitmapSrc->SurfObj,
+                                 DCDest->CombinedClip, XlateObj,
+                                 &DestRect, &SourceRect, &BlendObj);
+
+       if (XlateObj != NULL)
+               EngDeleteXlate(XlateObj);
+
+       BITMAPOBJ_UnlockBitmap(BitmapDest);
+       if (BitmapSrc != BitmapDest)
+               BITMAPOBJ_UnlockBitmap(BitmapSrc);
+       DC_UnlockDc(DCDest);
+       if (hDCSrc != hDCDest)
+               DC_UnlockDc(DCSrc);
 
        return Status;
 }
@@ -1317,31 +1507,35 @@ BITMAPOBJ_GetWidthBytes (INT bmWidth, INT bpp)
 HBITMAP FASTCALL
 BITMAPOBJ_CopyBitmap(HBITMAP  hBitmap)
 {
-       PBITMAPOBJ  bmp;
        HBITMAP  res;
        BITMAP  bm;
+       BITMAPOBJ *Bitmap;
 
-       bmp = BITMAPOBJ_LockBitmap(hBitmap);
-       if (bmp == NULL)
-       {
+        if (hBitmap == NULL)
                return 0;
-       }
-       res = 0;
 
-       bm = bmp->bitmap;
+       Bitmap = GDIOBJ_LockObj(hBitmap, GDI_OBJECT_TYPE_BITMAP);
+        if (Bitmap == NULL)
+               return 0;
+
+       BITMAP_GetObject(Bitmap, sizeof(BITMAP), &bm);
        bm.bmBits = NULL;
-       BITMAPOBJ_UnlockBitmap(hBitmap);
-       res = NtGdiCreateBitmapIndirect(&bm);
+       if (Bitmap->SurfObj.lDelta >= 0)
+               bm.bmHeight = -bm.bmHeight;
+
+       res = IntCreateBitmapIndirect(&bm);
        if(res)
        {
-               char *buf;
+               PBYTE buf;
 
-               buf = ExAllocatePoolWithTag (NonPagedPool, bm.bmWidthBytes * bm.bmHeight, TAG_BITMAP);
-               NtGdiGetBitmapBits (hBitmap, bm.bmWidthBytes * bm.bmHeight, buf);
-               NtGdiSetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
+               buf = ExAllocatePoolWithTag (PagedPool, bm.bmWidthBytes * abs(bm.bmHeight), TAG_BITMAP);
+               NtGdiGetBitmapBits (hBitmap, bm.bmWidthBytes * abs(bm.bmHeight), buf);
+               NtGdiSetBitmapBits (res, bm.bmWidthBytes * abs(bm.bmHeight), buf);
                ExFreePool (buf);
        }
 
+       GDIOBJ_UnlockObjByPtr(Bitmap);
+
        return  res;
 }
 
@@ -1363,8 +1557,16 @@ BITMAP_GetObject(BITMAPOBJ * bmp, INT count, LPVOID buffer)
        }
        else
        {
+               BITMAP Bitmap;
                if (count > (INT) sizeof(BITMAP)) count = sizeof(BITMAP);
-               memcpy(buffer, &bmp->bitmap, count);
+               Bitmap.bmType = 0;
+               Bitmap.bmWidth = bmp->SurfObj.sizlBitmap.cx;
+               Bitmap.bmHeight = bmp->SurfObj.sizlBitmap.cy;
+               Bitmap.bmWidthBytes = abs(bmp->SurfObj.lDelta);
+               Bitmap.bmPlanes = 1;
+               Bitmap.bmBitsPixel = BitsPerFormat(bmp->SurfObj.iBitmapFormat);
+               Bitmap.bmBits = bmp->SurfObj.pvBits;
+               memcpy(buffer, &Bitmap, count);
                return count;
        }
 }