Transform correctly the coordinates.
[reactos.git] / reactos / subsys / win32k / objects / bitmaps.c
index 92f193f..1800c5f 100644 (file)
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 /* $Id$ */
+
 #include <w32k.h>
 
+#define NDEBUG
+#include <debug.h>
+
 #define IN_RECT(r,x,y) \
 ( \
  (x) >= (r).left && \
@@ -37,7 +41,9 @@ NtGdiBitBlt(
        HDC  hDCSrc,
        INT  XSrc,
        INT  YSrc,
-       DWORD  ROP)
+       DWORD  ROP,
+       IN DWORD crBackColor,
+       IN FLONG fl)
 {
        PDC DCDest = NULL;
        PDC DCSrc  = NULL;
@@ -61,7 +67,7 @@ NtGdiBitBlt(
        }
        if (DCDest->IsIC)
        {
-               DC_UnlockDc(hDCDest);
+               DC_UnlockDc(DCDest);
                /* Yes, Windows really returns TRUE in this case */
                return TRUE;
        }
@@ -73,15 +79,15 @@ 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(hDCSrc);
-                               DC_UnlockDc(hDCDest);
+                               DC_UnlockDc(DCSrc);
+                               DC_UnlockDc(DCDest);
                                /* Yes, Windows really returns TRUE in this case */
                                return TRUE;
                        }
@@ -110,9 +116,11 @@ 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;
 
