bitmap.c 17.4 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 */
};

48

Alexandre Julliard's avatar
Alexandre Julliard committed
49
/******************************************************************************
Jon Griffiths's avatar
Jon Griffiths committed
50 51 52
 * CreateBitmap [GDI32.@]
 *
 * Creates a bitmap with the specified info.
Alexandre Julliard's avatar
Alexandre Julliard committed
53 54 55 56 57 58 59 60 61 62
 *
 * 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
63
 *    Failure: 0
Alexandre Julliard's avatar
Alexandre Julliard committed
64
 */
65
HBITMAP WINAPI CreateBitmap( INT width, INT height, UINT planes,
66
                             UINT bpp, LPCVOID bits )
Alexandre Julliard's avatar
Alexandre Julliard committed
67
{
68
    BITMAP bm;
Alexandre Julliard's avatar
Alexandre Julliard committed
69

70 71 72
    bm.bmType = 0;
    bm.bmWidth = width;
    bm.bmHeight = height;
73
    bm.bmWidthBytes = get_bitmap_stride( width, bpp );
74 75 76
    bm.bmPlanes = planes;
    bm.bmBitsPixel = bpp;
    bm.bmBits = (LPVOID)bits;
Alexandre Julliard's avatar
Alexandre Julliard committed
77

78
    return CreateBitmapIndirect( &bm );
Alexandre Julliard's avatar
Alexandre Julliard committed
79 80
}

Alexandre Julliard's avatar
Alexandre Julliard committed
81
/******************************************************************************
Jon Griffiths's avatar
Jon Griffiths committed
82 83 84
 * CreateCompatibleBitmap [GDI32.@]
 *
 * Creates a bitmap compatible with the DC.
Alexandre Julliard's avatar
Alexandre Julliard committed
85 86 87 88 89 90 91 92
 *
 * PARAMS
 *    hdc    [I] Handle to device context
 *    width  [I] Width of bitmap
 *    height [I] Height of bitmap
 *
 * RETURNS
 *    Success: Handle to bitmap
93
 *    Failure: 0
Alexandre Julliard's avatar
Alexandre Julliard committed
94
 */
