Add failure check.
[reactos.git] / reactos / lib / user32 / windows / bitmap.c
index 6be2803..d79281e 100644 (file)
@@ -49,29 +49,36 @@ LoadImageA(HINSTANCE hinst,
           int cyDesired,
           UINT fuLoad)
 {
-  LPWSTR lpszWName;
-  HANDLE Handle;
-  UNICODE_STRING NameString;
+   LPWSTR lpszWName;
+   HANDLE Handle;
+   UNICODE_STRING NameString;
 
-  if (HIWORD(lpszName))
-    {
+   if (HIWORD(lpszName))
+   {
       RtlCreateUnicodeStringFromAsciiz(&NameString, (LPSTR)lpszName);
       lpszWName = NameString.Buffer;
       Handle = LoadImageW(hinst, lpszWName, uType, cxDesired,
                          cyDesired, fuLoad);
       RtlFreeUnicodeString(&NameString);
-    }
-  else
-    {
+   }
+   else
+   {
       Handle = LoadImageW(hinst, (LPCWSTR)lpszName, uType, cxDesired,
                          cyDesired, fuLoad);
-    }
-  return(Handle);
+   }
+
+   return Handle;
 }
 
 
-HANDLE STATIC
-LoadCursorImage(HINSTANCE hinst, LPCWSTR lpszName, UINT fuLoad)
+static HANDLE
+LoadCursorIconImage(
+   HINSTANCE hinst,
+   LPCWSTR lpszName,
+   INT width,
+   INT height,
+   UINT fuLoad,
+   ULONG uType)
 {
    HANDLE hResource;
    HANDLE h2Resource;
@@ -80,72 +87,68 @@ LoadCursorImage(HINSTANCE hinst, LPCWSTR lpszName, UINT fuLoad)
    HANDLE hSection;
    CURSORICONDIR *IconDIR;
    HDC hScreenDc;
-   HANDLE hIcon;
+   HICON hIcon;
    ULONG HeaderSize;
    ULONG ColorCount;
+   ULONG ColorBits;
    PVOID Data;
    CURSORICONDIRENTRY* dirEntry;
    ICONIMAGE* SafeIconImage;
    GRPCURSORICONDIR* IconResDir;
    INT id;
    ICONIMAGE *ResIcon;
-   UINT ColorBits;
+   BOOL Icon = (uType == IMAGE_ICON);
 
    if (!(fuLoad & LR_LOADFROMFILE))
    {
       if (hinst == NULL)
-      {
-         hinst = GetModuleHandleW(L"USER32");
-      }
-      hResource = hfRes = FindResourceW(hinst, lpszName, RT_GROUP_CURSOR);
+         hinst = User32Instance;
+
+      hResource = hfRes = FindResourceW(hinst, lpszName,
+                                        Icon ? RT_GROUP_ICON : RT_GROUP_CURSOR);
       if (hResource == NULL)
-      {
          return NULL;
-      }
 
       if (fuLoad & LR_SHARED)
       {
-         /* FIXME - pass size! */
-         hIcon = (HANDLE)NtUserFindExistingCursorIcon(hinst, (HRSRC)hfRes, 0, 0);
+         hIcon = NtUserFindExistingCursorIcon(hinst, (HRSRC)hfRes, width, height);
          if (hIcon)
-         {
             return hIcon;
-         }
       }
 
       hResource = LoadResource(hinst, hResource);
       if (hResource == NULL)
-      {
          return NULL;
-      }
+
       IconResDir = LockResource(hResource);
       if (IconResDir == NULL)
-      {
          return NULL;
-      }
 
-      /* Find the best fitting in the IconResDir for this resolution. */
-      id = LookupIconIdFromDirectoryEx((PBYTE)IconResDir, TRUE,
-         32, 32, fuLoad & (LR_DEFAULTCOLOR | LR_MONOCHROME));
+      /*
+       * Find the best fitting in the IconResDir for this resolution
+       */
+
+      id = LookupIconIdFromDirectoryEx((PBYTE)IconResDir, Icon, width, height,
+                                       fuLoad & (LR_DEFAULTCOLOR | LR_MONOCHROME));
 
       h2Resource = FindResourceW(hinst, MAKEINTRESOURCEW(id),
-         MAKEINTRESOURCEW(RT_CURSOR));
+                                 Icon ? MAKEINTRESOURCEW(RT_ICON) :
+                                 MAKEINTRESOURCEW(RT_CURSOR));
+      if (h2Resource == NULL)
+         return NULL;
 
       hResource = LoadResource(hinst, h2Resource);
       if (hResource == NULL)
-      {
          return NULL;
-      }
 
       ResIcon = LockResource(hResource);
       if (ResIcon == NULL)
-      {
          return NULL;
-      }
 
-      hIcon = (HANDLE)CreateIconFromResourceEx((PBYTE)ResIcon,
-         SizeofResource(hinst, h2Resource), FALSE, 0x00030000,
-         32, 32, fuLoad & (LR_DEFAULTCOLOR | LR_MONOCHROME));
+      hIcon = CreateIconFromResourceEx((PBYTE)ResIcon,
+                                       SizeofResource(hinst, h2Resource),
+                                       Icon, 0x00030000, width, height,
+                                       fuLoad & (LR_DEFAULTCOLOR | LR_MONOCHROME));
       if (hIcon && 0 != (fuLoad & LR_SHARED))
       {
          NtUserSetCursorIconData((HICON)hIcon, NULL, NULL, hinst, (HRSRC)hfRes,
@@ -155,41 +158,48 @@ LoadCursorImage(HINSTANCE hinst, LPCWSTR lpszName, UINT fuLoad)
       return hIcon;
    }
 
+   /*
+    * FIXME: This code is incorrect and is likely to crash in many cases.
+    * In the file the cursor/icon directory records are stored like
+    * CURSORICONFILEDIR, but we treat them like CURSORICONDIR. In Wine
+    * this is solved by creating a fake cursor/icon directory in memory
+    * and passing that to CURSORICON_FindBestIcon.
+    */
+
    if (fuLoad & LR_SHARED)
    {
-      DbgPrint("FIXME: need LR_SHARED support loading cursor images from files\n");
+      DbgPrint("FIXME: need LR_SHARED support for loading icon images from files\n");
    }
 
    hFile = CreateFileW(lpszName, GENERIC_READ, FILE_SHARE_READ, NULL,
-      OPEN_EXISTING, 0, NULL);
-   if (hFile == NULL)
-   {
+                       OPEN_EXISTING, 0, NULL);
+   if (hFile == INVALID_HANDLE_VALUE)
       return NULL;
-   }
 
    hSection = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
    CloseHandle(hFile);
    if (hSection == NULL)
-   {
       return NULL;
-   }
 
    IconDIR = MapViewOfFile(hSection, FILE_MAP_READ, 0, 0, 0);
    CloseHandle(hSection);
-   if (IconDIR == NULL || 0 != IconDIR->idReserved
-       || (IMAGE_ICON != IconDIR->idType && IMAGE_CURSOR != IconDIR->idType))
+   if (IconDIR == NULL)
+      return NULL;
+
+   if (0 != IconDIR->idReserved ||
+       (IMAGE_ICON != IconDIR->idType && IMAGE_CURSOR != IconDIR->idType))
    {
+      UnmapViewOfFile(IconDIR);
       return NULL;
    }
 
-   /*
-    * Get a handle to the screen dc, the icon we create is going to be
-    * compatable with it.
-    */
-   hScreenDc = CreateCompatibleDC(0);
+   /* Get a handle to the screen dc, the icon we create is going to be
+    * compatable with this. */
+   hScreenDc = CreateDCW(NULL, NULL, NULL, NULL);
    if (hScreenDc == NULL)
    {
       UnmapViewOfFile(IconDIR);
+      RtlFreeHeap(GetProcessHeap(), 0, SafeIconImage);
       return NULL;
    }
 
@@ -209,17 +219,26 @@ LoadCursorImage(HINSTANCE hinst, LPCWSTR lpszName, UINT fuLoad)
    }
 
    /* Pick the best size. */
