Release 980601

Sun May 31 13:40:13 1998  Alexandre Julliard  <julliard@lrc.epfl.ch>

	* [if1632/signal.c]
	Added display of exception name.

	* [loader/task.c]
	Yet another attempt at fixing SwitchStackTo/SwitchStackBack.

	* [memory/selector.c] [relay32/builtin32.c] [tools/build.c]
	  [win32/kernel32.c]
	Generate an assembly stub for Win32 register functions to make
	their names available at link time.

	* [programs/*/Makefile.in]
	Added hacks to support old resource compiler.

Fri May 29 16:27:14 1998  Marcus Meissner <marcus@jet.franken.de>

	* [tools/testrun]
	Merge of my testscripts at home into one single perl program
	(tested with perl5). Works only on Linux due to 'ps' and 'ipcs'
	magic.

	* [controls/menu.c]
	Added some DefaultMenuItem stubs.

	* [debugger/stabs.c]
	Fixed stabs loading, now supports (int,int) typeinfo format used
	by gcc-2.8 and egcs-1. If it still crashes, please mail me.

	* [if1632/][relay32/]
	Added msvideo.dll (stubs only)
	Replaced some ptr by str for stringcases
	Added some new stubs (VxDCall, FindCloseNotif....)

	* [misc/network.c]
	Some argument fixes.

	* [misc/registry.c][misc/cpu.c]
	Registry initialization partially rewritten and enhanced.

	* [scheduler/*.c]
	Some additions so we don't do kill(0,SIGUSR1) (kill processgroup
	instead of targeted thread)
	Added SetThreadContext.

Thu May 28 23:59:59 1998  Bertho Stultiens <bertho@akhphd.au.dk>

	* [tools/wrc/*]
	New resource compiler version 1.0.0 (28-May-1998)

	* [Make.rules.in] [Makefile.in]
	Changed and added rc rules to point to tools/wrc/wrc.

	* [configure.in] [include/config.h.in]
	Added check for function 'stricmp'.

	* [include/resource.h]
	Commented out the old resource structure to catch references.
	It also includes wrc_rsc.h.

	* [include/wrc_rsc.h]
	New file. Definitions for the resources generated with wrc.

	* [include/windows.h]
	Added #ifdef RC_INVOKED to exclude stdarg.h.
	Added SS_NOTIFY flag.

	* [include/winnls.h]
	Added SUBLANG_* definitions and corrected some defaults.

	* [loader/libres.c]
	Changed the sysres load functions to support wrc generated
	resources.

	* [resource/sysres_*.rc]
	Added #include <windows.h>

	* [resource/sysres.c]
	Changed declarations to match wrc's output

	* [resource/Makefile.in]
	Changed rules to work with wrc.

	* [tools/makedep.c]
	Changed generation of .rc file dependencies to .s target.

Thu May 28 22:28:39 1998  Eric Kohl <ekohl@abo.rhein-zeitung.de>

	* [files/file.c][include/windows.c][relay32/kernel32.spec]
	Implemented GetFileAttributesEx32A/W.

	* [misc/imagelist.h][include/commctrl.h][relay32/comctl32.spec]
	Added ImageList_Read and ImageList_Write stubs.
	Added ImageList_AddIcon function.
	Added ImageList_LoadImage. It is the same as ImageList_LoadImage32A.

	* [controls/header.c]
	Fixed bitmap drawing bug.
	Added full bitmap support.

	* [include/commctrl.h]
	Added missing header macros.

	* [controls/toolbar.c][include/toolbar.h][include/commctrl.h]
	  [controls/commctrl.c] [relay32/comctl32.spec]
	First implementation of toolbar control.
	Implemented CreateToolbar, CreateToolbarEx and CreateMappedBitmap.

	* [controls/progress.c][controls/status.c]
	Some code cleanup.

	* [controls/commctrl.c][include/commctrl.h][relay32/comctl32.spec]
	Removed CreateStatusWindow16 and DrawStatusText16.
	CreateStatusWindow is the same as CreateStatusWindow32A.
	DrawStatusText is the same as DrawStatusText32A.

Thu May 28 16:01:28 1998  Matthew J. Francis  <asbel@dial.pipex.com>

	* [objects/bitmap.c] [objects/bitmap.h] [objects/oembitmap.c]
	  [objects/dc.c] [graphics/x11drv/bitblt.c]
	Added partial implementation of CreateDIBSection, with great thanks
	to Ulrich Weigand <weigand@informatik.uni-erlangen.de> for
	contributing the bulk of the patch.

Wed May 27 19:04:31 1998  Ulrich Weigand <weigand@informatik.uni-erlangen.de>

	* [win32/kernel32.c] [if1632/thunk.c] [include/flatthunk.h]
	ThunkConnect16 and related functions moved to emulator.

	* [loader/ne/segment.c]
	Call DllEntryPoint with correct arguments.

	* [relay32/builtin32.c]
	Bugfix: Relay debugging did not work for multiple processes.

	* [controls/menu.c]
	Bugfix: dwItemData was not set for MF_OWNERDRAW menus.

	* [if1632/relay.c] [relay32/relay386.c]
	Relay messages converted to use DPRINTF.

	* [controls/desktop.c] [relay32/user32.spec]
	Implemented PaintDesktop.

	* [files/profile.c] [if1632/kernel.spec] [misc/network.c]
	  [misc/printdrv.c] [relay32/winspool.spec] 
	  [win32/ordinals.c] [relay32/kernel32.spec]
	Some stubs added.

	* [relay32/mpr.spec]
	All ordinals were off by one.

Tue May 26 13:32:57 1998  Bill Hawes <whawes@star.net>

	* [misc/lstr.c] [include/casemap.h] [tools/unimap.pl]
	Added Unicode case conversion routines towupper/towlower,
	with mapping tables casemap.h created by tools/unimap.pl.

	* [misc/ntdll.c] [include/winnls.h] [relay32/ntdll.spec]
	  [relay32/advapi.spec]
	Minimal implementation of IsTextUnicode, just enough to get
	NT4 notepad to open ascii/unicode files.

	* [Make.rules.in] [resources/sysres_En.rc]
	Added include file dlgs.h for building resource files, so that
	resources can refer to defined values (e.g. pshHelp).

	* [misc/crtdll.c] [relay32/crtdll.spec]
	Use towupper/towlower for 32W case conversions.

	* [memory/string.c]
	Use towupper for 32W case conversions.

	* [ole/ole2nls.c]
	Use towupper for 32W case conversions; fix mem leak; minor cleanup

	* [controls/edit.c]
	Added soft break flag to edit state. Print unknown action values
	for WM_VSCROLL (action 190 occurs when running NT4 notepad.)

Mon May 25 22:42:40 1998  Uwe Bonnes <bon@elektron.ikp.physik.tu-darmstadt.de>

	* [files/file.c]
	Care for a pathological case in SetFilePointer.

	* [graphics/x11drv/xfont.c]
	Handle longer Font names in LFD_ComposeLFD and try to catch errors.

	* [loader/pe_image.c]
	Unload Dummymodule when PE_LoadLibraryEx32A fails with
	PE_LoadImage (makes Encarta 98 installer proceed).

	* [misc/registry.c]
	Move a check for a special case in RegCreateKeyEx32W after the
	check for existence.

Tue May 25 20:18:26 1998  Matthew Becker <mbecker@glasscity.net>

	* [misc/ntdll.c]
	Added some stubs, just guessing at the size of their param lists.

	* [misc/registry.c]
	Added stubs for RegUnLoadKey, RegSetKeySecurity, RegSaveKey,
	RegRestoreKey, and RegReplaceKey

	* [programs/regtest/regtest.c]
	Updated registry testing program.

Sun May 24 18:11:40 1998  Alex Priem <alexp@sci.kun.nl>

	* [file/profile.c]
	Added flag 'return_values' to PROFILE_GetSection.

Sun May 24 13:41:10 1998  James Juran <jrj120@psu.edu>

	* [misc/shell.c] [files/directory.c]
	Documentation/debugging info additions.

	* [*/*.c] [include/*.h]
	Moved many extern function definitions to appropriate header files.
	Cleaned up a few compile warnings.
	If #include "debug.h" is present, removed #include <stdio.h>.
	debug.h includes stdio.h, so it is not necessary to include both.

	* [graphics/*.c] [if1632/signal.c] [ipc/*.c] [scheduler/*.c]
	  [win32/*.c] [windows/*.c]
	Final patch to convert fprintf statements to new debugging interface.
	Some fprintfs are still left, especially in the debugger/ directory.
	However, IMHO, it's not worth the effort to change the rest.

Fri May 22 21:58:35 1998  Morten Welinder  <terra@diku.dk>

	* [windows/keyboard.c]
	Correct handling of keys "`-=[]\;',./".

Fri May 22 12:06:00 1998  Per Lindström <pelinstr@algonet.se>

	* [include/windows.h] [relay32/kernel32.spec] [win32/console.c]
	Added stub for ReadConsoleOutputCharacter32A.

Thu May 21 16:45:48 1998  Pascal Cuoq <pcuoq@ens-lyon.fr>

	* [ole/ole2nls.c]
	Began better implementation of LCMapString32A.
	Not very well tested yet, and still need improvements.

	* [controls/scroll.c]
	Documented functions.

Wed May 20 21:37:56 1998  Peter Hunnisett <hunnise@nortel.ca>

	* [include/windows.h][misc/main.c]
	Change SystemParameterInfo to support SPI_GETHIGHCONTRAST. Also
	include some missing SPI_ definitions.

	* [include/dsound.h][multimedia/dsound.c][relay32/dplayx.spec]
	Added stubs for DirectPlayLobbyCreate[AW]. Not sure if these
	should go into a new files dplayx.c? Anyone care?

	* [include/winnls.h]
	Added two missing flags for the CompareString32 functions.
diff --git a/objects/dib.c b/objects/dib.c
index a5ae6e3..e41edeb 100644
--- a/objects/dib.c
+++ b/objects/dib.c
@@ -4,7 +4,6 @@
  * Copyright 1993,1994  Alexandre Julliard
  */
 
-#include <stdio.h>
 #include <stdlib.h>
 #include "ts_xlib.h"
 #include "ts_xutil.h"
@@ -15,8 +14,6 @@
 #include "color.h"
 #include "debug.h"
 
-extern void CLIPPING_UpdateGCRegion(DC* );
-
 static int bitmapDepthTable[] = { 8, 1, 32, 16, 24, 15, 4, 0 };
 static int ximageDepthTable[] = { 0, 0, 0,  0,  0,  0,  0 };
 
@@ -30,8 +27,9 @@
     DWORD             infoWidth;
     WORD              depth;
     WORD              infoBpp;
-    const BITMAPINFO *info;
-    WORD              coloruse;
+    WORD              compression;
+    RGBQUAD          *colorMap;
+    int               nColorMap;
     Drawable          drawable;
     GC                gc;
     int               xSrc;
@@ -145,13 +143,14 @@
  * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
  */
 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, DWORD *width,
-                              int *height, WORD *bpp )
+                              int *height, WORD *bpp, WORD *compr )
 {
     if (header->biSize == sizeof(BITMAPINFOHEADER))
     {
         *width  = header->biWidth;
         *height = header->biHeight;
         *bpp    = header->biBitCount;
+        *compr  = header->biCompression;
         return 1;
     }
     if (header->biSize == sizeof(BITMAPCOREHEADER))
@@ -160,6 +159,7 @@
         *width  = core->bcWidth;
         *height = core->bcHeight;
         *bpp    = core->bcBitCount;
+        *compr  = 0;
         return 0;
     }
     WARN(bitmap, "(%ld): wrong size for header\n", header->biSize );