95
HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
Alexandre Julliard's avatar
Alexandre Julliard committed
96
{
97 98 99
    char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
    BITMAPINFO *bi = (BITMAPINFO *)buffer;
    DIBSECTION dib;
Alexandre Julliard's avatar
Alexandre Julliard committed
100

101
    TRACE("(%p,%d,%d)\n", hdc, width, height);
102

103
    if (GetObjectType( hdc ) != OBJ_MEMDC)
104 105 106 107
        return CreateBitmap( width, height,
                             GetDeviceCaps(hdc, PLANES), GetDeviceCaps(hdc, BITSPIXEL), NULL );

    switch (GetObjectW( GetCurrentObject( hdc, OBJ_BITMAP ), sizeof(dib), &dib ))
108
    {
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    case sizeof(BITMAP): /* A device-dependent bitmap is selected in the DC */
        return CreateBitmap( width, height, dib.dsBm.bmPlanes, dib.dsBm.bmBitsPixel, NULL );

    case sizeof(DIBSECTION): /* A DIB section is selected in the DC */
        bi->bmiHeader = dib.dsBmih;
        bi->bmiHeader.biWidth  = width;
        bi->bmiHeader.biHeight = height;
        if (dib.dsBmih.biCompression == BI_BITFIELDS)  /* copy the color masks */
            memcpy(bi->bmiColors, dib.dsBitfields, sizeof(dib.dsBitfields));
        else if (dib.dsBmih.biBitCount <= 8)  /* copy the color table */
            GetDIBColorTable(hdc, 0, 256, bi->bmiColors);
        return CreateDIBSection( hdc, bi, DIB_RGB_COLORS, NULL, NULL, 0 );

    default:
        return 0;
124
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
125 126 127
}


Alexandre Julliard's avatar
Alexandre Julliard committed
128
/******************************************************************************
Jon Griffiths's avatar
Jon Griffiths committed
129 130
 * CreateBitmapIndirect [GDI32.@]
 *
131 132 133 134
 * 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
135 136 137
 *
 * RETURNS
 *    Success: Handle to bitmap
138 139 140
 *    Failure: NULL. Use GetLastError() to determine the cause.
 *
 * NOTES
141
 *  If a width or height of 0 is given, a 1x1 monochrome bitmap is returned.
Alexandre Julliard's avatar
Alexandre Julliard committed
142
 */
143
HBITMAP WINAPI CreateBitmapIndirect( const BITMAP *bmp )
Alexandre Julliard's avatar
Alexandre Julliard committed
144
{
145 146 147
    BITMAP bm;
    BITMAPOBJ *bmpobj;
    HBITMAP hbitmap;
148 149
    INT dib_stride;
    SIZE_T size;
150

151
    if (!bmp || bmp->bmType)
152 153 154 155 156
    {
        SetLastError( ERROR_INVALID_PARAMETER );
        return NULL;
    }

157 158 159 160 161 162
    if (bmp->bmWidth > 0x7ffffff || bmp->bmHeight > 0x7ffffff)
    {
        SetLastError( ERROR_INVALID_PARAMETER );
        return 0;
    }

163 164 165 166
    bm = *bmp;

    if (!bm.bmWidth || !bm.bmHeight)
    {
167
        return GetStockObject( DEFAULT_BITMAP );
168 169 170 171 172 173 174 175 176
    }
    else
    {
        if (bm.bmHeight < 0)
            bm.bmHeight = -bm.bmHeight;
        if (bm.bmWidth < 0)
            bm.bmWidth = -bm.bmWidth;
    }

177 178 179 180 181 182 183
    if (bm.bmPlanes != 1)
    {
        FIXME("planes = %d\n", bm.bmPlanes);
        SetLastError( ERROR_INVALID_PARAMETER );
        return NULL;
    }

184 185 186 187 188 189 190 191 192 193 194 195 196
    /* 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;
    }

197
    /* Windows ignores the provided bm.bmWidthBytes */
198
    bm.bmWidthBytes = get_bitmap_stride( bm.bmWidth, bm.bmBitsPixel );
199 200 201 202 203

    dib_stride = get_dib_stride( bm.bmWidth, bm.bmBitsPixel );
    size = dib_stride * bm.bmHeight;
    /* Check for overflow (dib_stride itself must be ok because of the constraint on bm.bmWidth above). */
    if (dib_stride != size / bm.bmHeight)
204
    {
205
        SetLastError( ERROR_INVALID_PARAMETER );
206 207
        return 0;
    }
208

209
    /* Create the BITMAPOBJ */
210
    if (!(bmpobj = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*bmpobj) )))
211 212
    {
        SetLastError( ERROR_NOT_ENOUGH_MEMORY );
213
        return 0;
214 215
    }

216
    bmpobj->dib.dsBm = bm;
217 218 219 220 221 222 223
    bmpobj->dib.dsBm.bmBits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size );
    if (!bmpobj->dib.dsBm.bmBits)
    {
        HeapFree( GetProcessHeap(), 0, bmpobj );
        SetLastError( ERROR_NOT_ENOUGH_MEMORY );
        return 0;
    }
224

225
    if (!(hbitmap = alloc_gdi_handle( bmpobj, OBJ_BITMAP, &bitmap_funcs )))
226
    {
227
        HeapFree( GetProcessHeap(), 0, bmpobj->dib.dsBm.bmBits );
228 229 230 231
        HeapFree( GetProcessHeap(), 0, bmpobj );
        return 0;
    }

232 233 234
    if (bm.bmBits)
        SetBitmapBits( hbitmap, bm.bmHeight * bm.bmWidthBytes, bm.bmBits );

235 236
    TRACE("%dx%d, bpp %d planes %d: returning %p\n", bm.bmWidth, bm.bmHeight,
          bm.bmBitsPixel, bm.bmPlanes, hbitmap);
237

238
    return hbitmap;
