dib.c 35.4 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1
/*
Alexandre Julliard's avatar
Alexandre Julliard committed
2
 * GDI device-independent bitmaps
Alexandre Julliard's avatar
Alexandre Julliard committed
3
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
4
 * Copyright 1993,1994  Alexandre Julliard
5
 *
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
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Alexandre Julliard's avatar
Alexandre Julliard committed
19 20
 */

21
#include <stdarg.h>
22
#include <stdlib.h>
23
#include <string.h>
24

25
#include "windef.h"
26
#include "winbase.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
27
#include "bitmap.h"
28
#include "selectors.h"
29
#include "gdi.h"
Michael Stefaniuc's avatar
Michael Stefaniuc committed
30
#include "wownt32.h"
31
#include "wine/debug.h"
32
#include "palette.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
33

34
WINE_DEFAULT_DEBUG_CHANNEL(bitmap);
35

Alexandre Julliard's avatar
Alexandre Julliard committed
36 37 38 39 40 41 42
/***********************************************************************
 *           DIB_GetDIBWidthBytes
 *
 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
 */
int DIB_GetDIBWidthBytes( int width, int depth )
Alexandre Julliard's avatar
Alexandre Julliard committed
43
{
Alexandre Julliard's avatar
Alexandre Julliard committed
44 45 46 47
    int words;

    switch(depth)
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
48 49 50 51 52 53 54 55
	case 1:  words = (width + 31) / 32; break;
	case 4:  words = (width + 7) / 8; break;
	case 8:  words = (width + 3) / 4; break;
	case 15:
	case 16: words = (width + 1) / 2; break;
	case 24: words = (width * 3 + 3)/4; break;

	default:
56
            WARN("(%d): Unsupported depth\n", depth );
Alexandre Julliard's avatar
Alexandre Julliard committed
57 58 59
	/* fall through */
	case 32:
	        words = width;
Alexandre Julliard's avatar
Alexandre Julliard committed
60 61
    }
    return 4 * words;
Alexandre Julliard's avatar
Alexandre Julliard committed
62
}
Alexandre Julliard's avatar
Alexandre Julliard committed
63

64 65 66 67 68 69 70 71 72 73
/***********************************************************************
 *           DIB_GetDIBImageBytes
 *
 * Return the number of bytes used to hold the image in a DIB bitmap.
 */
int DIB_GetDIBImageBytes( int width, int height, int depth )
{
    return DIB_GetDIBWidthBytes( width, depth ) * abs( height );
}

Alexandre Julliard's avatar
Alexandre Julliard committed
74

Alexandre Julliard's avatar
Alexandre Julliard committed
75 76 77
/***********************************************************************
 *           DIB_BitmapInfoSize
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
78
 * Return the size of the bitmap info structure including color table.
Alexandre Julliard's avatar
Alexandre Julliard committed
79
 */
80
int DIB_BitmapInfoSize( const BITMAPINFO * info, WORD coloruse )
Alexandre Julliard's avatar
Alexandre Julliard committed
81
{
Alexandre Julliard's avatar
Alexandre Julliard committed
82 83 84 85 86
    int colors;

    if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
    {
        BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)info;
Alexandre Julliard's avatar
Alexandre Julliard committed
87
        colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
88 89 90 91 92 93
        return sizeof(BITMAPCOREHEADER) + colors *
             ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
    }
    else  /* assume BITMAPINFOHEADER */
    {
        colors = info->bmiHeader.biClrUsed;
Alexandre Julliard's avatar
Alexandre Julliard committed
94
        if (!colors && (info->bmiHeader.biBitCount <= 8))
Alexandre Julliard's avatar
Alexandre Julliard committed
95 96 97 98
            colors = 1 << info->bmiHeader.biBitCount;
        return sizeof(BITMAPINFOHEADER) + colors *
               ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
99 100 101
}


Alexandre Julliard's avatar
Alexandre Julliard committed
102 103 104 105
/***********************************************************************
 *           DIB_GetBitmapInfo
 *
 * Get the info from a bitmap header.
106 107
 * Return 1 for INFOHEADER, 0 for COREHEADER,
 * 4 for V4HEADER, 5 for V5HEADER, -1 for error.
Alexandre Julliard's avatar
Alexandre Julliard committed
108
 */
109
static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, DWORD *width,
Alexandre Julliard's avatar
Alexandre Julliard committed
110
                              int *height, WORD *bpp, WORD *compr )
Alexandre Julliard's avatar
Alexandre Julliard committed
111 112 113 114 115 116
{
    if (header->biSize == sizeof(BITMAPINFOHEADER))
    {
        *width  = header->biWidth;
        *height = header->biHeight;
        *bpp    = header->biBitCount;
Alexandre Julliard's avatar
Alexandre Julliard committed
117
        *compr  = header->biCompression;
Alexandre Julliard's avatar
Alexandre Julliard committed
118 119 120 121 122 123 124 125
        return 1;
    }
    if (header->biSize == sizeof(BITMAPCOREHEADER))
    {
        BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)header;
        *width  = core->bcWidth;
        *height = core->bcHeight;
        *bpp    = core->bcBitCount;
Alexandre Julliard's avatar
Alexandre Julliard committed
126
        *compr  = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
127 128
        return 0;
    }
129 130 131 132 133 134
    if (header->biSize == sizeof(BITMAPV4HEADER))
    {
        BITMAPV4HEADER *v4hdr = (BITMAPV4HEADER *)header;
        *width  = v4hdr->bV4Width;
        *height = v4hdr->bV4Height;
        *bpp    = v4hdr->bV4BitCount;
135
        *compr  = v4hdr->bV4V4Compression;
136 137 138 139 140 141 142 143 144 145 146 147
        return 4;
    }
    if (header->biSize == sizeof(BITMAPV5HEADER))
    {
        BITMAPV5HEADER *v5hdr = (BITMAPV5HEADER *)header;
        *width  = v5hdr->bV5Width;
        *height = v5hdr->bV5Height;
        *bpp    = v5hdr->bV5BitCount;
        *compr  = v5hdr->bV5Compression;
        return 5;
    }
    ERR("(%ld): unknown/wrong size for header\n", header->biSize );