@@ -171,15 +171,15 @@
  *           DIB_BuildColorMap
  *
  * Build the color map from the bitmap palette. Should not be called
- * for a 24-bit deep bitmap.
+ * for a >8-bit deep bitmap.
  */
-static int *DIB_BuildColorMap( DC *dc, WORD coloruse, WORD depth,
-                               const BITMAPINFO *info )
+static RGBQUAD *DIB_BuildColorMap( const BITMAPINFO *info, 
+				   HPALETTE16 hPalette, int *nColors )
 {
     int i, colors;
     BOOL32 isInfo;
     WORD *colorPtr;
-    int *colorMapping;
+    RGBQUAD *colorMapping;
 
     if ((isInfo = (info->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))))
     {
@@ -192,45 +192,90 @@
         colors = 1 << ((BITMAPCOREHEADER *)&info->bmiHeader)->bcBitCount;
         colorPtr = (WORD *)((BITMAPCOREINFO *)info)->bmciColors;
     }
-    if (!(colorMapping = (int *)HeapAlloc(GetProcessHeap(), 0,
-                                          colors * sizeof(int) ))) return NULL;
 
-    if (coloruse == DIB_RGB_COLORS)
+    if (colors > 256)
+    {
+        ERR(bitmap, "DIB_BuildColorMap called with >256 colors!\n");
+        return NULL;
+    }
+
+    if (!(colorMapping = (RGBQUAD *)HeapAlloc(GetProcessHeap(), 0,
+                                              colors * sizeof(RGBQUAD) ))) 
+	return NULL;
+
+    if (hPalette == 0) /* DIB_RGB_COLORS */
     {
         if (isInfo)
         {
             RGBQUAD * rgb = (RGBQUAD *)colorPtr;
-        
-            if (depth == 1)  /* Monochrome */
-                for (i = 0; i < colors; i++, rgb++)
-                    colorMapping[i] = (rgb->rgbRed + rgb->rgbGreen + 
-                                       rgb->rgbBlue > 255*3/2);
-            else
-                for (i = 0; i < colors; i++, rgb++)
-                    colorMapping[i] = COLOR_ToPhysical( dc, RGB(rgb->rgbRed,
-                                                                rgb->rgbGreen,
-                                                                rgb->rgbBlue));
+
+            for (i = 0; i < colors; i++, rgb++)
+                colorMapping[i] = *rgb;
         }
         else
         {
             RGBTRIPLE * rgb = (RGBTRIPLE *)colorPtr;
         
-            if (depth == 1)  /* Monochrome */
-                for (i = 0; i < colors; i++, rgb++)
-                    colorMapping[i] = (rgb->rgbtRed + rgb->rgbtGreen + 
-                                       rgb->rgbtBlue > 255*3/2);
-            else
-                for (i = 0; i < colors; i++, rgb++)
-                    colorMapping[i] = COLOR_ToPhysical( dc, RGB(rgb->rgbtRed,
-                                                               rgb->rgbtGreen,
-                                                               rgb->rgbtBlue));
+            for (i = 0; i < colors; i++, rgb++)
+                colorMapping[i].rgbRed   = rgb->rgbtRed,
+                colorMapping[i].rgbGreen = rgb->rgbtGreen,
+                colorMapping[i].rgbBlue  = rgb->rgbtBlue;
         }
     }
     else  /* DIB_PAL_COLORS */
     {
+	PALETTEOBJ *palPtr = (PALETTEOBJ *)GDI_GetObjPtr(hPalette, PALETTE_MAGIC);
+	if (!palPtr) 
+	{
+	    HeapFree(GetProcessHeap(), 0, colorMapping);
+	    return NULL;
+	}
+
         for (i = 0; i < colors; i++, colorPtr++)
-            colorMapping[i] = COLOR_ToPhysical( dc, PALETTEINDEX(*colorPtr) );
+	    if (*colorPtr >= palPtr->logpalette.palNumEntries)
+	    {
+		colorMapping[i].rgbRed   = 0;
+		colorMapping[i].rgbGreen = 0;
+		colorMapping[i].rgbBlue  = 0;
+	    }
+	    else
+	    {
+		PALETTEENTRY *pe = palPtr->logpalette.palPalEntry + *colorPtr;
+		colorMapping[i].rgbRed   = pe->peRed;
+		colorMapping[i].rgbGreen = pe->peGreen;
+		colorMapping[i].rgbBlue  = pe->peBlue;
+	    }
+	
+	GDI_HEAP_UNLOCK(hPalette);
     }
