dib.c 48.3 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
static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
155
                              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, BITMAP_MAGIC )))
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
    if (!(dc = get_dc_ptr( hdc )))
340 341
    {
        if (coloruse == DIB_RGB_COLORS) FIXME( "shouldn't require a DC for DIB_RGB_COLORS\n" );
342 343 344
        return 0;
    }

345 346
    update_dc( dc );

347 348
    if (!(bitmap = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
    {
349
        release_dc_ptr( dc );
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 );
Alexandre Julliard's avatar
Alexandre Julliard committed
368
    return result;
Alexandre Julliard's avatar
Alexandre Julliard committed
369 370 371
}


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

383 384
    if (!bits) return 0;

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

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

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

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

412
    if (!(dc = get_dc_ptr( hdc ))) return 0;
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427

    if ((bitmap = GDI_GetObjPtr( dc->hBitmap, BITMAP_MAGIC )))
    {
        /* 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
428

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

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

Alexandre Julliard's avatar
Alexandre Julliard committed
436

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

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

447 448
    if (dc->funcs->pGetDIBColorTable)
        result = dc->funcs->pGetDIBColorTable(dc->physDev, startpos, entries, colors);
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
    else
    {
        BITMAPOBJ *bitmap = GDI_GetObjPtr( dc->hBitmap, BITMAP_MAGIC );
        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 );
        }
    }
467
    release_dc_ptr( dc );
468
    return result;
Alexandre Julliard's avatar
Alexandre Julliard committed
469
}
Alexandre Julliard's avatar
Alexandre Julliard committed
470

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

Andrew Ziem's avatar
Andrew Ziem committed
477
static const RGBQUAD EGAColorsQuads[16] = {
478
/* rgbBlue, rgbGreen, rgbRed, rgbReserved */
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
    { 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
497
static const RGBTRIPLE EGAColorsTriples[16] = {
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
/* 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 }
};
516

Andrew Ziem's avatar
Andrew Ziem committed
517
static const RGBQUAD DefLogPaletteQuads[20] = { /* Copy of Default Logical Palette */
518
/* rgbBlue, rgbGreen, rgbRed, rgbReserved */
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
    { 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
541
static const RGBTRIPLE DefLogPaletteTriples[20] = { /* Copy of Default Logical Palette */
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
/* 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
565

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

597
    if (!info) return 0;
598

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

618 619 620
    colorPtr = (LPBYTE) info + (WORD) info->bmiHeader.biSize;
    rgbTriples = (RGBTRIPLE *) colorPtr;
    rgbQuads = (RGBQUAD *) colorPtr;
621

622
    /* Transfer color info */
623

624
    switch (bpp)
625
    {
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
    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 );
644
            info->bmiHeader.biCompression = (bmp->bitmap.bmBitsPixel > 8) ? BI_BITFIELDS : BI_RGB;
645 646 647 648 649
            switch(bmp->bitmap.bmBitsPixel)
            {
            case 15:
                info->bmiHeader.biBitCount = 16;
                break;
650 651
            case 24:
                info->bmiHeader.biBitCount = 32;
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
                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:
671
        if (!core_header) info->bmiHeader.biClrUsed = 0;
672

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

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

684
                    for (i=0; i < colors; i++, index++)
685
                    {
686 687 688
                        index->rgbtRed   = bmp->color_table[i].rgbRed;
                        index->rgbtGreen = bmp->color_table[i].rgbGreen;
                        index->rgbtBlue  = bmp->color_table[i].rgbBlue;
689 690 691 692
                    }
                }
                else
                {
693 694
                    if (colors != 1 << bpp) info->bmiHeader.biClrUsed = colors;
                    memcpy(colorPtr, bmp->color_table, colors * sizeof(RGBQUAD));
695
                }
696
            }
697
            else {
698
                WORD *index = colorPtr;
699 700 701
                for(i = 0; i < 1 << info->bmiHeader.biBitCount; i++, index++)
                    *index = i;
            }
702
        }
703
        else {
704 705 706 707
            if (coloruse == DIB_PAL_COLORS) {
                for (i = 0; i < (1 << bpp); i++)
                    ((WORD *)colorPtr)[i] = (WORD)i;
            }
708 709 710 711
            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 */
712 713 714 715 716
                PALETTEENTRY palEntry[256];

                memset( palEntry, 0, sizeof(palEntry) );
                if (!GetPaletteEntries( dc->hPalette, 0, 1 << bmp->bitmap.bmBitsPixel, palEntry ))
                {
717
                    release_dc_ptr( dc );
718 719 720
                    GDI_ReleaseObj( hbitmap );
                    return 0;
                }
721
                for (i = 0; i < (1 << bmp->bitmap.bmBitsPixel); i++) {
722 723 724 725 726 727 728 729 730 731 732 733
                    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;
734 735 736
                    }
                }
            } else {
737
                switch (bpp) {
738
                case 1:
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
                    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;
                    }
755 756 757
                    break;

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

763 764 765 766
                    break;

                case 8:
                    {
767 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
                        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++;
                                    }
                        }
806 807 808 809
                    }
                }
            }
        }
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
        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;
