update for HEAD-2003091401
[reactos.git] / subsys / win32k / objects / dib.c
index 869cad6..ed11bfc 100644 (file)
@@ -1,3 +1,24 @@
+/*
+ * $Id$
+ *
+ * ReactOS W32 Subsystem
+ * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 ReactOS Team
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
 #undef WIN32_LEAN_AND_MEAN
 #include <windows.h>
 #include <stdlib.h>
 #include <win32k/debug.h>
 #include "../eng/handle.h"
 #include <ntos/minmax.h>
+#include <include/error.h>
 #include <include/inteng.h>
+#include <include/eng.h>
+#include <include/dib.h>
+#include <internal/safe.h>
+#include <include/surface.h>
+#include <include/palette.h>
 
 #define NDEBUG
 #include <win32k/debug1.h>
 
-UINT STDCALL W32kSetDIBColorTable(HDC  hDC,
+UINT STDCALL NtGdiSetDIBColorTable(HDC  hDC,
                            UINT  StartIndex,
                            UINT  Entries,
                            CONST RGBQUAD  *Colors)
@@ -18,11 +45,11 @@ UINT STDCALL W32kSetDIBColorTable(HDC  hDC,
   PDC dc;
   PALETTEENTRY * palEntry;
   PPALOBJ palette;
-  RGBQUAD *end;
+  const RGBQUAD *end;
 
-  if (!(dc = (PDC)AccessUserObject(hDC))) return 0;
+  if (!(dc = (PDC)AccessUserObject((ULONG)hDC))) return 0;
 
-  if (!(palette = (PPALOBJ)AccessUserObject(dc->DevInfo.hpalDefault)))
+  if (!(palette = (PPALOBJ)PALETTE_LockPalette((ULONG)dc->DevInfo->hpalDefault)))
   {
 //    GDI_ReleaseObj( hdc );
     return 0;
@@ -30,9 +57,10 @@ UINT STDCALL W32kSetDIBColorTable(HDC  hDC,
 
   // Transfer color info
 
-  if (dc->w.bitsPerPixel <= 8) {
+  if (dc->w.bitsPerPixel <= 8)
+  {
     palEntry = palette->logpalette->palPalEntry + StartIndex;
-    if (StartIndex + Entries > (1 << dc->w.bitsPerPixel))
+    if (StartIndex + Entries > (UINT) (1 << dc->w.bitsPerPixel))
       Entries = (1 << dc->w.bitsPerPixel) - StartIndex;
 
     if (StartIndex + Entries > palette->logpalette->palNumEntries)
@@ -44,64 +72,68 @@ UINT STDCALL W32kSetDIBColorTable(HDC  hDC,
       palEntry->peGreen = Colors->rgbGreen;
       palEntry->peBlue  = Colors->rgbBlue;
     }
-  } else {
+  }
+  else
+  {
     Entries = 0;
   }
 
-//  GDI_ReleaseObj(dc->DevInfo.hpalDefault);
+  PALETTE_UnlockPalette(dc->DevInfo->hpalDefault);
 //  GDI_ReleaseObj(hdc);
 
   return Entries;
 }
 
 // Converts a DIB to a device-dependent bitmap
-INT STDCALL W32kSetDIBits(HDC  hDC,
-                   HBITMAP  hBitmap,
-                   UINT  StartScan,
-                   UINT  ScanLines,
-                   CONST VOID  *Bits,
-                   CONST BITMAPINFO  *bmi,
-                   UINT  ColorUse)
+INT STDCALL
+NtGdiSetDIBits(
+       HDC  hDC,
+       HBITMAP  hBitmap,
+       UINT  StartScan,
+       UINT  ScanLines,
+       CONST VOID  *Bits,
+       CONST BITMAPINFO  *bmi,
+       UINT  ColorUse)
 {
-  DC *dc;
-  BITMAPOBJ *bitmap;
-  HBITMAP SourceBitmap, DestBitmap;
-  INT result = 0;
-  BOOL copyBitsResult;
-  PSURFOBJ DestSurf, SourceSurf;
-  PSURFGDI DestGDI;
-  SIZEL        SourceSize;
-  POINTL ZeroPoint;
-  RECTL        DestRect;
-  PXLATEOBJ XlateObj;
-  PPALGDI hDCPalette;
-  RGBQUAD *lpRGB;
-  HPALETTE DDB_Palette, DIB_Palette;
-  ULONG DDB_Palette_Type, DIB_Palette_Type;
-  PBYTE vBits = Bits;
-  INT scanDirection = 1, DIBWidth;
+  DC         *dc;
+  BITMAPOBJ  *bitmap;
+  HBITMAP     SourceBitmap, DestBitmap;
+  INT         result = 0;
+  BOOL        copyBitsResult;
+  PSURFOBJ    DestSurf, SourceSurf;
+  PSURFGDI    DestGDI;
+  SIZEL       SourceSize;
+  POINTL      ZeroPoint;
+  RECTL       DestRect;
+  PXLATEOBJ   XlateObj;
+  PPALGDI     hDCPalette;
+  //RGBQUAD  *lpRGB;
+  HPALETTE    DDB_Palette, DIB_Palette;
+  ULONG       DDB_Palette_Type, DIB_Palette_Type;
+  const BYTE *vBits = (const BYTE*)Bits;
+  INT         scanDirection = 1, DIBWidth;
 
   // Check parameters
-  if (!(dc = DC_HandleToPtr(hDC)))
+  if (!(dc = DC_LockDc(hDC)))
      return 0;
 
-  if (!(bitmap = (BITMAPOBJ *)GDIOBJ_LockObj(hBitmap, GO_BITMAP_MAGIC)))
+  if (!(bitmap = BITMAPOBJ_LockBitmap(hBitmap)))
   {
-    DC_ReleasePtr(hDC);
+    DC_UnlockDc(hDC);
     return 0;
   }
 
   // Get RGB values
-  if (ColorUse == DIB_PAL_COLORS)
-    lpRGB = DIB_MapPaletteColors(hDC, bmi);
-  else
-    lpRGB = &bmi->bmiColors[0];
+  //if (ColorUse == DIB_PAL_COLORS)
+  //  lpRGB = DIB_MapPaletteColors(hDC, bmi);
+  //else
+  //  lpRGB = &bmi->bmiColors[0];
 
   // Create a temporary surface for the destination bitmap
   DestBitmap = BitmapToSurf(bitmap);
 
-  DestSurf   = (PSURFOBJ) AccessUserObject( DestBitmap );
-  DestGDI    = (PSURFGDI) AccessInternalObject( DestBitmap );
+  DestSurf   = (PSURFOBJ) AccessUserObject( (ULONG)DestBitmap );
+  DestGDI    = (PSURFGDI) AccessInternalObject( (ULONG)DestBitmap );
 
   // Create source surface
   SourceSize.cx = bmi->bmiHeader.biWidth;
@@ -121,16 +153,17 @@ INT STDCALL W32kSetDIBits(HDC  hDC,
                                  DIBWidth * scanDirection,
                                  BitmapFormat(bmi->bmiHeader.biBitCount, bmi->bmiHeader.biCompression),
                                  0,
-                                 vBits);
-  SourceSurf = (PSURFOBJ)AccessUserObject(SourceBitmap);
+                                 (PVOID)vBits );
+  SourceSurf = (PSURFOBJ)AccessUserObject((ULONG)SourceBitmap);
 
   // Destination palette obtained from the hDC
-  hDCPalette = (PPALGDI)AccessInternalObject(dc->DevInfo.hpalDefault);
+  hDCPalette = PALETTE_LockPalette(dc->DevInfo->hpalDefault);
   DDB_Palette_Type = hDCPalette->Mode;
-  DDB_Palette = dc->DevInfo.hpalDefault;
+  DDB_Palette = dc->DevInfo->hpalDefault;
+  PALETTE_UnlockPalette(dc->DevInfo->hpalDefault);
 
   // Source palette obtained from the BITMAPINFO
-  DIB_Palette = BuildDIBPalette(bmi, &DIB_Palette_Type);
+  DIB_Palette = BuildDIBPalette ( (PBITMAPINFO)bmi, (PINT)&DIB_Palette_Type );
 
   // Determine XLATEOBJ for color translation
   XlateObj = IntEngCreateXlate(DDB_Palette_Type, DIB_Palette_Type, DDB_Palette, DIB_Palette);
@@ -155,37 +188,39 @@ INT STDCALL W32kSetDIBits(HDC  hDC,
 
   // Clean up
   EngDeleteXlate(XlateObj);
-  EngDeletePalette(DIB_Palette);
+  PALETTE_FreePalette(DIB_Palette);
   EngDeleteSurface(SourceBitmap);
   EngDeleteSurface(DestBitmap);
 
 //  if (ColorUse == DIB_PAL_COLORS)
 //    WinFree((LPSTR)lpRGB);
 
-//  GDI_ReleaseObj(hBitmap); unlock?
-  GDIOBJ_UnlockObj(hBitmap, GO_BITMAP_MAGIC);
-  DC_ReleasePtr(hDC);
+  BITMAPOBJ_UnlockBitmap(hBitmap);
+  DC_UnlockDc(hDC);
 
   return result;
 }
 
-INT STDCALL W32kSetDIBitsToDevice(HDC  hDC,
-                           INT  XDest,
-                           INT  YDest,
-                           DWORD  Width,
-                           DWORD  Height,
-                           INT  XSrc,
-                           INT  YSrc,
-                           UINT  StartScan,
-                           UINT  ScanLines,
-                           CONST VOID  *Bits,
-                           CONST BITMAPINFO  *bmi,
-                           UINT  ColorUse)
+INT STDCALL
+NtGdiSetDIBitsToDevice(
+       HDC  hDC,
+       INT  XDest,
+       INT  YDest,
+       DWORD  Width,
+       DWORD  Height,
+       INT  XSrc,
+       INT  YSrc,
+       UINT  StartScan,
+       UINT  ScanLines,
+       CONST VOID  *Bits,
+       CONST BITMAPINFO  *bmi,
+       UINT  ColorUse)
 {
-
+  UNIMPLEMENTED;
+  return 0;
 }
 
-UINT STDCALL W32kGetDIBColorTable(HDC  hDC,
+UINT STDCALL NtGdiGetDIBColorTable(HDC  hDC,
                            UINT  StartIndex,
                            UINT  Entries,
                            RGBQUAD  *Colors)
@@ -194,18 +229,165 @@ UINT STDCALL W32kGetDIBColorTable(HDC  hDC,
 }
 
 // Converts a device-dependent bitmap to a DIB
-INT STDCALL W32kGetDIBits(HDC  hDC,
+INT STDCALL NtGdiGetDIBits(HDC  hDC,
                    HBITMAP hBitmap,
                    UINT  StartScan,
                    UINT  ScanLines,
                    LPVOID  Bits,
-                   LPBITMAPINFO   bi,
+                   LPBITMAPINFO UnsafeInfo,
                    UINT  Usage)
 {
-  UNIMPLEMENTED;
+  BITMAPINFO Info;
+  BITMAPCOREHEADER *Core;
+  PBITMAPOBJ BitmapObj;
+  INT Result;
+  NTSTATUS Status;
+  PDC DCObj;
+  PPALGDI PalGdi;
+  struct
+    {
+    BITMAPINFO Info;
+    DWORD BitFields[3];
+    } InfoWithBitFields;
+  DWORD *BitField;
+  DWORD InfoSize;
+
+  BitmapObj = BITMAPOBJ_LockBitmap(hBitmap);
+  if (NULL == BitmapObj)
+    {
+      SetLastWin32Error(ERROR_INVALID_HANDLE);
+      return 0;
+    }
+
+  RtlZeroMemory(&Info, sizeof(BITMAPINFO));
+  Status = MmCopyFromCaller(&(Info.bmiHeader.biSize),
+                            &(UnsafeInfo->bmiHeader.biSize),
+                            sizeof(DWORD));
+  if (! NT_SUCCESS(Status))
+    {
+    SetLastNtError(Status);
+    BITMAPOBJ_UnlockBitmap(hBitmap);
+    return 0;
+    }
+
+  /* If the bits are not requested, UnsafeInfo can point to either a
+     BITMAPINFOHEADER or a BITMAPCOREHEADER */
+  if (sizeof(BITMAPINFOHEADER) != Info.bmiHeader.biSize &&
+      (sizeof(BITMAPCOREHEADER) != Info.bmiHeader.biSize ||
+       NULL != Bits))
+    {
+      SetLastWin32Error(ERROR_INVALID_PARAMETER);
+      BITMAPOBJ_UnlockBitmap(hBitmap);
+      return 0;
+    }
+
+  Status = MmCopyFromCaller(&(Info.bmiHeader),
+                            &(UnsafeInfo->bmiHeader),
+                            Info.bmiHeader.biSize);
+  if (! NT_SUCCESS(Status))
+    {
+      SetLastNtError(Status);
+      BITMAPOBJ_UnlockBitmap(hBitmap);
+      return 0;
+    }
+
+  if (NULL == Bits)
+    {
+      if (sizeof(BITMAPINFOHEADER) == Info.bmiHeader.biSize)
+       {
+         if (0 != Info.bmiHeader.biBitCount)
+           {
+             UNIMPLEMENTED;
+           }
+
+         Info.bmiHeader.biWidth = BitmapObj->bitmap.bmWidth;
+         Info.bmiHeader.biHeight = BitmapObj->bitmap.bmHeight;
+         Info.bmiHeader.biPlanes = BitmapObj->bitmap.bmPlanes;
+         Info.bmiHeader.biBitCount = BitmapObj->bitmap.bmBitsPixel;
+         Info.bmiHeader.biCompression = BI_RGB;
+         Info.bmiHeader.biSizeImage = BitmapObj->bitmap.bmHeight * BitmapObj->bitmap.bmWidthBytes;
+       }
+      else
+       {
+         Core = (BITMAPCOREHEADER *)(&Info.bmiHeader);
+         if (0 != Core->bcBitCount)
+           {
+             UNIMPLEMENTED;
+           }
+
+         Core->bcWidth = BitmapObj->bitmap.bmWidth;
+         Core->bcHeight = BitmapObj->bitmap.bmHeight;
+         Core->bcPlanes = BitmapObj->bitmap.bmPlanes;
+         Core->bcBitCount = BitmapObj->bitmap.bmBitsPixel;
+       }
+
+      Status = MmCopyToCaller(UnsafeInfo, &Info, Info.bmiHeader.biSize);
+      if (! NT_SUCCESS(Status))
+       {
+         SetLastNtError(Status);
+         BITMAPOBJ_UnlockBitmap(hBitmap);
+         return 0;
+       }
+      Result = 1;
+    }
+  else if (0 == StartScan && Info.bmiHeader.biHeight == (LONG) (StartScan + ScanLines) &&
+           Info.bmiHeader.biWidth == BitmapObj->bitmap.bmWidth &&
+           Info.bmiHeader.biHeight == BitmapObj->bitmap.bmHeight &&
+           Info.bmiHeader.biPlanes == BitmapObj->bitmap.bmPlanes &&
+           Info.bmiHeader.biBitCount == BitmapObj->bitmap.bmBitsPixel &&
+           8 < Info.bmiHeader.biBitCount)
+    {
+      Info.bmiHeader.biSizeImage = BitmapObj->bitmap.bmHeight * BitmapObj->bitmap.bmWidthBytes;
+      Status = MmCopyToCaller(Bits, BitmapObj->bitmap.bmBits, Info.bmiHeader.biSizeImage);
+      if (! NT_SUCCESS(Status))
+       {
+         SetLastNtError(Status);
+         BITMAPOBJ_UnlockBitmap(hBitmap);
+         return 0;
+       }
+      RtlZeroMemory(&InfoWithBitFields, sizeof(InfoWithBitFields));
+      RtlCopyMemory(&(InfoWithBitFields.Info), &Info, sizeof(BITMAPINFO));
+      if (BI_BITFIELDS == Info.bmiHeader.biCompression)
+       {
+         DCObj = DC_LockDc(hDC);
+         if (NULL == DCObj)
+           {
+             SetLastWin32Error(ERROR_INVALID_HANDLE);
+             BITMAPOBJ_UnlockBitmap(hBitmap);
+             return 0;
+           }
+         PalGdi = PALETTE_LockPalette(DCObj->w.hPalette);
+         BitField = (DWORD *) ((char *) &InfoWithBitFields + InfoWithBitFields.Info.bmiHeader.biSize);
+         BitField[0] = PalGdi->RedMask;
+         BitField[1] = PalGdi->GreenMask;
+         BitField[2] = PalGdi->BlueMask;
+         PALETTE_UnlockPalette(DCObj->w.hPalette);
+         InfoSize = InfoWithBitFields.Info.bmiHeader.biSize + 3 * sizeof(DWORD);
+         DC_UnlockDc(hDC);
+       }
+      else
+       {
+         InfoSize = Info.bmiHeader.biSize;
+       }
+      Status = MmCopyToCaller(UnsafeInfo, &InfoWithBitFields, InfoSize);
+      if (! NT_SUCCESS(Status))
+       {
+         SetLastNtError(Status);
+         BITMAPOBJ_UnlockBitmap(hBitmap);
+         return 0;
+       }
+    }
+  else
+    {
+    UNIMPLEMENTED;
+    }
+
+  BITMAPOBJ_UnlockBitmap(hBitmap);
+
+  return Result;
 }
 