+
+    *nColors = colors;
+    return colorMapping;
+}
+
+/***********************************************************************
+ *           DIB_PhysicalColorMap
+ *
+ * Translate color map to physical colors
+ */
+static int *DIB_PhysicalColorMap( DC *dc, int depth, 
+				  RGBQUAD *colorMap, int nColorMap)
+{
+    int *colorMapping, i;
+
+    if (!nColorMap) return NULL;
+    if (!(colorMapping = (int *)HeapAlloc(GetProcessHeap(), 0,
+                                          nColorMap * sizeof(int) ))) return NULL;
+
+    if (depth == 1) /* Monochrome */
+	for (i = 0; i < nColorMap; i++, colorMap++)
+	    colorMapping[i] = (colorMap->rgbRed + colorMap->rgbGreen + 
+                               colorMap->rgbBlue > 255*3/2);
+    else
+	for (i = 0; i < nColorMap; i++, colorMap++)
+            colorMapping[i] = COLOR_ToPhysical( dc, RGB(colorMap->rgbRed,
+                                                        colorMap->rgbGreen,
+                                                        colorMap->rgbBlue));
     return colorMapping;
 }
 
@@ -771,17 +816,13 @@
 {
     int *colorMapping;
     XImage *bmpImage;
-    DWORD compression = 0;
     int lines;
 
-    if (descr->info->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
-        compression = descr->info->bmiHeader.biCompression;
-
-      /* Build the color mapping table */
+      /* Translate the color mapping table */
 
     if (descr->infoBpp > 8) colorMapping = NULL;
-    else if (!(colorMapping = DIB_BuildColorMap( descr->dc, descr->coloruse,
-                                                 descr->depth, descr->info )))
+    else if (!(colorMapping = DIB_PhysicalColorMap( descr->dc, descr->depth,
+						    descr->colorMap, descr->nColorMap )))
         return 0;
 
     if( descr->dc->w.flags & DC_DIRTY ) CLIPPING_UpdateGCRegion(descr->dc);
@@ -798,14 +839,14 @@
 			    descr->width, descr->xSrc, colorMapping, bmpImage );
 	break;
     case 4:
-	if (compression) DIB_SetImageBits_RLE4( descr->lines, descr->bits,
+	if (descr->compression) DIB_SetImageBits_RLE4( descr->lines, descr->bits,
                                                 descr->infoWidth, descr->width, descr->xSrc,
                                                 colorMapping, bmpImage );
 	else DIB_SetImageBits_4( descr->lines, descr->bits, descr->infoWidth,
                                  descr->width, descr->xSrc, colorMapping, bmpImage );
 	break;
     case 8:
-	if (compression) DIB_SetImageBits_RLE8( descr->lines, descr->bits,
+	if (descr->compression) DIB_SetImageBits_RLE8( descr->lines, descr->bits,
                                                 descr->infoWidth, descr->width, descr->xSrc,
                                                 colorMapping, bmpImage );
 	else DIB_SetImageBits_8( descr->lines, descr->bits, descr->infoWidth,
@@ -925,7 +966,7 @@
         return 0;
     }
     if (DIB_GetBitmapInfo( &info->bmiHeader, &descr.infoWidth, &height,
-                           &descr.infoBpp ) == -1)
+                           &descr.infoBpp, &descr.compression ) == -1)
     {
         GDI_HEAP_UNLOCK( hbitmap );
         GDI_HEAP_UNLOCK( hdc );
@@ -941,11 +982,25 @@
     }
     if (startscan + lines > height) lines = height - startscan;
 
+    if (descr.infoBpp <= 8)
+    {
+        descr.colorMap = DIB_BuildColorMap( info, coloruse == DIB_PAL_COLORS?
+                                            descr.dc->w.hPalette : 0, 
+                                            &descr.nColorMap);
+
+        if (!descr.colorMap)
+        {
+            GDI_HEAP_UNLOCK( hbitmap );
+            GDI_HEAP_UNLOCK( hdc );
+            return 0;
+        } 
+    }
+
+
+
     descr.bits      = bits;
     descr.lines     = tmpheight >= 0 ? lines : -lines;
     descr.depth     = bmp->bitmap.bmBitsPixel;
-    descr.info      = info;
-    descr.coloruse  = coloruse;
     descr.drawable  = bmp->pixmap;
     descr.gc        = BITMAP_GC(bmp);
     descr.xSrc      = 0;
@@ -959,6 +1014,8 @@
     result = CALL_LARGE_STACK( DIB_SetImageBits, &descr );
     LeaveCriticalSection( &X11DRV_CritSection );
 
+    HeapFree(GetProcessHeap(), 0, descr.colorMap);
+
     GDI_HEAP_UNLOCK( hdc );
     GDI_HEAP_UNLOCK( hbitmap );
     return result;
@@ -1000,8 +1057,8 @@
 	dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
 	if (!dc) return 0;
     }
