From 8946a33c4b0d85ac1c8dd5129cfb85c932d73438 Mon Sep 17 00:00:00 2001 From: Alexandre Julliard Date: Tue, 10 Jan 2012 14:43:12 +0100 Subject: [PATCH] gdi32: Simplify CreateDIBSection and avoid using the reference DC if not needed. --- dlls/gdi32/dib.c | 54 +++++++++++++++--------------------------------------- 1 file changed, 15 insertions(+), 39 deletions(-) diff --git a/dlls/gdi32/dib.c b/dlls/gdi32/dib.c index 0f179725dda..3a8fadb45fe 100644 --- a/dlls/gdi32/dib.c +++ b/dlls/gdi32/dib.c @@ -1486,11 +1486,8 @@ HBITMAP WINAPI CreateDIBSection(HDC hdc, CONST BITMAPINFO *bmi, UINT usage, char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )]; BITMAPINFO *info = (BITMAPINFO *)buffer; HBITMAP ret = 0; - DC *dc; - BOOL bDesktopDC = FALSE; DIBSECTION *dib; BITMAPOBJ *bmp; - RGBQUAD *color_table = NULL; void *mapBits = NULL; if (bits) *bits = NULL; @@ -1502,7 +1499,8 @@ HBITMAP WINAPI CreateDIBSection(HDC hdc, CONST BITMAPINFO *bmi, UINT usage, WARN( "%u planes not properly supported\n", info->bmiHeader.biPlanes ); } - if (!(dib = HeapAlloc( GetProcessHeap(), 0, sizeof(*dib) ))) return 0; + if (!(bmp = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*bmp) ))) return 0; + if (!(dib = HeapAlloc( GetProcessHeap(), 0, sizeof(*dib) ))) goto error; TRACE("format (%d,%d), planes %d, bpp %d, %s, size %d %s\n", info->bmiHeader.biWidth, info->bmiHeader.biHeight, @@ -1519,14 +1517,17 @@ HBITMAP WINAPI CreateDIBSection(HDC hdc, CONST BITMAPINFO *bmi, UINT usage, dib->dsBm.bmBits = NULL; dib->dsBmih = info->bmiHeader; + bmp->funcs = &dib_driver; + bmp->dib = dib; + if (info->bmiHeader.biBitCount <= 8) /* build the color table */ { if (usage == DIB_PAL_COLORS && !fill_color_table_from_pal_colors( info, hdc )) goto error; dib->dsBmih.biClrUsed = info->bmiHeader.biClrUsed; - if (!(color_table = HeapAlloc( GetProcessHeap(), 0, dib->dsBmih.biClrUsed * sizeof(RGBQUAD) ))) + if (!(bmp->color_table = HeapAlloc( GetProcessHeap(), 0, dib->dsBmih.biClrUsed*sizeof(RGBQUAD) ))) goto error; - memcpy( color_table, info->bmiColors, dib->dsBmih.biClrUsed * sizeof(RGBQUAD) ); + memcpy( bmp->color_table, info->bmiColors, dib->dsBmih.biClrUsed * sizeof(RGBQUAD) ); } /* set dsBitfields values */ @@ -1572,46 +1573,21 @@ HBITMAP WINAPI CreateDIBSection(HDC hdc, CONST BITMAPINFO *bmi, UINT usage, if (!dib->dsBm.bmBits) goto error; - /* If the reference hdc is null, take the desktop dc */ - if (hdc == 0) - { - hdc = CreateCompatibleDC(0); - bDesktopDC = TRUE; - } - - if (!(dc = get_dc_ptr( hdc ))) goto error; - - /* create Device Dependent Bitmap and add DIB pointer */ - ret = CreateBitmap( dib->dsBm.bmWidth, dib->dsBm.bmHeight, 1, - (info->bmiHeader.biBitCount == 1) ? 1 : GetDeviceCaps(hdc, BITSPIXEL), NULL ); + bmp->bitmap = dib->dsBm; + bmp->bitmap.bmWidthBytes = get_bitmap_stride( info->bmiHeader.biWidth, info->bmiHeader.biBitCount ); + bmp->bitmap.bmBits = NULL; - if (ret && ((bmp = GDI_GetObjPtr(ret, OBJ_BITMAP)))) - { - PHYSDEV physdev = GET_DC_PHYSDEV( dc, pCreateDIBSection ); - bmp->dib = dib; - bmp->header.funcs = &dib_funcs; - bmp->funcs = physdev->funcs; - bmp->color_table = color_table; - GDI_ReleaseObj( ret ); - - if (!physdev->funcs->pCreateDIBSection( physdev, ret, info, usage )) - { - DeleteObject( ret ); - ret = 0; - } - } + if (!(ret = alloc_gdi_handle( &bmp->header, OBJ_BITMAP, &dib_funcs ))) goto error; - release_dc_ptr( dc ); - if (bDesktopDC) DeleteDC( hdc ); - if (ret && bits) *bits = dib->dsBm.bmBits; + if (bits) *bits = dib->dsBm.bmBits; return ret; error: - if (bDesktopDC) DeleteDC( hdc ); if (section) UnmapViewOfFile( mapBits ); - else if (!offset) VirtualFree( dib->dsBm.bmBits, 0, MEM_RELEASE ); - HeapFree( GetProcessHeap(), 0, color_table ); + else VirtualFree( dib->dsBm.bmBits, 0, MEM_RELEASE ); + HeapFree( GetProcessHeap(), 0, bmp->color_table ); HeapFree( GetProcessHeap(), 0, dib ); + HeapFree( GetProcessHeap(), 0, bmp ); return 0; } -- 2.11.4.GIT