-INT STDCALL W32kStretchDIBits(HDC  hDC,
+INT STDCALL NtGdiStretchDIBits(HDC  hDC,
                        INT  XDest,
                        INT  YDest,
                        INT  DestWidth,
@@ -222,14 +404,14 @@ INT STDCALL W32kStretchDIBits(HDC  hDC,
   UNIMPLEMENTED;
 }
 
-LONG STDCALL W32kGetBitmapBits(HBITMAP  hBitmap,
+LONG STDCALL NtGdiGetBitmapBits(HBITMAP  hBitmap,
                         LONG  Count,
                         LPVOID  Bits)
 {
   PBITMAPOBJ  bmp;
   LONG  height, ret;
 
-  bmp = BITMAPOBJ_HandleToPtr (hBitmap);
+  bmp = BITMAPOBJ_LockBitmap (hBitmap);
   if (!bmp)
   {
     return 0;
@@ -299,7 +481,7 @@ LONG STDCALL W32kGetBitmapBits(HBITMAP  hBitmap,
 
 // The CreateDIBitmap function creates a device-dependent bitmap (DDB) from a DIB and, optionally, sets the bitmap bits
 // The DDB that is created will be whatever bit depth your reference DC is
-HBITMAP STDCALL W32kCreateDIBitmap(HDC hdc, const BITMAPINFOHEADER *header,
+HBITMAP STDCALL NtGdiCreateDIBitmap(HDC hdc, const BITMAPINFOHEADER *header,
                                DWORD init, LPCVOID bits, const BITMAPINFO *data,
                                UINT coloruse)
 {
@@ -359,30 +541,26 @@ HBITMAP STDCALL W32kCreateDIBitmap(HDC hdc, const BITMAPINFOHEADER *header,
 
   // Now create the bitmap
 
-  if(fColor)
-  {
-    // If we are using indexed colors, then we need to create a bitmap that is compatible with the palette
-    if(coloruse == DIB_PAL_COLORS)
+  if (init == CBM_INIT)
     {
-      handle = W32kCreateCompatibleBitmap(hdc, width, height);
+      handle = NtGdiCreateCompatibleBitmap(hdc, width, height);
     }
-    else if(coloruse == DIB_RGB_COLORS) {
-      handle = W32kCreateBitmap(width, height, 1, 24, NULL);
+  else
+    {
+      handle = NtGdiCreateBitmap(width, height, 1, bpp, NULL);
     }
-  }
-  else handle = W32kCreateBitmap(width, height, 1, 1, NULL);
 
   if (!handle) return 0;
 
   if (init == CBM_INIT)
   {
-    W32kSetDIBits(hdc, handle, 0, height, bits, data, coloruse);
+    NtGdiSetDIBits(hdc, handle, 0, height, bits, data, coloruse);
   }
 
   return handle;
 }
 
-HBITMAP STDCALL W32kCreateDIBSection(HDC hDC,
+HBITMAP STDCALL NtGdiCreateDIBSection(HDC hDC,
                               CONST BITMAPINFO  *bmi,
                               UINT  Usage,
                               VOID  *Bits,
@@ -396,23 +574,25 @@ HBITMAP STDCALL W32kCreateDIBSection(HDC hDC,
   // If the reference hdc is null, take the desktop dc
   if (hDC == 0)
   {
-    hDC = W32kCreateCompatableDC(0);
+    hDC = NtGdiCreateCompatableDC(0);
     bDesktopDC = TRUE;
   }
 
-  if ((dc = DC_HandleToPtr(hDC)))
+  if ((dc = DC_LockDc(hDC)))
   {
-    hbitmap = DIB_CreateDIBSection(dc, bmi, Usage, Bits, hSection, dwOffset, 0);
-       DC_ReleasePtr(hDC);
+    hbitmap = DIB_CreateDIBSection ( dc, (BITMAPINFO*)bmi, Usage, Bits,
+      hSection, dwOffset, 0);
+    DC_UnlockDc(hDC);
   }
 
   if (bDesktopDC)
-    W32kDeleteDC(hDC);
+    NtGdiDeleteDC(hDC);
 
   return hbitmap;
 }
 
-HBITMAP DIB_CreateDIBSection(
+HBITMAP STDCALL
+DIB_CreateDIBSection(
   PDC dc, BITMAPINFO *bmi, UINT usage,
   LPVOID *bits, HANDLE section,
   DWORD offset, DWORD ovr_pitch)
@@ -423,7 +603,8 @@ HBITMAP DIB_CreateDIBSection(
 
   // Fill BITMAP32 structure with DIB data
   BITMAPINFOHEADER *bi = &bmi->bmiHeader;
-  INT effHeight, totalSize;
+  INT effHeight;
+  ULONG totalSize;
   UINT Entries = 0;
   BITMAP bm;
 
@@ -435,7 +616,7 @@ HBITMAP DIB_CreateDIBSection(
   bm.bmType = 0;
   bm.bmWidth = bi->biWidth;
   bm.bmHeight = effHeight;
-  bm.bmWidthBytes = ovr_pitch ? ovr_pitch : DIB_GetDIBWidthBytes(bm.bmWidth, bi->biBitCount);
+  bm.bmWidthBytes = ovr_pitch ? ovr_pitch : (ULONG) DIB_GetDIBWidthBytes(bm.bmWidth, bi->biBitCount);
 
   bm.bmPlanes = bi->biPlanes;
   bm.bmBitsPixel = bi->biBitCount;
@@ -444,12 +625,12 @@ HBITMAP DIB_CreateDIBSection(
   // Get storage location for DIB bits.  Only use biSizeImage if it's valid and
   // we're dealing with a compressed bitmap.  Otherwise, use width * height.
   totalSize = bi->biSizeImage && bi->biCompression != BI_RGB
-    ? bi->biSizeImage : bm.bmWidthBytes * effHeight;
+    ? bi->biSizeImage : (ULONG) (bm.bmWidthBytes * effHeight);
 
   if (section)
 /*    bm.bmBits = MapViewOfFile(section, FILE_MAP_ALL_ACCESS,
                              0L, offset, totalSize); */
-    DPRINT("DIB_CreateDIBSection: Cannot yet handle section DIBs\n");
+    DbgPrint("DIB_CreateDIBSection: Cannot yet handle section DIBs\n");
   else if (ovr_pitch && offset)
     bm.bmBits = (LPVOID) offset;
   else {
@@ -506,19 +687,25 @@ HBITMAP DIB_CreateDIBSection(
   // Create Device Dependent Bitmap and add DIB pointer
   if (dib)
   {
-    res = W32kCreateDIBitmap(dc->hSelf, bi, 0, NULL, bmi, usage);
-    if (res)
-    {
-      bmp = BITMAPOBJ_HandleToPtr (res);
-      if (bmp)
+    res = NtGdiCreateDIBitmap(dc->hSelf, bi, 0, NULL, bmi, usage);
+    if (! res)
       {
-        bmp->dib = (DIBSECTION *) dib;
+       return NULL;
+      } 
+    bmp = BITMAPOBJ_LockBitmap(res);
+    if (NULL == bmp)
+      {
+       NtGdiDeleteObject(bmp);
+       return NULL;
       }
-    }
+    bmp->dib = (DIBSECTION *) dib;
+    /* Install user-mode bits instead of kernel-mode bits */
+    ExFreePool(bmp->bitmap.bmBits);
+    bmp->bitmap.bmBits = bm.bmBits;
 
     /* WINE NOTE: WINE makes use of a colormap, which is a color translation table between the DIB and the X physical
                   device. Obviously, this is left out of the ReactOS implementation. Instead, we call
-                  W32kSetDIBColorTable. */
+                  NtGdiSetDIBColorTable. */
     if(bi->biBitCount == 1) { Entries = 2; } else
     if(bi->biBitCount == 4) { Entries = 16; } else
     if(bi->biBitCount == 8) { Entries = 256; }
@@ -541,32 +728,19 @@ HBITMAP DIB_CreateDIBSection(
 
     if (dib) { ExFreePool(dib); dib = NULL; }
     if (bmp) { bmp = NULL; }
-    if (res) { GDIOBJ_FreeObj(res, GO_BITMAP_MAGIC, GDIOBJFLAG_DEFAULT); res = 0; }
+    if (res) { BITMAPOBJ_FreeBitmap(res); res = 0; }
   }
 
-  // Install fault handler, if possible
-/*  if (bm.bmBits)
-  {
-    if (VIRTUAL_SetFaultHandler(bm.bmBits, DIB_FaultHandler, (LPVOID)res))
+  if (bmp)
     {
-      if (section || offset)
-      {
-        DIB_DoProtectDIBSection( bmp, PAGE_READWRITE );
-        if (dib) dib->status = DIB_AppMod;
-      }
-      else
-      {
-        DIB_DoProtectDIBSection( bmp, PAGE_READONLY );
-        if (dib) dib->status = DIB_InSync;
-      }
+      BITMAPOBJ_UnlockBitmap(res);
     }
-  } */
-
-  if( bmp )
-       BITMAPOBJ_ReleasePtr(res);
 
   // Return BITMAP handle and storage location
-  if (bm.bmBits && bits) *bits = bm.bmBits;
+  if (NULL != bm.bmBits && NULL != bits)
+    {
+      *bits = bm.bmBits;
+    }
 
   return res;
 }
@@ -578,7 +752,7 @@ HBITMAP DIB_CreateDIBSection(
  * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
  * 11/16/1999 (RJJ) lifted from wine
  */
-int DIB_GetDIBWidthBytes(int  width, int  depth)
+INT FASTCALL DIB_GetDIBWidthBytes (INT width, INT depth)
 {
   int words;
 
@@ -607,7 +781,7 @@ int DIB_GetDIBWidthBytes(int  width, int  depth)
  * 11/16/1999 (RJJ) lifted from wine
  */
 
-int DIB_GetDIBImageBytes (int  width, int  height, int  depth)
+INT STDCALL DIB_GetDIBImageBytes (INT  width, INT height, INT depth)
 {
   return DIB_GetDIBWidthBytes( width, depth ) * (height < 0 ? -height : height);
 }
@@ -619,7 +793,7 @@ int DIB_GetDIBImageBytes (int  width, int  height, int  depth)
  * 11/16/1999 (RJJ) lifted from wine
  */
 
-int DIB_BitmapInfoSize (const BITMAPINFO * info, WORD coloruse)
+INT FASTCALL DIB_BitmapInfoSize (const BITMAPINFO * info, WORD coloruse)
 {
   int colors;
 
@@ -637,8 +811,11 @@ int DIB_BitmapInfoSize (const BITMAPINFO * info, WORD coloruse)
   }
 }
 
-int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, DWORD *width,
-                              int *height, WORD *bpp, WORD *compr )
+INT STDCALL DIB_GetBitmapInfo (const BITMAPINFOHEADER *header,
+                              PDWORD width,
+                              PINT height,
+                              PWORD bpp,
+                              PWORD compr)
 {
   if (header->biSize == sizeof(BITMAPINFOHEADER))
   {
@@ -663,8 +840,8 @@ int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, DWORD *width,
 
 // Converts a Device Independent Bitmap (DIB) to a Device Dependant Bitmap (DDB)
 // The specified Device Context (DC) defines what the DIB should be converted to
-PBITMAPOBJ DIBtoDDB(HGLOBAL hPackedDIB, HDC hdc) // FIXME: This should be removed. All references to this function should
-                                                // change to W32kSetDIBits
+PBITMAPOBJ FASTCALL DIBtoDDB(HGLOBAL hPackedDIB, HDC hdc) // FIXME: This should be removed. All references to this function should
+                                                // change to NtGdiSetDIBits
 {
   HBITMAP hBmp = 0;
   PBITMAPOBJ pBmp = NULL;
@@ -675,89 +852,105 @@ PBITMAPOBJ DIBtoDDB(HGLOBAL hPackedDIB, HDC hdc) // FIXME: This should be remove
   // pPackedDIB = (LPBYTE)GlobalLock(hPackedDIB);
   dib = hPackedDIB;
 
-  pbits = (dib + DIB_BitmapInfoSize(&dib->dsBmih, DIB_RGB_COLORS));
+  pbits = (LPBYTE)(dib + DIB_BitmapInfoSize((BITMAPINFO*)&dib->dsBmih, DIB_RGB_COLORS));
 
   // Create a DDB from the DIB
-  hBmp = W32kCreateDIBitmap(hdc, &dib->dsBmih, CBM_INIT, (LPVOID)pbits, &dib->dsBmih, DIB_RGB_COLORS);
+  hBmp = NtGdiCreateDIBitmap ( hdc, &dib->dsBmih, CBM_INIT,
+    (LPVOID)pbits, (BITMAPINFO*)&dib->dsBmih, DIB_RGB_COLORS);
 
   // GlobalUnlock(hPackedDIB);
 
   // Retrieve the internal Pixmap from the DDB
-  pBmp = (BITMAPOBJ *)GDIOBJ_LockObj(hBmp, GO_BITMAP_MAGIC);
+  pBmp = BITMAPOBJ_LockBitmap(hBmp);
 
   return pBmp;
 }
 
-RGBQUAD *DIB_MapPaletteColors(PDC dc, LPBITMAPINFO lpbmi)
+RGBQUAD * FASTCALL
+DIB_MapPaletteColors(PDC dc, CONST BITMAPINFO* lpbmi)
 {
   RGBQUAD *lpRGB;
-  int nNumColors,i;
+  ULONG nNumColors,i;
   DWORD *lpIndex;
   PPALOBJ palObj;
 
-  palObj = AccessUserObject(dc->DevInfo.hpalDefault);
+  palObj = (PPALOBJ) PALETTE_LockPalette(dc->DevInfo->hpalDefault);
 
-  if (palObj == NULL) {
+  if (NULL == palObj)
+    {
 //      RELEASEDCINFO(hDC);
-    return NULL;
-  }
+      return NULL;
+    }
 
   nNumColors = 1 << lpbmi->bmiHeader.biBitCount;
   if (lpbmi->bmiHeader.biClrUsed)
-    nNumColors = min(nNumColors, lpbmi->bmiHeader.biClrUsed);
+    {
+      nNumColors = min(nNumColors, lpbmi->bmiHeader.biClrUsed);
+    }
 
   lpRGB = (RGBQUAD *)ExAllocatePool(NonPagedPool, sizeof(RGBQUAD) * nNumColors);
   lpIndex = (DWORD *)&lpbmi->bmiColors[0];
 
-  for (i=0; i<nNumColors; i++) {
-    lpRGB[i].rgbRed = palObj->logpalette->palPalEntry[*lpIndex].peRed;
-    lpRGB[i].rgbGreen = palObj->logpalette->palPalEntry[*lpIndex].peGreen;
-    lpRGB[i].rgbBlue = palObj->logpalette->palPalEntry[*lpIndex].peBlue;
-    lpIndex++;
-  }
+  for (i = 0; i < nNumColors; i++)
+    {
+      lpRGB[i].rgbRed = palObj->logpalette->palPalEntry[*lpIndex].peRed;
+      lpRGB[i].rgbGreen = palObj->logpalette->palPalEntry[*lpIndex].peGreen;
+      lpRGB[i].rgbBlue = palObj->logpalette->palPalEntry[*lpIndex].peBlue;
+      lpIndex++;
+    }
 //    RELEASEDCINFO(hDC);
-//    RELEASEPALETTEINFO(hPalette);
+  PALETTE_UnlockPalette(dc->DevInfo->hpalDefault);
+
   return lpRGB;
 }
 
-PALETTEENTRY *DIBColorTableToPaletteEntries(PALETTEENTRY *palEntries, const RGBQUAD *DIBColorTable, ULONG ColorCount)
+PPALETTEENTRY STDCALL
+DIBColorTableToPaletteEntries (
+       PPALETTEENTRY palEntries,
+       const RGBQUAD *DIBColorTable,
+       ULONG ColorCount
+       )
 {
   ULONG i;
 
-  for(i=0; i<ColorCount; i++)
-  {
-    palEntries->peRed   = DIBColorTable->rgbRed;
-    palEntries->peGreen = DIBColorTable->rgbGreen;
-    palEntries->peBlue  = DIBColorTable->rgbBlue;
-    palEntries++;
-    DIBColorTable++;
-  }
+  for (i = 0; i < ColorCount; i++)
+    {
+      palEntries->peRed   = DIBColorTable->rgbRed;
+      palEntries->peGreen = DIBColorTable->rgbGreen;
+      palEntries->peBlue  = DIBColorTable->rgbBlue;
+      palEntries++;
+      DIBColorTable++;
+    }
+
+  return palEntries;
 }
 
-HPALETTE BuildDIBPalette(BITMAPINFO *bmi, PINT paletteType)
+HPALETTE FASTCALL
+BuildDIBPalette (PBITMAPINFO bmi, PINT paletteType)
 {
   BYTE bits;
   ULONG ColorCount;
-  PALETTEENTRY *palEntries;
+  PALETTEENTRY *palEntries = NULL;
   HPALETTE hPal;
 
   // Determine Bits Per Pixel
   bits = bmi->bmiHeader.biBitCount;
 
   // Determine paletteType from Bits Per Pixel
-  if(bits <= 8)
-  {
-    *paletteType = PAL_INDEXED;
-  } else
-  if(bits < 24)
-  {
-    *paletteType = PAL_BITFIELDS;
-  } else {
-    *paletteType = PAL_RGB; // Would it be BGR, considering the BGR nature of the DIB color table?
-  }
+  if (bits <= 8)
+    {
+      *paletteType = PAL_INDEXED;
+    }
+  else if(bits < 24)
+    {
+      *paletteType = PAL_BITFIELDS;
+    }
+  else
+    {
+      *paletteType = PAL_BGR;
+    }
 
-  if (bmi->bmiHeader.biClrUsed == 0 &&
-      bmi->bmiHeader.biBitCount <= 8)
+  if (bmi->bmiHeader.biClrUsed == 0)
     {
       ColorCount = 1 << bmi->bmiHeader.biBitCount;
     }
@@ -766,10 +959,18 @@ HPALETTE BuildDIBPalette(BITMAPINFO *bmi, PINT paletteType)
       ColorCount = bmi->bmiHeader.biClrUsed;
     }
 
-  palEntries = ExAllocatePool(NonPagedPool, sizeof(PALETTEENTRY)*ColorCount);
-  DIBColorTableToPaletteEntries(palEntries, bmi->bmiColors, ColorCount);
-  hPal = EngCreatePalette(*paletteType, ColorCount, palEntries, 0, 0, 0);
-  ExFreePool(palEntries);
+  if (PAL_INDEXED == *paletteType)
+    {
+      palEntries = ExAllocatePool(NonPagedPool, sizeof(PALETTEENTRY)*ColorCount);
+      DIBColorTableToPaletteEntries(palEntries, bmi->bmiColors, ColorCount);
+    }
+  hPal = PALETTE_AllocPalette( *paletteType, ColorCount, (ULONG*)palEntries, 0, 0, 0 );
+  if (NULL != palEntries)
+    {
+      ExFreePool(palEntries);
+    }
 
   return hPal;
 }
+
+/* EOF */