829 830 831 832 833 834 835 836 837 838

    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
839
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
840

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1064 1065 1066
    /* 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. */
1067 1068
    if (!core_header)
    {
1069 1070 1071
        /* FIXME: biSizeImage should be calculated according to the selected
           compression algorithm if biCompression != BI_RGB */
        info->bmiHeader.biSizeImage = DIB_GetDIBImageBytes( width, height, bpp );
1072
        TRACE("biSizeImage = %d, ", info->bmiHeader.biSizeImage);
1073
    }
1074
    TRACE("biWidth = %d, biHeight = %d\n", width, height);
1075

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


Alexandre Julliard's avatar
Alexandre Julliard committed
1083
/***********************************************************************
1084
 *           CreateDIBitmap    (GDI32.@)
1085 1086 1087
 *
 * 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
1088
 */
1089
HBITMAP WINAPI CreateDIBitmap( HDC hdc, const BITMAPINFOHEADER *header,
Alexandre Julliard's avatar
Alexandre Julliard committed
1090
                            DWORD init, LPCVOID bits, const BITMAPINFO *data,
1091
                            UINT coloruse )
Alexandre Julliard's avatar
Alexandre Julliard committed
1092
{
1093
    HBITMAP handle;
1094 1095
    LONG width;
    LONG height;
1096 1097
    WORD planes, bpp;
    DWORD compr, size;
1098
    DC *dc;
Alexandre Julliard's avatar
Alexandre Julliard committed
1099

1100
    if (DIB_GetBitmapInfo( header, &width, &height, &planes, &bpp, &compr, &size ) == -1) return 0;
1101 1102 1103 1104 1105 1106 1107 1108
    
    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
1109
    if (height < 0) height = -height;
Alexandre Julliard's avatar
Alexandre Julliard committed
1110

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

1119 1120
    if (handle)
    {
1121 1122 1123 1124 1125 1126 1127 1128
        if (init == CBM_INIT)
        {
            if (SetDIBits( hdc, handle, 0, height, bits, data, coloruse ) == 0)
            {
                DeleteObject( handle );
                handle = 0;
            }
        }
1129

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

    return handle;
Alexandre Julliard's avatar
Alexandre Julliard committed
1142
}
Alexandre Julliard's avatar
Alexandre Julliard committed
1143

Alexandre Julliard's avatar
Alexandre Julliard committed
1144
/***********************************************************************
1145
 *           CreateDIBSection    (GDI.489)
Alexandre Julliard's avatar
Alexandre Julliard committed
1146
 */
1147
HBITMAP16 WINAPI CreateDIBSection16 (HDC16 hdc, const BITMAPINFO *bmi, UINT16 usage,
1148
                                     SEGPTR *bits16, HANDLE section, DWORD offset)
Alexandre Julliard's avatar
Alexandre Julliard committed
1149
{
1150 1151
    LPVOID bits32;
    HBITMAP hbitmap;
1152

Michael Stefaniuc's avatar
Michael Stefaniuc committed
1153
    hbitmap = CreateDIBSection( HDC_32(hdc), bmi, usage, &bits32, section, offset );
1154
    if (hbitmap)
1155
    {
1156
        BITMAPOBJ *bmp = GDI_GetObjPtr(hbitmap, BITMAP_MAGIC);
1157 1158
        if (bmp && bmp->dib && bits32)
        {
1159
            const BITMAPINFOHEADER *bi = &bmi->bmiHeader;
1160
            LONG width, height;
1161 1162
            WORD planes, bpp;
            DWORD compr, size;
1163 1164 1165 1166
            INT width_bytes;
            WORD count, sel;
            int i;

1167
            DIB_GetBitmapInfo(bi, &width, &height, &planes, &bpp, &compr, &size);
1168 1169 1170

            height = height >= 0 ? height : -height;
            width_bytes = DIB_GetDIBWidthBytes(width, bpp);
1171 1172

            if (!size || (compr != BI_RLE4 && compr != BI_RLE8)) size = width_bytes * height;
1173

1174
            /* calculate number of sel's needed for size with 64K steps */
1175 1176
            count = (size + 0xffff) / 0x10000;
            sel = AllocSelectorArray16(count);
1177 1178 1179 1180 1181 1182 1183

            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;
            }
1184 1185 1186 1187
            bmp->segptr_bits = MAKESEGPTR( sel, 0 );
            if (bits16) *bits16 = bmp->segptr_bits;
        }
        if (bmp) GDI_ReleaseObj( hbitmap );
1188
    }
Michael Stefaniuc's avatar
Michael Stefaniuc committed
1189
    return HBITMAP_16(hbitmap);
Alexandre Julliard's avatar
Alexandre Julliard committed
1190 1191
}