-   dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon(IconDIR, 32, 32, ColorBits);
+   dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon(IconDIR, width, height, ColorBits);
    if (!dirEntry)
    {
+      DeleteDC(hScreenDc);
       UnmapViewOfFile(IconDIR);
-      return(NULL);
+      return NULL;
    }
 
    SafeIconImage = RtlAllocateHeap(GetProcessHeap(), 0, dirEntry->dwBytesInRes);
+   if (SafeIconImage == NULL)
+   {
+      DeleteDC(hScreenDc);
+      UnmapViewOfFile(IconDIR);
+      return NULL;
+   }
+
    memcpy(SafeIconImage, ((PBYTE)IconDIR) + dirEntry->dwImageOffset, dirEntry->dwBytesInRes);
+   UnmapViewOfFile(IconDIR);
 
-   /* at this point we have a copy of the icon image to play with */
+   /* At this point we have a copy of the icon image to play with. */
 
    SafeIconImage->icHeader.biHeight = SafeIconImage->icHeader.biHeight /2;
 
@@ -233,411 +252,169 @@ LoadCursorImage(HINSTANCE hinst, LPCWSTR lpszName, UINT fuLoad)
    {
       ColorCount = SafeIconImage->icHeader.biClrUsed;
       if (ColorCount == 0 && SafeIconImage->icHeader.biBitCount <= 8)
-       {
-         ColorCount = 1 << SafeIconImage->icHeader.biBitCount;
-       }
+         ColorCount = 1 << SafeIconImage->icHeader.biBitCount;
       HeaderSize = sizeof(BITMAPINFOHEADER) + ColorCount * sizeof(RGBQUAD);
    }
 
