Transform correctly the coordinates.
[reactos.git] / reactos / subsys / win32k / objects / bitmaps.c
index ce69a5f..1800c5f 100644 (file)
@@ -41,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;
@@ -114,6 +116,8 @@ NtGdiBitBlt(
        DestRect.right  = XDest+Width;
        DestRect.bottom = YDest+Height;
 
+        IntLPtoDP(DCDest, (LPPOINT)&DestRect, 2);
+
        SourcePoint.x = XSrc;
        SourcePoint.y = YSrc;
 
@@ -396,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");
@@ -434,7 +434,7 @@ 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!!!!!! */
@@ -447,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)
 {
@@ -542,13 +563,32 @@ NtGdiCreateCompatibleBitmap(
 }
 
 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
@@ -676,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...
@@ -704,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);
@@ -720,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;
@@ -857,7 +898,7 @@ 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 = NtGdiCreateCompatibleDC(hdcDest);
@@ -871,9 +912,9 @@ NtGdiMaskBlt (
        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 */
 
@@ -883,13 +924,13 @@ NtGdiMaskBlt (
        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);
@@ -907,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;
@@ -928,7 +971,7 @@ LONG STDCALL
 NtGdiSetBitmapBits(
        HBITMAP  hBitmap,
        DWORD  Bytes,
-       CONST VOID *Bits)
+       IN PBYTE Bits)
 {
        LONG height, ret;
        PBITMAPOBJ bmp;
@@ -1022,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);
 }
@@ -1037,7 +1079,7 @@ NtGdiSetPixelV(
        INT  Y,
        COLORREF  Color)
 {
-       HBRUSH NewBrush = NtGdiCreateSolidBrush(Color);
+       HBRUSH NewBrush = NtGdiCreateSolidBrush(Color, NULL);
        HGDIOBJ OldBrush;
 
        if (NewBrush == NULL)
@@ -1066,7 +1108,8 @@ NtGdiStretchBlt(
        INT  YOriginSrc,
        INT  WidthSrc,
        INT  HeightSrc,
-       DWORD  ROP)
+       DWORD  ROP,
+       IN DWORD dwBackColor)
 {
        PDC DCDest = NULL;
        PDC DCSrc  = NULL;
@@ -1285,6 +1328,146 @@ failed:
        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;
+               }
+       }
+
+       /* 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;
+}
+
 /*  Internal Functions  */
 
 INT FASTCALL
@@ -1340,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);