1192
/* Copy/synthesize RGB palette from BITMAPINFO. Ripped from dlls/winex11.drv/dib.c */
1193 1194 1195
static void DIB_CopyColorTable( DC *dc, BITMAPOBJ *bmp, WORD coloruse, const BITMAPINFO *info )
{
    RGBQUAD *colorTable;
1196
    unsigned int colors, i;
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
    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
    {
1236
        PALETTEENTRY entries[256];
1237
        const WORD *index = (const WORD*) ((const BYTE*) info + (WORD) info->bmiHeader.biSize);
1238
        UINT count = GetPaletteEntries( dc->hPalette, 0, colors, entries );
1239

1240
        for (i = 0; i < colors; i++, index++)
1241
        {
1242 1243 1244 1245 1246
            PALETTEENTRY *entry = &entries[*index % count];
            colorTable[i].rgbRed = entry->peRed;
            colorTable[i].rgbGreen = entry->peGreen;
            colorTable[i].rgbBlue = entry->peBlue;
            colorTable[i].rgbReserved = 0;
1247 1248 1249 1250 1251 1252
        }
    }
    bmp->color_table = colorTable;
    bmp->nb_colors = colors;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1253
/***********************************************************************
1254
 *           CreateDIBSection    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1255
 */
1256 1257
HBITMAP WINAPI CreateDIBSection(HDC hdc, CONST BITMAPINFO *bmi, UINT usage,
                                VOID **bits, HANDLE section, DWORD offset)
Alexandre Julliard's avatar
Alexandre Julliard committed
1258
{
1259
    HBITMAP ret = 0;
1260 1261
    DC *dc;
    BOOL bDesktopDC = FALSE;
1262 1263 1264 1265 1266 1267 1268 1269
    DIBSECTION *dib;
    BITMAPOBJ *bmp;
    int bitmap_type;
    LONG width, height;
    WORD planes, bpp;
    DWORD compression, sizeImage;
    void *mapBits = NULL;

1270 1271 1272 1273 1274
    if(!bmi){
        if(bits) *bits = NULL;
        return NULL;
    }

1275 1276 1277 1278
    if (((bitmap_type = DIB_GetBitmapInfo( &bmi->bmiHeader, &width, &height,
                                           &planes, &bpp, &compression, &sizeImage )) == -1))
        return 0;

1279 1280
    if (compression != BI_RGB && compression != BI_BITFIELDS)
    {
1281
        TRACE("can't create a compressed (%u) dibsection\n", compression);
1282 1283 1284
        return 0;
    }

1285 1286
    if (!(dib = HeapAlloc( GetProcessHeap(), 0, sizeof(*dib) ))) return 0;

1287
    TRACE("format (%d,%d), planes %d, bpp %d, size %d, %s\n",
1288 1289 1290 1291 1292
          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;
1293
    dib->dsBm.bmWidthBytes = DIB_GetDIBWidthBytes(width, bpp);
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
    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);
    }

1319 1320 1321 1322
    /* set number of entries in bmi.bmiColors table */
    if( bpp <= 8 )
        dib->dsBmih.biClrUsed = 1 << bpp;

1323
    dib->dsBmih.biSizeImage = dib->dsBm.bmWidthBytes * dib->dsBm.bmHeight;
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333

    /* 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:
1334 1335 1336
        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;
1337 1338 1339
        break;
    case 24:
    case 32:
1340 1341 1342
        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;
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
        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;
    }
1374 1375 1376 1377 1378 1379 1380 1381

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

1382
    if (!(dc = get_dc_ptr( hdc ))) goto error;
1383 1384 1385 1386 1387 1388

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

    if (ret && ((bmp = GDI_GetObjPtr(ret, BITMAP_MAGIC))))
1389
    {
1390 1391
        bmp->dib = dib;
        bmp->funcs = dc->funcs;
1392 1393
        /* create local copy of DIB palette */
        if (bpp <= 8) DIB_CopyColorTable( dc, bmp, usage, bmi );
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
        GDI_ReleaseObj( ret );

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

1406
    release_dc_ptr( dc );
1407 1408 1409
    if (bDesktopDC) DeleteDC( hdc );
    if (ret && bits) *bits = dib->dsBm.bmBits;
    return ret;
1410

1411 1412 1413 1414 1415 1416
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
1417
}