-   /* make data point to the start of the XOR image data */
+   /* Make data point to the start of the XOR image data. */
    Data = (PBYTE)SafeIconImage + HeaderSize;
 
-   hIcon = ICON_CreateCursorFromData(hScreenDc, Data, SafeIconImage, 32, 32, dirEntry->Info.cursor.wXHotspot, dirEntry->Info.cursor.wYHotspot);
-   DeleteDC(hScreenDc);
+   hIcon = ICON_CreateIconFromData(hScreenDc, Data, SafeIconImage, width, height, width/2, height/2);
    RtlFreeHeap(GetProcessHeap(), 0, SafeIconImage);
-   return hIcon;
-}
-
-
-HANDLE STATIC
-LoadIconImage(HINSTANCE hinst, LPCWSTR lpszName, INT width, INT height, UINT fuLoad)
-{
-  HANDLE hResource;
-  HANDLE h2Resource;
-  HANDLE hfRes;
-  HANDLE hFile;
-  HANDLE hSection;
-  CURSORICONDIR* IconDIR;
-  HDC hScreenDc;
-  HANDLE hIcon;
-  ULONG HeaderSize;
-  ULONG ColorCount;
-  PVOID Data;
-  CURSORICONDIRENTRY* dirEntry;
-  ICONIMAGE* SafeIconImage;
-  GRPCURSORICONDIR* IconResDir;
-  INT id;
-  ICONIMAGE *ResIcon;
-
-  if (!(fuLoad & LR_LOADFROMFILE))
-  {
-      if (hinst == NULL)
-         {
-           hinst = GetModuleHandleW(L"USER32");
-         }
-      hResource = hfRes = FindResourceW(hinst, lpszName, RT_GROUP_ICON);
-      if (hResource == NULL)
-         {
-           return(NULL);
-         }
-
-      if (fuLoad & LR_SHARED)
-          {
-            hIcon = NtUserFindExistingCursorIcon(hinst, (HRSRC)hfRes, width, height);
-            if(hIcon)
-              return hIcon;
-          }
-
-      hResource = LoadResource(hinst, hResource);
-      if (hResource == NULL)
-         {
-           return(NULL);
-         }
-      IconResDir = LockResource(hResource);
-      if (IconResDir == NULL)
-         {
-           return(NULL);
-         }
-
-      //find the best fitting in the IconResDir for this resolution
-      id = LookupIconIdFromDirectoryEx((PBYTE) IconResDir, TRUE,
-                width, height, fuLoad & (LR_DEFAULTCOLOR | LR_MONOCHROME));
-
-         h2Resource = FindResourceW(hinst,
-                     MAKEINTRESOURCEW(id),
-                     MAKEINTRESOURCEW(RT_ICON));
-
-      hResource = LoadResource(hinst, h2Resource);
-      if (hResource == NULL)
-         {
-           return(NULL);
-         }
-
-      ResIcon = LockResource(hResource);
-      if (ResIcon == NULL)
-         {
-           return(NULL);
-         }
-      hIcon = (HANDLE)CreateIconFromResourceEx((PBYTE) ResIcon,
-                        SizeofResource(hinst, h2Resource), TRUE, 0x00030000,
-                        width, height, fuLoad & (LR_DEFAULTCOLOR | LR_MONOCHROME));
-      if (hIcon && 0 != (fuLoad & LR_SHARED))
-      {
-        NtUserSetCursorIconData((HICON)hIcon, NULL, NULL, hinst, (HRSRC)hfRes,
-                                (HRSRC)NULL);
-      }
-      return hIcon;
-  }
-  else
-  {
-      /*
-       * FIXME: This code is incorrect and is likely to crash in many cases.
-       * In the file the cursor/icon directory records are stored like
-       * CURSORICONFILEDIR, but we treat them like CURSORICONDIR. In Wine
-       * this is solved by creating a fake cursor/icon directory in memory
-       * and passing that to CURSORICON_FindBestIcon.
-       */
-
-      if (fuLoad & LR_SHARED)
-      {
-        DbgPrint("FIXME: need LR_SHARED support for loading icon images from files\n");
-      }
-
-      hFile = CreateFileW(lpszName,
-                        GENERIC_READ,
-                        FILE_SHARE_READ,
-                        NULL,
-                        OPEN_EXISTING,
-                        0,
-                        NULL);
-      if (hFile == NULL)
-         {
-           return(NULL);
-         }
-
-      hSection = CreateFileMappingW(hFile,
-                                  NULL,
-                                  PAGE_READONLY,
-                                  0,
-                                  0,
-                                  NULL);
-
-      if (hSection == NULL)
-         {
-           CloseHandle(hFile);
-           return(NULL);
-         }
-      IconDIR = MapViewOfFile(hSection,
-                                FILE_MAP_READ,
-                                0,
-                                0,
-                                0);
-
-      if (IconDIR == NULL || 0 != IconDIR->idReserved
-          || (IMAGE_ICON != IconDIR->idType && IMAGE_CURSOR != IconDIR->idType))
-         {
-           CloseHandle(hFile);
-           CloseHandle(hSection);
-           return(NULL);
-         }
-
-      //pick the best size.
-      dirEntry = (CURSORICONDIRENTRY *)  CURSORICON_FindBestIcon( IconDIR, width, height, 1);
-
-
-      if (!dirEntry)
-         {
-              CloseHandle(hFile);
-              CloseHandle(hSection);
-              UnmapViewOfFile(IconDIR);
-              return(NULL);
-         }
-
-      SafeIconImage = RtlAllocateHeap(GetProcessHeap(), 0, dirEntry->dwBytesInRes);
-
-      memcpy(SafeIconImage, ((PBYTE)IconDIR) + dirEntry->dwImageOffset, dirEntry->dwBytesInRes);
-
-      CloseHandle(hFile);
-      CloseHandle(hSection);
-  }
-
-  //at this point we have a copy of the icon image to play with
-
-  SafeIconImage->icHeader.biHeight = SafeIconImage->icHeader.biHeight /2;
-
-  if (SafeIconImage->icHeader.biSize == sizeof(BITMAPCOREHEADER))
-    {
-      BITMAPCOREHEADER* Core = (BITMAPCOREHEADER*)SafeIconImage;
-      ColorCount = (Core->bcBitCount <= 8) ? (1 << Core->bcBitCount) : 0;
-      HeaderSize = sizeof(BITMAPCOREHEADER) + ColorCount * sizeof(RGBTRIPLE);
-    }
-  else
-    {
-      ColorCount = SafeIconImage->icHeader.biClrUsed;
-      if (ColorCount == 0 && SafeIconImage->icHeader.biBitCount <= 8)
-       {
-         ColorCount = 1 << SafeIconImage->icHeader.biBitCount;
-       }
-      HeaderSize = sizeof(BITMAPINFOHEADER) + ColorCount * sizeof(RGBQUAD);
-    }
-
-  //make data point to the start of the XOR image data
-  Data = (PBYTE)SafeIconImage + HeaderSize;
-
+   DeleteDC(hScreenDc);
 
-  //get a handle to the screen dc, the icon we create is going to be compatable with this
-  hScreenDc = CreateDCW(L"DISPLAY", NULL, NULL, NULL);
-  if (hScreenDc == NULL)
-  {
-      if (fuLoad & LR_LOADFROMFILE)
-         {
-               RtlFreeHeap(GetProcessHeap(), 0, SafeIconImage);
-        UnmapViewOfFile(IconDIR);
-         }
-      return(NULL);
-  }
-
-  hIcon = ICON_CreateIconFromData(hScreenDc, Data, SafeIconImage, width, height, width/2, height/2);
-  RtlFreeHeap(GetProcessHeap(), 0, SafeIconImage);
-  return hIcon;
+   return hIcon;
 }
 
 
