update for HEAD-2003091401
[reactos.git] / subsys / win32k / objects / palette.c
index b4e150d..73b2713 100644 (file)
@@ -1,10 +1,34 @@
+/*
+ *  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.
+ */
+/* $Id$ */
+
 #undef WIN32_LEAN_AND_MEAN
 #include <windows.h>
 #include <win32k/debug.h>
+#include <win32k/debug1.h>
 #include <win32k/bitmaps.h>
 #include <win32k/color.h>
+#include <win32k/gdiobj.h>
 #include <debug.h>
-#include "../include/palette.h"
+#include <include/palette.h>
+#include <include/object.h>
+#include <include/color.h>
 
 static int           PALETTE_firstFree = 0; 
 static unsigned char PALETTE_freeList[256];
@@ -16,13 +40,75 @@ int COLOR_gapEnd;
 int COLOR_gapFilled;
 int COLOR_max;
 
-PALETTEENTRY *ReturnSystemPalette(void)
+PPALETTEENTRY FASTCALL ReturnSystemPalette (VOID)
 {
   return COLOR_sysPal;
 }
 
+static BOOL FASTCALL
+PALETTE_InternalDelete(PPALGDI Palette)
+{
+  if (NULL != Palette->IndexedColors)
+    {
+      ExFreePool(Palette->IndexedColors);
+    }
+
+  return TRUE;
+}
+
+HPALETTE FASTCALL
+PALETTE_AllocPalette(ULONG Mode,
+                     ULONG NumColors,
+                     ULONG *Colors,
+                     ULONG Red,
+                     ULONG Green,
+                     ULONG Blue)
+{
+  HPALETTE NewPalette;
+  PPALGDI PalGDI;
+
+  NewPalette = (HPALETTE) GDIOBJ_AllocObj(sizeof(PALGDI), GDI_OBJECT_TYPE_PALETTE, (GDICLEANUPPROC) PALETTE_InternalDelete);
+  if (NULL == NewPalette)
+    {
+      return NULL;
+    }
+
+  PalGDI = PALETTE_LockPalette(NewPalette);
+  ASSERT( PalGDI );
+
+  PalGDI->Self = NewPalette;
+  PalGDI->Mode = Mode;
+
+  if (NULL != Colors)
+    {
+      PalGDI->IndexedColors = ExAllocatePool(NonPagedPool, sizeof(PALETTEENTRY) * NumColors);
+      if (NULL == PalGDI->IndexedColors)
+       {
+         PALETTE_UnlockPalette(NewPalette);
+         PALETTE_FreePalette(NewPalette);
+         return NULL;
+       }
+      RtlCopyMemory(PalGDI->IndexedColors, Colors, sizeof(PALETTEENTRY) * NumColors);
+    }
+
+  if (PAL_INDEXED == Mode)
+    {
+      PalGDI->NumColors = NumColors;
+    }
+  else if (PAL_BITFIELDS == Mode)
+    {
+      PalGDI->RedMask = Red;
+      PalGDI->GreenMask = Green;
+      PalGDI->BlueMask = Blue;
+    }
+
+  PALETTE_UnlockPalette(NewPalette);
+
+  return NewPalette;
+}
+
 // Create the system palette
-HPALETTE PALETTE_Init(void)
+HPALETTE FASTCALL PALETTE_Init(VOID)
 {
   int i;
   HPALETTE hpalette;
@@ -44,10 +130,10 @@ HPALETTE PALETTE_Init(void)
     palPtr->palPalEntry[i].peFlags = 0;
   }
 
-  hpalette = W32kCreatePalette(palPtr);
+  hpalette = NtGdiCreatePalette(palPtr);
   ExFreePool(palPtr);
 
-  palObj = (PPALOBJ)AccessUserObject(hpalette);
+  palObj = (PPALOBJ)PALETTE_LockPalette(hpalette);
   if (palObj)
   {
     if (!(palObj->mapping = ExAllocatePool(NonPagedPool, sizeof(int) * 20)))
@@ -55,7 +141,7 @@ HPALETTE PALETTE_Init(void)
       DbgPrint("Win32k: Can not create palette mapping -- out of memory!");
       return FALSE;
     }
-//      GDI_ReleaseObj( hpalette );
+    PALETTE_UnlockPalette(hpalette);
   }
 
 /*  palette_size = visual->map_entries; */
@@ -63,7 +149,7 @@ HPALETTE PALETTE_Init(void)
   return hpalette;
 }
 
