dib.c 46.6 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
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard's avatar
Alexandre Julliard committed
19 20
 */

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/*
  Important information:
  
  * Current Windows versions support two different DIB structures:

    - BITMAPCOREINFO / BITMAPCOREHEADER (legacy structures; used in OS/2)
    - BITMAPINFO / BITMAPINFOHEADER
  
    Most Windows API functions taking a BITMAPINFO* / BITMAPINFOHEADER* also
    accept the old "core" structures, and so must WINE.
    You can distinguish them by looking at the first member (bcSize/biSize),
    or use the internal function DIB_GetBitmapInfo.

    
  * The palettes are stored in different formats:

    - BITMAPCOREINFO: Array of RGBTRIPLE
    - BITMAPINFO:     Array of RGBQUAD

    
  * There are even more DIB headers, but they all extend BITMAPINFOHEADER:
    
    - BITMAPV4HEADER: Introduced in Windows 95 / NT 4.0
    - BITMAPV5HEADER: Introduced in Windows 98 / 2000
45 46 47 48
    
    If biCompression is BI_BITFIELDS, the color masks are at the same position
    in all the headers (they start at bmiColors of BITMAPINFOHEADER), because
    the new headers have structure members for the masks.
49 50 51 52 53 54 55 56 57 58 59 60 61 62


  * You should never access the color table using the bmiColors member,
    because the passed structure may have one of the extended headers
    mentioned above. Use this to calculate the location:
    
    BITMAPINFO* info;
    void* colorPtr = (LPBYTE) info + (WORD) info->bmiHeader.biSize;

    
  * More information:
    Search for "Bitmap Structures" in MSDN
*/

63
#include <stdarg.h>
64
#include <stdlib.h>
65
#include <string.h>
66

67
#include "windef.h"
68
#include "winbase.h"
Michael Stefaniuc's avatar
Michael Stefaniuc committed
69
#include "wownt32.h"
70
#include "gdi_private.h"
71
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
72

73
WINE_DEFAULT_DEBUG_CHANNEL(bitmap);
74

75 76 77 78 79 80

/*
  Some of the following helper functions are duplicated in
  dlls/x11drv/dib.c
*/

Alexandre Julliard's avatar
Alexandre Julliard committed
81 82 83 84 85 86
/***********************************************************************
 *           DIB_GetDIBWidthBytes
 *
 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
 */
int DIB_GetDIBWidthBytes( int width, int depth )
Alexandre Julliard's avatar
Alexandre Julliard committed
87
{
Alexandre Julliard's avatar
Alexandre Julliard committed
88 89 90 91
    int words;

    switch(depth)
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
92 93 94 95 96 97 98 99
	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:
100
            WARN("(%d): Unsupported depth\n", depth );
Alexandre Julliard's avatar
Alexandre Julliard committed
101 102 103
	/* fall through */
	case 32:
	        words = width;
Alexandre Julliard's avatar
Alexandre Julliard committed
104 105
    }
    return 4 * words;
Alexandre Julliard's avatar
Alexandre Julliard committed
106
}
Alexandre Julliard's avatar
Alexandre Julliard committed
107

108 109 110 111 112 113 114 115 116 117
/***********************************************************************
 *           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
118

Alexandre Julliard's avatar
Alexandre Julliard committed
119
/***********************************************************************
120
 *           bitmap_info_size
Alexandre Julliard's avatar
Alexandre Julliard committed
121
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
122
 * Return the size of the bitmap info structure including color table.
Alexandre Julliard's avatar
Alexandre Julliard committed
123
 */
124
int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
Alexandre Julliard's avatar
Alexandre Julliard committed
125
{
126
    int colors, masks = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
127 128 129

    if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
    {
130
        const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
Alexandre Julliard's avatar
Alexandre Julliard committed
131
        colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
132 133 134 135 136 137
        return sizeof(BITMAPCOREHEADER) + colors *
             ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
    }
    else  /* assume BITMAPINFOHEADER */
    {
        colors = info->bmiHeader.biClrUsed;
138
        if (colors > 256) colors = 256;
Alexandre Julliard's avatar
Alexandre Julliard committed
139
        if (!colors && (info->bmiHeader.biBitCount <= 8))
Alexandre Julliard's avatar
Alexandre Julliard committed
140
            colors = 1 << info->bmiHeader.biBitCount;
141 142
        if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
        return sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) + colors *
Alexandre Julliard's avatar
Alexandre Julliard committed
143 144
               ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
145 146 147
}


Alexandre Julliard's avatar
Alexandre Julliard committed
148 149 150 151
/***********************************************************************
 *           DIB_GetBitmapInfo
 *
 * Get the info from a bitmap header.
152
 * Return 0 for COREHEADER, 1 for INFOHEADER, -1 for error.
Alexandre Julliard's avatar
Alexandre Julliard committed
153
 */
154 155
int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
                       LONG *height, WORD *planes, WORD *bpp, DWORD *compr, DWORD *size )