Alexandre Julliard's avatar
Alexandre Julliard committed
148 149 150 151
    return -1;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
152
/***********************************************************************
153
 *           StretchDIBits   (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
154
 */
155 156 157 158
INT WINAPI StretchDIBits(HDC hdc, INT xDst, INT yDst, INT widthDst,
                       INT heightDst, INT xSrc, INT ySrc, INT widthSrc,
                       INT heightSrc, const void *bits,
                       const BITMAPINFO *info, UINT wUsage, DWORD dwRop )
Alexandre Julliard's avatar
Alexandre Julliard committed
159
{
160 161 162 163
    DC *dc;

    if (!bits || !info)
	return 0;
164

165 166 167
    dc = DC_GetDCUpdate( hdc );
    if(!dc) return FALSE;
    
168
    if(dc->funcs->pStretchDIBits)
169
    {
170
        heightSrc = dc->funcs->pStretchDIBits(dc->physDev, xDst, yDst, widthDst,
171 172 173 174 175 176
                                              heightDst, xSrc, ySrc, widthSrc,
                                              heightSrc, bits, info, wUsage, dwRop);
        GDI_ReleaseObj( hdc );
    }
    else /* use StretchBlt */
    {
177 178
        HBITMAP hBitmap, hOldBitmap;
	HDC hdcMem;
179

180
        GDI_ReleaseObj( hdc );
181
	hdcMem = CreateCompatibleDC( hdc );
182 183 184 185 186 187
	if (info->bmiHeader.biCompression == BI_RLE4 ||
	    info->bmiHeader.biCompression == BI_RLE8) {

	   /* when RLE compression is used, there may be some gaps (ie the DIB doesn't
	    * contain all the rectangle described in bmiHeader, but only part of it.
	    * This mean that those undescribed pixels must be left untouched.
188
	    * So, we first copy on a memory bitmap the current content of the
189 190 191 192 193 194 195 196 197 198
	    * destination rectangle, blit the DIB bits on top of it - hence leaving
	    * the gaps untouched -, and blitting the rectangle back.
	    * This insure that gaps are untouched on the destination rectangle
	    * Not doing so leads to trashed images (the gaps contain what was on the
	    * memory bitmap => generally black or garbage)
	    * Unfortunately, RLE DIBs without gaps will be slowed down. But this is
	    * another speed vs correctness issue. Anyway, if speed is needed, then the
	    * pStretchDIBits function shall be implemented.
	    * ericP (2000/09/09)
	    */
199
	   hBitmap = CreateCompatibleBitmap(hdc, info->bmiHeader.biWidth,
200 201
					    info->bmiHeader.biHeight);
	   hOldBitmap = SelectObject( hdcMem, hBitmap );
202

203 204 205 206
	   /* copy existing bitmap from destination dc */
	   StretchBlt( hdcMem, xSrc, abs(info->bmiHeader.biHeight) - heightSrc - ySrc,
		       widthSrc, heightSrc, hdc, xDst, yDst, widthDst, heightDst,
		       dwRop );
207 208 209
	   SetDIBits(hdcMem, hBitmap, 0, info->bmiHeader.biHeight, bits,
		     info, DIB_RGB_COLORS);

210 211 212 213 214 215
	} else {
	   hBitmap = CreateDIBitmap( hdc, &info->bmiHeader, CBM_INIT,
				     bits, info, wUsage );
	   hOldBitmap = SelectObject( hdcMem, hBitmap );
	}

216 217
        /* Origin for DIBitmap may be bottom left (positive biHeight) or top
           left (negative biHeight) */
218
        StretchBlt( hdc, xDst, yDst, widthDst, heightDst,
219
		    hdcMem, xSrc, abs(info->bmiHeader.biHeight) - heightSrc - ySrc,
220
		    widthSrc, heightSrc, dwRop );
221 222 223
	SelectObject( hdcMem, hOldBitmap );
	DeleteDC( hdcMem );
	DeleteObject( hBitmap );
224
    }
225
    return heightSrc;
Alexandre Julliard's avatar
Alexandre Julliard committed
226 227
}

228

Alexandre Julliard's avatar
Alexandre Julliard committed
229
/******************************************************************************
230
 * SetDIBits [GDI32.@]  Sets pixels in a bitmap using colors from DIB
Alexandre Julliard's avatar
Alexandre Julliard committed
231 232 233 234 235 236 237 238 239 240 241 242 243
 *
 * PARAMS
 *    hdc       [I] Handle to device context
 *    hbitmap   [I] Handle to bitmap
 *    startscan [I] Starting scan line
 *    lines     [I] Number of scan lines
 *    bits      [I] Array of bitmap bits
 *    info      [I] Address of structure with data
 *    coloruse  [I] Type of color indexes to use
 *
 * RETURNS
 *    Success: Number of scan lines copied
 *    Failure: 0
Alexandre Julliard's avatar
Alexandre Julliard committed
244
 */
245
INT WINAPI SetDIBits( HDC hdc, HBITMAP hbitmap, UINT startscan,
246 247
		      UINT lines, LPCVOID bits, const BITMAPINFO *info,
		      UINT coloruse )