-    if (DIB_GetBitmapInfo( &info->bmiHeader, &width,
-                           &height, &descr.infoBpp ) == -1)
+    if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height, 
+			   &descr.infoBpp, &descr.compression ) == -1)
         return 0;
     tmpheight = height;
     if (height < 0) height = -height;
@@ -1017,13 +1074,21 @@
     DC_SetupGCForText( dc );  /* To have the correct colors */
     TSXSetFunction( display, dc->u.x.gc, DC_XROPfunction[dc->w.ROPmode-1] );
 
+    if (descr.infoBpp <= 8)
+    {
+        descr.colorMap = DIB_BuildColorMap( info, coloruse == DIB_PAL_COLORS?
+                                            descr.dc->w.hPalette : 0, 
+                                            &descr.nColorMap );
+
+        if (!descr.colorMap)
+            return 0;
+    }
+
     descr.dc        = dc;
     descr.bits      = bits;
     descr.lines     = tmpheight >= 0 ? lines : -lines;
     descr.infoWidth = width;
     descr.depth     = dc->w.bitsPerPixel;
-    descr.info      = info;
-    descr.coloruse  = coloruse;
     descr.drawable  = dc->u.x.drawable;
     descr.gc        = dc->u.x.gc;
     descr.xSrc      = xSrc;