Alexandre Julliard's avatar
Alexandre Julliard committed
156 157 158
{
    if (header->biSize == sizeof(BITMAPCOREHEADER))
    {
159
        const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
Alexandre Julliard's avatar
Alexandre Julliard committed
160 161
        *width  = core->bcWidth;
        *height = core->bcHeight;
162
        *planes = core->bcPlanes;
Alexandre Julliard's avatar
Alexandre Julliard committed
163
        *bpp    = core->bcBitCount;
Alexandre Julliard's avatar
Alexandre Julliard committed
164
        *compr  = 0;
165
        *size   = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
166 167
        return 0;
    }
168
    if (header->biSize >= sizeof(BITMAPINFOHEADER)) /* assume BITMAPINFOHEADER */
169
    {
170 171 172 173 174 175 176
        *width  = header->biWidth;
        *height = header->biHeight;
        *planes = header->biPlanes;
        *bpp    = header->biBitCount;
        *compr  = header->biCompression;
        *size   = header->biSizeImage;
        return 1;
177
    }
178
    ERR("(%d): unknown/wrong size for header\n", header->biSize );
Alexandre Julliard's avatar
Alexandre Julliard committed
179 180 181 182
    return -1;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
183
/***********************************************************************
184
 *           StretchDIBits   (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
185
 */
186 187 188 189
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
190
{
191
    DC *dc;
192
    INT ret;
193 194 195

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

197
    if (!(dc = get_dc_ptr( hdc ))) return 0;
198

199
    if(dc->funcs->pStretchDIBits)
200
    {
201
        update_dc( dc );
202 203 204
        ret = dc->funcs->pStretchDIBits(dc->physDev, xDst, yDst, widthDst,
                                        heightDst, xSrc, ySrc, widthSrc,
                                        heightSrc, bits, info, wUsage, dwRop);
205
        release_dc_ptr( dc );
206 207 208
    }
    else /* use StretchBlt */
    {
209 210
        LONG height;
        LONG width;
211 212
        WORD planes, bpp;
        DWORD compr, size;
213 214
        HBITMAP hBitmap;
        BOOL fastpath = FALSE;
215

216
        release_dc_ptr( dc );
217

218
        if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height, &planes, &bpp, &compr, &size ) == -1)
219 220 221 222 223 224 225 226 227 228
        {
            ERR("Invalid bitmap\n");
            return 0;
        }

        if (width < 0)
        {
            ERR("Bitmap has a negative width\n");
            return 0;
        }
229

230 231 232 233 234 235
        hBitmap = GetCurrentObject(hdc, OBJ_BITMAP);

        if (xDst == 0 && yDst == 0 && xSrc == 0 && ySrc == 0 &&
            widthDst == widthSrc && heightDst == heightSrc &&
            info->bmiHeader.biCompression == BI_RGB &&
            dwRop == SRCCOPY)
236
        {
237
            BITMAPOBJ *bmp;
238
            if ((bmp = GDI_GetObjPtr( hBitmap, OBJ_BITMAP )))
239 240 241 242 243 244 245 246
            {
                if (bmp->bitmap.bmBitsPixel == bpp &&
                    bmp->bitmap.bmWidth == widthSrc &&
                    bmp->bitmap.bmHeight == heightSrc &&
                    bmp->bitmap.bmPlanes == planes)
                    fastpath = TRUE;
                GDI_ReleaseObj( hBitmap );
            }
247
        }
248

249 250 251 252
        if (fastpath)
        {
            /* fast path */
            TRACE("using fast path\n");
253
            ret = SetDIBits( hdc, hBitmap, 0, height, bits, info, wUsage);
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
        }
        else
        {
            /* slow path - need to use StretchBlt */
            HBITMAP hOldBitmap;
            HPALETTE hpal = NULL;
            HDC hdcMem;

            hdcMem = CreateCompatibleDC( hdc );
            hBitmap = CreateCompatibleBitmap(hdc, width, height);
            hOldBitmap = SelectObject( hdcMem, hBitmap );
            if(wUsage == DIB_PAL_COLORS)
            {
                hpal = GetCurrentObject(hdc, OBJ_PAL);
                hpal = SelectPalette(hdcMem, hpal, FALSE);
            }

            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.
                 * So, we first copy on a memory bitmap the current content of the
                 * 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)
                 */

                /* copy existing bitmap from destination dc */
                StretchBlt( hdcMem, xSrc, abs(height) - heightSrc - ySrc,
                            widthSrc, heightSrc, hdc, xDst, yDst, widthDst, heightDst,
                            dwRop );
            }

295
            ret = SetDIBits(hdcMem, hBitmap, 0, height, bits, info, wUsage);
296 297 298

            /* Origin for DIBitmap may be bottom left (positive biHeight) or top
               left (negative biHeight) */
299 300 301
            if (ret) StretchBlt( hdc, xDst, yDst, widthDst, heightDst,
                                 hdcMem, xSrc, abs(height) - heightSrc - ySrc,
                                 widthSrc, heightSrc, dwRop );
302 303 304 305 306
            if(hpal)
                SelectPalette(hdcMem, hpal, FALSE);
            SelectObject( hdcMem, hOldBitmap );
            DeleteDC( hdcMem );
            DeleteObject( hBitmap );
307
        }
308
    }
309
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
310 311
}

312

Alexandre Julliard's avatar
Alexandre Julliard committed
313
/******************************************************************************
Jon Griffiths's avatar
Jon Griffiths committed
314 315 316
 * SetDIBits [GDI32.@]
 *
 * Sets pixels in a bitmap using colors from DIB.
Alexandre Julliard's avatar
Alexandre Julliard committed
317 318 319 320 321 322 323 324 325 326 327 328 329
 *
 * 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
330
 */
331
INT WINAPI SetDIBits( HDC hdc, HBITMAP hbitmap, UINT startscan,
332 333
		      UINT lines, LPCVOID bits, const BITMAPINFO *info,
		      UINT coloruse )
Alexandre Julliard's avatar
Alexandre Julliard committed
334
{
335
    DC *dc;
336
    BITMAPOBJ *bitmap;
337
    INT result = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
338

339 340
    if (coloruse == DIB_RGB_COLORS) hdc = CreateCompatibleDC(0);

341
    if (!(dc = get_dc_ptr( hdc )))
342 343
        return 0;

344 345
    update_dc( dc );

346
    if (!(bitmap = GDI_GetObjPtr( hbitmap, OBJ_BITMAP )))
347
    {
348
        release_dc_ptr( dc );
349
        if (coloruse == DIB_RGB_COLORS) DeleteDC(hdc);
350 351
        return 0;
    }
352

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

355 356 357 358 359 360 361
    result = lines;
    if (bitmap->funcs)
    {
        if (bitmap->funcs != dc->funcs)
            ERR( "not supported: DDB bitmap %p not belonging to device %p\n", hbitmap, hdc );
        else if (dc->funcs->pSetDIBits)
            result = dc->funcs->pSetDIBits( dc->physDev, hbitmap, startscan, lines,
362
                                            bits, info, coloruse );
363
    }
364 365 366

 done:
    GDI_ReleaseObj( hbitmap );
367
    release_dc_ptr( dc );
368
    if (coloruse == DIB_RGB_COLORS) DeleteDC(hdc);
Alexandre Julliard's avatar
Alexandre Julliard committed
369
    return result;
Alexandre Julliard's avatar
Alexandre Julliard committed
370 371 372
}


Alexandre Julliard's avatar
Alexandre Julliard committed
373
/***********************************************************************
374
 *           SetDIBitsToDevice   (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
375
 */