Alexandre Julliard's avatar
Alexandre Julliard committed
239 240 241
}


Alexandre Julliard's avatar
Alexandre Julliard committed
242
/***********************************************************************
Jon Griffiths's avatar
Jon Griffiths committed
243 244 245
 * GetBitmapBits [GDI32.@]
 *
 * Copies bitmap bits of bitmap to buffer.
246
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
247 248 249
 * RETURNS
 *    Success: Number of bytes copied
 *    Failure: 0
Alexandre Julliard's avatar
Alexandre Julliard committed
250
 */
251 252
LONG WINAPI GetBitmapBits(
    HBITMAP hbitmap, /* [in]  Handle to bitmap */
Alexandre Julliard's avatar
Alexandre Julliard committed
253
    LONG count,        /* [in]  Number of bytes to copy */
254
    LPVOID bits)       /* [out] Pointer to buffer to receive bits */
Alexandre Julliard's avatar
Alexandre Julliard committed
255
{
256 257 258 259 260
    char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
    BITMAPINFO *info = (BITMAPINFO *)buffer;
    struct gdi_image_bits src_bits;
    struct bitblt_coords src;
    int dst_stride, max, ret;
261
    BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
262

263
    if (!bmp) return 0;
264

265 266
    dst_stride = get_bitmap_stride( bmp->dib.dsBm.bmWidth, bmp->dib.dsBm.bmBitsPixel );
    ret = max = dst_stride * bmp->dib.dsBm.bmHeight;
267
    if (!bits) goto done;
268
    if (count < 0 || count > max) count = max;
269 270 271
    ret = count;

    src.visrect.left = 0;
272
    src.visrect.right = bmp->dib.dsBm.bmWidth;
273 274 275 276 277 278
    src.visrect.top = 0;
    src.visrect.bottom = (count + dst_stride - 1) / dst_stride;
    src.x = src.y = 0;
    src.width = src.visrect.right - src.visrect.left;
    src.height = src.visrect.bottom - src.visrect.top;

279
    if (!get_image_from_bitmap( bmp, info, &src_bits, &src ))
280 281
    {
        const char *src_ptr = src_bits.ptr;
282
        int src_stride = info->bmiHeader.biSizeImage / abs( info->bmiHeader.biHeight );
283

284
        /* GetBitmapBits returns 16-bit aligned data */
285

286
        if (info->bmiHeader.biHeight > 0)
287
        {
288 289
            src_ptr += (info->bmiHeader.biHeight - 1) * src_stride;
            src_stride = -src_stride;
290
        }
291 292 293 294
        src_ptr += src.visrect.top * src_stride;

        if (src_stride == dst_stride) memcpy( bits, src_ptr, count );
        else while (count > 0)
295
        {
296 297 298 299
            memcpy( bits, src_ptr, min( count, dst_stride ) );
            src_ptr += src_stride;
            bits = (char *)bits + dst_stride;
            count -= dst_stride;
300
        }
301
        if (src_bits.free) src_bits.free( &src_bits );
302
    }
303
    else ret = 0;
304

305
done:
306
    GDI_ReleaseObj( hbitmap );
307
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
308 309 310
}


Alexandre Julliard's avatar
Alexandre Julliard committed
311
/******************************************************************************
Jon Griffiths's avatar
Jon Griffiths committed
312 313 314
 * SetBitmapBits [GDI32.@]
 *
 * Sets bits of color data for a bitmap.
Alexandre Julliard's avatar
Alexandre Julliard committed
315 316 317 318
 *
 * RETURNS
 *    Success: Number of bytes used in setting the bitmap bits
 *    Failure: 0
Alexandre Julliard's avatar
Alexandre Julliard committed
319
 */
320 321
LONG WINAPI SetBitmapBits(
    HBITMAP hbitmap, /* [in] Handle to bitmap */
Alexandre Julliard's avatar
Alexandre Julliard committed
322
    LONG count,        /* [in] Number of bytes in bitmap array */
323
    LPCVOID bits)      /* [in] Address of array with bitmap bits */