@@ -137,17 +145,17 @@ NtGdiBitBlt(
                {
                        if (UsesSource && hDCSrc != hDCDest)
                        {
-                               DC_UnlockDc(hDCSrc);
+                               DC_UnlockDc(DCSrc);
                        }
                        if(BitmapDest != NULL)
                        {
-                                BITMAPOBJ_UnlockBitmap(DCDest->w.hBitmap);
+                                BITMAPOBJ_UnlockBitmap(BitmapDest);
                        }
                        if(BitmapSrc != NULL && BitmapSrc != BitmapDest)
                        {
-                                BITMAPOBJ_UnlockBitmap(DCSrc->w.hBitmap);
+                                BITMAPOBJ_UnlockBitmap(BitmapSrc);
                        }
-                       DC_UnlockDc(hDCDest);
+                       DC_UnlockDc(DCDest);
                        SetLastWin32Error(ERROR_INVALID_HANDLE);
                        return FALSE;
                }
@@ -191,20 +199,20 @@ NtGdiBitBlt(
                        {
                                if (UsesSource && hDCSrc != hDCDest)
                                {
-                                       DC_UnlockDc(hDCSrc);
+                                       DC_UnlockDc(DCSrc);
                                }
-                               DC_UnlockDc(hDCDest);
+                               DC_UnlockDc(DCDest);
                                if(BitmapDest != NULL)
                                {
-                                       BITMAPOBJ_UnlockBitmap(DCDest->w.hBitmap);
+                                       BITMAPOBJ_UnlockBitmap(BitmapDest);
                                }
                                if(BitmapSrc != NULL && BitmapSrc != BitmapDest)
                                {
-                                       BITMAPOBJ_UnlockBitmap(DCSrc->w.hBitmap);
+                                       BITMAPOBJ_UnlockBitmap(BitmapSrc);
                                }
                                if(BrushObj != NULL)
                                {
-                                        BRUSHOBJ_UnlockBrush(DCDest->w.hBrush);
+                                        BRUSHOBJ_UnlockBrush(BrushObj);
                                }
                                SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES);
                                return FALSE;
@@ -213,30 +221,32 @@ NtGdiBitBlt(
        }
 
        /* Perform the bitblt operation */
-       Status = IntEngBitBlt(BitmapDest, BitmapSrc, NULL, DCDest->CombinedClip, XlateObj,
-                             &DestRect, &SourcePoint, NULL, BrushObj ? &BrushInst.BrushObject : NULL,
+       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);
-               
+
         if(BitmapDest != NULL)
         {
-                BITMAPOBJ_UnlockBitmap(DCDest->w.hBitmap);
+                BITMAPOBJ_UnlockBitmap(BitmapDest);
         }
        if (UsesSource && BitmapSrc != BitmapDest)
        {
-               BITMAPOBJ_UnlockBitmap(DCSrc->w.hBitmap);
+               BITMAPOBJ_UnlockBitmap(BitmapSrc);
        }
        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;
 }
@@ -264,7 +274,7 @@ NtGdiTransparentBlt(
   USHORT PalDestMode, PalSrcMode;
   ULONG TransparentColor = 0;
   BOOL Ret = FALSE;
-  
+
   if(!(DCDest = DC_LockDc(hdcDst)))
   {
     DPRINT1("Invalid destination dc handle (0x%08x) passed to NtGdiTransparentBlt\n", hdcDst);
@@ -273,14 +283,14 @@ NtGdiTransparentBlt(
   }
   if (DCDest->IsIC)
   {
-    DC_UnlockDc(hdcDst);
+    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;
@@ -291,39 +301,39 @@ NtGdiTransparentBlt(
   }
   if (DCSrc->IsIC)
   {
-    DC_UnlockDc(hdcSrc);
+    DC_UnlockDc(DCSrc);
     if(hdcDst != hdcSrc)
     {
-      DC_UnlockDc(hdcDst);
+      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;
-  
+
   if(DCSrc->w.hPalette)
     SourcePalette = DCSrc->w.hPalette;
-  
+
   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;
   }
@@ -331,31 +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);
-  
+
   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;
@@ -364,23 +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(BitmapDest, BitmapSrc, DCDest->CombinedClip, XlateObj, &rcDest, &rcSrc, 
+
+  Ret = IntEngTransparentBlt(&BitmapDest->SurfObj, &BitmapSrc->SurfObj,
+                             DCDest->CombinedClip, XlateObj, &rcDest, &rcSrc,
                              TransparentColor, 0);
-  
+
 done:
-  BITMAPOBJ_UnlockBitmap(DCDest->w.hBitmap);
-  BITMAPOBJ_UnlockBitmap(DCSrc->w.hBitmap);
-  DC_UnlockDc(hdcSrc);
+  BITMAPOBJ_UnlockBitmap(BitmapDest);
+  BITMAPOBJ_UnlockBitmap(BitmapSrc);
+  DC_UnlockDc(DCSrc);
   if(hdcDst != hdcSrc)
   {
-    DC_UnlockDc(hdcDst);
+    DC_UnlockDc(DCDest);
   }
   if(XlateObj)
   {
@@ -389,37 +400,33 @@ done:
   return Ret;
 }
 
-HBITMAP STDCALL
-NtGdiCreateBitmap(
-       INT  Width,
-       INT  Height,
-       UINT  Planes,
-       UINT  BitsPerPel,
-       CONST VOID *Bits)
+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! */
-   BitsPerPel = (BYTE)BitsPerPel * (BYTE)Planes;
+   BitsPixel = BM->bmBitsPixel * BM->bmPlanes;
 
    /* Check parameters */
-   if (!Height || !Width)
+   if (0 == BM->bmHeight || 0 == BM->bmWidth)
    {
       Size.cx = Size.cy = 1;
    }
    else
    {
-      Size.cx = abs(Width);
-      Size.cy = abs(Height);
+      Size.cx = abs(BM->bmWidth);
+      Size.cy = abs(BM->bmHeight);
    }
 
    /* Create the bitmap object. */
-   hBitmap = IntCreateBitmap(Size, BITMAPOBJ_GetWidthBytes(Width, BitsPerPel),
-                             BitmapFormat(BitsPerPel, BI_RGB),
-                             (Height < 0 ? BMF_TOPDOWN : 0) |
-                             (Bits == NULL ? 0 : BMF_NOZEROINIT), NULL);
+   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");
@@ -427,12 +434,12 @@ NtGdiCreateBitmap(
    }
 
    DPRINT("NtGdiCreateBitmap:%dx%d, %d BPP colors returning %08x\n",
-          Size.cx, Size.cy, BitsPerPel, hBitmap);
+          Size.cx, Size.cy, BitsPixel, hBitmap);
 
    bmp = BITMAPOBJ_LockBitmap( hBitmap );
    /* FIXME - bmp can be NULL!!!!!! */
    bmp->flFlags = BITMAPOBJ_IS_APIBITMAP;
-   BITMAPOBJ_UnlockBitmap( hBitmap );
+   BITMAPOBJ_UnlockBitmap( bmp );
 
    /*
     * NOTE: It's ugly practice, but we are using the object even
@@ -440,14 +447,35 @@ NtGdiCreateBitmap(
     * to us it should be safe.
     */
 
-   if (Bits != NULL)
+   if (NULL != BM->bmBits)
    {
-      NtGdiSetBitmapBits(hBitmap, bmp->SurfObj.cjBits, Bits);
+      NtGdiSetBitmapBits(hBitmap, bmp->SurfObj.cjBits, BM->bmBits);
    }
 
    return hBitmap;
 }
 
+HBITMAP STDCALL
+NtGdiCreateBitmap(
+       INT  Width,
+       INT  Height,
+       UINT  Planes,
+       UINT  BitsPixel,
+       IN OPTIONAL LPBYTE Bits)
+{
+   BITMAP BM;
+
+   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;
+
+   return IntCreateBitmapIndirect(&BM);
+}
+
 BOOL INTERNAL_CALL
 BITMAP_Cleanup(PVOID ObjectBody)
 {
@@ -469,6 +497,12 @@ BITMAP_Cleanup(PVOID ObjectBody)
                }
        }
 
+       if (NULL != pBmp->BitsLock)
+       {
+               ExFreePoolWithTag(pBmp->BitsLock, TAG_BITMAPOBJ);
+               pBmp->BitsLock = NULL;
+       }
+
        return TRUE;
 }
 
@@ -524,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
@@ -557,7 +610,7 @@ NtGdiExtFloodFill(
        UINT  FillType)
 {
    DPRINT1("FIXME: NtGdiExtFloodFill is UNIMPLEMENTED\n");
-   
+
    /* lie and say we succeded */
        return TRUE;
 }
@@ -587,7 +640,7 @@ NtGdiGetBitmapDimensionEx(
 
        *Dimension = bmp->dimension;
 
-       BITMAPOBJ_UnlockBitmap(hBitmap);
+       BITMAPOBJ_UnlockBitmap(bmp);
 
        return  TRUE;
 }
@@ -612,7 +665,7 @@ NtGdiGetPixel(HDC hDC, INT XPos, INT YPos)
        }
        if (dc->IsIC)
        {
-               DC_UnlockDc(hDC);
+               DC_UnlockDc(dc);
                return Result;
        }
        XPos += dc->w.DCOrgX;
