bitmap.c 21.2 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4
/*
 * GDI bitmap objects
 *
 * Copyright 1993 Alexandre Julliard
5
 *           1998 Huw D M Davies
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard's avatar
Alexandre Julliard committed
20
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
21

22
#include <stdarg.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
23
#include <stdlib.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
24
#include <string.h>
25

26 27 28
#include "windef.h"
#include "winbase.h"
#include "wingdi.h"
29
#include "gdi_private.h"
30
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
31

32
WINE_DEFAULT_DEBUG_CHANNEL(bitmap);
33

Alexandre Julliard's avatar
Alexandre Julliard committed
34

35
static HGDIOBJ BITMAP_SelectObject( HGDIOBJ handle, HDC hdc );
36
static INT BITMAP_GetObject( HGDIOBJ handle, INT count, LPVOID buffer );
37
static BOOL BITMAP_DeleteObject( HGDIOBJ handle );
38 39 40 41 42 43 44 45 46 47

static const struct gdi_obj_funcs bitmap_funcs =
{
    BITMAP_SelectObject,  /* pSelectObject */
    BITMAP_GetObject,     /* pGetObjectA */
    BITMAP_GetObject,     /* pGetObjectW */
    NULL,                 /* pUnrealizeObject */
    BITMAP_DeleteObject   /* pDeleteObject */
};

Alexandre Julliard's avatar
Alexandre Julliard committed
48
/***********************************************************************
Huw D M Davies's avatar
Huw D M Davies committed
49
 *           BITMAP_GetWidthBytes
Alexandre Julliard's avatar
Alexandre Julliard committed
50
 *
Huw D M Davies's avatar
Huw D M Davies committed
51 52
 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
 * data.
Alexandre Julliard's avatar
Alexandre Julliard committed
53
 */