-HANDLE STATIC
+static HANDLE
 LoadBitmapImage(HINSTANCE hInstance, LPCWSTR lpszName, UINT fuLoad)
 {
-  HANDLE hResource;
-  HANDLE hFile;
-  HANDLE hSection;
-  BITMAPINFO* BitmapInfo;
-  BITMAPINFO* PrivateInfo;
-  HDC hScreenDc;
-  HANDLE hBitmap;
-  ULONG HeaderSize;
-  ULONG ColorCount;
-  PVOID Data;
-
-  if (!(fuLoad & LR_LOADFROMFILE))
-    {
+   HANDLE hResource;
+   HANDLE hFile;
+   HANDLE hSection;
+   LPBITMAPINFO BitmapInfo;
+   LPBITMAPINFO PrivateInfo;
+   HDC hScreenDc;
+   HANDLE hBitmap;
+   ULONG HeaderSize;
+   ULONG ColorCount;
+   PVOID Data;
+
+   if (!(fuLoad & LR_LOADFROMFILE))
+   {
       if (hInstance == NULL)
-       {
-         hInstance = GetModuleHandleW(L"USER32");
-       }
+         hInstance = User32Instance;
+
       hResource = FindResourceW(hInstance, lpszName, RT_BITMAP);
       if (hResource == NULL)
-       {
-         return(NULL);
-       }
+         return NULL;
       hResource = LoadResource(hInstance, hResource);
       if (hResource == NULL)
-       {
-         return(NULL);
-       }
+         return NULL;
       BitmapInfo = LockResource(hResource);
       if (BitmapInfo == NULL)
-       {
-         return(NULL);
-       }
-    }
-  else
-    {
-      hFile = CreateFileW(lpszName,
-                        GENERIC_READ,
-                        FILE_SHARE_READ,
-                        NULL,
-                        OPEN_EXISTING,
-                        0,
-                        NULL);
-      if (hFile == NULL)
-       {
-         return(NULL);
-       }
-      hSection = CreateFileMappingW(hFile,
-                                  NULL,
-                                  PAGE_READONLY,
-                                  0,
-                                  0,
-                                  NULL);
+         return NULL;
+   }
+   else
+   {
+      hFile = CreateFileW(lpszName, GENERIC_READ, FILE_SHARE_READ, NULL,
+                          OPEN_EXISTING, 0, NULL);
+      if (hFile == INVALID_HANDLE_VALUE)
+         return NULL;
+
+      hSection = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
       CloseHandle(hFile);
       if (hSection == NULL)
-       {
-         return(NULL);
-       }
-      BitmapInfo = MapViewOfFile(hSection,
-                                FILE_MAP_READ,
-                                0,
-                                0,
-                                0);
+         return NULL;
+
+      BitmapInfo = MapViewOfFile(hSection, FILE_MAP_READ, 0, 0, 0);
       CloseHandle(hSection);
       if (BitmapInfo == NULL)
-       {
-         return(NULL);
-       }
-       /* offset BitmapInfo by 14 bytes to acount for the size of BITMAPFILEHEADER
-          unfortunatly sizeof(BITMAPFILEHEADER) = 16, but the acutal size should be 14!
-       */
-       BitmapInfo = (BITMAPINFO*)(((PBYTE)BitmapInfo) + 14);
-    }
-
-  if (BitmapInfo->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
-    {
+         return NULL;
+
+      BitmapInfo = (LPBITMAPINFO)((ULONG_PTR)BitmapInfo + sizeof(BITMAPFILEHEADER));
+   }
+
+   if (BitmapInfo->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
+   {
       BITMAPCOREHEADER* Core = (BITMAPCOREHEADER*)BitmapInfo;
       ColorCount = (Core->bcBitCount <= 8) ? (1 << Core->bcBitCount) : 0;
       HeaderSize = sizeof(BITMAPCOREHEADER) + ColorCount * sizeof(RGBTRIPLE);
-    }
-  else
-    {
+   }
+   else
+   {
       ColorCount = BitmapInfo->bmiHeader.biClrUsed;
       if (ColorCount == 0 && BitmapInfo->bmiHeader.biBitCount <= 8)
-       {
-         ColorCount = 1 << BitmapInfo->bmiHeader.biBitCount;
-       }
+         ColorCount = 1 << BitmapInfo->bmiHeader.biBitCount;
       HeaderSize = sizeof(BITMAPINFOHEADER) + ColorCount * sizeof(RGBQUAD);
-    }
-  Data = (PVOID)((ULONG_PTR)BitmapInfo + HeaderSize);
+   }
+   Data = (PVOID)((ULONG_PTR)BitmapInfo + HeaderSize);
 
-  PrivateInfo = RtlAllocateHeap(GetProcessHeap(), 0, HeaderSize);
-  if (PrivateInfo == NULL)
-    {
+   PrivateInfo = RtlAllocateHeap(GetProcessHeap(), 0, HeaderSize);
+   if (PrivateInfo == NULL)
+   {
       if (fuLoad & LR_LOADFROMFILE)
-       {
-         UnmapViewOfFile(BitmapInfo);
-       }
-      return(NULL);
-    }
-  memcpy(PrivateInfo, BitmapInfo, HeaderSize);
-
-  /* FIXME: Handle color conversion and transparency. */
-
-  hScreenDc = CreateCompatibleDC(NULL);
-  if (hScreenDc == NULL)
-    {
+         UnmapViewOfFile(BitmapInfo);
+      return NULL;
+   }
+   memcpy(PrivateInfo, BitmapInfo, HeaderSize);
+
+   /* FIXME: Handle color conversion and transparency. */
+
+   hScreenDc = CreateCompatibleDC(NULL);
+   if (hScreenDc == NULL)
+   {
       RtlFreeHeap(GetProcessHeap(), 0, PrivateInfo);
       if (fuLoad & LR_LOADFROMFILE)
-       {
-         UnmapViewOfFile(BitmapInfo);
-       }
-      return(NULL);
-    }
-
-  if (fuLoad & LR_CREATEDIBSECTION)
-    {
+         UnmapViewOfFile(BitmapInfo);
+      return NULL;
+   }
+
+   if (fuLoad & LR_CREATEDIBSECTION)
+   {
       DIBSECTION Dib;
 
       hBitmap = CreateDIBSection(hScreenDc, PrivateInfo, DIB_RGB_COLORS, NULL,
-                                0, 0);
+                                 0, 0);
       GetObjectA(hBitmap, sizeof(DIBSECTION), &Dib);
       SetDIBits(hScreenDc, hBitmap, 0, Dib.dsBm.bmHeight, Data, BitmapInfo,
-               DIB_RGB_COLORS);
-    }
-  else
-    {
+                DIB_RGB_COLORS);
+   }
+   else
+   {
       hBitmap = CreateDIBitmap(hScreenDc, &PrivateInfo->bmiHeader, CBM_INIT,
-                              Data, PrivateInfo, DIB_RGB_COLORS);
-    }
+                               Data, PrivateInfo, DIB_RGB_COLORS);
+   }
 