@@ -639,16 +692,16 @@ NtGdiGetPixel(HDC hDC, INT XPos, INT YPos)
                                }
                                EngDeleteXlate(XlateObj);
                        }
-                       BITMAPOBJ_UnlockBitmap(dc->w.hBitmap);
+                       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 };
@@ -663,7 +716,7 @@ 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...
@@ -671,7 +724,7 @@ NtGdiGetPixel(HDC hDC, INT XPos, INT YPos)
                                        if ( bmpobj )
                                        {
                                                Result = *(COLORREF*)bmpobj->SurfObj.pvScan0;
-                                               BITMAPOBJ_UnlockBitmap ( hBmpTmp );
+                                               BITMAPOBJ_UnlockBitmap ( bmpobj );
                                        }
                                }
                                NtGdiDeleteObject ( hBmpTmp );
@@ -691,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);
@@ -707,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;
@@ -844,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);
@@ -894,18 +948,20 @@ 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;
@@ -915,7 +971,7 @@ LONG STDCALL
 NtGdiSetBitmapBits(
        HBITMAP  hBitmap,
        DWORD  Bytes,
-       CONST VOID *Bits)
+       IN PBYTE Bits)
 {
        LONG height, ret;
        PBITMAPOBJ bmp;
@@ -970,7 +1026,7 @@ NtGdiSetBitmapBits(
                ret = Bytes;
        }
 
-       BITMAPOBJ_UnlockBitmap(hBitmap);
+       BITMAPOBJ_UnlockBitmap(bmp);
 
        return ret;
 }
@@ -997,7 +1053,7 @@ NtGdiSetBitmapDimensionEx(
        bmp->dimension.cx = Width;
        bmp->dimension.cy = Height;
 
-       BITMAPOBJ_UnlockBitmap (hBitmap);
+       BITMAPOBJ_UnlockBitmap (bmp);
 
        return TRUE;
 }
@@ -1009,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);
 }
@@ -1024,7 +1079,7 @@ NtGdiSetPixelV(
        INT  Y,
        COLORREF  Color)
 {
-       HBRUSH NewBrush = NtGdiCreateSolidBrush(Color);
+       HBRUSH NewBrush = NtGdiCreateSolidBrush(Color, NULL);
        HGDIOBJ OldBrush;
 
        if (NewBrush == NULL)
@@ -1053,7 +1108,8 @@ NtGdiStretchBlt(
        INT  YOriginSrc,
        INT  WidthSrc,
        INT  HeightSrc,
-       DWORD  ROP)
+       DWORD  ROP,
+       IN DWORD dwBackColor)
 {
        PDC DCDest = NULL;
        PDC DCSrc  = NULL;
@@ -1081,7 +1137,7 @@ NtGdiStretchBlt(
        }
        if (DCDest->IsIC)
        {
-               DC_UnlockDc(hDCDest);
+               DC_UnlockDc(DCDest);
                /* Yes, Windows really returns TRUE in this case */
                return TRUE;
        }
@@ -1093,15 +1149,15 @@ 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(hDCSrc);
-                               DC_UnlockDc(hDCDest);
+                               DC_UnlockDc(DCSrc);
+                               DC_UnlockDc(DCDest);
                                /* Yes, Windows really returns TRUE in this case */
                                return TRUE;
                        }
@@ -1210,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;
                }
@@ -1237,35 +1293,177 @@ NtGdiStretchBlt(
                {
                        if (UsesSource && hDCSrc != hDCDest)
                        {
-                               DC_UnlockDc(hDCSrc);
+                               DC_UnlockDc(DCSrc);
                        }
-                       DC_UnlockDc(hDCDest);
+                       DC_UnlockDc(DCDest);
                        SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES);
                        return FALSE;
                }
        }
 
        /* Perform the bitblt operation */