376 377 378 379
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
380
{
381
    INT ret;
382
    DC *dc;
Alexandre Julliard's avatar
Alexandre Julliard committed
383

384 385
    if (!bits) return 0;

386
    if (!(dc = get_dc_ptr( hdc ))) return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
387

388
    if(dc->funcs->pSetDIBitsToDevice)
389 390
    {
        update_dc( dc );
391
        ret = dc->funcs->pSetDIBitsToDevice( dc->physDev, xDest, yDest, cx, cy, xSrc,
392 393
					     ySrc, startscan, lines, bits,
					     info, coloruse );
394
    }
395
    else {
396
        FIXME("unimplemented on hdc %p\n", hdc);
397
	ret = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
398
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
399

400
    release_dc_ptr( dc );
401
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
402 403
}

Alexandre Julliard's avatar
Alexandre Julliard committed
404
/***********************************************************************
405
 *           SetDIBColorTable    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
406
 */
407
UINT WINAPI SetDIBColorTable( HDC hdc, UINT startpos, UINT entries, CONST RGBQUAD *colors )
Alexandre Julliard's avatar
Alexandre Julliard committed
408 409
{
    DC * dc;
410
    UINT result = 0;
411
    BITMAPOBJ * bitmap;
Alexandre Julliard's avatar
Alexandre Julliard committed
412

413
    if (!(dc = get_dc_ptr( hdc ))) return 0;
414

415
    if ((bitmap = GDI_GetObjPtr( dc->hBitmap, OBJ_BITMAP )))
416 417 418 419 420 421 422 423 424 425 426 427 428
    {
        /* Check if currently selected bitmap is a DIB */
        if (bitmap->color_table)
        {
            if (startpos < bitmap->nb_colors)
            {
                if (startpos + entries > bitmap->nb_colors) entries = bitmap->nb_colors - startpos;
                memcpy(bitmap->color_table + startpos, colors, entries * sizeof(RGBQUAD));
                result = entries;
            }
        }
        GDI_ReleaseObj( dc->hBitmap );
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
429

430
    if (dc->funcs->pSetDIBColorTable)
431
        dc->funcs->pSetDIBColorTable(dc->physDev, startpos, entries, colors);
Alexandre Julliard's avatar
Alexandre Julliard committed
432

433
    release_dc_ptr( dc );
434
    return result;
Alexandre Julliard's avatar
Alexandre Julliard committed
435 436
}

Alexandre Julliard's avatar
Alexandre Julliard committed
437

Alexandre Julliard's avatar
Alexandre Julliard committed
438
/***********************************************************************
439
 *           GetDIBColorTable    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
440
 */
441
UINT WINAPI GetDIBColorTable( HDC hdc, UINT startpos, UINT entries, RGBQUAD *colors )
Alexandre Julliard's avatar
Alexandre Julliard committed
442 443
{
    DC * dc;
444
    UINT result = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
445

446
    if (!(dc = get_dc_ptr( hdc ))) return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
447

448 449
    if (dc->funcs->pGetDIBColorTable)
        result = dc->funcs->pGetDIBColorTable(dc->physDev, startpos, entries, colors);
450 451
    else
    {
452
        BITMAPOBJ *bitmap = GDI_GetObjPtr( dc->hBitmap, OBJ_BITMAP );
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
        if (bitmap)
        {
            /* Check if currently selected bitmap is a DIB */
            if (bitmap->color_table)
            {
                if (startpos < bitmap->nb_colors)
                {
                    if (startpos + entries > bitmap->nb_colors) entries = bitmap->nb_colors - startpos;
                    memcpy(colors, bitmap->color_table + startpos, entries * sizeof(RGBQUAD));
                    result = entries;
                }
            }
            GDI_ReleaseObj( dc->hBitmap );
        }
    }
468
    release_dc_ptr( dc );
469
    return result;
Alexandre Julliard's avatar
Alexandre Julliard committed
470
}
Alexandre Julliard's avatar
Alexandre Julliard committed
471

472 473 474
/* 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
475
   NB. RGBQUAD and PALETTEENTRY have different orderings of red, green
476 477
   and blue - sigh */

Andrew Ziem's avatar
Andrew Ziem committed
478
static const RGBQUAD EGAColorsQuads[16] = {
479
/* rgbBlue, rgbGreen, rgbRed, rgbReserved */
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
    { 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 }
};

Andrew Ziem's avatar
Andrew Ziem committed
498
static const RGBTRIPLE EGAColorsTriples[16] = {
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
/* rgbBlue, rgbGreen, rgbRed */
    { 0x00, 0x00, 0x00 },
    { 0x00, 0x00, 0x80 },
    { 0x00, 0x80, 0x00 },
    { 0x00, 0x80, 0x80 },
    { 0x80, 0x00, 0x00 },
    { 0x80, 0x00, 0x80 },
    { 0x80, 0x80, 0x00 },
    { 0x80, 0x80, 0x80 },
    { 0xc0, 0xc0, 0xc0 },
    { 0x00, 0x00, 0xff },
    { 0x00, 0xff, 0x00 },
    { 0x00, 0xff, 0xff },
    { 0xff, 0x00, 0x00 } ,
    { 0xff, 0x00, 0xff },
    { 0xff, 0xff, 0x00 },
    { 0xff, 0xff, 0xff }
};
517

Andrew Ziem's avatar
Andrew Ziem committed
518
static const RGBQUAD DefLogPaletteQuads[20] = { /* Copy of Default Logical Palette */
519
/* rgbBlue, rgbGreen, rgbRed, rgbReserved */
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
    { 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 }
};

Andrew Ziem's avatar
Andrew Ziem committed
542
static const RGBTRIPLE DefLogPaletteTriples[20] = { /* Copy of Default Logical Palette */
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
/* rgbBlue, rgbGreen, rgbRed */
    { 0x00, 0x00, 0x00 },
    { 0x00, 0x00, 0x80 },
    { 0x00, 0x80, 0x00 },
    { 0x00, 0x80, 0x80 },
    { 0x80, 0x00, 0x00 },
    { 0x80, 0x00, 0x80 },
    { 0x80, 0x80, 0x00 },
    { 0xc0, 0xc0, 0xc0 },
    { 0xc0, 0xdc, 0xc0 },
    { 0xf0, 0xca, 0xa6 },
    { 0xf0, 0xfb, 0xff },
    { 0xa4, 0xa0, 0xa0 },
    { 0x80, 0x80, 0x80 },
    { 0x00, 0x00, 0xf0 },
    { 0x00, 0xff, 0x00 },
    { 0x00, 0xff, 0xff },
    { 0xff, 0x00, 0x00 },
    { 0xff, 0x00, 0xff },
    { 0xff, 0xff, 0x00 },
    { 0xff, 0xff, 0xff}
};

Alexandre Julliard's avatar
Alexandre Julliard committed
566

Alexandre Julliard's avatar
Alexandre Julliard committed
567
/******************************************************************************
Jon Griffiths's avatar
Jon Griffiths committed
568 569 570
 * GetDIBits [GDI32.@]
 *
 * Retrieves bits of bitmap and copies to buffer.
Alexandre Julliard's avatar
Alexandre Julliard committed
571 572 573 574
 *
 * RETURNS
 *    Success: Number of scan lines copied from bitmap
 *    Failure: 0
Alexandre Julliard's avatar
Alexandre Julliard committed
575
 */
576 577 578 579 580
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 */
581
    LPVOID bits,       /* [out] Address of array for bitmap bits */
Alexandre Julliard's avatar
Alexandre Julliard committed
582
    BITMAPINFO * info, /* [out] Address of structure with bitmap data */
583
    UINT coloruse)   /* [in]  RGB or palette index */
Alexandre Julliard's avatar
Alexandre Julliard committed
584 585
{
    DC * dc;
Alexandre Julliard's avatar
Alexandre Julliard committed
586
    BITMAPOBJ * bmp;
587
    int i;
588 589 590 591
    int bitmap_type;
    BOOL core_header;
    LONG width;
    LONG height;
592 593
    WORD planes, bpp;
    DWORD compr, size;
594 595 596
    void* colorPtr;
    RGBTRIPLE* rgbTriples;
    RGBQUAD* rgbQuads;
Alexandre Julliard's avatar
Alexandre Julliard committed
597

598
    if (!info) return 0;
599

600
    bitmap_type = DIB_GetBitmapInfo( &info->bmiHeader, &width, &height, &planes, &bpp, &compr, &size);
601 602 603 604 605 606
    if (bitmap_type == -1)
    {
        ERR("Invalid bitmap format\n");
        return 0;
    }
    core_header = (bitmap_type == 0);
607
    if (!(dc = get_dc_ptr( hdc )))
608
    {
609
        SetLastError( ERROR_INVALID_PARAMETER );
610 611
        return 0;
    }
612
    update_dc( dc );
613
    if (!(bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP )))
614
    {
615
        release_dc_ptr( dc );
Alexandre Julliard's avatar
Alexandre Julliard committed
616
	return 0;
617
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
618

619
    colorPtr = (LPBYTE) info + (WORD) info->bmiHeader.biSize;
620 621
    rgbTriples = colorPtr;
    rgbQuads = colorPtr;
622

623
    /* Transfer color info */
624

625
    switch (bpp)
626
    {
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
    case 0:  /* query bitmap info only */
        if (core_header)
        {
            BITMAPCOREHEADER* coreheader = (BITMAPCOREHEADER*) info;
            coreheader->bcWidth = bmp->bitmap.bmWidth;
            coreheader->bcHeight = bmp->bitmap.bmHeight;
            coreheader->bcPlanes = 1;
            coreheader->bcBitCount = bmp->bitmap.bmBitsPixel;
        }
        else
        {
            info->bmiHeader.biWidth = bmp->bitmap.bmWidth;
            info->bmiHeader.biHeight = bmp->bitmap.bmHeight;
            info->bmiHeader.biPlanes = 1;
            info->bmiHeader.biSizeImage =
                DIB_GetDIBImageBytes( bmp->bitmap.bmWidth,
                                      bmp->bitmap.bmHeight,
                                      bmp->bitmap.bmBitsPixel );
645
            info->bmiHeader.biCompression = (bmp->bitmap.bmBitsPixel > 8) ? BI_BITFIELDS : BI_RGB;
646 647 648 649 650
            switch(bmp->bitmap.bmBitsPixel)
            {
            case 15:
                info->bmiHeader.biBitCount = 16;
                break;
651 652
            case 24:
                info->bmiHeader.biBitCount = 32;
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
                break;
            default:
                info->bmiHeader.biBitCount = bmp->bitmap.bmBitsPixel;
                break;
            }
            info->bmiHeader.biXPelsPerMeter = 0;
            info->bmiHeader.biYPelsPerMeter = 0;
            info->bmiHeader.biClrUsed = 0;
            info->bmiHeader.biClrImportant = 0;

            /* Windows 2000 doesn't touch the additional struct members if
               it's a BITMAPV4HEADER or a BITMAPV5HEADER */
        }
        lines = abs(bmp->bitmap.bmHeight);
        goto done;

    case 1:
    case 4:
    case 8:
672
        if (!core_header) info->bmiHeader.biClrUsed = 0;
673

674 675
	/* If the bitmap object already has a dib section at the
	   same color depth then get the color map from it */
676
	if (bmp->dib && bmp->dib->dsBm.bmBitsPixel == bpp) {
677
            if(coloruse == DIB_RGB_COLORS) {
678
                unsigned int colors = min( bmp->nb_colors, 1 << bpp );
679 680 681

                if (core_header)
                {
682 683
                    /* Convert the color table (RGBQUAD to RGBTRIPLE) */
                    RGBTRIPLE* index = rgbTriples;
684

685
                    for (i=0; i < colors; i++, index++)
686
                    {
687 688 689
                        index->rgbtRed   = bmp->color_table[i].rgbRed;
                        index->rgbtGreen = bmp->color_table[i].rgbGreen;
                        index->rgbtBlue  = bmp->color_table[i].rgbBlue;
690 691 692 693
                    }
                }
                else
                {
694 695
                    if (colors != 1 << bpp) info->bmiHeader.biClrUsed = colors;
                    memcpy(colorPtr, bmp->color_table, colors * sizeof(RGBQUAD));
696
                }
697
            }
698
            else {
699
                WORD *index = colorPtr;
700 701 702
                for(i = 0; i < 1 << info->bmiHeader.biBitCount; i++, index++)
                    *index = i;
            }
703
        }
704
        else {
705 706 707 708
            if (coloruse == DIB_PAL_COLORS) {
                for (i = 0; i < (1 << bpp); i++)
                    ((WORD *)colorPtr)[i] = (WORD)i;
            }
709 710 711 712
            else if(bpp > 1 && bpp == bmp->bitmap.bmBitsPixel) {
                /* For color DDBs in native depth (mono DDBs always have
                   a black/white palette):
                   Generate the color map from the selected palette */
713 714 715 716 717
                PALETTEENTRY palEntry[256];

                memset( palEntry, 0, sizeof(palEntry) );
                if (!GetPaletteEntries( dc->hPalette, 0, 1 << bmp->bitmap.bmBitsPixel, palEntry ))
                {
718
                    release_dc_ptr( dc );
719 720 721
                    GDI_ReleaseObj( hbitmap );
                    return 0;
                }
722
                for (i = 0; i < (1 << bmp->bitmap.bmBitsPixel); i++) {
723 724 725 726 727 728 729 730 731 732 733 734
                    if (core_header)
                    {
                        rgbTriples[i].rgbtRed   = palEntry[i].peRed;
                        rgbTriples[i].rgbtGreen = palEntry[i].peGreen;
                        rgbTriples[i].rgbtBlue  = palEntry[i].peBlue;
                    }
                    else
                    {
                        rgbQuads[i].rgbRed      = palEntry[i].peRed;
                        rgbQuads[i].rgbGreen    = palEntry[i].peGreen;
                        rgbQuads[i].rgbBlue     = palEntry[i].peBlue;
                        rgbQuads[i].rgbReserved = 0;
735 736 737
                    }
                }
            } else {
738
                switch (bpp) {
739
                case 1:
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
                    if (core_header)
                    {
                        rgbTriples[0].rgbtRed = rgbTriples[0].rgbtGreen =
                            rgbTriples[0].rgbtBlue = 0;
                        rgbTriples[1].rgbtRed = rgbTriples[1].rgbtGreen =
                            rgbTriples[1].rgbtBlue = 0xff;
                    }
                    else
                    {    
                        rgbQuads[0].rgbRed = rgbQuads[0].rgbGreen =
                            rgbQuads[0].rgbBlue = 0;
                        rgbQuads[0].rgbReserved = 0;
                        rgbQuads[1].rgbRed = rgbQuads[1].rgbGreen =
                            rgbQuads[1].rgbBlue = 0xff;
                        rgbQuads[1].rgbReserved = 0;
                    }
756 757 758
                    break;

                case 4:
759 760 761 762 763
                    if (core_header)
                        memcpy(colorPtr, EGAColorsTriples, sizeof(EGAColorsTriples));
                    else
                        memcpy(colorPtr, EGAColorsQuads, sizeof(EGAColorsQuads));

764 765 766 767
                    break;

                case 8:
                    {
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
                        if (core_header)
                        {
                            INT r, g, b;
                            RGBTRIPLE *color;

                            memcpy(rgbTriples, DefLogPaletteTriples,
                                       10 * sizeof(RGBTRIPLE));
                            memcpy(rgbTriples + 246, DefLogPaletteTriples + 10,
                                       10 * sizeof(RGBTRIPLE));
                            color = rgbTriples + 10;
                            for(r = 0; r <= 5; r++) /* FIXME */
                                for(g = 0; g <= 5; g++)
                                    for(b = 0; b <= 5; b++) {
                                        color->rgbtRed =   (r * 0xff) / 5;
                                        color->rgbtGreen = (g * 0xff) / 5;
                                        color->rgbtBlue =  (b * 0xff) / 5;
                                        color++;
                                    }
                        }
                        else
                        {
                            INT r, g, b;
                            RGBQUAD *color;

                            memcpy(rgbQuads, DefLogPaletteQuads,
                                       10 * sizeof(RGBQUAD));
                            memcpy(rgbQuads + 246, DefLogPaletteQuads + 10,
                                   10 * sizeof(RGBQUAD));
                            color = rgbQuads + 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++;
                                    }
                        }
807 808 809 810
                    }
                }
            }
        }
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
        break;

    case 15:
        if (info->bmiHeader.biCompression == BI_BITFIELDS)
        {
            ((PDWORD)info->bmiColors)[0] = 0x7c00;
            ((PDWORD)info->bmiColors)[1] = 0x03e0;
            ((PDWORD)info->bmiColors)[2] = 0x001f;
        }
        break;

    case 16:
        if (info->bmiHeader.biCompression == BI_BITFIELDS)
        {
            ((PDWORD)info->bmiColors)[0] = 0xf800;
            ((PDWORD)info->bmiColors)[1] = 0x07e0;
            ((PDWORD)info->bmiColors)[2] = 0x001f;
        }
        break;
830 831 832 833 834 835 836 837 838 839

    case 24:
    case 32:
        if (info->bmiHeader.biCompression == BI_BITFIELDS)
        {
            ((PDWORD)info->bmiColors)[0] = 0xff0000;
            ((PDWORD)info->bmiColors)[1] = 0x00ff00;
            ((PDWORD)info->bmiColors)[2] = 0x0000ff;
        }
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
840
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
841

842
    if (bits && lines)
843
    {
Andreas Mohr's avatar
Andreas Mohr committed
844
        /* If the bitmap object already have a dib section that contains image data, get the bits from it */
845
        if(bmp->dib && bmp->dib->dsBm.bmBitsPixel >= 15 && bpp >= 15)
Karl Lessard's avatar
Karl Lessard committed
846 847
        {
            /*FIXME: Only RGB dibs supported for now */
848
            unsigned int srcwidth = bmp->dib->dsBm.bmWidth, srcwidthb = bmp->dib->dsBm.bmWidthBytes;
849 850
            unsigned int dstwidth = width;
            int dstwidthb = DIB_GetDIBWidthBytes( width, bpp );
851
            LPBYTE dbits = bits, sbits = (LPBYTE) bmp->dib->dsBm.bmBits + (startscan * srcwidthb);
852
            unsigned int x, y, width, widthb;
Karl Lessard's avatar
Karl Lessard committed
853

854
            if ((height < 0) ^ (bmp->dib->dsBmih.biHeight < 0))
Karl Lessard's avatar
Karl Lessard committed
855
            {
Huw D M Davies's avatar
Huw D M Davies committed
856
                dbits = (LPBYTE)bits + (dstwidthb * (lines-1));
Karl Lessard's avatar
Karl Lessard committed
857 858 859
                dstwidthb = -dstwidthb;
            }

860
            switch( bpp ) {
Karl Lessard's avatar
Karl Lessard committed
861

Karl Lessard's avatar
Karl Lessard committed
862
	    case 15:
Karl Lessard's avatar
Karl Lessard committed
863 864 865 866 867 868 869 870 871 872 873
            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 */
                        {
874
                            widthb = min(srcwidthb, abs(dstwidthb));
Karl Lessard's avatar
Karl Lessard committed
875 876
                            /* FIXME: BI_BITFIELDS not supported yet */
                            for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
877
                                memcpy(dbits, sbits, widthb);
Karl Lessard's avatar
Karl Lessard committed
878 879 880 881 882
                        }
                        break;

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

885
                            width = min(srcwidth, dstwidth);
Karl Lessard's avatar
Karl Lessard committed
886
                            for( y = 0; y < lines; y++) {
887
                                for( x = 0; x < width; x++, srcbits += 3)
888 889 890 891
                                    *dstbits++ = ((srcbits[0] >> 3) & bmask) |
                                                 (((WORD)srcbits[1] << 2) & gmask) |
                                                 (((WORD)srcbits[2] << 7) & rmask);

Karl Lessard's avatar
Karl Lessard committed
892
                                dstbits = (LPWORD)(dbits+=dstwidthb);
Huw D M Davies's avatar
Huw D M Davies committed
893
                                srcbits = (sbits += srcwidthb);
Karl Lessard's avatar
Karl Lessard committed
894 895 896 897 898 899 900 901 902
                            }
                        }
                        break;

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

903
                            width = min(srcwidth, dstwidth);
Karl Lessard's avatar
Karl Lessard committed
904
                            for( y = 0; y < lines; y++) {
905
                                for( x = 0; x < width; x++ ) {
Karl Lessard's avatar
Karl Lessard committed
906
                                    val = *srcbits++;
907 908
                                    *dstbits++ = (WORD)(((val >> 3) & bmask) | ((val >> 6) & gmask) |
                                                       ((val >> 9) & rmask));
Karl Lessard's avatar
Karl Lessard committed
909
                                }
Huw D M Davies's avatar
Huw D M Davies committed
910 911
                                dstbits = (LPWORD)(dbits+=dstwidthb);
                                srcbits = (LPDWORD)(sbits+=srcwidthb);
Karl Lessard's avatar
Karl Lessard committed
912 913 914 915 916 917 918 919 920 921 922 923 924 925
                            }
                        }
                        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
926
                    LPBYTE dstbits = dbits;
Karl Lessard's avatar
Karl Lessard committed
927 928 929 930 931 932 933 934

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

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

935
                            width = min(srcwidth, dstwidth);
Karl Lessard's avatar
Karl Lessard committed
936 937
                            /* FIXME: BI_BITFIELDS not supported yet */
                            for( y = 0; y < lines; y++) {
938
                                for( x = 0; x < width; x++ ) {
Karl Lessard's avatar
Karl Lessard committed
939 940
                                    val = *srcbits++;
                                    *dstbits++ = (BYTE)(((val << 3) & 0xf8) | ((val >> 2) & 0x07));
941 942
                                    *dstbits++ = (BYTE)(((val >> 2) & 0xf8) | ((val >> 7) & 0x07));
                                    *dstbits++ = (BYTE)(((val >> 7) & 0xf8) | ((val >> 12) & 0x07));
Karl Lessard's avatar
Karl Lessard committed
943
                                }
944
                                dstbits = dbits+=dstwidthb;
Karl Lessard's avatar
Karl Lessard committed
945 946 947 948 949 950 951
                                srcbits = (LPWORD)(sbits+=srcwidthb);
                            }
                        }
                        break;

                    case 24: /* 24 bpp srcDIB -> 24 bpp dstDIB */
                        {
952
                            widthb = min(srcwidthb, abs(dstwidthb));
Karl Lessard's avatar
Karl Lessard committed
953
                            for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
954
                                memcpy(dbits, sbits, widthb);
Karl Lessard's avatar
Karl Lessard committed
955 956 957 958 959
                        }
                        break;

                    case 32: /* 32 bpp srcDIB -> 24 bpp dstDIB */
                        {
960
                            LPBYTE srcbits = sbits;
Karl Lessard's avatar
Karl Lessard committed
961

962
                            width = min(srcwidth, dstwidth);
Karl Lessard's avatar
Karl Lessard committed
963
                            for( y = 0; y < lines; y++) {
964
                                for( x = 0; x < width; x++, srcbits++ ) {
Karl Lessard's avatar
Karl Lessard committed
965 966 967 968
                                    *dstbits++ = *srcbits++;
                                    *dstbits++ = *srcbits++;
                                    *dstbits++ = *srcbits++;
                                }
969 970
                                dstbits = dbits+=dstwidthb;
                                srcbits = sbits+=srcwidthb;
Karl Lessard's avatar
Karl Lessard committed
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
                            }
                        }
                        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;

995
                            width = min(srcwidth, dstwidth);
Karl Lessard's avatar
Karl Lessard committed
996 997
                            /* FIXME: BI_BITFIELDS not supported yet */
                            for( y = 0; y < lines; y++) {
998
                                for( x = 0; x < width; x++ ) {
Karl Lessard's avatar
Karl Lessard committed
999
                                    val = (DWORD)*srcbits++;
1000
                                    *dstbits++ = ((val << 3) & 0xf8) | ((val >> 2) & 0x07) |
Karl Lessard's avatar
Karl Lessard committed
1001
                                                 ((val << 6) & 0xf800) | ((val << 1) & 0x0700) |
1002
                                                 ((val << 9) & 0xf80000) | ((val << 4) & 0x070000);
Karl Lessard's avatar
Karl Lessard committed
1003
                                }
Huw D M Davies's avatar
Huw D M Davies committed
1004 1005
                                dstbits=(LPDWORD)(dbits+=dstwidthb);
                                srcbits=(LPWORD)(sbits+=srcwidthb);
Karl Lessard's avatar
Karl Lessard committed
1006 1007 1008 1009 1010 1011
                            }
                        }
                        break;

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

1014
                            width = min(srcwidth, dstwidth);
Karl Lessard's avatar
Karl Lessard committed
1015
                            for( y = 0; y < lines; y++) {
1016
                                for( x = 0; x < width; x++, srcbits+=3 )
1017 1018 1019
                                    *dstbits++ =  srcbits[0] |
                                                 (srcbits[1] <<  8) |
                                                 (srcbits[2] << 16);
Huw D M Davies's avatar
Huw D M Davies committed
1020
                                dstbits=(LPDWORD)(dbits+=dstwidthb);
Karl Lessard's avatar
Karl Lessard committed
1021 1022 1023 1024 1025
                                srcbits=(sbits+=srcwidthb);
                            }
                        }
                        break;

