[GDI32]
[reactos.git] / reactos / dll / win32 / gdi32 / objects / utils.c
index 0d66080..7ed0d22 100644 (file)
 
 BOOL WINAPI
 CalculateColorTableSize(
-   CONST BITMAPINFOHEADER *BitmapInfoHeader,
-   UINT *ColorSpec,
-   UINT *ColorTableSize)
+    CONST BITMAPINFOHEADER *BitmapInfoHeader,
+    UINT *ColorSpec,
+    UINT *ColorTableSize)
 {
-   WORD BitCount;
-   DWORD ClrUsed;
-   DWORD Compression;
-
-   /*
-    * At first get some basic parameters from the passed BitmapInfoHeader
-    * structure. It can have one of the following formats:
-    * - BITMAPCOREHEADER (the oldest one with totally different layout
-    *                     from the others)
-    * - BITMAPINFOHEADER (the standard and most common header)
-    * - BITMAPV4HEADER (extension of BITMAPINFOHEADER)
-    * - BITMAPV5HEADER (extension of BITMAPV4HEADER)
-    */
-
-   if (BitmapInfoHeader->biSize == sizeof(BITMAPCOREHEADER))
-   {
-      BitCount = ((LPBITMAPCOREHEADER)BitmapInfoHeader)->bcBitCount;
-      ClrUsed = 0;
-      Compression = BI_RGB;
-   }
-   else
-   {
-      BitCount = BitmapInfoHeader->biBitCount;
-      ClrUsed = BitmapInfoHeader->biClrUsed;
-      Compression = BitmapInfoHeader->biCompression;
-   }
-
-   switch (Compression)
-   {
-      case BI_BITFIELDS:
-         if (*ColorSpec == DIB_PAL_COLORS)
+    WORD BitCount;
+    DWORD ClrUsed;
+    DWORD Compression;
+
+    /*
+     * At first get some basic parameters from the passed BitmapInfoHeader
+     * structure. It can have one of the following formats:
+     * - BITMAPCOREHEADER (the oldest one with totally different layout
+     *                     from the others)
+     * - BITMAPINFOHEADER (the standard and most common header)
+     * - BITMAPV4HEADER (extension of BITMAPINFOHEADER)
+     * - BITMAPV5HEADER (extension of BITMAPV4HEADER)
+     */
+
+    if (BitmapInfoHeader->biSize == sizeof(BITMAPCOREHEADER))
+    {
+        BitCount = ((LPBITMAPCOREHEADER)BitmapInfoHeader)->bcBitCount;
+        ClrUsed = 0;
+        Compression = BI_RGB;
+    }
+    else
+    {
+        BitCount = BitmapInfoHeader->biBitCount;
+        ClrUsed = BitmapInfoHeader->biClrUsed;
+        Compression = BitmapInfoHeader->biCompression;
+    }
+
+    switch (Compression)
+    {
+    case BI_BITFIELDS:
+        if (*ColorSpec == DIB_PAL_COLORS)
             *ColorSpec = DIB_RGB_COLORS;
 
-         if (BitCount != 16 && BitCount != 32)
+        if (BitCount != 16 && BitCount != 32)
             return FALSE;
 
-         /*
-          * For BITMAPV4HEADER/BITMAPV5HEADER the masks are included in
-          * the structure itself (bV4RedMask, bV4GreenMask, and bV4BlueMask).
-          * For BITMAPINFOHEADER the color masks are stored in the palette.
-          */
+        /*
+         * For BITMAPV4HEADER/BITMAPV5HEADER the masks are included in
+         * the structure itself (bV4RedMask, bV4GreenMask, and bV4BlueMask).
+         * For BITMAPINFOHEADER the color masks are stored in the palette.
+         */
 
-         if (BitmapInfoHeader->biSize > sizeof(BITMAPINFOHEADER))
+        if (BitmapInfoHeader->biSize > sizeof(BITMAPINFOHEADER))
             *ColorTableSize = 0;
-         else
+        else
             *ColorTableSize = 3;
 
-         return TRUE;
-
-      case BI_RGB:
-         switch (BitCount)
-         {
-            case 1:
-               *ColorTableSize = ClrUsed ? min(ClrUsed, 2) : 2;
-               return TRUE;
-
-            case 4:
-               *ColorTableSize = ClrUsed ? min(ClrUsed, 16) : 16;
-               return TRUE;
-
-            case 8:
-               *ColorTableSize = ClrUsed ? min(ClrUsed, 256) : 256;
-               return TRUE;
-
-            default:
-               if (*ColorSpec == DIB_PAL_COLORS)
-                  *ColorSpec = DIB_RGB_COLORS;
-               if (BitCount != 16 && BitCount != 24 && BitCount != 32)
-                  return FALSE;
-               *ColorTableSize = ClrUsed;
-               return TRUE;
-         }
-
-      case BI_RLE4:
-         if (BitCount == 4)
-         {
+        return TRUE;
+
+    case BI_RGB:
+        switch (BitCount)
+        {
+        case 1:
+            *ColorTableSize = ClrUsed ? min(ClrUsed, 2) : 2;
+            return TRUE;
+
+        case 4:
+            *ColorTableSize = ClrUsed ? min(ClrUsed, 16) : 16;
+            return TRUE;
+
+        case 8:
+            *ColorTableSize = ClrUsed ? min(ClrUsed, 256) : 256;
+            return TRUE;
+
+        default:
+            if (*ColorSpec == DIB_PAL_COLORS)
+                *ColorSpec = DIB_RGB_COLORS;
+            if (BitCount != 16 && BitCount != 24 && BitCount != 32)
+                return FALSE;
+            *ColorTableSize = ClrUsed;
+            return TRUE;
+        }
+
+    case BI_RLE4:
+        if (BitCount == 4)
+        {
             *ColorTableSize = ClrUsed ? min(ClrUsed, 16) : 16;
             return TRUE;
-         }
-         return FALSE;
+        }
+        return FALSE;
 
-      case BI_RLE8:
-         if (BitCount == 8)
-         {
+    case BI_RLE8:
+        if (BitCount == 8)
+        {
             *ColorTableSize = ClrUsed ? min(ClrUsed, 256) : 256;
             return TRUE;
-         }
-         return FALSE;
+        }
+        return FALSE;
 
-      case BI_JPEG:
-      case BI_PNG:
-         *ColorTableSize = ClrUsed;
-         return TRUE;
+    case BI_JPEG:
+    case BI_PNG:
+        *ColorTableSize = ClrUsed;
+        return TRUE;
 
-      default:
-         return FALSE;
-   }
+    default:
+        return FALSE;
+    }
 }
 
 /**
@@ -170,142 +170,142 @@ CalculateColorTableSize(
 
 LPBITMAPINFO WINAPI
 ConvertBitmapInfo(
-   CONST BITMAPINFO *BitmapInfo,
-   UINT ColorSpec,
-   UINT *BitmapInfoSize,
-   BOOL FollowedByData)
+    CONST BITMAPINFO *BitmapInfo,
+    UINT ColorSpec,
+    UINT *BitmapInfoSize,
+    BOOL FollowedByData)
 {
-   LPBITMAPINFO NewBitmapInfo = (LPBITMAPINFO)BitmapInfo;
-   LPBITMAPCOREINFO CoreBitmapInfo = (LPBITMAPCOREINFO)BitmapInfo;
-   DWORD Size = 0;
-   ULONG DataSize = 0;
-   UINT PaletteEntryCount = 0;
-
-   /*
-    * At first check if the passed BitmapInfo structure has valid size. It
-    * can have one of these headers: BITMAPCOREHEADER, BITMAPINFOHEADER,
-    * BITMAPV4HEADER or BITMAPV5HEADER (see CalculateColorTableSize for
-    * description).
-    */
-
-   if ( !BitmapInfo || 
-        (BitmapInfo->bmiHeader.biSize != sizeof(BITMAPCOREHEADER) &&
-       (BitmapInfo->bmiHeader.biSize < sizeof(BITMAPINFOHEADER) ||
-        BitmapInfo->bmiHeader.biSize > sizeof(BITMAPV5HEADER))))
-   {
-      return NULL;
-   }
-
-   /*
-    * Now calculate the color table size. Also if the bitmap info contains
-    * invalid color information it's rejected here.
-    */
-
-   if (!CalculateColorTableSize(&BitmapInfo->bmiHeader, &ColorSpec,
-                                &PaletteEntryCount))
-   {
-      return NULL;
-   }
-
-   /*
-    * Calculate the size of image data if applicable. We must be careful
-    * to do proper aligning on line ends.
-    */
-
-   if (FollowedByData)
-   {
-      DataSize = DIB_BitmapBitsSize((PBITMAPINFO)BitmapInfo );
-   }
-
-   /*
-    * If BitmapInfo was originally BITMAPCOREINFO then we need to convert
-    * it to the standard BITMAPINFO layout.
-    */
-
-   if (BitmapInfo->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
-   {
-      Size = sizeof(BITMAPINFOHEADER);
-      if (ColorSpec == DIB_RGB_COLORS)
-         Size += PaletteEntryCount * sizeof(RGBQUAD);
-      else
-         Size += PaletteEntryCount * sizeof(USHORT);
-      Size += DataSize;
-
-      NewBitmapInfo = RtlAllocateHeap(RtlGetProcessHeap(), 0, Size);
-      if (NewBitmapInfo == NULL)
-      {
-         return NULL;
-      }
-
-      NewBitmapInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
-      NewBitmapInfo->bmiHeader.biWidth = CoreBitmapInfo->bmciHeader.bcWidth;
-      NewBitmapInfo->bmiHeader.biHeight = CoreBitmapInfo->bmciHeader.bcHeight;
-      NewBitmapInfo->bmiHeader.biPlanes = CoreBitmapInfo->bmciHeader.bcPlanes;
-      NewBitmapInfo->bmiHeader.biBitCount = CoreBitmapInfo->bmciHeader.bcBitCount;
-      NewBitmapInfo->bmiHeader.biCompression = BI_RGB;
-      NewBitmapInfo->bmiHeader.biSizeImage = 0;
-      NewBitmapInfo->bmiHeader.biXPelsPerMeter = 0;
-      NewBitmapInfo->bmiHeader.biYPelsPerMeter = 0;
-      NewBitmapInfo->bmiHeader.biClrUsed = 0;
-      NewBitmapInfo->bmiHeader.biClrImportant = 0;
-
-      if (PaletteEntryCount != 0)
-      {
-         if (ColorSpec == DIB_RGB_COLORS)
-         {
-            ULONG Index;
-
-            for (Index = 0; Index < PaletteEntryCount; Index++)
+    LPBITMAPINFO NewBitmapInfo = (LPBITMAPINFO)BitmapInfo;
+    LPBITMAPCOREINFO CoreBitmapInfo = (LPBITMAPCOREINFO)BitmapInfo;
+    DWORD Size = 0;
+    ULONG DataSize = 0;
+    UINT PaletteEntryCount = 0;
+
+    /*
+     * At first check if the passed BitmapInfo structure has valid size. It
+     * can have one of these headers: BITMAPCOREHEADER, BITMAPINFOHEADER,
+     * BITMAPV4HEADER or BITMAPV5HEADER (see CalculateColorTableSize for
+     * description).
+     */
+
+    if ( !BitmapInfo ||
+            (BitmapInfo->bmiHeader.biSize != sizeof(BITMAPCOREHEADER) &&
+             (BitmapInfo->bmiHeader.biSize < sizeof(BITMAPINFOHEADER) ||
+              BitmapInfo->bmiHeader.biSize > sizeof(BITMAPV5HEADER))))
+    {
+        return NULL;
+    }
+
+    /*
+     * Now calculate the color table size. Also if the bitmap info contains
+     * invalid color information it's rejected here.
+     */
+
+    if (!CalculateColorTableSize(&BitmapInfo->bmiHeader, &ColorSpec,
+                                 &PaletteEntryCount))
+    {
+        return NULL;
+    }
+
+    /*
+     * Calculate the size of image data if applicable. We must be careful
+     * to do proper aligning on line ends.
+     */
+
+    if (FollowedByData)
+    {
+        DataSize = GdiGetBitmapBitsSize((PBITMAPINFO)BitmapInfo );
+    }
+
+    /*
+     * If BitmapInfo was originally BITMAPCOREINFO then we need to convert
+     * it to the standard BITMAPINFO layout.
+     */
+
+    if (BitmapInfo->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
+    {
+        Size = sizeof(BITMAPINFOHEADER);
+        if (ColorSpec == DIB_RGB_COLORS)
+            Size += PaletteEntryCount * sizeof(RGBQUAD);
+        else
+            Size += PaletteEntryCount * sizeof(USHORT);
+        Size += DataSize;
+
+        NewBitmapInfo = RtlAllocateHeap(RtlGetProcessHeap(), 0, Size);
+        if (NewBitmapInfo == NULL)
+        {
+            return NULL;
+        }
+
+        NewBitmapInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
+        NewBitmapInfo->bmiHeader.biWidth = CoreBitmapInfo->bmciHeader.bcWidth;
+        NewBitmapInfo->bmiHeader.biHeight = CoreBitmapInfo->bmciHeader.bcHeight;
+        NewBitmapInfo->bmiHeader.biPlanes = CoreBitmapInfo->bmciHeader.bcPlanes;
+        NewBitmapInfo->bmiHeader.biBitCount = CoreBitmapInfo->bmciHeader.bcBitCount;
+        NewBitmapInfo->bmiHeader.biCompression = BI_RGB;
+        NewBitmapInfo->bmiHeader.biSizeImage = 0;
+        NewBitmapInfo->bmiHeader.biXPelsPerMeter = 0;
+        NewBitmapInfo->bmiHeader.biYPelsPerMeter = 0;
+        NewBitmapInfo->bmiHeader.biClrUsed = 0;
+        NewBitmapInfo->bmiHeader.biClrImportant = 0;
+
+        if (PaletteEntryCount != 0)
+        {
+            if (ColorSpec == DIB_RGB_COLORS)
             {
-               NewBitmapInfo->bmiColors[Index].rgbRed =
-                  CoreBitmapInfo->bmciColors[Index].rgbtRed;
-               NewBitmapInfo->bmiColors[Index].rgbGreen =
-                  CoreBitmapInfo->bmciColors[Index].rgbtGreen;
-               NewBitmapInfo->bmiColors[Index].rgbBlue =
-                  CoreBitmapInfo->bmciColors[Index].rgbtBlue;
-               NewBitmapInfo->bmiColors[Index].rgbReserved = 0;
+                ULONG Index;
+
+                for (Index = 0; Index < PaletteEntryCount; Index++)
+                {
+                    NewBitmapInfo->bmiColors[Index].rgbRed =
+                        CoreBitmapInfo->bmciColors[Index].rgbtRed;
+                    NewBitmapInfo->bmiColors[Index].rgbGreen =
+                        CoreBitmapInfo->bmciColors[Index].rgbtGreen;
+                    NewBitmapInfo->bmiColors[Index].rgbBlue =
+                        CoreBitmapInfo->bmciColors[Index].rgbtBlue;
+                    NewBitmapInfo->bmiColors[Index].rgbReserved = 0;
+                }
             }
-         }
-         else
-         {
-            RtlCopyMemory(NewBitmapInfo->bmiColors,
-                          CoreBitmapInfo->bmciColors,
-                          PaletteEntryCount * sizeof(USHORT));
-         }
-      }
-
-      if (FollowedByData)
-      {
-         ULONG_PTR NewDataPtr, OldDataPtr;
-
-         if (ColorSpec == DIB_RGB_COLORS)
-         {
-            NewDataPtr = (ULONG_PTR)(NewBitmapInfo->bmiColors +
-                         PaletteEntryCount);
-            OldDataPtr = (ULONG_PTR)(CoreBitmapInfo->bmciColors +
-                         PaletteEntryCount);
-         }
-         else
-         {
-            NewDataPtr = (ULONG_PTR)(NewBitmapInfo->bmiColors) +
-                         PaletteEntryCount * sizeof(USHORT);
-            OldDataPtr = (ULONG_PTR)(CoreBitmapInfo->bmciColors) +
-                         PaletteEntryCount * sizeof(USHORT);
-         }
-
-         RtlCopyMemory((PVOID)NewDataPtr, (PVOID)OldDataPtr, DataSize);
-      }
-   }
-
-   Size = NewBitmapInfo->bmiHeader.biSize;
-   if (ColorSpec == DIB_RGB_COLORS)
-      Size += PaletteEntryCount * sizeof(RGBQUAD);
-   else
-      Size += PaletteEntryCount * sizeof(USHORT);
-   Size += DataSize;
-   *BitmapInfoSize = Size;
-
-   return NewBitmapInfo;
+            else
+            {
+                RtlCopyMemory(NewBitmapInfo->bmiColors,
+                              CoreBitmapInfo->bmciColors,
+                              PaletteEntryCount * sizeof(USHORT));
+            }
+        }
+
+        if (FollowedByData)
+        {
+            ULONG_PTR NewDataPtr, OldDataPtr;
+
+            if (ColorSpec == DIB_RGB_COLORS)
+            {
+                NewDataPtr = (ULONG_PTR)(NewBitmapInfo->bmiColors +
+                                         PaletteEntryCount);
+                OldDataPtr = (ULONG_PTR)(CoreBitmapInfo->bmciColors +
+                                         PaletteEntryCount);
+            }
+            else
+            {
+                NewDataPtr = (ULONG_PTR)(NewBitmapInfo->bmiColors) +
+                             PaletteEntryCount * sizeof(USHORT);
+                OldDataPtr = (ULONG_PTR)(CoreBitmapInfo->bmciColors) +
+                             PaletteEntryCount * sizeof(USHORT);
+            }
+
+            RtlCopyMemory((PVOID)NewDataPtr, (PVOID)OldDataPtr, DataSize);
+        }
+    }
+
+    Size = NewBitmapInfo->bmiHeader.biSize;
+    if (ColorSpec == DIB_RGB_COLORS)
+        Size += PaletteEntryCount * sizeof(RGBQUAD);
+    else
+        Size += PaletteEntryCount * sizeof(USHORT);
+    Size += DataSize;
+    *BitmapInfoSize = Size;
+
+    return NewBitmapInfo;
 }
 
 VOID
@@ -315,20 +315,21 @@ LogFontA2W(LPLOGFONTW pW, CONST LOGFONTA *pA)
 #define COPYS(f,len) MultiByteToWideChar ( CP_THREAD_ACP, 0, pA->f, len, pW->f, len )
 #define COPYN(f) pW->f = pA->f
 
-  COPYN(lfHeight);
-  COPYN(lfWidth);
-  COPYN(lfEscapement);
-  COPYN(lfOrientation);
-  COPYN(lfWeight);
-  COPYN(lfItalic);
-  COPYN(lfUnderline);
-  COPYN(lfStrikeOut);
-  COPYN(lfCharSet);
-  COPYN(lfOutPrecision);
-  COPYN(lfClipPrecision);
-  COPYN(lfQuality);
-  COPYN(lfPitchAndFamily);
-  COPYS(lfFaceName,LF_FACESIZE);
+    COPYN(lfHeight);
+    COPYN(lfWidth);
+    COPYN(lfEscapement);
+    COPYN(lfOrientation);
+    COPYN(lfWeight);
+    COPYN(lfItalic);
+    COPYN(lfUnderline);
+    COPYN(lfStrikeOut);
+    COPYN(lfCharSet);
+    COPYN(lfOutPrecision);
+    COPYN(lfClipPrecision);
+    COPYN(lfQuality);
+    COPYN(lfPitchAndFamily);
+    COPYS(lfFaceName,LF_FACESIZE);
+    pW->lfFaceName[LF_FACESIZE - 1] = '\0';
 
 #undef COPYN
 #undef COPYS
@@ -341,20 +342,21 @@ LogFontW2A(LPLOGFONTA pA, CONST LOGFONTW *pW)
 #define COPYS(f,len) WideCharToMultiByte ( CP_THREAD_ACP, 0, pW->f, len, pA->f, len, NULL, NULL )
 #define COPYN(f) pA->f = pW->f
 
-  COPYN(lfHeight);
-  COPYN(lfWidth);
-  COPYN(lfEscapement);
-  COPYN(lfOrientation);
-  COPYN(lfWeight);
-  COPYN(lfItalic);
-  COPYN(lfUnderline);
-  COPYN(lfStrikeOut);
-  COPYN(lfCharSet);
-  COPYN(lfOutPrecision);
-  COPYN(lfClipPrecision);
-  COPYN(lfQuality);
-  COPYN(lfPitchAndFamily);
-  COPYS(lfFaceName,LF_FACESIZE);
+    COPYN(lfHeight);
+    COPYN(lfWidth);
+    COPYN(lfEscapement);
+    COPYN(lfOrientation);
+    COPYN(lfWeight);
+    COPYN(lfItalic);
+    COPYN(lfUnderline);
+    COPYN(lfStrikeOut);
+    COPYN(lfCharSet);
+    COPYN(lfOutPrecision);
+    COPYN(lfClipPrecision);
+    COPYN(lfQuality);
+    COPYN(lfPitchAndFamily);
+    COPYS(lfFaceName,LF_FACESIZE);
+    pA->lfFaceName[LF_FACESIZE - 1] = '\0';
 
 #undef COPYN
 #undef COPYS
@@ -367,13 +369,13 @@ EnumLogFontExW2A( LPENUMLOGFONTEXA fontA, CONST ENUMLOGFONTEXW *fontW )
     LogFontW2A( (LPLOGFONTA)fontA, (CONST LOGFONTW *)fontW );
 
     WideCharToMultiByte( CP_THREAD_ACP, 0, fontW->elfFullName, -1,
-                        (LPSTR) fontA->elfFullName, LF_FULLFACESIZE, NULL, NULL );
+                         (LPSTR) fontA->elfFullName, LF_FULLFACESIZE, NULL, NULL );
     fontA->elfFullName[LF_FULLFACESIZE-1] = '\0';
     WideCharToMultiByte( CP_THREAD_ACP, 0, fontW->elfStyle, -1,
-                        (LPSTR) fontA->elfStyle, LF_FACESIZE, NULL, NULL );
+                         (LPSTR) fontA->elfStyle, LF_FACESIZE, NULL, NULL );
     fontA->elfStyle[LF_FACESIZE-1] = '\0';
     WideCharToMultiByte( CP_THREAD_ACP, 0, fontW->elfScript, -1,
-                        (LPSTR) fontA->elfScript, LF_FACESIZE, NULL, NULL );
+                         (LPSTR) fontA->elfScript, LF_FACESIZE, NULL, NULL );
     fontA->elfScript[LF_FACESIZE-1] = '\0';
 }