-       Status = IntEngStretchBlt(BitmapDest, BitmapSrc, NULL, DCDest->CombinedClip,
-               XlateObj, &DestRect, &SourceRect, NULL, NULL, NULL, COLORONCOLOR);
+       Status = IntEngStretchBlt(&BitmapDest->SurfObj, &BitmapSrc->SurfObj,
+                                  NULL, DCDest->CombinedClip, XlateObj,
+                                  &DestRect, &SourceRect, NULL, NULL, NULL,
+                                  COLORONCOLOR);
 
        if (UsesSource)
                EngDeleteXlate(XlateObj);
        if (UsesPattern)
        {
-               BRUSHOBJ_UnlockBrush(DCDest->w.hBrush);
+               BRUSHOBJ_UnlockBrush(BrushObj);
        }
 failed:
        if (UsesSource && DCSrc->w.hBitmap != DCDest->w.hBitmap)
        {
-               BITMAPOBJ_UnlockBitmap(DCSrc->w.hBitmap);
+               BITMAPOBJ_UnlockBitmap(BitmapSrc);
        }
-       BITMAPOBJ_UnlockBitmap(DCDest->w.hBitmap);
+       BITMAPOBJ_UnlockBitmap(BitmapDest);
        if (UsesSource && hDCSrc != hDCDest)
        {
-               DC_UnlockDc(hDCSrc);
+               DC_UnlockDc(DCSrc);
+       }
+       DC_UnlockDc(DCDest);
+
+       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)
+       {
+               DPRINT1("Invalid destination dc handle (0x%08x) passed to NtGdiAlphaBlend\n", hDCDest);
+               SetLastWin32Error(ERROR_INVALID_HANDLE);
+               return FALSE;
+       }
+       if (DCDest->IsIC)
+       {
+               DC_UnlockDc(DCDest);
+               /* Yes, Windows really returns TRUE in this case */
+               return TRUE;
+       }
+
+       if (hDCSrc != hDCDest)
+       {
+               DCSrc = DC_LockDc(hDCSrc);
+               if (NULL == DCSrc)
+               {
+                       DC_UnlockDc(DCDest);
+                       DPRINT1("Invalid source dc handle (0x%08x) passed to NtGdiAlphaBlend\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
+       {
+               DCSrc = DCDest;
+       }
+
+       /* 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;
+
+       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)
+       {
+               XlateObj = NULL;
+       }
+       else
+       {
+               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;
 }
@@ -1325,10 +1523,10 @@ BITMAPOBJ_CopyBitmap(HBITMAP  hBitmap)
        if (Bitmap->SurfObj.lDelta >= 0)
                bm.bmHeight = -bm.bmHeight;
 
-       res = NtGdiCreateBitmapIndirect(&bm);
+       res = IntCreateBitmapIndirect(&bm);
        if(res)
        {
-               char *buf;
+               PBYTE buf;
 
                buf = ExAllocatePoolWithTag (PagedPool, bm.bmWidthBytes * abs(bm.bmHeight), TAG_BITMAP);
                NtGdiGetBitmapBits (hBitmap, bm.bmWidthBytes * abs(bm.bmHeight), buf);
@@ -1336,7 +1534,7 @@ BITMAPOBJ_CopyBitmap(HBITMAP  hBitmap)
                ExFreePool (buf);
        }
 
-       GDIOBJ_UnlockObj(hBitmap);
+       GDIOBJ_UnlockObjByPtr(Bitmap);
 
        return  res;
 }
@@ -1362,12 +1560,12 @@ BITMAP_GetObject(BITMAPOBJ * bmp, INT count, LPVOID buffer)
                BITMAP Bitmap;
                if (count > (INT) sizeof(BITMAP)) count = sizeof(BITMAP);
                Bitmap.bmType = 0;
-               Bitmap.bmWidth = bmp->SurfObj.sizlBitmap.cx; 
-               Bitmap.bmHeight = bmp->SurfObj.sizlBitmap.cy; 
+               Bitmap.bmWidth = bmp->SurfObj.sizlBitmap.cx;
+               Bitmap.bmHeight = bmp->SurfObj.sizlBitmap.cy;
                Bitmap.bmWidthBytes = abs(bmp->SurfObj.lDelta);
-               Bitmap.bmPlanes = 1; 
+               Bitmap.bmPlanes = 1;
                Bitmap.bmBitsPixel = BitsPerFormat(bmp->SurfObj.iBitmapFormat);
-               Bitmap.bmBits = bmp->SurfObj.pvBits; 
+               Bitmap.bmBits = bmp->SurfObj.pvBits;
                memcpy(buffer, &Bitmap, count);
                return count;
        }