1026
                    case 32: /* 32 bpp srcDIB -> 32 bpp dstDIB */
Karl Lessard's avatar
Karl Lessard committed
1027
                        {
1028
                            widthb = min(srcwidthb, abs(dstwidthb));
Karl Lessard's avatar
Karl Lessard committed
1029
                            /* FIXME: BI_BITFIELDS not supported yet */
1030 1031 1032
                            for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb) {
                                memcpy(dbits, sbits, widthb);
                            }
Karl Lessard's avatar
Karl Lessard committed
1033 1034 1035
                        }
                        break;

1036 1037
                    default: /* ? bit bmp -> 32 bit DIB */
                        FIXME("32 bit DIB %d bit bitmap\n",
Karl Lessard's avatar
Karl Lessard committed
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
                        bmp->bitmap.bmBitsPixel);
                        break;
                    }
                }
                break;

            default: /* ? bit DIB */
                FIXME("Unsupported DIB depth %d\n", info->bmiHeader.biBitCount);
                break;
            }
        }
        /* Otherwise, get bits from the XImage */
1050
        else
Alexandre Julliard's avatar
Alexandre Julliard committed
1051
        {
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
            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
1062
    }
1063
    else lines = abs(height);
1064

1065 1066 1067
    /* The knowledge base article Q81498 ("DIBs and Their Uses") states that
       if bits == NULL and bpp != 0, only biSizeImage and the color table are
       filled in. */