-  RtlFreeHeap(GetProcessHeap(), 0, PrivateInfo);
-  DeleteDC(hScreenDc);
-  if (fuLoad & LR_LOADFROMFILE)
-    {
+   RtlFreeHeap(GetProcessHeap(), 0, PrivateInfo);
+   DeleteDC(hScreenDc);
+   if (fuLoad & LR_LOADFROMFILE)
       UnmapViewOfFile(BitmapInfo);
-    }
-  return(hBitmap);
+
+   return hBitmap;
 }
 
 HANDLE STDCALL
-LoadImageW(HINSTANCE hinst,
-          LPCWSTR lpszName,
-          UINT uType,
-          int cxDesired,
-          int cyDesired,
-          UINT fuLoad)
+LoadImageW(
+   IN HINSTANCE hinst,
+   IN LPCWSTR lpszName,
+   IN UINT uType,
+   IN INT cxDesired,
+   IN INT cyDesired,
+   IN UINT fuLoad)
 {
-  if (fuLoad & LR_DEFAULTSIZE)
-    {
+   if (fuLoad & LR_DEFAULTSIZE)
+   {
       if (uType == IMAGE_ICON)
-       {
-         if (cxDesired == 0)
-           {
-             cxDesired = GetSystemMetrics(SM_CXICON);
-           }
-         if (cyDesired == 0)
-           {
-             cyDesired = GetSystemMetrics(SM_CYICON);
-           }
-       }
-      else if (uType == IMAGE_CURSOR)
-       {
-         if (cxDesired == 0)
-           {
-             cxDesired = GetSystemMetrics(SM_CXCURSOR);
-           }
-         if (cyDesired == 0)
-           {
-             cyDesired = GetSystemMetrics(SM_CYCURSOR);
-           }
-       }
-    }
-
-  switch (uType)
-    {
-    case IMAGE_BITMAP:
-      {
-       return(LoadBitmapImage(hinst, lpszName, fuLoad));
-      }
-    case IMAGE_CURSOR:
       {
-       return(LoadCursorImage(hinst, lpszName, fuLoad));
+         if (cxDesired == 0)
+            cxDesired = GetSystemMetrics(SM_CXICON);
+         if (cyDesired == 0)
+            cyDesired = GetSystemMetrics(SM_CYICON);
       }
-    case IMAGE_ICON:
+      else if (uType == IMAGE_CURSOR)
       {
-       return(LoadIconImage(hinst, lpszName, cxDesired, cyDesired, fuLoad));
+         if (cxDesired == 0)
+            cxDesired = GetSystemMetrics(SM_CXCURSOR);
+         if (cyDesired == 0)
+            cyDesired = GetSystemMetrics(SM_CYCURSOR);
       }
-    default:
-      DbgBreakPoint();
-      break;
-    }
-  return(NULL);
+   }
+
+   switch (uType)
+   {
+      case IMAGE_BITMAP:
+         return LoadBitmapImage(hinst, lpszName, fuLoad);
+      case IMAGE_CURSOR:
+      case IMAGE_ICON:
+         return LoadCursorIconImage(hinst, lpszName, cxDesired, cyDesired,
+                                    fuLoad, uType);
+      default:
+         break;
+   }
+
+   return NULL;
 }
 
 