@@ -1036,6 +1101,8 @@
     EnterCriticalSection( &X11DRV_CritSection );
     result = CALL_LARGE_STACK( DIB_SetImageBits, &descr );
     LeaveCriticalSection( &X11DRV_CritSection );
+
+    HeapFree(GetProcessHeap(), 0, descr.colorMap);
     return result;
 }
 
@@ -1161,7 +1228,7 @@
     PALETTEOBJ * palette;
     XImage * bmpImage;
     int i, x, y;
-        
+
     if (!lines) return 0;
     dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
     if (!dc) 
@@ -1193,7 +1260,7 @@
 	    else ((WORD *)info->bmiColors)[i] = (WORD)i;
 	}
     }
-    
+
     if (bits)
     {	
 	BYTE*	bbits = bits;
@@ -1367,8 +1434,9 @@
     DWORD width;
     int height;
     WORD bpp;
+    WORD compr;
 
-    if (DIB_GetBitmapInfo( header, &width, &height, &bpp ) == -1) return 0;
+    if (DIB_GetBitmapInfo( header, &width, &height, &bpp, &compr ) == -1) return 0;
     if (height < 0) height = -height;
 
     /* Check if we should create a monochrome or color bitmap. */
@@ -1415,7 +1483,7 @@
 
     /* Now create the bitmap */
 
-    handle = fColor ? CreateCompatibleBitmap32( hdc, width, height ) :
+    handle = fColor ? CreateBitmap32( width, height, 1, screenDepth, NULL ) :
                       CreateBitmap32( width, height, 1, 1, NULL );
     if (!handle) return 0;
 
@@ -1431,7 +1499,7 @@
 				     LPVOID **bits, HANDLE16 section,
 				     DWORD offset)
 {
-  return CreateDIBSection32 (hdc, bmi, usage, bits, section, offset);
+    return CreateDIBSection32(hdc, bmi, usage, bits, section, offset);
 }
 
 /***********************************************************************
@@ -1441,35 +1509,173 @@
 				     LPVOID **bits,HANDLE32 section,
 				     DWORD offset)
 {
-  HBITMAP32 res = 0;
+    HBITMAP32 res = 0;
+    BITMAPOBJ *bmp = NULL;
+    DIBSECTION *dib = NULL;
+    RGBQUAD *colorMap = NULL;
+    int nColorMap;
 
-  FIXME(bitmap,
-	  "(%d,[w=%ld,h=%ld],%d,%p,0x%08x,%ld),semistub\n",
-	  hdc,bmi->bmiHeader.biWidth,bmi->bmiHeader.biHeight,
-	  usage,bits,section,offset);
+    /* Fill BITMAP32 structure with DIB data */
+    BITMAPINFOHEADER *bi = &bmi->bmiHeader;
+    BITMAP32 bm;
 