-static void PALETTE_FormatSystemPalette(void)
+static void FASTCALL PALETTE_FormatSystemPalette(void)
 {
   // Build free list so we'd have an easy way to find
   // out if there are any available colorcells. 
@@ -83,62 +169,7 @@ static void PALETTE_FormatSystemPalette(void)
   PALETTE_freeList[j] = 0;
 }
 
-/* Ported from WINE 20020804 (graphics\x11drv\palette.c) */
-static int SysPaletteLookupPixel( COLORREF col, BOOL skipReserved )
-{
-  int i, best = 0, diff = 0x7fffffff;
-  int r,g,b;
-
-  for( i = 0; i < palette_size && diff ; i++ )
-  {
-    if( !(COLOR_sysPal[i].peFlags & PC_SYS_USED) || (skipReserved && COLOR_sysPal[i].peFlags  & PC_SYS_RESERVED) )
-      continue;
-
-    r = COLOR_sysPal[i].peRed - GetRValue(col);
-    g = COLOR_sysPal[i].peGreen - GetGValue(col);
-    b = COLOR_sysPal[i].peBlue - GetBValue(col);
-
-    r = r*r + g*g + b*b;
-
-    if( r < diff ) { best = i; diff = r; }
-  }
-  return best;
-}
-
-/* Ported from WINE 20020804 (graphics\x11drv\palette.c) */
-/* Make sure this is required - ROS's xlate may make this redundant */
-UINT WINAPI GetNearestPaletteIndex(
-    HPALETTE hpalette, /* [in] Handle of logical color palette */
-    COLORREF color)      /* [in] Color to be matched */
-{
-  PPALOBJ palObj = (PPALOBJ)AccessUserObject(hpalette);
-  UINT    index  = 0;
-
-  if( palObj )
-  {
-    int i, diff = 0x7fffffff;
-    int r,g,b;
-    PALETTEENTRY* entry = palObj->logpalette->palPalEntry;
-
-    for( i = 0; i < palObj->logpalette->palNumEntries && diff ; i++, entry++)
-    {
-      if (!(entry->peFlags & PC_SYS_USED)) continue;
-
-      r = entry->peRed - GetRValue(color);
-      g = entry->peGreen - GetGValue(color);
-      b = entry->peBlue - GetBValue(color);
-
-      r = r*r + g*g + b*b;
-
-      if( r < diff ) { index = i; diff = r; }
-    }
-//        GDI_ReleaseObj( hpalette );
-  }
-  DPRINT("(%04x,%06lx): returning %d\n", hpalette, color, index );
-  return index;
-}
-
-void PALETTE_ValidateFlags(PALETTEENTRY* lpPalE, int size)
+VOID FASTCALL PALETTE_ValidateFlags(PALETTEENTRY* lpPalE, INT size)
 {
   int i = 0;
   for( ; i<size ; i++ )
@@ -147,7 +178,7 @@ void PALETTE_ValidateFlags(PALETTEENTRY* lpPalE, int size)
 
 // Set the color-mapping table for selected palette. 
 // Return number of entries which mapping has changed.
-int PALETTE_SetMapping(PPALOBJ palPtr, UINT uStart, UINT uNum, BOOL mapOnly)
+INT STDCALL PALETTE_SetMapping(PPALOBJ palPtr, UINT uStart, UINT uNum, BOOL mapOnly)
 {
   char flag;
   int  prevMapping = (palPtr->mapping) ? 1 : 0;
@@ -249,138 +280,4 @@ int PALETTE_SetMapping(PPALOBJ palPtr, UINT uStart, UINT uNum, BOOL mapOnly)
   return iRemapped;
 }
 
-/* Return the physical color closest to 'color'. */
-/* Ported from WINE 20020804 (graphics\x11drv\palette.c) */
-int PALETTE_ToPhysical( PDC dc, COLORREF color )
-{
-    WORD            index = 0;
-    HPALETTE        hPal = (dc)? dc->w.hPalette: W32kGetStockObject(DEFAULT_PALETTE);
-    unsigned char   spec_type = color >> 24;
-    PPALOBJ         palPtr = (PPALOBJ)AccessUserObject(hPal);
-
-    /* palPtr can be NULL when DC is being destroyed */
-    if( !palPtr ) return 0;
-
-    if ( PALETTE_PaletteFlags & PALETTE_FIXED )
-    {
-        /* there is no colormap limitation; we are going to have to compute
-         * the pixel value from the visual information stored earlier
-        */
-
-       unsigned        long red, green, blue;
-       unsigned        idx = 0;
-
-       switch(spec_type)
-        {
-          case 1: /* PALETTEINDEX */
-
-            if( (idx = color & 0xffff) >= palPtr->logpalette->palNumEntries)
-            {
-                DPRINT("RGB(%lx) : idx %d is out of bounds, assuming black\n", color, idx);
-//             GDI_ReleaseObj( hPal );
-                return 0;
-            }
-
-            if( palPtr->mapping )
-           {
-                int ret = palPtr->mapping[idx];
-//             GDI_ReleaseObj( hPal );
-               return ret;
-           }
-           color = *(COLORREF*)(palPtr->logpalette->palPalEntry + idx);
-           break;
-
-         default:
-           color &= 0xffffff;
-           /* fall through to RGB */
-
-         case 0: /* RGB */
-           if( dc && (dc->w.bitsPerPixel == 1) )
-           {
-//             GDI_ReleaseObj( hPal );
-               return (((color >> 16) & 0xff) +
-                       ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? 1 : 0;
-           }
-
-       }
-
-        red = GetRValue(color); green = GetGValue(color); blue = GetBValue(color);
-
-       if (PALETTE_Graymax)
-        {
-           /* grayscale only; return scaled value */
-//         GDI_ReleaseObj( hPal );
-            return ( (red * 30 + green * 59 + blue * 11) * PALETTE_Graymax) / 25500;
-       }
-       else
-        {
-           /* scale each individually and construct the TrueColor pixel value */
-           if (PALETTE_PRed.scale < 8)
-               red = red >> (8-PALETTE_PRed.scale);
-           else if (PALETTE_PRed.scale > 8)
-               red =   red   << (PALETTE_PRed.scale-8) |
-                        red   >> (16-PALETTE_PRed.scale);
-           if (PALETTE_PGreen.scale < 8)
-               green = green >> (8-PALETTE_PGreen.scale);
-           else if (PALETTE_PGreen.scale > 8)
-               green = green << (PALETTE_PGreen.scale-8) |
-                        green >> (16-PALETTE_PGreen.scale);
-           if (PALETTE_PBlue.scale < 8)
-               blue =  blue  >> (8-PALETTE_PBlue.scale);
-           else if (PALETTE_PBlue.scale > 8)
-               blue =  blue  << (PALETTE_PBlue.scale-8) |
-                        blue  >> (16-PALETTE_PBlue.scale);
-
-//         GDI_ReleaseObj( hPal );
-            return (red << PALETTE_PRed.shift) | (green << PALETTE_PGreen.shift) | (blue << PALETTE_PBlue.shift);
-        }
-    }
-    else
-    {
-
-       if( !palPtr->mapping )
-            DPRINT("Palette %04x is not realized\n", dc->w.hPalette);
-
-       switch(spec_type)       /* we have to peruse DC and system palette */
-       {
-           default:
-               color &= 0xffffff;
-               /* fall through to RGB */
-
-                   case 0:  /* RGB */
-               if( dc && (dc->w.bitsPerPixel == 1) )
-               {
-//                 GDI_ReleaseObj( hPal );
-                   return (((color >> 16) & 0xff) +
-                           ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? 1 : 0;
-               }
-
-               index = SysPaletteLookupPixel( color, FALSE);
-
-/*                if (PALETTE_PaletteToXPixel) index = PALETTE_PaletteToXPixel[index]; */
-
-               /* DPRINT(palette,"RGB(%lx) -> pixel %i\n", color, index);
-                */
-               break;
-                   case 1:  /* PALETTEINDEX */
-               index = color & 0xffff;
-
-               if( index >= palPtr->logpalette->palNumEntries )
-                   DbgPrint("RGB(%lx) : index %i is out of bounds\n", color, index);
-               else if( palPtr->mapping ) index = palPtr->mapping[index];
-
-               /*  DPRINT(palette,"PALETTEINDEX(%04x) -> pixel %i\n", (WORD)color, index);
-                */
-               break;
-            case 2:  /* PALETTERGB */
-                index = GetNearestPaletteIndex( hPal, color );
-                if (palPtr->mapping) index = palPtr->mapping[index];
-               /* DPRINT(palette,"PALETTERGB(%lx) -> pixel %i\n", color, index);
-                */
-               break;
-       }
-    }
-
-//    GDI_ReleaseObj( hPal );
-    return index;
-}
+/* EOF */