1068 1069
    if (!core_header)
    {
1070 1071 1072
        /* FIXME: biSizeImage should be calculated according to the selected
           compression algorithm if biCompression != BI_RGB */
        info->bmiHeader.biSizeImage = DIB_GetDIBImageBytes( width, height, bpp );
1073
        TRACE("biSizeImage = %d, ", info->bmiHeader.biSizeImage);
1074
    }
1075
    TRACE("biWidth = %d, biHeight = %d\n", width, height);
1076

1077
done:
1078
    release_dc_ptr( dc );
1079
    GDI_ReleaseObj( hbitmap );
Alexandre Julliard's avatar
Alexandre Julliard committed
1080 1081 1082 1083
    return lines;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1084
/***********************************************************************
1085
 *           CreateDIBitmap    (GDI32.@)
1086 1087 1088
 *
 * Creates a DDB (device dependent bitmap) from a DIB.
 * The DDB will have the same color depth as the reference DC.
Alexandre Julliard's avatar
Alexandre Julliard committed
1089
 */
1090
HBITMAP WINAPI CreateDIBitmap( HDC hdc, const BITMAPINFOHEADER *header,
Alexandre Julliard's avatar
Alexandre Julliard committed
1091
                            DWORD init, LPCVOID bits, const BITMAPINFO *data,
1092
                            UINT coloruse )