-  if (bmi->bmiHeader.biHeight < 0 ) bmi->bmiHeader.biHeight = -bmi->bmiHeader.biHeight;
-  if (bmi->bmiHeader.biWidth < 0 ) bmi->bmiHeader.biWidth = -bmi->bmiHeader.biWidth;
-  /* FIXME.  The following line isn't quite right.  */
-  res = CreateDIBitmap32 (hdc, &bmi->bmiHeader, 0, NULL, bmi, 0);
-  if (res)
+    TRACE(bitmap, "format (%ld,%ld), planes %d, bpp %d, size %ld, colors %ld (%s)\n",
+	  bi->biWidth, bi->biHeight, bi->biPlanes, bi->biBitCount,
+	  bi->biSizeImage, bi->biClrUsed, usage == DIB_PAL_COLORS? "PAL" : "RGB");
+
+    if (bi->biCompression)
     {
-      BITMAP32 bmp;
-      if (GetObject32A (res, sizeof (bmp), &bmp))
-	{
-            /* FIXME: this is wrong! (bmBits is always NULL) */
-            if (bits) *bits = bmp.bmBits;
-	    /* hmpf */
-	    TRACE(bitmap,"allocating %ld bytes of memory\n",
-                bmi->bmiHeader.biWidth*bmi->bmiHeader.biHeight*4);
-	    if (bits) *bits = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
-                bmi->bmiHeader.biWidth*bmi->bmiHeader.biHeight*4);
-            return res;
-	}
+	FIXME(bitmap, "Compression not implemented!\n");
+	return 0;
     }
 