Alexandre Julliard's avatar
Alexandre Julliard committed
324
{
325 326
    char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
    BITMAPINFO *info = (BITMAPINFO *)buffer;
327
    BITMAPOBJ *bmp;
328 329 330 331
    DWORD err;
    int i, src_stride, dst_stride;
    struct bitblt_coords src, dst;
    struct gdi_image_bits src_bits;
332
    HRGN clip = NULL;
333

334 335 336 337
    if (!bits) return 0;

    bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
    if (!bmp) return 0;
338

Alexandre Julliard's avatar
Alexandre Julliard committed
339
    if (count < 0) {
340
	WARN("(%d): Negative number of bytes passed???\n", count );
Alexandre Julliard's avatar
Alexandre Julliard committed
341 342
	count = -count;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
343

344 345
    src_stride = get_bitmap_stride( bmp->dib.dsBm.bmWidth, bmp->dib.dsBm.bmBitsPixel );
    count = min( count, src_stride * bmp->dib.dsBm.bmHeight );
346

347
    dst_stride = get_dib_stride( bmp->dib.dsBm.bmWidth, bmp->dib.dsBm.bmBitsPixel );
348 349 350

    src.visrect.left   = src.x = 0;
    src.visrect.top    = src.y = 0;
351
    src.visrect.right  = src.width = bmp->dib.dsBm.bmWidth;
352
    src.visrect.bottom = src.height = (count + src_stride - 1 ) / src_stride;
353 354
    dst = src;

355 356 357
    if (count % src_stride)
    {
        HRGN last_row;
358
        int extra_pixels = ((count % src_stride) << 3) / bmp->dib.dsBm.bmBitsPixel;
359

360
        if ((count % src_stride << 3) % bmp->dib.dsBm.bmBitsPixel)
361 362 363 364 365 366 367 368
            FIXME( "Unhandled partial pixel\n" );
        clip = CreateRectRgn( src.visrect.left, src.visrect.top,
                              src.visrect.right, src.visrect.bottom - 1 );
        last_row = CreateRectRgn( src.visrect.left, src.visrect.bottom - 1,
                                  src.visrect.left + extra_pixels, src.visrect.bottom );
        CombineRgn( clip, clip, last_row, RGN_OR );
        DeleteObject( last_row );
    }
369 370

    TRACE("(%p, %d, %p) %dx%d %d bpp fetched height: %d\n",
371 372
          hbitmap, count, bits, bmp->dib.dsBm.bmWidth, bmp->dib.dsBm.bmHeight,
          bmp->dib.dsBm.bmBitsPixel, src.height );
373

374 375 376 377 378 379 380 381 382
    if (src_stride == dst_stride)
    {
        src_bits.ptr = (void *)bits;
        src_bits.is_copy = FALSE;
        src_bits.free = NULL;
    }
    else
    {
        if (!(src_bits.ptr = HeapAlloc( GetProcessHeap(), 0, dst.height * dst_stride )))
383
        {
384 385
            GDI_ReleaseObj( hbitmap );
            return 0;
386
        }
387 388
        src_bits.is_copy = TRUE;
        src_bits.free = free_heap_bits;
389
        for (i = 0; i < count / src_stride; i++)
390
            memcpy( (char *)src_bits.ptr + i * dst_stride, (char *)bits + i * src_stride, src_stride );
391 392
        if (count % src_stride)
            memcpy( (char *)src_bits.ptr + i * dst_stride, (char *)bits + i * src_stride, count % src_stride );
393 394
    }

395 396 397
    /* query the color info */
    info->bmiHeader.biSize          = sizeof(info->bmiHeader);
    info->bmiHeader.biPlanes        = 1;
398
    info->bmiHeader.biBitCount      = bmp->dib.dsBm.bmBitsPixel;
399 400 401 402 403 404 405 406
    info->bmiHeader.biCompression   = BI_RGB;
    info->bmiHeader.biXPelsPerMeter = 0;
    info->bmiHeader.biYPelsPerMeter = 0;
    info->bmiHeader.biClrUsed       = 0;
    info->bmiHeader.biClrImportant  = 0;
    info->bmiHeader.biWidth         = 0;
    info->bmiHeader.biHeight        = 0;
    info->bmiHeader.biSizeImage     = 0;
407
    err = put_image_into_bitmap( bmp, 0, info, NULL, NULL, NULL );
Alexandre Julliard's avatar
Alexandre Julliard committed
408

409 410
    if (!err || err == ERROR_BAD_FORMAT)
    {
411
        info->bmiHeader.biWidth     = bmp->dib.dsBm.bmWidth;
412 413
        info->bmiHeader.biHeight    = -dst.height;
        info->bmiHeader.biSizeImage = dst.height * dst_stride;
414
        err = put_image_into_bitmap( bmp, clip, info, &src_bits, &src, &dst );
415 416
    }
    if (err) count = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
417

418
    if (clip) DeleteObject( clip );
419
    if (src_bits.free) src_bits.free( &src_bits );
420
    GDI_ReleaseObj( hbitmap );
421
    return count;
Alexandre Julliard's avatar
Alexandre Julliard committed
422 423
}

424 425 426 427

/***********************************************************************
 *           BITMAP_SelectObject
 */
428
static HGDIOBJ BITMAP_SelectObject( HGDIOBJ handle, HDC hdc )
429
{
430
    HGDIOBJ ret;
431 432
    BITMAPOBJ *bitmap;
    DC *dc;
433
    PHYSDEV physdev;
434

435
    if (!(dc = get_dc_ptr( hdc ))) return 0;
436 437 438 439 440 441

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

445
    if (!(bitmap = GDI_GetObjPtr( handle, OBJ_BITMAP )))
446 447 448 449 450
    {
        ret = 0;
        goto done;
    }

451
    if (handle != GetStockObject(DEFAULT_BITMAP) && GDI_get_ref_count( handle ))
452 453
    {
        WARN( "Bitmap already selected in another DC\n" );
454
        GDI_ReleaseObj( handle );
455 456
        ret = 0;
        goto done;
457 458
    }

459 460 461 462 463 464 465 466 467
    if (bitmap->dib.dsBm.bmBitsPixel != 1 &&
        bitmap->dib.dsBm.bmBitsPixel != GetDeviceCaps( hdc, BITSPIXEL ))
    {
        WARN( "Wrong format bitmap %u bpp\n", bitmap->dib.dsBm.bmBitsPixel );
        GDI_ReleaseObj( handle );
        ret = 0;
        goto done;
    }

468
    physdev = GET_DC_PHYSDEV( dc, pSelectBitmap );
469
    if (!physdev->funcs->pSelectBitmap( physdev, handle ))
470 471 472 473 474
    {
        GDI_ReleaseObj( handle );
        ret = 0;
    }
    else
475
    {
476
        dc->hBitmap = handle;
477
        GDI_inc_ref_count( handle );
478
        dc->dirty = 0;
479 480
        dc->vis_rect.left   = 0;
        dc->vis_rect.top    = 0;
481 482
        dc->vis_rect.right  = bitmap->dib.dsBm.bmWidth;
        dc->vis_rect.bottom = bitmap->dib.dsBm.bmHeight;
483
        dc->device_rect = dc->vis_rect;
484
        GDI_ReleaseObj( handle );
485
        DC_InitDC( dc );
486
        GDI_dec_ref_count( ret );
487
    }
488

489
 done:
490
    release_dc_ptr( dc );
491 492 493 494
    return ret;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
495
/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
496
 *           BITMAP_DeleteObject
Alexandre Julliard's avatar
Alexandre Julliard committed
497
 */
498
static BOOL BITMAP_DeleteObject( HGDIOBJ handle )
Alexandre Julliard's avatar
Alexandre Julliard committed
499
{
500
    BITMAPOBJ *bmp = free_gdi_handle( handle );
501 502

    if (!bmp) return FALSE;
503
    HeapFree( GetProcessHeap(), 0, bmp->dib.dsBm.bmBits );
504 505
    HeapFree( GetProcessHeap(), 0, bmp );
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
506 507
}

508

Alexandre Julliard's avatar
Alexandre Julliard committed
509
/***********************************************************************
510
 *           BITMAP_GetObject
Alexandre Julliard's avatar
Alexandre Julliard committed
511
 */
512
static INT BITMAP_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
Alexandre Julliard's avatar
Alexandre Julliard committed
513
{
514
    INT ret = 0;
515
    BITMAPOBJ *bmp = GDI_GetObjPtr( handle, OBJ_BITMAP );
516

517
    if (!bmp) return 0;
518

519
    if (!buffer) ret = sizeof(BITMAP);
520
    else if (count >= sizeof(BITMAP))
Alexandre Julliard's avatar
Alexandre Julliard committed
521
    {
522 523 524
        BITMAP *bitmap = buffer;
        *bitmap = bmp->dib.dsBm;
        bitmap->bmBits = NULL;
525
        ret = sizeof(BITMAP);
Alexandre Julliard's avatar
Alexandre Julliard committed
526
    }
527 528
    GDI_ReleaseObj( handle );
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
529
}
530

Alexandre Julliard's avatar
Alexandre Julliard committed
531

Alexandre Julliard's avatar
Alexandre Julliard committed
532
/******************************************************************************
Jon Griffiths's avatar
Jon Griffiths committed
533 534 535
 * CreateDiscardableBitmap [GDI32.@]
 *
 * Creates a discardable bitmap.
Alexandre Julliard's avatar
Alexandre Julliard committed
536 537 538 539
 *
 * RETURNS
 *    Success: Handle to bitmap
 *    Failure: NULL
Alexandre Julliard's avatar
Alexandre Julliard committed
540
 */
541 542 543 544
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
545
{
546
    return CreateCompatibleBitmap( hdc, width, height );
Alexandre Julliard's avatar
Alexandre Julliard committed
547
}
Alexandre Julliard's avatar
Alexandre Julliard committed
548

Alexandre Julliard's avatar
Alexandre Julliard committed
549

Alexandre Julliard's avatar
Alexandre Julliard committed
550
/******************************************************************************
Jon Griffiths's avatar
Jon Griffiths committed
551 552 553
 * GetBitmapDimensionEx [GDI32.@]
 *
 * Retrieves dimensions of a bitmap.
Alexandre Julliard's avatar
Alexandre Julliard committed
554 555 556 557
 *
 * RETURNS
 *    Success: TRUE
 *    Failure: FALSE
Alexandre Julliard's avatar
Alexandre Julliard committed
558
 */
559 560 561
BOOL WINAPI GetBitmapDimensionEx(
    HBITMAP hbitmap, /* [in]  Handle to bitmap */
    LPSIZE size)     /* [out] Address of struct receiving dimensions */
Alexandre Julliard's avatar
Alexandre Julliard committed
562
{
563
    BITMAPOBJ * bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
Alexandre Julliard's avatar
Alexandre Julliard committed
564
    if (!bmp) return FALSE;
565
    *size = bmp->size;
566
    GDI_ReleaseObj( hbitmap );
Alexandre Julliard's avatar
Alexandre Julliard committed
567 568 569 570
    return TRUE;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
571
/******************************************************************************
Jon Griffiths's avatar
Jon Griffiths committed
572 573 574
 * SetBitmapDimensionEx [GDI32.@]
 *
 * Assigns dimensions to a bitmap.
575 576
 * 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
577 578 579 580 581
 *
 * RETURNS
 *    Success: TRUE
 *    Failure: FALSE
 */
582 583 584 585 586
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
587
{
588
    BITMAPOBJ * bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
Alexandre Julliard's avatar
Alexandre Julliard committed
589
    if (!bmp) return FALSE;
590 591 592
    if (prevSize) *prevSize = bmp->size;
    bmp->size.cx = x;
    bmp->size.cy = y;
593
    GDI_ReleaseObj( hbitmap );
Alexandre Julliard's avatar
Alexandre Julliard committed
594 595
    return TRUE;
}