Alexandre Julliard's avatar
Alexandre Julliard committed
1093
{
1094
    HBITMAP handle;
1095 1096
    LONG width;
    LONG height;
1097 1098
    WORD planes, bpp;
    DWORD compr, size;
1099
    DC *dc;
Alexandre Julliard's avatar
Alexandre Julliard committed
1100

1101 1102
    if (!header) return 0;

1103
    if (DIB_GetBitmapInfo( header, &width, &height, &planes, &bpp, &compr, &size ) == -1) return 0;
1104 1105 1106 1107 1108 1109 1110 1111
    
    if (width < 0)
    {
        TRACE("Bitmap has a negative width\n");
        return 0;
    }
    
    /* Top-down DIBs have a negative height */
Alexandre Julliard's avatar
Alexandre Julliard committed
1112
    if (height < 0) height = -height;
Alexandre Julliard's avatar
Alexandre Julliard committed
1113

1114
    TRACE("hdc=%p, header=%p, init=%u, bits=%p, data=%p, coloruse=%u (bitmap: width=%d, height=%d, bpp=%u, compr=%u)\n",
1115 1116
           hdc, header, init, bits, data, coloruse, width, height, bpp, compr);
    
1117 1118
    if (hdc == NULL)
        handle = CreateBitmap( width, height, 1, 1, NULL );
Alexandre Julliard's avatar
Alexandre Julliard committed
1119
    else
1120
        handle = CreateCompatibleBitmap( hdc, width, height );
1121

1122 1123
    if (handle)
    {
Huw Davies's avatar
Huw Davies committed
1124
        if (init & CBM_INIT)
1125 1126 1127 1128 1129 1130 1131
        {
            if (SetDIBits( hdc, handle, 0, height, bits, data, coloruse ) == 0)
            {
                DeleteObject( handle );
                handle = 0;
            }
        }
1132

1133
        else if (hdc && ((dc = get_dc_ptr( hdc )) != NULL) )
1134
        {
1135 1136 1137 1138 1139
            if (!BITMAP_SetOwnerDC( handle, dc ))
            {
                DeleteObject( handle );
                handle = 0;
            }
1140
            release_dc_ptr( dc );
1141 1142
        }
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1143 1144

    return handle;
Alexandre Julliard's avatar
Alexandre Julliard committed
1145
}
Alexandre Julliard's avatar
Alexandre Julliard committed
1146

1147
/* Copy/synthesize RGB palette from BITMAPINFO. Ripped from dlls/winex11.drv/dib.c */
1148 1149 1150
static void DIB_CopyColorTable( DC *dc, BITMAPOBJ *bmp, WORD coloruse, const BITMAPINFO *info )
{
    RGBQUAD *colorTable;
1151
    unsigned int colors, i;
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
    BOOL core_info = info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER);

    if (core_info)
    {
        colors = 1 << ((const BITMAPCOREINFO*) info)->bmciHeader.bcBitCount;
    }
    else
    {
        colors = info->bmiHeader.biClrUsed;
        if (!colors) colors = 1 << info->bmiHeader.biBitCount;
    }

    if (colors > 256) {
        ERR("called with >256 colors!\n");
        return;
    }

    if (!(colorTable = HeapAlloc(GetProcessHeap(), 0, colors * sizeof(RGBQUAD) ))) return;

    if(coloruse == DIB_RGB_COLORS)
    {
        if (core_info)
        {
           /* Convert RGBTRIPLEs to RGBQUADs */
           for (i=0; i < colors; i++)
           {
               colorTable[i].rgbRed   = ((const BITMAPCOREINFO*) info)->bmciColors[i].rgbtRed;
               colorTable[i].rgbGreen = ((const BITMAPCOREINFO*) info)->bmciColors[i].rgbtGreen;
               colorTable[i].rgbBlue  = ((const BITMAPCOREINFO*) info)->bmciColors[i].rgbtBlue;
               colorTable[i].rgbReserved = 0;
           }
        }
        else
        {
            memcpy(colorTable, (const BYTE*) info + (WORD) info->bmiHeader.biSize, colors * sizeof(RGBQUAD));
        }
    }
    else
    {
1191
        PALETTEENTRY entries[256];
1192
        const WORD *index = (const WORD*) ((const BYTE*) info + (WORD) info->bmiHeader.biSize);
1193
        UINT count = GetPaletteEntries( dc->hPalette, 0, colors, entries );
1194

1195
        for (i = 0; i < colors; i++, index++)
1196
        {
1197 1198 1199 1200 1201
            PALETTEENTRY *entry = &entries[*index % count];
            colorTable[i].rgbRed = entry->peRed;
            colorTable[i].rgbGreen = entry->peGreen;
            colorTable[i].rgbBlue = entry->peBlue;
            colorTable[i].rgbReserved = 0;
1202 1203 1204 1205 1206 1207
        }
    }
    bmp->color_table = colorTable;
    bmp->nb_colors = colors;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1208
/***********************************************************************
1209
 *           CreateDIBSection    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1210
 */
1211 1212
HBITMAP WINAPI CreateDIBSection(HDC hdc, CONST BITMAPINFO *bmi, UINT usage,
                                VOID **bits, HANDLE section, DWORD offset)
Alexandre Julliard's avatar
Alexandre Julliard committed
1213
{
1214
    HBITMAP ret = 0;
1215 1216
    DC *dc;
    BOOL bDesktopDC = FALSE;
1217 1218 1219 1220 1221 1222 1223 1224
    DIBSECTION *dib;
    BITMAPOBJ *bmp;
    int bitmap_type;
    LONG width, height;
    WORD planes, bpp;
    DWORD compression, sizeImage;
    void *mapBits = NULL;

1225 1226 1227 1228 1229
    if(!bmi){
        if(bits) *bits = NULL;
        return NULL;
    }

1230 1231 1232 1233
    if (((bitmap_type = DIB_GetBitmapInfo( &bmi->bmiHeader, &width, &height,
                                           &planes, &bpp, &compression, &sizeImage )) == -1))
        return 0;

1234 1235
    if (compression != BI_RGB && compression != BI_BITFIELDS)
    {
1236
        TRACE("can't create a compressed (%u) dibsection\n", compression);
1237 1238 1239
        return 0;
    }

1240 1241
    if (!(dib = HeapAlloc( GetProcessHeap(), 0, sizeof(*dib) ))) return 0;

1242
    TRACE("format (%d,%d), planes %d, bpp %d, size %d, %s\n",
1243 1244 1245 1246 1247
          width, height, planes, bpp, sizeImage, usage == DIB_PAL_COLORS? "PAL" : "RGB");

    dib->dsBm.bmType       = 0;
    dib->dsBm.bmWidth      = width;
    dib->dsBm.bmHeight     = height >= 0 ? height : -height;
1248
    dib->dsBm.bmWidthBytes = DIB_GetDIBWidthBytes(width, bpp);
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
    dib->dsBm.bmPlanes     = planes;
    dib->dsBm.bmBitsPixel  = bpp;
    dib->dsBm.bmBits       = NULL;

    if (!bitmap_type)  /* core header */
    {
        /* convert the BITMAPCOREHEADER to a BITMAPINFOHEADER */
        dib->dsBmih.biSize = sizeof(BITMAPINFOHEADER);
        dib->dsBmih.biWidth = width;
        dib->dsBmih.biHeight = height;
        dib->dsBmih.biPlanes = planes;
        dib->dsBmih.biBitCount = bpp;
        dib->dsBmih.biCompression = compression;
        dib->dsBmih.biXPelsPerMeter = 0;
        dib->dsBmih.biYPelsPerMeter = 0;
        dib->dsBmih.biClrUsed = 0;
        dib->dsBmih.biClrImportant = 0;
    }
    else
    {
        /* truncate extended bitmap headers (BITMAPV4HEADER etc.) */
        dib->dsBmih = bmi->bmiHeader;
        dib->dsBmih.biSize = sizeof(BITMAPINFOHEADER);
    }

1274 1275 1276 1277
    /* set number of entries in bmi.bmiColors table */
    if( bpp <= 8 )
        dib->dsBmih.biClrUsed = 1 << bpp;

1278
    dib->dsBmih.biSizeImage = dib->dsBm.bmWidthBytes * dib->dsBm.bmHeight;
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288

    /* set dsBitfields values */
    if (usage == DIB_PAL_COLORS || bpp <= 8)
    {
        dib->dsBitfields[0] = dib->dsBitfields[1] = dib->dsBitfields[2] = 0;
    }
    else switch( bpp )
    {
    case 15:
    case 16:
1289 1290 1291
        dib->dsBitfields[0] = (compression == BI_BITFIELDS) ? *(const DWORD *)bmi->bmiColors       : 0x7c00;
        dib->dsBitfields[1] = (compression == BI_BITFIELDS) ? *((const DWORD *)bmi->bmiColors + 1) : 0x03e0;
        dib->dsBitfields[2] = (compression == BI_BITFIELDS) ? *((const DWORD *)bmi->bmiColors + 2) : 0x001f;
1292 1293 1294
        break;
    case 24:
    case 32:
1295 1296 1297
        dib->dsBitfields[0] = (compression == BI_BITFIELDS) ? *(const DWORD *)bmi->bmiColors       : 0xff0000;
        dib->dsBitfields[1] = (compression == BI_BITFIELDS) ? *((const DWORD *)bmi->bmiColors + 1) : 0x00ff00;
        dib->dsBitfields[2] = (compression == BI_BITFIELDS) ? *((const DWORD *)bmi->bmiColors + 2) : 0x0000ff;
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
        break;
    }

    /* get storage location for DIB bits */

    if (section)
    {
        SYSTEM_INFO SystemInfo;
        DWORD mapOffset;
        INT mapSize;

        GetSystemInfo( &SystemInfo );
        mapOffset = offset - (offset % SystemInfo.dwAllocationGranularity);
        mapSize = dib->dsBmih.biSizeImage + (offset - mapOffset);
        mapBits = MapViewOfFile( section, FILE_MAP_ALL_ACCESS, 0, mapOffset, mapSize );
        if (mapBits) dib->dsBm.bmBits = (char *)mapBits + (offset - mapOffset);
    }
    else
    {
        offset = 0;
        dib->dsBm.bmBits = VirtualAlloc( NULL, dib->dsBmih.biSizeImage,
                                         MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE );
    }
    dib->dshSection = section;
    dib->dsOffset = offset;

    if (!dib->dsBm.bmBits)
    {
        HeapFree( GetProcessHeap(), 0, dib );
        return 0;
    }
1329 1330 1331 1332 1333 1334 1335 1336

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

1337
    if (!(dc = get_dc_ptr( hdc ))) goto error;
1338 1339 1340 1341 1342

    /* create Device Dependent Bitmap and add DIB pointer */
    ret = CreateBitmap( dib->dsBm.bmWidth, dib->dsBm.bmHeight, 1,
                        (bpp == 1) ? 1 : GetDeviceCaps(hdc, BITSPIXEL), NULL );

1343
    if (ret && ((bmp = GDI_GetObjPtr(ret, OBJ_BITMAP))))
1344
    {
1345 1346
        bmp->dib = dib;
        bmp->funcs = dc->funcs;
1347 1348
        /* create local copy of DIB palette */
        if (bpp <= 8) DIB_CopyColorTable( dc, bmp, usage, bmi );
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
        GDI_ReleaseObj( ret );

        if (dc->funcs->pCreateDIBSection)
        {
            if (!dc->funcs->pCreateDIBSection(dc->physDev, ret, bmi, usage))
            {
                DeleteObject( ret );
                ret = 0;
            }
        }
1359
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1360

1361
    release_dc_ptr( dc );
1362 1363 1364
    if (bDesktopDC) DeleteDC( hdc );
    if (ret && bits) *bits = dib->dsBm.bmBits;
    return ret;
1365

1366 1367 1368 1369 1370 1371
error:
    if (bDesktopDC) DeleteDC( hdc );
    if (section) UnmapViewOfFile( mapBits );
    else if (!offset) VirtualFree( dib->dsBm.bmBits, 0, MEM_RELEASE );
    HeapFree( GetProcessHeap(), 0, dib );
    return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1372
}