Alexandre Julliard's avatar
Alexandre Julliard committed
248
{
249
    DC *dc;
250
    BITMAPOBJ *bitmap;
251
    INT result = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
252

253 254 255 256 257 258 259 260
    if (!(bitmap = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return 0;

    if (!(dc = DC_GetDCUpdate( hdc )))
    {
        if (coloruse == DIB_RGB_COLORS) FIXME( "shouldn't require a DC for DIB_RGB_COLORS\n" );
        GDI_ReleaseObj( hbitmap );
        return 0;
    }
261

262
    if (!bitmap->funcs && !BITMAP_SetOwnerDC( hbitmap, dc )) goto done;
Alexandre Julliard's avatar
Alexandre Julliard committed
263

264 265 266 267 268 269 270
    if (bitmap->funcs && bitmap->funcs->pSetDIBits)
        result = bitmap->funcs->pSetDIBits( dc->physDev, hbitmap, startscan, lines,
                                            bits, info, coloruse );
    else
        result = lines;

 done:
271
    GDI_ReleaseObj( hdc );
272
    GDI_ReleaseObj( hbitmap );
Alexandre Julliard's avatar
Alexandre Julliard committed
273
    return result;
Alexandre Julliard's avatar
Alexandre Julliard committed
274 275 276
}


Alexandre Julliard's avatar
Alexandre Julliard committed
277
/***********************************************************************
278
 *           SetDIBitsToDevice   (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
279
 */
280 281 282 283
INT WINAPI SetDIBitsToDevice(HDC hdc, INT xDest, INT yDest, DWORD cx,
                           DWORD cy, INT xSrc, INT ySrc, UINT startscan,
                           UINT lines, LPCVOID bits, const BITMAPINFO *info,
                           UINT coloruse )
Alexandre Julliard's avatar
Alexandre Julliard committed
284
{
285
    INT ret;
286
    DC *dc;
Alexandre Julliard's avatar
Alexandre Julliard committed
287

288
    if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
289

290
    if(dc->funcs->pSetDIBitsToDevice)
291
        ret = dc->funcs->pSetDIBitsToDevice( dc->physDev, xDest, yDest, cx, cy, xSrc,
292 293 294
					     ySrc, startscan, lines, bits,
					     info, coloruse );
    else {
295
        FIXME("unimplemented on hdc %p\n", hdc);
296
	ret = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
297
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
298

299
    GDI_ReleaseObj( hdc );
300
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
301 302
}

Alexandre Julliard's avatar
Alexandre Julliard committed
303
/***********************************************************************
304
 *           SetDIBColorTable    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
305
 */
306
UINT WINAPI SetDIBColorTable( HDC hdc, UINT startpos, UINT entries, RGBQUAD *colors )
Alexandre Julliard's avatar
Alexandre Julliard committed
307 308
{
    DC * dc;
309
    UINT result = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
310

311
    if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
312

313 314
    if (dc->funcs->pSetDIBColorTable)
        result = dc->funcs->pSetDIBColorTable(dc->physDev, startpos, entries, colors);
Alexandre Julliard's avatar
Alexandre Julliard committed
315

316
    GDI_ReleaseObj( hdc );
317
    return result;
Alexandre Julliard's avatar
Alexandre Julliard committed
318 319
}

Alexandre Julliard's avatar
Alexandre Julliard committed
320

Alexandre Julliard's avatar
Alexandre Julliard committed
321
/***********************************************************************
322
 *           GetDIBColorTable    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
323
 */
324
UINT WINAPI GetDIBColorTable( HDC hdc, UINT startpos, UINT entries, RGBQUAD *colors )
Alexandre Julliard's avatar
Alexandre Julliard committed
325 326
{
    DC * dc;
327
    UINT result = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
328

329
    if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
330

331 332
    if (dc->funcs->pGetDIBColorTable)
        result = dc->funcs->pGetDIBColorTable(dc->physDev, startpos, entries, colors);
333

334
    GDI_ReleaseObj( hdc );
335
    return result;
Alexandre Julliard's avatar
Alexandre Julliard committed
336
}
Alexandre Julliard's avatar
Alexandre Julliard committed
337

338 339 340
/* FIXME the following two structs should be combined with __sysPalTemplate in
   objects/color.c - this should happen after de-X11-ing both of these
   files.
Andreas Mohr's avatar
Andreas Mohr committed
341
   NB. RGBQUAD and PALETTEENTRY have different orderings of red, green
342 343
   and blue - sigh */

344
static RGBQUAD EGAColors[16] = {
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
/* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
    { 0x00, 0x00, 0x00, 0x00 },
    { 0x00, 0x00, 0x80, 0x00 },
    { 0x00, 0x80, 0x00, 0x00 },
    { 0x00, 0x80, 0x80, 0x00 },
    { 0x80, 0x00, 0x00, 0x00 },
    { 0x80, 0x00, 0x80, 0x00 },
    { 0x80, 0x80, 0x00, 0x00 },
    { 0x80, 0x80, 0x80, 0x00 },
    { 0xc0, 0xc0, 0xc0, 0x00 },
    { 0x00, 0x00, 0xff, 0x00 },
    { 0x00, 0xff, 0x00, 0x00 },
    { 0x00, 0xff, 0xff, 0x00 },
    { 0xff, 0x00, 0x00, 0x00 },
    { 0xff, 0x00, 0xff, 0x00 },
    { 0xff, 0xff, 0x00, 0x00 },
    { 0xff, 0xff, 0xff, 0x00 }
};


static RGBQUAD DefLogPalette[20] = { /* Copy of Default Logical Palette */
/* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
    { 0x00, 0x00, 0x00, 0x00 },
    { 0x00, 0x00, 0x80, 0x00 },
    { 0x00, 0x80, 0x00, 0x00 },
    { 0x00, 0x80, 0x80, 0x00 },
    { 0x80, 0x00, 0x00, 0x00 },
    { 0x80, 0x00, 0x80, 0x00 },
    { 0x80, 0x80, 0x00, 0x00 },
    { 0xc0, 0xc0, 0xc0, 0x00 },
    { 0xc0, 0xdc, 0xc0, 0x00 },
    { 0xf0, 0xca, 0xa6, 0x00 },
    { 0xf0, 0xfb, 0xff, 0x00 },
    { 0xa4, 0xa0, 0xa0, 0x00 },
    { 0x80, 0x80, 0x80, 0x00 },
    { 0x00, 0x00, 0xf0, 0x00 },
    { 0x00, 0xff, 0x00, 0x00 },
    { 0x00, 0xff, 0xff, 0x00 },
    { 0xff, 0x00, 0x00, 0x00 },
    { 0xff, 0x00, 0xff, 0x00 },
    { 0xff, 0xff, 0x00, 0x00 },
    { 0xff, 0xff, 0xff, 0x00 }
};

Alexandre Julliard's avatar
Alexandre Julliard committed
389

Alexandre Julliard's avatar
Alexandre Julliard committed
390
/******************************************************************************
391
 * GetDIBits [GDI32.@]  Retrieves bits of bitmap and copies to buffer
Alexandre Julliard's avatar
Alexandre Julliard committed
392 393 394 395
 *
 * RETURNS
 *    Success: Number of scan lines copied from bitmap
 *    Failure: 0
Alexandre Julliard's avatar
Alexandre Julliard committed
396 397
 *
 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/func/src/f30_14.htm
Alexandre Julliard's avatar
Alexandre Julliard committed
398
 */
399 400 401 402 403
INT WINAPI GetDIBits(
    HDC hdc,         /* [in]  Handle to device context */
    HBITMAP hbitmap, /* [in]  Handle to bitmap */
    UINT startscan,  /* [in]  First scan line to set in dest bitmap */
    UINT lines,      /* [in]  Number of scan lines to copy */
404
    LPVOID bits,       /* [out] Address of array for bitmap bits */
Alexandre Julliard's avatar
Alexandre Julliard committed
405
    BITMAPINFO * info, /* [out] Address of structure with bitmap data */
406
    UINT coloruse)   /* [in]  RGB or palette index */
Alexandre Julliard's avatar
Alexandre Julliard committed
407 408
{
    DC * dc;
Alexandre Julliard's avatar
Alexandre Julliard committed
409
    BITMAPOBJ * bmp;
410
    int i;
Alexandre Julliard's avatar
Alexandre Julliard committed
411

412
    if (!info) return 0;
413
    if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
414
    if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
415
    {
416
        GDI_ReleaseObj( hdc );
Alexandre Julliard's avatar
Alexandre Julliard committed
417
	return 0;
418
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
419

420 421 422 423
    /* Transfer color info */

    if (info->bmiHeader.biBitCount <= 8 && info->bmiHeader.biBitCount > 0 ) {

424
	info->bmiHeader.biClrUsed = 0;
425

426 427 428 429
	/* If the bitmap object already has a dib section at the
	   same color depth then get the color map from it */
	if (bmp->dib && bmp->dib->dsBm.bmBitsPixel == info->bmiHeader.biBitCount) {
	    GetDIBColorTable(hdc, 0, 1 << info->bmiHeader.biBitCount, info->bmiColors);
Alexandre Julliard's avatar
Alexandre Julliard committed
430
	}
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
        else {
            if(info->bmiHeader.biBitCount >= bmp->bitmap.bmBitsPixel) {
                /* Generate the color map from the selected palette */
                PALETTEENTRY * palEntry;
                PALETTEOBJ * palette;
                if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->hPalette, PALETTE_MAGIC ))) {
                    GDI_ReleaseObj( hdc );
                    GDI_ReleaseObj( hbitmap );
                    return 0;
                }
                palEntry = palette->logpalette.palPalEntry;
                for (i = 0; i < (1 << bmp->bitmap.bmBitsPixel); i++, palEntry++) {
                    if (coloruse == DIB_RGB_COLORS) {
                        info->bmiColors[i].rgbRed      = palEntry->peRed;
                        info->bmiColors[i].rgbGreen    = palEntry->peGreen;
                        info->bmiColors[i].rgbBlue     = palEntry->peBlue;
                        info->bmiColors[i].rgbReserved = 0;
                    }
                    else ((WORD *)info->bmiColors)[i] = (WORD)i;
                }
                GDI_ReleaseObj( dc->hPalette );
            } else {
                switch (info->bmiHeader.biBitCount) {
                case 1:
                    info->bmiColors[0].rgbRed = info->bmiColors[0].rgbGreen =
                        info->bmiColors[0].rgbBlue = 0;
                    info->bmiColors[0].rgbReserved = 0;
                    info->bmiColors[1].rgbRed = info->bmiColors[1].rgbGreen =
                        info->bmiColors[1].rgbBlue = 0xff;
                    info->bmiColors[1].rgbReserved = 0;
                    break;

                case 4:
                    memcpy(info->bmiColors, EGAColors, sizeof(EGAColors));
                    break;

                case 8:
                    {
                        INT r, g, b;
                        RGBQUAD *color;

                        memcpy(info->bmiColors, DefLogPalette,
                               10 * sizeof(RGBQUAD));
                        memcpy(info->bmiColors + 246, DefLogPalette + 10,
                               10 * sizeof(RGBQUAD));
                        color = info->bmiColors + 10;
                        for(r = 0; r <= 5; r++) /* FIXME */
                            for(g = 0; g <= 5; g++)
                                for(b = 0; b <= 5; b++) {
                                    color->rgbRed =   (r * 0xff) / 5;
                                    color->rgbGreen = (g * 0xff) / 5;
                                    color->rgbBlue =  (b * 0xff) / 5;
                                    color->rgbReserved = 0;
                                    color++;
                                }
                    }
                }
            }
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
490
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
491

492
    if (bits && lines)
493
    {
Andreas Mohr's avatar
Andreas Mohr committed
494
        /* If the bitmap object already have a dib section that contains image data, get the bits from it */
Karl Lessard's avatar
Karl Lessard committed
495
        if(bmp->dib && bmp->dib->dsBm.bmBitsPixel >= 15 && info->bmiHeader.biBitCount >= 15)
Karl Lessard's avatar
Karl Lessard committed
496 497
        {
            /*FIXME: Only RGB dibs supported for now */
498
            unsigned int srcwidth = bmp->dib->dsBm.bmWidth, srcwidthb = bmp->dib->dsBm.bmWidthBytes;
Karl Lessard's avatar
Karl Lessard committed
499
            int dstwidthb = DIB_GetDIBWidthBytes( info->bmiHeader.biWidth, info->bmiHeader.biBitCount );
500
            LPBYTE dbits = bits, sbits = (LPBYTE) bmp->dib->dsBm.bmBits + (startscan * srcwidthb);
501
            unsigned int x, y;
Karl Lessard's avatar
Karl Lessard committed
502 503 504

            if ((info->bmiHeader.biHeight < 0) ^ (bmp->dib->dsBmih.biHeight < 0))
            {
Huw D M Davies's avatar
Huw D M Davies committed
505
                dbits = (LPBYTE)bits + (dstwidthb * (lines-1));
Karl Lessard's avatar
Karl Lessard committed
506 507 508 509 510
                dstwidthb = -dstwidthb;
            }

            switch( info->bmiHeader.biBitCount ) {

Karl Lessard's avatar
Karl Lessard committed
511
	    case 15:
Karl Lessard's avatar
Karl Lessard committed
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
            case 16: /* 16 bpp dstDIB */
                {
                    LPWORD dstbits = (LPWORD)dbits;
                    WORD rmask = 0x7c00, gmask= 0x03e0, bmask = 0x001f;

                    /* FIXME: BI_BITFIELDS not supported yet */

                    switch(bmp->dib->dsBm.bmBitsPixel) {

                    case 16: /* 16 bpp srcDIB -> 16 bpp dstDIB */
                        {
                            /* FIXME: BI_BITFIELDS not supported yet */
                            for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
                                memcpy(dbits, sbits, srcwidthb);
                        }
                        break;

                    case 24: /* 24 bpp srcDIB -> 16 bpp dstDIB */
                        {
Huw D M Davies's avatar
Huw D M Davies committed
531
                            LPBYTE srcbits = sbits;
Karl Lessard's avatar
Karl Lessard committed
532 533

                            for( y = 0; y < lines; y++) {
534 535 536 537 538
                                for( x = 0; x < srcwidth; x++, srcbits += 3)
                                    *dstbits++ = ((srcbits[0] >> 3) & bmask) |
                                                 (((WORD)srcbits[1] << 2) & gmask) |
                                                 (((WORD)srcbits[2] << 7) & rmask);

Karl Lessard's avatar
Karl Lessard committed
539
                                dstbits = (LPWORD)(dbits+=dstwidthb);
Huw D M Davies's avatar
Huw D M Davies committed
540
                                srcbits = (sbits += srcwidthb);
Karl Lessard's avatar
Karl Lessard committed
541 542 543 544 545 546 547 548 549 550 551 552
                            }
                        }
                        break;

                    case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
                        {
                            LPDWORD srcbits = (LPDWORD)sbits;
                            DWORD val;

                            for( y = 0; y < lines; y++) {
                                for( x = 0; x < srcwidth; x++ ) {
                                    val = *srcbits++;
553 554
                                    *dstbits++ = (WORD)(((val >> 3) & bmask) | ((val >> 6) & gmask) |
                                                       ((val >> 9) & rmask));
Karl Lessard's avatar
Karl Lessard committed
555
                                }
Huw D M Davies's avatar
Huw D M Davies committed
556 557
                                dstbits = (LPWORD)(dbits+=dstwidthb);
                                srcbits = (LPDWORD)(sbits+=srcwidthb);
Karl Lessard's avatar
Karl Lessard committed
558 559 560 561 562 563 564 565 566 567 568 569 570 571
                            }
                        }
                        break;

                    default: /* ? bit bmp -> 16 bit DIB */
                        FIXME("15/16 bit DIB %d bit bitmap\n",
                        bmp->bitmap.bmBitsPixel);
                        break;
                    }
                }
                break;

            case 24: /* 24 bpp dstDIB */
                {
Huw D M Davies's avatar
Huw D M Davies committed
572
                    LPBYTE dstbits = dbits;
Karl Lessard's avatar
Karl Lessard committed
573 574 575 576 577 578 579 580 581 582 583 584 585

                    switch(bmp->dib->dsBm.bmBitsPixel) {

                    case 16: /* 16 bpp srcDIB -> 24 bpp dstDIB */
                        {
                            LPWORD srcbits = (LPWORD)sbits;
                            WORD val;

                            /* FIXME: BI_BITFIELDS not supported yet */
                            for( y = 0; y < lines; y++) {
                                for( x = 0; x < srcwidth; x++ ) {
                                    val = *srcbits++;
                                    *dstbits++ = (BYTE)(((val << 3) & 0xf8) | ((val >> 2) & 0x07));
586 587
                                    *dstbits++ = (BYTE)(((val >> 2) & 0xf8) | ((val >> 7) & 0x07));
                                    *dstbits++ = (BYTE)(((val >> 7) & 0xf8) | ((val >> 12) & 0x07));
Karl Lessard's avatar
Karl Lessard committed
588
                                }
Huw D M Davies's avatar
Huw D M Davies committed
589
                                dstbits = (LPBYTE)(dbits+=dstwidthb);
Karl Lessard's avatar
Karl Lessard committed
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
                                srcbits = (LPWORD)(sbits+=srcwidthb);
                            }
                        }
                        break;

                    case 24: /* 24 bpp srcDIB -> 24 bpp dstDIB */
                        {
                            for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
                                memcpy(dbits, sbits, srcwidthb);
                        }
                        break;

                    case 32: /* 32 bpp srcDIB -> 24 bpp dstDIB */
                        {
                            LPBYTE srcbits = (LPBYTE)sbits;

                            for( y = 0; y < lines; y++) {
                                for( x = 0; x < srcwidth; x++, srcbits++ ) {
                                    *dstbits++ = *srcbits++;
                                    *dstbits++ = *srcbits++;
                                    *dstbits++ = *srcbits++;
                                }
                                dstbits=(LPBYTE)(dbits+=dstwidthb);
                                srcbits = (LPBYTE)(sbits+=srcwidthb);
                            }
                        }
                        break;

                    default: /* ? bit bmp -> 24 bit DIB */
                        FIXME("24 bit DIB %d bit bitmap\n",
                              bmp->bitmap.bmBitsPixel);
                        break;
                    }
                }
                break;

            case 32: /* 32 bpp dstDIB */
                {
                    LPDWORD dstbits = (LPDWORD)dbits;

                    /* FIXME: BI_BITFIELDS not supported yet */

                    switch(bmp->dib->dsBm.bmBitsPixel) {
                        case 16: /* 16 bpp srcDIB -> 32 bpp dstDIB */
                        {
                            LPWORD srcbits = (LPWORD)sbits;
                            DWORD val;

                            /* FIXME: BI_BITFIELDS not supported yet */
                            for( y = 0; y < lines; y++) {
                                for( x = 0; x < srcwidth; x++ ) {
                                    val = (DWORD)*srcbits++;
642
                                    *dstbits++ = ((val << 3) & 0xf8) | ((val >> 2) & 0x07) |
Karl Lessard's avatar
Karl Lessard committed
643
                                                 ((val << 6) & 0xf800) | ((val << 1) & 0x0700) |
644
                                                 ((val << 9) & 0xf80000) | ((val << 4) & 0x070000);
Karl Lessard's avatar
Karl Lessard committed
645
                                }
Huw D M Davies's avatar
Huw D M Davies committed
646 647
                                dstbits=(LPDWORD)(dbits+=dstwidthb);
                                srcbits=(LPWORD)(sbits+=srcwidthb);
Karl Lessard's avatar
Karl Lessard committed
648 649 650 651 652 653
                            }
                        }
                        break;

                    case 24: /* 24 bpp srcDIB -> 32 bpp dstDIB */
                        {
Huw D M Davies's avatar
Huw D M Davies committed
654
                            LPBYTE srcbits = sbits;
Karl Lessard's avatar
Karl Lessard committed
655 656

                            for( y = 0; y < lines; y++) {
657
                                for( x = 0; x < srcwidth; x++, srcbits+=3 )
658
                                    *dstbits++ = ((DWORD)*srcbits) & 0x00ffffff;
Huw D M Davies's avatar
Huw D M Davies committed
659
                                dstbits=(LPDWORD)(dbits+=dstwidthb);
Karl Lessard's avatar
Karl Lessard committed
660 661 662 663 664 665 666 667 668 669 670 671 672
                                srcbits=(sbits+=srcwidthb);
                            }
                        }
                        break;

                    case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
                        {
                            /* FIXME: BI_BITFIELDS not supported yet */
                            for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
                                memcpy(dbits, sbits, srcwidthb);
                        }
                        break;

673 674
                    default: /* ? bit bmp -> 32 bit DIB */
                        FIXME("32 bit DIB %d bit bitmap\n",
Karl Lessard's avatar
Karl Lessard committed
675 676 677 678 679 680 681 682 683 684 685 686
                        bmp->bitmap.bmBitsPixel);
                        break;
                    }
                }
                break;

            default: /* ? bit DIB */
                FIXME("Unsupported DIB depth %d\n", info->bmiHeader.biBitCount);
                break;
            }
        }
        /* Otherwise, get bits from the XImage */
687
        else
Alexandre Julliard's avatar
Alexandre Julliard committed
688
        {
689 690 691 692 693 694 695 696 697 698
            if (!bmp->funcs && !BITMAP_SetOwnerDC( hbitmap, dc )) lines = 0;
            else
            {
                if (bmp->funcs && bmp->funcs->pGetDIBits)
                    lines = bmp->funcs->pGetDIBits( dc->physDev, hbitmap, startscan,
                                                    lines, bits, info, coloruse );
                else
                    lines = 0;  /* FIXME: should copy from bmp->bitmap.bmBits */
            }
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
699
    }
700
    else if( info->bmiHeader.biSize >= sizeof(BITMAPINFOHEADER) )
Alexandre Julliard's avatar
Alexandre Julliard committed
701 702
    {
	/* fill in struct members */
703 704 705 706 707 708 709

        if( info->bmiHeader.biBitCount == 0)
	{
	    info->bmiHeader.biWidth = bmp->bitmap.bmWidth;
	    info->bmiHeader.biHeight = bmp->bitmap.bmHeight;
	    info->bmiHeader.biPlanes = 1;
	    info->bmiHeader.biBitCount = bmp->bitmap.bmBitsPixel;
710
	    info->bmiHeader.biSizeImage =
711 712 713
                             DIB_GetDIBImageBytes( bmp->bitmap.bmWidth,
						   bmp->bitmap.bmHeight,
						   bmp->bitmap.bmBitsPixel );
714 715 716 717
	    info->bmiHeader.biCompression = 0;
	}
	else
	{
718 719 720 721
	    info->bmiHeader.biSizeImage = DIB_GetDIBImageBytes(
					       info->bmiHeader.biWidth,
					       info->bmiHeader.biHeight,
					       info->bmiHeader.biBitCount );
722
	}
Alexandre Julliard's avatar
Alexandre Julliard committed
723
    }
724

725
    TRACE("biSizeImage = %ld, biWidth = %ld, biHeight = %ld\n",
726 727
	  info->bmiHeader.biSizeImage, info->bmiHeader.biWidth,
	  info->bmiHeader.biHeight);
728

729 730
    GDI_ReleaseObj( hdc );
    GDI_ReleaseObj( hbitmap );
731

Alexandre Julliard's avatar
Alexandre Julliard committed
732 733 734 735
    return lines;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
736
/***********************************************************************
737
 *           CreateDIBitmap    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
738
 */
739
HBITMAP WINAPI CreateDIBitmap( HDC hdc, const BITMAPINFOHEADER *header,
Alexandre Julliard's avatar
Alexandre Julliard committed
740
                            DWORD init, LPCVOID bits, const BITMAPINFO *data,
741
                            UINT coloruse )
Alexandre Julliard's avatar
Alexandre Julliard committed
742
{
743 744
    HBITMAP handle;
    BOOL fColor;
Alexandre Julliard's avatar
Alexandre Julliard committed
745 746
    DWORD width;
    int height;
Alexandre Julliard's avatar
Alexandre Julliard committed
747
    WORD bpp;
Alexandre Julliard's avatar
Alexandre Julliard committed
748
    WORD compr;
749
    DC *dc;
Alexandre Julliard's avatar
Alexandre Julliard committed
750

Alexandre Julliard's avatar
Alexandre Julliard committed
751
    if (DIB_GetBitmapInfo( header, &width, &height, &bpp, &compr ) == -1) return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
752
    if (height < 0) height = -height;
Alexandre Julliard's avatar
Alexandre Julliard committed
753 754 755

    /* Check if we should create a monochrome or color bitmap. */
    /* We create a monochrome bitmap only if it has exactly 2  */
756
    /* colors, which are black followed by white, nothing else.  */
Alexandre Julliard's avatar
Alexandre Julliard committed
757 758 759
    /* In all other cases, we create a color bitmap.           */

    if (bpp != 1) fColor = TRUE;
760
    else if ((coloruse != DIB_RGB_COLORS) || !data) fColor = FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
761
    else
Alexandre Julliard's avatar
Alexandre Julliard committed
762
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
763 764 765 766
        if (data->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
        {
            RGBQUAD *rgb = data->bmiColors;
            DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
767

768
	    /* Check if the first color of the colormap is black */
769
	    if ((col == RGB(0,0,0)))
Alexandre Julliard's avatar
Alexandre Julliard committed
770 771 772
            {
                rgb++;
                col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
773 774
		/* If the second color is white, create a monochrome bitmap */
                fColor =  (col != RGB(0xff,0xff,0xff));
Alexandre Julliard's avatar
Alexandre Julliard committed
775
            }
776 777 778
	    /* Note : If the first color of the colormap is white
	       followed by black, we have to create a color bitmap.
	       If we don't the white will be displayed in black later on!*/
Alexandre Julliard's avatar
Alexandre Julliard committed
779 780 781 782 783 784
            else fColor = TRUE;
        }
        else if (data->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
        {
            RGBTRIPLE *rgb = ((BITMAPCOREINFO *)data)->bmciColors;
            DWORD col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
785
            if ((col == RGB(0,0,0)))
Alexandre Julliard's avatar
Alexandre Julliard committed
786 787 788
            {
                rgb++;
                col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
789
                fColor = (col != RGB(0xff,0xff,0xff));
Alexandre Julliard's avatar
Alexandre Julliard committed
790 791 792
            }
            else fColor = TRUE;
        }
793 794 795 796 797
        else if (data->bmiHeader.biSize == sizeof(BITMAPV4HEADER))
        { /* FIXME: correct ? */
            RGBQUAD *rgb = data->bmiColors;
            DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );

798
	    /* Check if the first color of the colormap is black */
799 800 801 802 803 804 805
	    if ((col == RGB(0,0,0)))
            {
                rgb++;
                col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
		/* If the second color is white, create a monochrome bitmap */
                fColor =  (col != RGB(0xff,0xff,0xff));
            }
806 807 808
	    /* Note : If the first color of the colormap is white
	       followed by black, we have to create a color bitmap.
	       If we don't the white will be displayed in black later on!*/
809 810
            else fColor = TRUE;
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
811 812
        else
        {
813
            ERR("(%ld): wrong/unknown size for data\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
814 815 816
                     data->bmiHeader.biSize );
            return 0;
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
817
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
818 819 820

    /* Now create the bitmap */

821 822
    if (!(dc = DC_GetDCPtr( hdc ))) return 0;

823 824 825
    if (fColor)
        handle = CreateBitmap( width, height, GetDeviceCaps( hdc, PLANES ),
                               GetDeviceCaps( hdc, BITSPIXEL ), NULL );
826 827
    else handle = CreateBitmap( width, height, 1, 1, NULL );

828 829 830 831 832 833 834 835 836
    if (handle)
    {
        if (init == CBM_INIT) SetDIBits( hdc, handle, 0, height, bits, data, coloruse );
        else if (!BITMAP_SetOwnerDC( handle, dc ))
        {
            DeleteObject( handle );
            handle = 0;
        }
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
837

838
    GDI_ReleaseObj( hdc );
Alexandre Julliard's avatar
Alexandre Julliard committed
839
    return handle;
Alexandre Julliard's avatar
Alexandre Julliard committed
840
}
Alexandre Julliard's avatar
Alexandre Julliard committed
841

Alexandre Julliard's avatar
Alexandre Julliard committed
842
/***********************************************************************
843
 *           CreateDIBSection    (GDI.489)
Alexandre Julliard's avatar
Alexandre Julliard committed
844 845
 */
HBITMAP16 WINAPI CreateDIBSection16 (HDC16 hdc, BITMAPINFO *bmi, UINT16 usage,
846
                                     SEGPTR *bits16, HANDLE section, DWORD offset)
Alexandre Julliard's avatar
Alexandre Julliard committed
847
{
848 849
    LPVOID bits32;
    HBITMAP hbitmap;
850

Michael Stefaniuc's avatar
Michael Stefaniuc committed
851
    hbitmap = CreateDIBSection( HDC_32(hdc), bmi, usage, &bits32, section, offset );
852
    if (hbitmap)
853
    {
854 855 856 857 858 859 860 861 862
        BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr(hbitmap, BITMAP_MAGIC);
        if (bmp && bmp->dib && bits32)
        {
            BITMAPINFOHEADER *bi = &bmi->bmiHeader;
            INT height = bi->biHeight >= 0 ? bi->biHeight : -bi->biHeight;
            INT width_bytes = DIB_GetDIBWidthBytes(bi->biWidth, bi->biBitCount);
            INT size  = (bi->biSizeImage && bi->biCompression != BI_RGB) ?
                         bi->biSizeImage : width_bytes * height;

863 864 865 866 867 868 869 870 871 872 873
            /* calculate number of sel's needed for size with 64K steps */
            WORD count = (size + 0xffff) / 0x10000;
            WORD sel = AllocSelectorArray16(count);
            int i;

            for (i = 0; i < count; i++)
            {
                SetSelectorBase(sel + (i << __AHSHIFT), (DWORD)bits32 + i * 0x10000);
                SetSelectorLimit16(sel + (i << __AHSHIFT), size - 1); /* yep, limit is correct */
                size -= 0x10000;
            }
874 875 876 877
            bmp->segptr_bits = MAKESEGPTR( sel, 0 );
            if (bits16) *bits16 = bmp->segptr_bits;
        }
        if (bmp) GDI_ReleaseObj( hbitmap );
878
    }
Michael Stefaniuc's avatar
Michael Stefaniuc committed
879
    return HBITMAP_16(hbitmap);
Alexandre Julliard's avatar
Alexandre Julliard committed
880 881 882
}

/***********************************************************************
883
 *           DIB_CreateDIBSection
Alexandre Julliard's avatar
Alexandre Julliard committed
884
 */
885 886 887
HBITMAP DIB_CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
			     LPVOID *bits, HANDLE section,
			     DWORD offset, DWORD ovr_pitch)
Alexandre Julliard's avatar
Alexandre Julliard committed
888
{
889
    HBITMAP hbitmap = 0;
890 891 892 893 894 895 896 897 898 899
    DC *dc;
    BOOL bDesktopDC = FALSE;

    /* If the reference hdc is null, take the desktop dc */
    if (hdc == 0)
    {
        hdc = CreateCompatibleDC(0);
        bDesktopDC = TRUE;
    }

900 901
    if ((dc = DC_GetDCPtr( hdc )))
    {
902
        hbitmap = dc->funcs->pCreateDIBSection(dc->physDev, bmi, usage, bits, section, offset, ovr_pitch);
903 904
        GDI_ReleaseObj(hdc);
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
905

906 907 908
    if (bDesktopDC)
      DeleteDC(hdc);

909
    return hbitmap;
Alexandre Julliard's avatar
Alexandre Julliard committed
910 911
}

912
/***********************************************************************
913
 *           CreateDIBSection    (GDI32.@)
914 915 916 917 918 919 920 921
 */
HBITMAP WINAPI CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
				LPVOID *bits, HANDLE section,
				DWORD offset)
{
    return DIB_CreateDIBSection(hdc, bmi, usage, bits, section, offset, 0);
}

922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
/***********************************************************************
 *           DIB_CreateDIBFromBitmap
 *  Allocates a packed DIB and copies the bitmap data into it.
 */
HGLOBAL DIB_CreateDIBFromBitmap(HDC hdc, HBITMAP hBmp)
{
    BITMAPOBJ *pBmp = NULL;
    HGLOBAL hPackedDIB = 0;
    LPBYTE pPackedDIB = NULL;
    LPBITMAPINFOHEADER pbmiHeader = NULL;
    unsigned int width, height, depth, cDataSize = 0, cPackedSize = 0,
                 OffsetBits = 0, nLinesCopied = 0;

    /* Get a pointer to the BITMAPOBJ structure */
    pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
937
    if (!pBmp) return hPackedDIB;
938 939 940 941 942

    /* Get the bitmap dimensions */
    width = pBmp->bitmap.bmWidth;
    height = pBmp->bitmap.bmHeight;
    depth = pBmp->bitmap.bmBitsPixel;
943

944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
    /*
     * A packed DIB contains a BITMAPINFO structure followed immediately by
     * an optional color palette and the pixel data.
     */

    /* Calculate the size of the packed DIB */
    cDataSize = DIB_GetDIBImageBytes( width, height, depth );
    cPackedSize = sizeof(BITMAPINFOHEADER)
                  + ( (depth <= 8) ? (sizeof(RGBQUAD) * (1 << depth)) : 0 )
                  + cDataSize;
    /* Get the offset to the bits */
    OffsetBits = cPackedSize - cDataSize;

    /* Allocate the packed DIB */
    TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
    hPackedDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE /*| GMEM_ZEROINIT*/,
                             cPackedSize );
    if ( !hPackedDIB )
    {
        WARN("Could not allocate packed DIB!\n");
        goto END;
    }
966

967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
    /* A packed DIB starts with a BITMAPINFOHEADER */
    pPackedDIB = (LPBYTE)GlobalLock(hPackedDIB);
    pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;

    /* Init the BITMAPINFOHEADER */
    pbmiHeader->biSize = sizeof(BITMAPINFOHEADER);
    pbmiHeader->biWidth = width;
    pbmiHeader->biHeight = height;
    pbmiHeader->biPlanes = 1;
    pbmiHeader->biBitCount = depth;
    pbmiHeader->biCompression = BI_RGB;
    pbmiHeader->biSizeImage = 0;
    pbmiHeader->biXPelsPerMeter = pbmiHeader->biYPelsPerMeter = 0;
    pbmiHeader->biClrUsed = 0;
    pbmiHeader->biClrImportant = 0;

    /* Retrieve the DIB bits from the bitmap and fill in the
     * DIB color table if present */
985

986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
    nLinesCopied = GetDIBits(hdc,                       /* Handle to device context */
                             hBmp,                      /* Handle to bitmap */
                             0,                         /* First scan line to set in dest bitmap */
                             height,                    /* Number of scan lines to copy */
                             pPackedDIB + OffsetBits,   /* [out] Address of array for bitmap bits */
                             (LPBITMAPINFO) pbmiHeader, /* [out] Address of BITMAPINFO structure */
                             0);                        /* RGB or palette index */
    GlobalUnlock(hPackedDIB);

    /* Cleanup if GetDIBits failed */
    if (nLinesCopied != height)
    {
        TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, height);
        GlobalFree(hPackedDIB);
        hPackedDIB = 0;
    }

END:
1004
    GDI_ReleaseObj( hBmp );
1005 1006
    return hPackedDIB;
}