54
INT BITMAP_GetWidthBytes( INT bmWidth, INT bpp )
Alexandre Julliard's avatar
Alexandre Julliard committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
{
    switch(bpp)
    {
    case 1:
	return 2 * ((bmWidth+15) >> 4);

    case 24:
	bmWidth *= 3; /* fall through */
    case 8:
	return bmWidth + (bmWidth & 1);

    case 32:
	return bmWidth * 4;

    case 16:
    case 15:
	return bmWidth * 2;

    case 4:
	return 2 * ((bmWidth+3) >> 2);

    default:
77
	WARN("Unknown depth %d, please report.\n", bpp );
Alexandre Julliard's avatar
Alexandre Julliard committed
78 79 80
    }
    return -1;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
81

Alexandre Julliard's avatar
Alexandre Julliard committed
82

Alexandre Julliard's avatar
Alexandre Julliard committed
83
/******************************************************************************
Jon Griffiths's avatar
Jon Griffiths committed
84 85 86
 * CreateBitmap [GDI32.@]
 *
 * Creates a bitmap with the specified info.
Alexandre Julliard's avatar
Alexandre Julliard committed
87 88 89 90 91 92 93 94 95 96
 *
 * PARAMS
 *    width  [I] bitmap width
 *    height [I] bitmap height
 *    planes [I] Number of color planes
 *    bpp    [I] Number of bits to identify a color
 *    bits   [I] Pointer to array containing color data
 *
 * RETURNS
 *    Success: Handle to bitmap
97
 *    Failure: 0
Alexandre Julliard's avatar
Alexandre Julliard committed
98
 */
99
HBITMAP WINAPI CreateBitmap( INT width, INT height, UINT planes,
100
                             UINT bpp, LPCVOID bits )
Alexandre Julliard's avatar
Alexandre Julliard committed
101
{
102
    BITMAP bm;
Alexandre Julliard's avatar
Alexandre Julliard committed
103

104 105 106 107 108 109 110
    bm.bmType = 0;
    bm.bmWidth = width;
    bm.bmHeight = height;
    bm.bmWidthBytes = BITMAP_GetWidthBytes( width, bpp );
    bm.bmPlanes = planes;
    bm.bmBitsPixel = bpp;
    bm.bmBits = (LPVOID)bits;
Alexandre Julliard's avatar
Alexandre Julliard committed
111

112
    return CreateBitmapIndirect( &bm );
Alexandre Julliard's avatar
Alexandre Julliard committed
113 114
}

Alexandre Julliard's avatar
Alexandre Julliard committed
115
/******************************************************************************
Jon Griffiths's avatar
Jon Griffiths committed
116 117 118
 * CreateCompatibleBitmap [GDI32.@]
 *
 * Creates a bitmap compatible with the DC.
Alexandre Julliard's avatar
Alexandre Julliard committed
119 120 121 122 123 124 125 126
 *
 * PARAMS
 *    hdc    [I] Handle to device context
 *    width  [I] Width of bitmap
 *    height [I] Height of bitmap
 *
 * RETURNS
 *    Success: Handle to bitmap
127
 *    Failure: 0
Alexandre Julliard's avatar
Alexandre Julliard committed
128
 */
129
HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
Alexandre Julliard's avatar
Alexandre Julliard committed
130
{
131
    HBITMAP hbmpRet = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
132

133
    TRACE("(%p,%d,%d) =\n", hdc, width, height);
134

135
    if (GetObjectType( hdc ) != OBJ_MEMDC)
136
    {
137 138 139 140
        hbmpRet = CreateBitmap(width, height,
                               GetDeviceCaps(hdc, PLANES),
                               GetDeviceCaps(hdc, BITSPIXEL),
                               NULL);
141
    }
142
    else  /* Memory DC */
143
    {
144 145 146 147 148
        DIBSECTION dib;
        HBITMAP bitmap = GetCurrentObject( hdc, OBJ_BITMAP );
        INT size = GetObjectW( bitmap, sizeof(dib), &dib );

        if (!size) return 0;
149

150
        if (size == sizeof(BITMAP))
151
        {
152
            /* A device-dependent bitmap is selected in the DC */
153
            hbmpRet = CreateBitmap(width, height,
154 155
                                   dib.dsBm.bmPlanes,
                                   dib.dsBm.bmBitsPixel,
156
                                   NULL);
157
        }
158
        else
159
        {
160 161 162
            /* A DIB section is selected in the DC */
            BITMAPINFO *bi;
            void *bits;
163

164 165 166 167 168
            /* Allocate memory for a BITMAPINFOHEADER structure and a
               color table. The maximum number of colors in a color table
               is 256 which corresponds to a bitmap with depth 8.
               Bitmaps with higher depths don't have color tables. */
            bi = HeapAlloc(GetProcessHeap(), 0, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
169

170 171 172 173 174
            if (bi)
            {
                bi->bmiHeader.biSize          = sizeof(bi->bmiHeader);
                bi->bmiHeader.biWidth         = width;
                bi->bmiHeader.biHeight        = height;
175 176 177
                bi->bmiHeader.biPlanes        = dib.dsBmih.biPlanes;
                bi->bmiHeader.biBitCount      = dib.dsBmih.biBitCount;
                bi->bmiHeader.biCompression   = dib.dsBmih.biCompression;
178
                bi->bmiHeader.biSizeImage     = 0;
179 180 181 182
                bi->bmiHeader.biXPelsPerMeter = dib.dsBmih.biXPelsPerMeter;
                bi->bmiHeader.biYPelsPerMeter = dib.dsBmih.biYPelsPerMeter;
                bi->bmiHeader.biClrUsed       = dib.dsBmih.biClrUsed;
                bi->bmiHeader.biClrImportant  = dib.dsBmih.biClrImportant;
183 184 185 186

                if (bi->bmiHeader.biCompression == BI_BITFIELDS)
                {
                    /* Copy the color masks */
187
                    CopyMemory(bi->bmiColors, dib.dsBitfields, 3 * sizeof(DWORD));
188 189
                }
                else if (bi->bmiHeader.biBitCount <= 8)
190
                {
191 192
                    /* Copy the color table */
                    GetDIBColorTable(hdc, 0, 256, bi->bmiColors);
193
                }
194 195 196

                hbmpRet = CreateDIBSection(hdc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
                HeapFree(GetProcessHeap(), 0, bi);
197
            }
198
        }
199
    }
200

201
    TRACE("\t\t%p\n", hbmpRet);
Alexandre Julliard's avatar
Alexandre Julliard committed
202
    return hbmpRet;
Alexandre Julliard's avatar
Alexandre Julliard committed
203 204 205
}


Alexandre Julliard's avatar
Alexandre Julliard committed
206
/******************************************************************************
Jon Griffiths's avatar
Jon Griffiths committed
207 208
 * CreateBitmapIndirect [GDI32.@]
 *
209 210 211 212
 * Creates a bitmap with the specified info.
 *
 * PARAMS
 *  bmp [I] Pointer to the bitmap info describing the bitmap
Alexandre Julliard's avatar
Alexandre Julliard committed
213 214 215
 *
 * RETURNS
 *    Success: Handle to bitmap
216 217 218 219
 *    Failure: NULL. Use GetLastError() to determine the cause.
 *
 * NOTES
 *  If a width or height of 0 are given, a 1x1 monochrome bitmap is returned.
Alexandre Julliard's avatar
Alexandre Julliard committed
220
 */
221
HBITMAP WINAPI CreateBitmapIndirect( const BITMAP *bmp )
Alexandre Julliard's avatar
Alexandre Julliard committed
222
{
223 224 225 226
    BITMAP bm;
    BITMAPOBJ *bmpobj;
    HBITMAP hbitmap;

227
    if (!bmp || bmp->bmType)
228 229 230 231 232
    {
        SetLastError( ERROR_INVALID_PARAMETER );
        return NULL;
    }

233 234 235 236 237 238
    if (bmp->bmWidth > 0x7ffffff || bmp->bmHeight > 0x7ffffff)
    {
        SetLastError( ERROR_INVALID_PARAMETER );
        return 0;
    }

239 240 241 242
    bm = *bmp;

    if (!bm.bmWidth || !bm.bmHeight)
    {
243
        return GetStockObject( DEFAULT_BITMAP );
244 245 246 247 248 249 250 251 252
    }
    else
    {
        if (bm.bmHeight < 0)
            bm.bmHeight = -bm.bmHeight;
        if (bm.bmWidth < 0)
            bm.bmWidth = -bm.bmWidth;
    }

253 254 255 256 257 258 259
    if (bm.bmPlanes != 1)
    {
        FIXME("planes = %d\n", bm.bmPlanes);
        SetLastError( ERROR_INVALID_PARAMETER );
        return NULL;
    }

260 261 262 263 264 265 266 267 268 269 270 271 272
    /* Windows only uses 1, 4, 8, 16, 24 and 32 bpp */
    if(bm.bmBitsPixel == 1)         bm.bmBitsPixel = 1;
    else if(bm.bmBitsPixel <= 4)    bm.bmBitsPixel = 4;
    else if(bm.bmBitsPixel <= 8)    bm.bmBitsPixel = 8;
    else if(bm.bmBitsPixel <= 16)   bm.bmBitsPixel = 16;
    else if(bm.bmBitsPixel <= 24)   bm.bmBitsPixel = 24;
    else if(bm.bmBitsPixel <= 32)   bm.bmBitsPixel = 32;
    else {
        WARN("Invalid bmBitsPixel %d, returning ERROR_INVALID_PARAMETER\n", bm.bmBitsPixel);
        SetLastError(ERROR_INVALID_PARAMETER);
        return NULL;
    }

273 274
    /* Windows ignores the provided bm.bmWidthBytes */
    bm.bmWidthBytes = BITMAP_GetWidthBytes( bm.bmWidth, bm.bmBitsPixel );
275
    /* XP doesn't allow to create bitmaps larger than 128 Mb */
276
    if (bm.bmHeight > 128 * 1024 * 1024 / bm.bmWidthBytes)
277 278 279 280
    {
        SetLastError( ERROR_NOT_ENOUGH_MEMORY );
        return 0;
    }
281

282 283
    /* Create the BITMAPOBJ */
    if (!(bmpobj = HeapAlloc( GetProcessHeap(), 0, sizeof(*bmpobj) )))
284 285
    {
        SetLastError( ERROR_NOT_ENOUGH_MEMORY );
286
        return 0;
287 288 289 290 291 292 293 294
    }

    bmpobj->size.cx = 0;
    bmpobj->size.cy = 0;
    bmpobj->bitmap = bm;
    bmpobj->bitmap.bmBits = NULL;
    bmpobj->funcs = NULL;
    bmpobj->dib = NULL;
295 296
    bmpobj->color_table = NULL;
    bmpobj->nb_colors = 0;
297

298 299 300 301 302 303
    if (!(hbitmap = alloc_gdi_handle( &bmpobj->header, OBJ_BITMAP, &bitmap_funcs )))
    {
        HeapFree( GetProcessHeap(), 0, bmpobj );
        return 0;
    }

304 305 306
    if (bm.bmBits)
        SetBitmapBits( hbitmap, bm.bmHeight * bm.bmWidthBytes, bm.bmBits );

307 308 309
    TRACE("%dx%d, %d colors returning %p\n", bm.bmWidth, bm.bmHeight,
          1 << (bm.bmPlanes * bm.bmBitsPixel), hbitmap);

310
    return hbitmap;
Alexandre Julliard's avatar
Alexandre Julliard committed
311 312 313
}


Alexandre Julliard's avatar
Alexandre Julliard committed
314
/***********************************************************************
Jon Griffiths's avatar
Jon Griffiths committed
315 316 317
 * GetBitmapBits [GDI32.@]
 *
 * Copies bitmap bits of bitmap to buffer.
318
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
319 320 321
 * RETURNS
 *    Success: Number of bytes copied
 *    Failure: 0
Alexandre Julliard's avatar
Alexandre Julliard committed
322
 */
323 324
LONG WINAPI GetBitmapBits(
    HBITMAP hbitmap, /* [in]  Handle to bitmap */
Alexandre Julliard's avatar
Alexandre Julliard committed
325
    LONG count,        /* [in]  Number of bytes to copy */
326
    LPVOID bits)       /* [out] Pointer to buffer to receive bits */
Alexandre Julliard's avatar
Alexandre Julliard committed
327
{
328
    BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
329
    LONG height, ret;
330

331
    if (!bmp) return 0;
332

333 334 335 336
    if (bmp->dib)  /* simply copy the bits from the DIB */
    {
        DIBSECTION *dib = bmp->dib;
        const char *src = dib->dsBm.bmBits;
337
        INT width_bytes = BITMAP_GetWidthBytes(dib->dsBm.bmWidth, dib->dsBm.bmBitsPixel);
338
        LONG max = width_bytes * bmp->bitmap.bmHeight;
339 340 341 342 343 344 345

        if (!bits)
        {
            ret = max;
            goto done;
        }

346 347
        if (count > max) count = max;
        ret = count;
348 349

        /* GetBitmapBits returns not 32-bit aligned data */
350 351 352 353 354 355 356

        if (bmp->dib->dsBmih.biHeight >= 0)  /* not top-down, need to flip contents vertically */
        {
            src += dib->dsBm.bmWidthBytes * dib->dsBm.bmHeight;
            while (count > 0)
            {
                src -= dib->dsBm.bmWidthBytes;
357 358 359 360 361 362 363 364 365 366 367 368 369
                memcpy( bits, src, min( count, width_bytes ) );
                bits = (char *)bits + width_bytes;
                count -= width_bytes;
            }
        }
        else
        {
            while (count > 0)
            {
                memcpy( bits, src, min( count, width_bytes ) );
                src += dib->dsBm.bmWidthBytes;
                bits = (char *)bits + width_bytes;
                count -= width_bytes;
370 371 372 373 374
            }
        }
        goto done;
    }

375 376
    /* If the bits vector is null, the function should return the read size */
    if(bits == NULL)
377 378 379 380
    {
        ret = bmp->bitmap.bmWidthBytes * bmp->bitmap.bmHeight;
        goto done;
    }
381

Alexandre Julliard's avatar
Alexandre Julliard committed
382
    if (count < 0) {
383
	WARN("(%d): Negative number of bytes passed???\n", count );
Alexandre Julliard's avatar
Alexandre Julliard committed
384 385
	count = -count;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
386

387
    /* Only get entire lines */
Alexandre Julliard's avatar
Alexandre Julliard committed
388 389
    height = count / bmp->bitmap.bmWidthBytes;
    if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
390
    count = height * bmp->bitmap.bmWidthBytes;
391 392
    if (count == 0)
      {
393
	WARN("Less than one entire line requested\n");
394 395
        ret = 0;
        goto done;
396 397
      }

Alexandre Julliard's avatar
Alexandre Julliard committed
398

399
    TRACE("(%p, %d, %p) %dx%d %d colors fetched height: %d\n",
400 401
          hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
          1 << bmp->bitmap.bmBitsPixel, height );
Alexandre Julliard's avatar
Alexandre Julliard committed
402

403
    if(bmp->funcs && bmp->funcs->pGetBitmapBits)
404
    {
405
        TRACE("Calling device specific BitmapBits\n");
406
        ret = bmp->funcs->pGetBitmapBits(hbitmap, bits, count);
407
    } else {
Alexandre Julliard's avatar
Alexandre Julliard committed
408

409
        if(!bmp->bitmap.bmBits) {
410 411 412
	    TRACE("Bitmap is empty\n");
	    memset(bits, 0, count);
	    ret = count;
413 414 415
	} else {
	    memcpy(bits, bmp->bitmap.bmBits, count);
	    ret = count;
Alexandre Julliard's avatar
Alexandre Julliard committed
416
	}
Alexandre Julliard's avatar
Alexandre Julliard committed
417

Alexandre Julliard's avatar
Alexandre Julliard committed
418
    }
419 420
 done:
    GDI_ReleaseObj( hbitmap );
421
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
422 423 424
}


Alexandre Julliard's avatar
Alexandre Julliard committed
425
/******************************************************************************
Jon Griffiths's avatar
Jon Griffiths committed
426 427 428
 * SetBitmapBits [GDI32.@]
 *
 * Sets bits of color data for a bitmap.
Alexandre Julliard's avatar
Alexandre Julliard committed
429 430 431 432
 *
 * RETURNS
 *    Success: Number of bytes used in setting the bitmap bits
 *    Failure: 0
Alexandre Julliard's avatar
Alexandre Julliard committed
433
 */
434 435
LONG WINAPI SetBitmapBits(
    HBITMAP hbitmap, /* [in] Handle to bitmap */
Alexandre Julliard's avatar
Alexandre Julliard committed
436
    LONG count,        /* [in] Number of bytes in bitmap array */
437
    LPCVOID bits)      /* [in] Address of array with bitmap bits */
Alexandre Julliard's avatar
Alexandre Julliard committed
438
{
439
    BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
440
    LONG height, ret;
441

442 443
    if ((!bmp) || (!bits))
	return 0;
444

Alexandre Julliard's avatar
Alexandre Julliard committed
445
    if (count < 0) {
446
	WARN("(%d): Negative number of bytes passed???\n", count );
Alexandre Julliard's avatar
Alexandre Julliard committed
447 448
	count = -count;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
449

450 451 452 453
    if (bmp->dib)  /* simply copy the bits into the DIB */
    {
        DIBSECTION *dib = bmp->dib;
        char *dest = dib->dsBm.bmBits;
454
        LONG max = dib->dsBm.bmWidthBytes * dib->dsBm.bmHeight;
455 456 457 458 459 460 461 462 463 464
        if (count > max) count = max;
        ret = count;

        if (bmp->dib->dsBmih.biHeight >= 0)  /* not top-down, need to flip contents vertically */
        {
            dest += dib->dsBm.bmWidthBytes * dib->dsBm.bmHeight;
            while (count > 0)
            {
                dest -= dib->dsBm.bmWidthBytes;
                memcpy( dest, bits, min( count, dib->dsBm.bmWidthBytes ) );
465
                bits = (const char *)bits + dib->dsBm.bmWidthBytes;
466 467 468 469 470 471 472 473 474
                count -= dib->dsBm.bmWidthBytes;
            }
        }
        else memcpy( dest, bits, count );

        GDI_ReleaseObj( hbitmap );
        return ret;
    }

475
    /* Only get entire lines */
Alexandre Julliard's avatar
Alexandre Julliard committed
476 477
    height = count / bmp->bitmap.bmWidthBytes;
    if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
478
    count = height * bmp->bitmap.bmWidthBytes;
Alexandre Julliard's avatar
Alexandre Julliard committed
479

480
    TRACE("(%p, %d, %p) %dx%d %d colors fetched height: %d\n",
481 482
          hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
          1 << bmp->bitmap.bmBitsPixel, height );
Alexandre Julliard's avatar
Alexandre Julliard committed
483

484
    if(bmp->funcs && bmp->funcs->pSetBitmapBits) {
Alexandre Julliard's avatar
Alexandre Julliard committed
485

486
        TRACE("Calling device specific BitmapBits\n");
487
        ret = bmp->funcs->pSetBitmapBits(hbitmap, bits, count);
488
    } else {
Alexandre Julliard's avatar
Alexandre Julliard committed
489

490 491 492
        if(!bmp->bitmap.bmBits) /* Alloc enough for entire bitmap */
	    bmp->bitmap.bmBits = HeapAlloc( GetProcessHeap(), 0, count );
	if(!bmp->bitmap.bmBits) {
493
	    WARN("Unable to allocate bit buffer\n");
494 495 496 497 498
	    ret = 0;
	} else {
	    memcpy(bmp->bitmap.bmBits, bits, count);
	    ret = count;
	}
Alexandre Julliard's avatar
Alexandre Julliard committed
499 500
    }

501
    GDI_ReleaseObj( hbitmap );
502
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
503 504
}

Alexandre Julliard's avatar
Alexandre Julliard committed
505
/**********************************************************************
506
 *		BITMAP_CopyBitmap
Alexandre Julliard's avatar
Alexandre Julliard committed
507
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
508
 */
509
HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
Alexandre Julliard's avatar
Alexandre Julliard committed
510
{
511 512
    HBITMAP res = 0;
    BITMAP bm;
Alexandre Julliard's avatar
Alexandre Julliard committed
513

514
    if (!GetObjectW( hbitmap, sizeof(bm), &bm )) return 0;
515
    res = CreateBitmapIndirect(&bm);
516 517 518 519

    if(res) {
        char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
			       bm.bmHeight );
520 521
        GetBitmapBits (hbitmap, bm.bmWidthBytes * bm.bmHeight, buf);
	SetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
522
	HeapFree( GetProcessHeap(), 0, buf );
Alexandre Julliard's avatar
Alexandre Julliard committed
523 524 525 526
    }
    return res;
}

527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542

/***********************************************************************
 *           BITMAP_SetOwnerDC
 *
 * Set the type of DC that owns the bitmap. This is used when the
 * bitmap is selected into a device to initialize the bitmap function
 * table.
 */
BOOL BITMAP_SetOwnerDC( HBITMAP hbitmap, DC *dc )
{
    BITMAPOBJ *bitmap;
    BOOL ret;

    /* never set the owner of the stock bitmap since it can be selected in multiple DCs */
    if (hbitmap == GetStockObject(DEFAULT_BITMAP)) return TRUE;

543
    if (!(bitmap = GDI_GetObjPtr( hbitmap, OBJ_BITMAP ))) return FALSE;
544 545 546 547

    ret = TRUE;
    if (!bitmap->funcs)  /* not owned by a DC yet */
    {
548 549
        if (dc->funcs->pCreateBitmap) ret = dc->funcs->pCreateBitmap( dc->physDev, hbitmap,
                                                                      bitmap->bitmap.bmBits );
550 551 552 553
        if (ret) bitmap->funcs = dc->funcs;
    }
    else if (bitmap->funcs != dc->funcs)
    {
554
        FIXME( "Trying to select bitmap %p in different DC type\n", hbitmap );
555 556 557 558 559 560 561 562 563 564
        ret = FALSE;
    }
    GDI_ReleaseObj( hbitmap );
    return ret;
}


/***********************************************************************
 *           BITMAP_SelectObject
 */
565
static HGDIOBJ BITMAP_SelectObject( HGDIOBJ handle, HDC hdc )
566
{
567
    HGDIOBJ ret;
568 569
    BITMAPOBJ *bitmap;
    DC *dc;
570

571
    if (!(dc = get_dc_ptr( hdc ))) return 0;
572 573 574 575 576 577

    if (GetObjectType( hdc ) != OBJ_MEMDC)
    {
        ret = 0;
        goto done;
    }
578
    ret = dc->hBitmap;
579 580
    if (handle == dc->hBitmap) goto done;  /* nothing to do */

581
    if (!(bitmap = GDI_GetObjPtr( handle, OBJ_BITMAP )))
582 583 584 585 586
    {
        ret = 0;
        goto done;
    }

587
    if (bitmap->header.selcount && (handle != GetStockObject(DEFAULT_BITMAP)))
588 589
    {
        WARN( "Bitmap already selected in another DC\n" );
590
        GDI_ReleaseObj( handle );
591 592
        ret = 0;
        goto done;
593 594 595 596
    }

    if (!bitmap->funcs && !BITMAP_SetOwnerDC( handle, dc ))
    {
597
        GDI_ReleaseObj( handle );
598 599
        ret = 0;
        goto done;
600 601
    }

602 603 604 605 606 607
    if (dc->funcs->pSelectBitmap && !dc->funcs->pSelectBitmap( dc->physDev, handle ))
    {
        GDI_ReleaseObj( handle );
        ret = 0;
    }
    else
608
    {
609
        dc->hBitmap = handle;
610
        GDI_inc_ref_count( handle );
611
        dc->dirty = 0;
612
        SetRectRgn( dc->hVisRgn, 0, 0, bitmap->bitmap.bmWidth, bitmap->bitmap.bmHeight);
613
        GDI_ReleaseObj( handle );
614
        DC_InitDC( dc );
615
        GDI_dec_ref_count( ret );
616
    }
617

618
 done:
619
    release_dc_ptr( dc );
620 621 622 623
    return ret;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
624
/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
625
 *           BITMAP_DeleteObject
Alexandre Julliard's avatar
Alexandre Julliard committed
626
 */
627
static BOOL BITMAP_DeleteObject( HGDIOBJ handle )
Alexandre Julliard's avatar
Alexandre Julliard committed
628
{
629
    const DC_FUNCTIONS *funcs;
630
    BITMAPOBJ *bmp = GDI_GetObjPtr( handle, OBJ_BITMAP );
631 632

    if (!bmp) return FALSE;
633 634
    funcs = bmp->funcs;
    GDI_ReleaseObj( handle );
635

636 637 638
    if (funcs && funcs->pDeleteBitmap) funcs->pDeleteBitmap( handle );

    if (!(bmp = free_gdi_handle( handle ))) return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
639

640
    HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
Alexandre Julliard's avatar
Alexandre Julliard committed
641

642 643 644
    if (bmp->dib)
    {
        DIBSECTION *dib = bmp->dib;
Alexandre Julliard's avatar
Alexandre Julliard committed
645

646 647 648 649 650 651 652 653 654 655 656 657 658
        if (dib->dsBm.bmBits)
        {
            if (dib->dshSection)
            {
                SYSTEM_INFO SystemInfo;
                GetSystemInfo( &SystemInfo );
                UnmapViewOfFile( (char *)dib->dsBm.bmBits -
                                 (dib->dsOffset % SystemInfo.dwAllocationGranularity) );
            }
            else if (!dib->dsOffset)
                VirtualFree(dib->dsBm.bmBits, 0L, MEM_RELEASE );
        }
        HeapFree(GetProcessHeap(), 0, dib);
659
        HeapFree(GetProcessHeap(), 0, bmp->color_table);
660
    }
661
    return HeapFree( GetProcessHeap(), 0, bmp );
Alexandre Julliard's avatar
Alexandre Julliard committed
662 663
}

664

Alexandre Julliard's avatar
Alexandre Julliard committed
665
/***********************************************************************
666
 *           BITMAP_GetObject
Alexandre Julliard's avatar
Alexandre Julliard committed
667
 */
668
static INT BITMAP_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
Alexandre Julliard's avatar
Alexandre Julliard committed
669
{
670
    INT ret;
671
    BITMAPOBJ *bmp = GDI_GetObjPtr( handle, OBJ_BITMAP );
672

673
    if (!bmp) return 0;
674

675 676 677
    if (!buffer) ret = sizeof(BITMAP);
    else if (count < sizeof(BITMAP)) ret = 0;
    else if (bmp->dib)
Alexandre Julliard's avatar
Alexandre Julliard committed
678
    {
679
	if (count >= sizeof(DIBSECTION))
Alexandre Julliard's avatar
Alexandre Julliard committed
680
	{
681
            memcpy( buffer, bmp->dib, sizeof(DIBSECTION) );
682
            ret = sizeof(DIBSECTION);
Alexandre Julliard's avatar
Alexandre Julliard committed
683
	}
684 685 686 687
	else /* if (count >= sizeof(BITMAP)) */
        {
            DIBSECTION *dib = bmp->dib;
            memcpy( buffer, &dib->dsBm, sizeof(BITMAP) );
688
            ret = sizeof(BITMAP);
Alexandre Julliard's avatar
Alexandre Julliard committed
689 690 691 692
	}
    }
    else
    {
693
        memcpy( buffer, &bmp->bitmap, sizeof(BITMAP) );
694
        ((BITMAP *) buffer)->bmBits = NULL;
695
        ret = sizeof(BITMAP);
Alexandre Julliard's avatar
Alexandre Julliard committed
696
    }
697 698
    GDI_ReleaseObj( handle );
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
699
}
700

Alexandre Julliard's avatar
Alexandre Julliard committed
701

Alexandre Julliard's avatar
Alexandre Julliard committed
702
/******************************************************************************
Jon Griffiths's avatar
Jon Griffiths committed
703 704 705
 * CreateDiscardableBitmap [GDI32.@]
 *
 * Creates a discardable bitmap.
Alexandre Julliard's avatar
Alexandre Julliard committed
706 707 708 709
 *
 * RETURNS
 *    Success: Handle to bitmap
 *    Failure: NULL
Alexandre Julliard's avatar
Alexandre Julliard committed
710
 */
711 712 713 714
HBITMAP WINAPI CreateDiscardableBitmap(
    HDC hdc,    /* [in] Handle to device context */
    INT width,  /* [in] Bitmap width */
    INT height) /* [in] Bitmap height */
Alexandre Julliard's avatar
Alexandre Julliard committed
715
{
716
    return CreateCompatibleBitmap( hdc, width, height );
Alexandre Julliard's avatar
Alexandre Julliard committed
717
}
Alexandre Julliard's avatar
Alexandre Julliard committed
718

Alexandre Julliard's avatar
Alexandre Julliard committed
719

Alexandre Julliard's avatar
Alexandre Julliard committed
720
/******************************************************************************
Jon Griffiths's avatar
Jon Griffiths committed
721 722 723
 * GetBitmapDimensionEx [GDI32.@]
 *
 * Retrieves dimensions of a bitmap.
Alexandre Julliard's avatar
Alexandre Julliard committed
724 725 726 727
 *
 * RETURNS
 *    Success: TRUE
 *    Failure: FALSE
Alexandre Julliard's avatar
Alexandre Julliard committed
728
 */
729 730 731
BOOL WINAPI GetBitmapDimensionEx(
    HBITMAP hbitmap, /* [in]  Handle to bitmap */
    LPSIZE size)     /* [out] Address of struct receiving dimensions */
Alexandre Julliard's avatar
Alexandre Julliard committed
732
{
733
    BITMAPOBJ * bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
Alexandre Julliard's avatar
Alexandre Julliard committed
734
    if (!bmp) return FALSE;
735
    *size = bmp->size;
736
    GDI_ReleaseObj( hbitmap );
Alexandre Julliard's avatar
Alexandre Julliard committed
737 738 739 740
    return TRUE;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
741
/******************************************************************************
Jon Griffiths's avatar
Jon Griffiths committed
742 743 744
 * SetBitmapDimensionEx [GDI32.@]
 *
 * Assigns dimensions to a bitmap.
745 746
 * MSDN says that this function will fail if hbitmap is a handle created by
 * CreateDIBSection, but that's not true on Windows 2000.
Alexandre Julliard's avatar
Alexandre Julliard committed
747 748 749 750 751
 *
 * RETURNS
 *    Success: TRUE
 *    Failure: FALSE
 */
752 753 754 755 756
BOOL WINAPI SetBitmapDimensionEx(
    HBITMAP hbitmap, /* [in]  Handle to bitmap */
    INT x,           /* [in]  Bitmap width */
    INT y,           /* [in]  Bitmap height */
    LPSIZE prevSize) /* [out] Address of structure for orig dims */
Alexandre Julliard's avatar
Alexandre Julliard committed
757
{
758
    BITMAPOBJ * bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
Alexandre Julliard's avatar
Alexandre Julliard committed
759
    if (!bmp) return FALSE;
760 761 762
    if (prevSize) *prevSize = bmp->size;
    bmp->size.cx = x;
    bmp->size.cy = y;
763
    GDI_ReleaseObj( hbitmap );
Alexandre Julliard's avatar
Alexandre Julliard committed
764 765
    return TRUE;
}