-  /* Error.  */
-  if (res) DeleteObject32 (res);
-  if (bits) *bits = NULL;
-  return 0;
+    bm.bmType = 0;
+    bm.bmWidth = bi->biWidth;
+    bm.bmHeight = bi->biHeight;
+    bm.bmWidthBytes = DIB_GetDIBWidthBytes(bm.bmWidth, bi->biBitCount);
+    bm.bmPlanes = bi->biPlanes;
+    bm.bmBitsPixel = bi->biBitCount;
+    bm.bmBits = NULL;
+
+    /* Get storage location for DIB bits */
+    if (section)
+	FIXME(bitmap, "section != NULL not implemented!\n");	
+    else
+    {
+	INT32 totalSize = bi->biSizeImage? bi->biSizeImage
+                                         : bm.bmWidthBytes * bm.bmHeight;
+
+	bm.bmBits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, totalSize);
+    }
+
+    /* Allocate Memory for DIB and fill structure */
+    if (bm.bmBits)
+	dib = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DIBSECTION));
+    if (dib)
+    {
+	dib->dsBm = bm;
+	dib->dsBmih = *bi;
+	/* FIXME: dib->dsBitfields ??? */
+	dib->dshSection = section;
+	dib->dsOffset = offset;
+    }
+
+    /* Create Color Map */
+    if (dib && bm.bmBitsPixel <= 8)
+    {
+        HPALETTE16 hPalette = 0;
+        if (usage == DIB_PAL_COLORS)
+        {
+            DC *dc = (DC *)GDI_GetObjPtr(hdc, DC_MAGIC);
+            if (!dc) 
+	        dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
+            if (dc)
+	    {
+		hPalette = dc->w.hPalette;
+		GDI_HEAP_UNLOCK(hdc);
+	    }
+	}
+	colorMap = DIB_BuildColorMap(bmi, hPalette, &nColorMap);
+    }
+
+    /* Create Device Dependent Bitmap and add DIB/ColorMap info */
+    if (dib) 
+    {
+       res = CreateDIBitmap32(hdc, bi, 0, NULL, bmi, usage);
+       bmp = (BITMAPOBJ *) GDI_GetObjPtr(res, BITMAP_MAGIC);
+       if (bmp)
+       {
+	    bmp->dibSection = dib;
+            bmp->colorMap   = colorMap;
+	    bmp->nColorMap  = nColorMap;
+	    GDI_HEAP_UNLOCK(res);
+       }
+    }
+
+    /* Clean up in case of errors */
+    if (!res || !bmp || !dib || !bm.bmBits || (bm.bmBitsPixel <= 8 && !colorMap))
+    {
+	if (bm.bmBits && section) 
+	    HeapFree(GetProcessHeap(), 0, bm.bmBits), bm.bmBits = NULL;
+
+	if (colorMap) HeapFree(GetProcessHeap(), 0, colorMap), colorMap = NULL;
+	if (dib) HeapFree(GetProcessHeap(), 0, dib), dib = NULL;
+	if (res) DeleteObject32(res), res = 0;
+    }
+
+    /* Return BITMAP handle and storage location */
+    if (bm.bmBits && bits) *bits = bm.bmBits;
+    return res;
 }