@@ -647,7 +424,7 @@ LoadImageW(HINSTANCE hinst,
 HBITMAP STDCALL
 LoadBitmapA(HINSTANCE hInstance, LPCSTR lpBitmapName)
 {
-  return(LoadImageA(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0, 0));
+   return LoadImageA(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0, 0);
 }
 
 
@@ -657,7 +434,7 @@ LoadBitmapA(HINSTANCE hInstance, LPCSTR lpBitmapName)
 HBITMAP STDCALL
 LoadBitmapW(HINSTANCE hInstance, LPCWSTR lpBitmapName)
 {
-  return(LoadImageW(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0, 0));
+   return LoadImageW(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0, 0);
 }
 
 
@@ -665,50 +442,67 @@ LoadBitmapW(HINSTANCE hInstance, LPCWSTR lpBitmapName)
  * @unimplemented
  */
 HANDLE WINAPI
-CopyImage(HANDLE hnd, UINT type, INT desiredx, INT desiredy, UINT flags)
+CopyImage(
+   IN HANDLE hnd,
+   IN UINT type,
+   IN INT desiredx,
+   IN INT desiredy,
+   IN UINT flags)
 {
-       switch (type)
-       {
-        case IMAGE_BITMAP:
-                       {
-                               DbgPrint("WARNING:  Incomplete implementation of CopyImage!\n");
-                       /* FIXME:  support flags LR_COPYDELETEORG, LR_COPYFROMRESOURCE,
-                                                                LR_COPYRETURNORG, LR_CREATEDIBSECTION,
-                                                                and LR_MONOCHROME; */
-                               HBITMAP res;
-                               BITMAP bm;
-
-                               if (!GetObjectW(hnd, sizeof(bm), &bm)) return 0;
-                               bm.bmBits = NULL;
-                               if ((res = CreateBitmapIndirect(&bm)))
-                               {
-                    char *buf = HeapAlloc(GetProcessHeap(), 0, bm.bmWidthBytes * bm.bmHeight);
-                                       GetBitmapBits(hnd, bm.bmWidthBytes * bm.bmHeight, buf);
-                                       SetBitmapBits(res, bm.bmWidthBytes * bm.bmHeight, buf);
-                                       HeapFree(GetProcessHeap(), 0, buf);
-                               }
-                return res;
-                       }
-               case IMAGE_ICON:
-                       {
-                               static BOOL IconMsgDisplayed = FALSE;
-                               /* FIXME: support loading the image as shared from an instance */
-                               if (!IconMsgDisplayed) {
-                                       DbgPrint("FIXME: CopyImage doesn't support IMAGE_ICON correctly!\n");
-                                       IconMsgDisplayed = TRUE;
-                               }
-                       return CopyIcon(hnd);
-                       }
-               case IMAGE_CURSOR:
-                       {
-                               static BOOL IconMsgDisplayed = FALSE;
-                               /* FIXME: support loading the image as shared from an instance */
-                               if (!IconMsgDisplayed) {
-                                       DbgPrint("FIXME: CopyImage doesn't support IMAGE_CURSOR correctly!\n");
-                                       IconMsgDisplayed = TRUE;
-                               }
-                               return CopyCursor(hnd);
-                       }
-       }
-       return 0;
+   HBITMAP res;
+   BITMAP bm;
+
+   switch (type)
+   {
+      case IMAGE_BITMAP:
+         {
+            DbgPrint("WARNING:  Incomplete implementation of CopyImage!\n");
+            /*
+             * FIXME: Support flags LR_COPYDELETEORG, LR_COPYFROMRESOURCE,
+             * LR_COPYRETURNORG, LR_CREATEDIBSECTION and LR_MONOCHROME.
+             */
+            if (!GetObjectW(hnd, sizeof(bm), &bm))
+                return NULL;
+            bm.bmBits = NULL;
+            if ((res = CreateBitmapIndirect(&bm)))
+            {
+               char *buf = HeapAlloc(GetProcessHeap(), 0, bm.bmWidthBytes * bm.bmHeight);
+               if (buf == NULL)
+               {
+                  DeleteObject(res);
+                  return NULL;
+               }
+               GetBitmapBits(hnd, bm.bmWidthBytes * bm.bmHeight, buf);
+               SetBitmapBits(res, bm.bmWidthBytes * bm.bmHeight, buf);
+               HeapFree(GetProcessHeap(), 0, buf);
+            }
+            return res;
+         }
+
+      case IMAGE_ICON:
+         {
+            static BOOL IconMsgDisplayed = FALSE;
+            /* FIXME: support loading the image as shared from an instance */
+            if (!IconMsgDisplayed)
+            {
+               DbgPrint("FIXME: CopyImage doesn't support IMAGE_ICON correctly!\n");
+               IconMsgDisplayed = TRUE;
+            }
+            return CopyIcon(hnd);
+         }
+
+      case IMAGE_CURSOR:
+         {
+            static BOOL IconMsgDisplayed = FALSE;
+            /* FIXME: support loading the image as shared from an instance */
+            if (!IconMsgDisplayed)
+            {
+               DbgPrint("FIXME: CopyImage doesn't support IMAGE_CURSOR correctly!\n");
+               IconMsgDisplayed = TRUE;
+            }
+            return CopyCursor(hnd);
+         }
+   }
+
+   return NULL;
 }