+
+/***********************************************************************
+ *           DIB_UpdateDIBSection
+ */
+void DIB_UpdateDIBSection( DC *dc, BOOL32 toDIB )
+{
+    BITMAPOBJ *bmp;
+    DIBSECTION *dib;
+
+    /* Ensure this is a Compatible DC that has a DIB section selected */
+
+    if (!dc) return;
+    if (!(dc->w.flags & DC_MEMORY)) return;
+
+    bmp = (BITMAPOBJ *)GDI_GetObjPtr( dc->w.hBitmap, BITMAP_MAGIC );
+    if (!bmp) return;
+
+    dib = bmp->dibSection;
+    if (!dib)
+    {
+	GDI_HEAP_UNLOCK(dc->w.hBitmap);
+	return;
+    }
+
+  
+    /* Copy Pixmap to bits */
+    if (toDIB)
+    {
+        /* Expect colour corruption if you uncomment this. -MF */
+//        BITMAPINFO bi;
+//        bi.bmiHeader = dib->dsBmih;
+//        bi.bmiHeader.biClrUsed = 0; /* don't copy palette */
+//        TRACE(bitmap, "Copying from Pixmap to DIB bits\n");
+//        GetDIBits32( dc->hSelf, dc->w.hBitmap, 0, dib->dsBmih.biHeight,
+//                     dib->dsBm.bmBits, &bi, 0 );
+    }
+
+    /* Copy bits to Pixmap */
+    else
+    {
+    	DIB_SETIMAGEBITS_DESCR descr;
+
+	TRACE(bitmap, "Copying from DIB bits to Pixmap\n"); 
+	if (DIB_GetBitmapInfo( &dib->dsBmih, &descr.infoWidth, &descr.lines,
+			       &descr.infoBpp, &descr.compression ) == -1)
+	{
+	    GDI_HEAP_UNLOCK(dc->w.hBitmap);
+	    return;
+	}
+
+        descr.dc        = dc;
+        descr.colorMap  = bmp->colorMap;
+        descr.nColorMap = bmp->nColorMap;
+        descr.bits      = dib->dsBm.bmBits;
+        descr.depth     = bmp->bitmap.bmBitsPixel;
+        descr.drawable  = bmp->pixmap;
+        descr.gc        = BITMAP_GC(bmp);
+        descr.xSrc      = 0;
+        descr.ySrc      = 0;
+        descr.xDest     = 0;
+        descr.yDest     = 0;
+        descr.width     = bmp->bitmap.bmWidth;
+        descr.height    = bmp->bitmap.bmHeight;
+
+        EnterCriticalSection( &X11DRV_CritSection );
+        CALL_LARGE_STACK( DIB_SetImageBits, &descr );
+        LeaveCriticalSection( &X11DRV_CritSection );
+    }
+
+    GDI_HEAP_UNLOCK(dc->w.hBitmap);
+}
+