Added entry for DirectSoundFullDuplexCreate.
[wine] / objects / dib.c
1 /*
2  * GDI device-independent bitmaps
3  *
4  * Copyright 1993,1994  Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "winbase.h"
25 #include "bitmap.h"
26 #include "selectors.h"
27 #include "gdi.h"
28 #include "wownt32.h"
29 #include "wine/debug.h"
30 #include "palette.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(bitmap);
33
34 /***********************************************************************
35  *           DIB_GetDIBWidthBytes
36  *
37  * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
38  * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
39  */
40 int DIB_GetDIBWidthBytes( int width, int depth )
41 {
42     int words;
43
44     switch(depth)
45     {
46         case 1:  words = (width + 31) / 32; break;
47         case 4:  words = (width + 7) / 8; break;
48         case 8:  words = (width + 3) / 4; break;
49         case 15:
50         case 16: words = (width + 1) / 2; break;
51         case 24: words = (width * 3 + 3)/4; break;
52
53         default:
54             WARN("(%d): Unsupported depth\n", depth );
55         /* fall through */
56         case 32:
57                 words = width;
58     }
59     return 4 * words;
60 }
61
62 /***********************************************************************
63  *           DIB_GetDIBImageBytes
64  *
65  * Return the number of bytes used to hold the image in a DIB bitmap.
66  */
67 int DIB_GetDIBImageBytes( int width, int height, int depth )
68 {
69     return DIB_GetDIBWidthBytes( width, depth ) * abs( height );
70 }
71
72
73 /***********************************************************************
74  *           DIB_BitmapInfoSize
75  *
76  * Return the size of the bitmap info structure including color table.
77  */
78 int DIB_BitmapInfoSize( const BITMAPINFO * info, WORD coloruse )
79 {
80     int colors;
81
82     if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
83     {
84         BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)info;
85         colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
86         return sizeof(BITMAPCOREHEADER) + colors *
87              ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
88     }
89     else  /* assume BITMAPINFOHEADER */
90     {
91         colors = info->bmiHeader.biClrUsed;
92         if (!colors && (info->bmiHeader.biBitCount <= 8))
93             colors = 1 << info->bmiHeader.biBitCount;
94         return sizeof(BITMAPINFOHEADER) + colors *
95                ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
96     }
97 }
98
99
100 /***********************************************************************
101  *           DIB_GetBitmapInfo
102  *
103  * Get the info from a bitmap header.
104  * Return 1 for INFOHEADER, 0 for COREHEADER,
105  * 4 for V4HEADER, 5 for V5HEADER, -1 for error.
106  */
107 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, DWORD *width,
108                               int *height, WORD *bpp, WORD *compr )
109 {
110     if (header->biSize == sizeof(BITMAPINFOHEADER))
111     {
112         *width  = header->biWidth;
113         *height = header->biHeight;
114         *bpp    = header->biBitCount;
115         *compr  = header->biCompression;
116         return 1;
117     }
118     if (header->biSize == sizeof(BITMAPCOREHEADER))
119     {
120         BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)header;
121         *width  = core->bcWidth;
122         *height = core->bcHeight;
123         *bpp    = core->bcBitCount;
124         *compr  = 0;
125         return 0;
126     }
127     if (header->biSize == sizeof(BITMAPV4HEADER))
128     {
129         BITMAPV4HEADER *v4hdr = (BITMAPV4HEADER *)header;
130         *width  = v4hdr->bV4Width;
131         *height = v4hdr->bV4Height;
132         *bpp    = v4hdr->bV4BitCount;
133         *compr  = v4hdr->bV4V4Compression;
134         return 4;
135     }
136     if (header->biSize == sizeof(BITMAPV5HEADER))
137     {
138         BITMAPV5HEADER *v5hdr = (BITMAPV5HEADER *)header;
139         *width  = v5hdr->bV5Width;
140         *height = v5hdr->bV5Height;
141         *bpp    = v5hdr->bV5BitCount;
142         *compr  = v5hdr->bV5Compression;
143         return 5;
144     }
145     ERR("(%ld): unknown/wrong size for header\n", header->biSize );
146     return -1;
147 }
148
149
150 /***********************************************************************
151  *           StretchDIBits   (GDI32.@)
152  */
153 INT WINAPI StretchDIBits(HDC hdc, INT xDst, INT yDst, INT widthDst,
154                        INT heightDst, INT xSrc, INT ySrc, INT widthSrc,
155                        INT heightSrc, const void *bits,
156                        const BITMAPINFO *info, UINT wUsage, DWORD dwRop )
157 {
158     DC *dc;
159
160     if (!bits || !info)
161         return 0;
162
163     dc = DC_GetDCUpdate( hdc );
164     if(!dc) return FALSE;
165     
166     if(dc->funcs->pStretchDIBits)
167     {
168         heightSrc = dc->funcs->pStretchDIBits(dc->physDev, xDst, yDst, widthDst,
169                                               heightDst, xSrc, ySrc, widthSrc,
170                                               heightSrc, bits, info, wUsage, dwRop);
171         GDI_ReleaseObj( hdc );
172     }
173     else /* use StretchBlt */
174     {
175         HBITMAP hBitmap, hOldBitmap;
176         HDC hdcMem;
177
178         GDI_ReleaseObj( hdc );
179         hdcMem = CreateCompatibleDC( hdc );
180         if (info->bmiHeader.biCompression == BI_RLE4 ||
181             info->bmiHeader.biCompression == BI_RLE8) {
182
183            /* when RLE compression is used, there may be some gaps (ie the DIB doesn't
184             * contain all the rectangle described in bmiHeader, but only part of it.
185             * This mean that those undescribed pixels must be left untouched.
186             * So, we first copy on a memory bitmap the current content of the
187             * destination rectangle, blit the DIB bits on top of it - hence leaving
188             * the gaps untouched -, and blitting the rectangle back.
189             * This insure that gaps are untouched on the destination rectangle
190             * Not doing so leads to trashed images (the gaps contain what was on the
191             * memory bitmap => generally black or garbage)
192             * Unfortunately, RLE DIBs without gaps will be slowed down. But this is
193             * another speed vs correctness issue. Anyway, if speed is needed, then the
194             * pStretchDIBits function shall be implemented.
195             * ericP (2000/09/09)
196             */
197            hBitmap = CreateCompatibleBitmap(hdc, info->bmiHeader.biWidth,
198                                             info->bmiHeader.biHeight);
199            hOldBitmap = SelectObject( hdcMem, hBitmap );
200
201            /* copy existing bitmap from destination dc */
202            StretchBlt( hdcMem, xSrc, abs(info->bmiHeader.biHeight) - heightSrc - ySrc,
203                        widthSrc, heightSrc, hdc, xDst, yDst, widthDst, heightDst,
204                        dwRop );
205            SetDIBits(hdcMem, hBitmap, 0, info->bmiHeader.biHeight, bits,
206                      info, DIB_RGB_COLORS);
207
208         } else {
209            hBitmap = CreateDIBitmap( hdc, &info->bmiHeader, CBM_INIT,
210                                      bits, info, wUsage );
211            hOldBitmap = SelectObject( hdcMem, hBitmap );
212         }
213
214         /* Origin for DIBitmap may be bottom left (positive biHeight) or top
215            left (negative biHeight) */
216         StretchBlt( hdc, xDst, yDst, widthDst, heightDst,
217                     hdcMem, xSrc, abs(info->bmiHeader.biHeight) - heightSrc - ySrc,
218                     widthSrc, heightSrc, dwRop );
219         SelectObject( hdcMem, hOldBitmap );
220         DeleteDC( hdcMem );
221         DeleteObject( hBitmap );
222     }
223     return heightSrc;
224 }
225
226
227 /******************************************************************************
228  * SetDIBits [GDI32.@]  Sets pixels in a bitmap using colors from DIB
229  *
230  * PARAMS
231  *    hdc       [I] Handle to device context
232  *    hbitmap   [I] Handle to bitmap
233  *    startscan [I] Starting scan line
234  *    lines     [I] Number of scan lines
235  *    bits      [I] Array of bitmap bits
236  *    info      [I] Address of structure with data
237  *    coloruse  [I] Type of color indexes to use
238  *
239  * RETURNS
240  *    Success: Number of scan lines copied
241  *    Failure: 0
242  */
243 INT WINAPI SetDIBits( HDC hdc, HBITMAP hbitmap, UINT startscan,
244                       UINT lines, LPCVOID bits, const BITMAPINFO *info,
245                       UINT coloruse )
246 {
247     DC *dc;
248     BITMAPOBJ *bitmap;
249     INT result = 0;
250
251     if (!(bitmap = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return 0;
252
253     if (!(dc = DC_GetDCUpdate( hdc )))
254     {
255         if (coloruse == DIB_RGB_COLORS) FIXME( "shouldn't require a DC for DIB_RGB_COLORS\n" );
256         GDI_ReleaseObj( hbitmap );
257         return 0;
258     }
259
260     if (!bitmap->funcs && !BITMAP_SetOwnerDC( hbitmap, dc )) goto done;
261
262     if (bitmap->funcs && bitmap->funcs->pSetDIBits)
263         result = bitmap->funcs->pSetDIBits( dc->physDev, hbitmap, startscan, lines,
264                                             bits, info, coloruse );
265     else
266         result = lines;
267
268  done:
269     GDI_ReleaseObj( hdc );
270     GDI_ReleaseObj( hbitmap );
271     return result;
272 }
273
274
275 /***********************************************************************
276  *           SetDIBitsToDevice   (GDI32.@)
277  */
278 INT WINAPI SetDIBitsToDevice(HDC hdc, INT xDest, INT yDest, DWORD cx,
279                            DWORD cy, INT xSrc, INT ySrc, UINT startscan,
280                            UINT lines, LPCVOID bits, const BITMAPINFO *info,
281                            UINT coloruse )
282 {
283     INT ret;
284     DC *dc;
285
286     if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
287
288     if(dc->funcs->pSetDIBitsToDevice)
289         ret = dc->funcs->pSetDIBitsToDevice( dc->physDev, xDest, yDest, cx, cy, xSrc,
290                                              ySrc, startscan, lines, bits,
291                                              info, coloruse );
292     else {
293         FIXME("unimplemented on hdc %p\n", hdc);
294         ret = 0;
295     }
296
297     GDI_ReleaseObj( hdc );
298     return ret;
299 }
300
301 /***********************************************************************
302  *           SetDIBColorTable    (GDI32.@)
303  */
304 UINT WINAPI SetDIBColorTable( HDC hdc, UINT startpos, UINT entries, RGBQUAD *colors )
305 {
306     DC * dc;
307     UINT result = 0;
308
309     if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
310
311     if (dc->funcs->pSetDIBColorTable)
312         result = dc->funcs->pSetDIBColorTable(dc->physDev, startpos, entries, colors);
313
314     GDI_ReleaseObj( hdc );
315     return result;
316 }
317
318
319 /***********************************************************************
320  *           GetDIBColorTable    (GDI32.@)
321  */
322 UINT WINAPI GetDIBColorTable( HDC hdc, UINT startpos, UINT entries, RGBQUAD *colors )
323 {
324     DC * dc;
325     UINT result = 0;
326
327     if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
328
329     if (dc->funcs->pGetDIBColorTable)
330         result = dc->funcs->pGetDIBColorTable(dc->physDev, startpos, entries, colors);
331
332     GDI_ReleaseObj( hdc );
333     return result;
334 }
335
336 /* FIXME the following two structs should be combined with __sysPalTemplate in
337    objects/color.c - this should happen after de-X11-ing both of these
338    files.
339    NB. RGBQUAD and PALETTEENTRY have different orderings of red, green
340    and blue - sigh */
341
342 static RGBQUAD EGAColors[16] = {
343 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
344     { 0x00, 0x00, 0x00, 0x00 },
345     { 0x00, 0x00, 0x80, 0x00 },
346     { 0x00, 0x80, 0x00, 0x00 },
347     { 0x00, 0x80, 0x80, 0x00 },
348     { 0x80, 0x00, 0x00, 0x00 },
349     { 0x80, 0x00, 0x80, 0x00 },
350     { 0x80, 0x80, 0x00, 0x00 },
351     { 0x80, 0x80, 0x80, 0x00 },
352     { 0xc0, 0xc0, 0xc0, 0x00 },
353     { 0x00, 0x00, 0xff, 0x00 },
354     { 0x00, 0xff, 0x00, 0x00 },
355     { 0x00, 0xff, 0xff, 0x00 },
356     { 0xff, 0x00, 0x00, 0x00 },
357     { 0xff, 0x00, 0xff, 0x00 },
358     { 0xff, 0xff, 0x00, 0x00 },
359     { 0xff, 0xff, 0xff, 0x00 }
360 };
361
362
363 static RGBQUAD DefLogPalette[20] = { /* Copy of Default Logical Palette */
364 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
365     { 0x00, 0x00, 0x00, 0x00 },
366     { 0x00, 0x00, 0x80, 0x00 },
367     { 0x00, 0x80, 0x00, 0x00 },
368     { 0x00, 0x80, 0x80, 0x00 },
369     { 0x80, 0x00, 0x00, 0x00 },
370     { 0x80, 0x00, 0x80, 0x00 },
371     { 0x80, 0x80, 0x00, 0x00 },
372     { 0xc0, 0xc0, 0xc0, 0x00 },
373     { 0xc0, 0xdc, 0xc0, 0x00 },
374     { 0xf0, 0xca, 0xa6, 0x00 },
375     { 0xf0, 0xfb, 0xff, 0x00 },
376     { 0xa4, 0xa0, 0xa0, 0x00 },
377     { 0x80, 0x80, 0x80, 0x00 },
378     { 0x00, 0x00, 0xf0, 0x00 },
379     { 0x00, 0xff, 0x00, 0x00 },
380     { 0x00, 0xff, 0xff, 0x00 },
381     { 0xff, 0x00, 0x00, 0x00 },
382     { 0xff, 0x00, 0xff, 0x00 },
383     { 0xff, 0xff, 0x00, 0x00 },
384     { 0xff, 0xff, 0xff, 0x00 }
385 };
386
387
388 /******************************************************************************
389  * GetDIBits [GDI32.@]  Retrieves bits of bitmap and copies to buffer
390  *
391  * RETURNS
392  *    Success: Number of scan lines copied from bitmap
393  *    Failure: 0
394  *
395  * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/func/src/f30_14.htm
396  */
397 INT WINAPI GetDIBits(
398     HDC hdc,         /* [in]  Handle to device context */
399     HBITMAP hbitmap, /* [in]  Handle to bitmap */
400     UINT startscan,  /* [in]  First scan line to set in dest bitmap */
401     UINT lines,      /* [in]  Number of scan lines to copy */
402     LPVOID bits,       /* [out] Address of array for bitmap bits */
403     BITMAPINFO * info, /* [out] Address of structure with bitmap data */
404     UINT coloruse)   /* [in]  RGB or palette index */
405 {
406     DC * dc;
407     BITMAPOBJ * bmp;
408     PALETTEENTRY * palEntry;
409     PALETTEOBJ * palette;
410     int i;
411
412     if (!info) return 0;
413     if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
414     if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
415     {
416         GDI_ReleaseObj( hdc );
417         return 0;
418     }
419     if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->hPalette, PALETTE_MAGIC )))
420     {
421         GDI_ReleaseObj( hdc );
422         GDI_ReleaseObj( hbitmap );
423         return 0;
424     }
425
426     /* Transfer color info */
427
428     if (info->bmiHeader.biBitCount <= 8 && info->bmiHeader.biBitCount > 0 ) {
429
430         info->bmiHeader.biClrUsed = 0;
431
432         if(info->bmiHeader.biBitCount >= bmp->bitmap.bmBitsPixel) {
433             palEntry = palette->logpalette.palPalEntry;
434             for (i = 0; i < (1 << bmp->bitmap.bmBitsPixel); i++, palEntry++) {
435                 if (coloruse == DIB_RGB_COLORS) {
436                     info->bmiColors[i].rgbRed      = palEntry->peRed;
437                     info->bmiColors[i].rgbGreen    = palEntry->peGreen;
438                     info->bmiColors[i].rgbBlue     = palEntry->peBlue;
439                     info->bmiColors[i].rgbReserved = 0;
440                 }
441                 else ((WORD *)info->bmiColors)[i] = (WORD)i;
442             }
443         } else {
444             switch (info->bmiHeader.biBitCount) {
445             case 1:
446                 info->bmiColors[0].rgbRed = info->bmiColors[0].rgbGreen =
447                   info->bmiColors[0].rgbBlue = 0;
448                 info->bmiColors[0].rgbReserved = 0;
449                 info->bmiColors[1].rgbRed = info->bmiColors[1].rgbGreen =
450                   info->bmiColors[1].rgbBlue = 0xff;
451                 info->bmiColors[1].rgbReserved = 0;
452                 break;
453
454             case 4:
455                 memcpy(info->bmiColors, EGAColors, sizeof(EGAColors));
456                 break;
457
458             case 8:
459                 {
460                 INT r, g, b;
461                 RGBQUAD *color;
462
463                 memcpy(info->bmiColors, DefLogPalette,
464                        10 * sizeof(RGBQUAD));
465                 memcpy(info->bmiColors + 246, DefLogPalette + 10,
466                        10 * sizeof(RGBQUAD));
467                 color = info->bmiColors + 10;
468                 for(r = 0; r <= 5; r++) /* FIXME */
469                     for(g = 0; g <= 5; g++)
470                         for(b = 0; b <= 5; b++) {
471                             color->rgbRed =   (r * 0xff) / 5;
472                             color->rgbGreen = (g * 0xff) / 5;
473                             color->rgbBlue =  (b * 0xff) / 5;
474                             color->rgbReserved = 0;
475                             color++;
476                         }
477                 }
478             }
479         }
480     }
481
482     GDI_ReleaseObj( dc->hPalette );
483
484     if (bits && lines)
485     {
486         /* If the bitmap object already have a dib section that contains image data, get the bits from it */
487         if(bmp->dib && bmp->dib->dsBm.bmBitsPixel >= 15 && info->bmiHeader.biBitCount >= 15)
488         {
489             /*FIXME: Only RGB dibs supported for now */
490             unsigned int srcwidth = bmp->dib->dsBm.bmWidth, srcwidthb = bmp->dib->dsBm.bmWidthBytes;
491             int dstwidthb = DIB_GetDIBWidthBytes( info->bmiHeader.biWidth, info->bmiHeader.biBitCount );
492             LPBYTE dbits = bits, sbits = (LPBYTE) bmp->dib->dsBm.bmBits + (startscan * srcwidthb);
493             unsigned int x, y;
494
495             if ((info->bmiHeader.biHeight < 0) ^ (bmp->dib->dsBmih.biHeight < 0))
496             {
497                 dbits = (LPBYTE)bits + (dstwidthb * (lines-1));
498                 dstwidthb = -dstwidthb;
499             }
500
501             switch( info->bmiHeader.biBitCount ) {
502
503             case 15:
504             case 16: /* 16 bpp dstDIB */
505                 {
506                     LPWORD dstbits = (LPWORD)dbits;
507                     WORD rmask = 0x7c00, gmask= 0x03e0, bmask = 0x001f;
508
509                     /* FIXME: BI_BITFIELDS not supported yet */
510
511                     switch(bmp->dib->dsBm.bmBitsPixel) {
512
513                     case 16: /* 16 bpp srcDIB -> 16 bpp dstDIB */
514                         {
515                             /* FIXME: BI_BITFIELDS not supported yet */
516                             for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
517                                 memcpy(dbits, sbits, srcwidthb);
518                         }
519                         break;
520
521                     case 24: /* 24 bpp srcDIB -> 16 bpp dstDIB */
522                         {
523                             LPBYTE srcbits = sbits;
524
525                             for( y = 0; y < lines; y++) {
526                                 for( x = 0; x < srcwidth; x++, srcbits += 3)
527                                     *dstbits++ = ((srcbits[0] >> 3) & bmask) |
528                                                  (((WORD)srcbits[1] << 2) & gmask) |
529                                                  (((WORD)srcbits[2] << 7) & rmask);
530
531                                 dstbits = (LPWORD)(dbits+=dstwidthb);
532                                 srcbits = (sbits += srcwidthb);
533                             }
534                         }
535                         break;
536
537                     case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
538                         {
539                             LPDWORD srcbits = (LPDWORD)sbits;
540                             DWORD val;
541
542                             for( y = 0; y < lines; y++) {
543                                 for( x = 0; x < srcwidth; x++ ) {
544                                     val = *srcbits++;
545                                     *dstbits++ = (WORD)(((val >> 3) & bmask) | ((val >> 6) & gmask) |
546                                                        ((val >> 9) & rmask));
547                                 }
548                                 dstbits = (LPWORD)(dbits+=dstwidthb);
549                                 srcbits = (LPDWORD)(sbits+=srcwidthb);
550                             }
551                         }
552                         break;
553
554                     default: /* ? bit bmp -> 16 bit DIB */
555                         FIXME("15/16 bit DIB %d bit bitmap\n",
556                         bmp->bitmap.bmBitsPixel);
557                         break;
558                     }
559                 }
560                 break;
561
562             case 24: /* 24 bpp dstDIB */
563                 {
564                     LPBYTE dstbits = dbits;
565
566                     switch(bmp->dib->dsBm.bmBitsPixel) {
567
568                     case 16: /* 16 bpp srcDIB -> 24 bpp dstDIB */
569                         {
570                             LPWORD srcbits = (LPWORD)sbits;
571                             WORD val;
572
573                             /* FIXME: BI_BITFIELDS not supported yet */
574                             for( y = 0; y < lines; y++) {
575                                 for( x = 0; x < srcwidth; x++ ) {
576                                     val = *srcbits++;
577                                     *dstbits++ = (BYTE)(((val << 3) & 0xf8) | ((val >> 2) & 0x07));
578                                     *dstbits++ = (BYTE)(((val >> 2) & 0xf8) | ((val >> 7) & 0x07));
579                                     *dstbits++ = (BYTE)(((val >> 7) & 0xf8) | ((val >> 12) & 0x07));
580                                 }
581                                 dstbits = (LPBYTE)(dbits+=dstwidthb);
582                                 srcbits = (LPWORD)(sbits+=srcwidthb);
583                             }
584                         }
585                         break;
586
587                     case 24: /* 24 bpp srcDIB -> 24 bpp dstDIB */
588                         {
589                             for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
590                                 memcpy(dbits, sbits, srcwidthb);
591                         }
592                         break;
593
594                     case 32: /* 32 bpp srcDIB -> 24 bpp dstDIB */
595                         {
596                             LPBYTE srcbits = (LPBYTE)sbits;
597
598                             for( y = 0; y < lines; y++) {
599                                 for( x = 0; x < srcwidth; x++, srcbits++ ) {
600                                     *dstbits++ = *srcbits++;
601                                     *dstbits++ = *srcbits++;
602                                     *dstbits++ = *srcbits++;
603                                 }
604                                 dstbits=(LPBYTE)(dbits+=dstwidthb);
605                                 srcbits = (LPBYTE)(sbits+=srcwidthb);
606                             }
607                         }
608                         break;
609
610                     default: /* ? bit bmp -> 24 bit DIB */
611                         FIXME("24 bit DIB %d bit bitmap\n",
612                               bmp->bitmap.bmBitsPixel);
613                         break;
614                     }
615                 }
616                 break;
617
618             case 32: /* 32 bpp dstDIB */
619                 {
620                     LPDWORD dstbits = (LPDWORD)dbits;
621
622                     /* FIXME: BI_BITFIELDS not supported yet */
623
624                     switch(bmp->dib->dsBm.bmBitsPixel) {
625                         case 16: /* 16 bpp srcDIB -> 32 bpp dstDIB */
626                         {
627                             LPWORD srcbits = (LPWORD)sbits;
628                             DWORD val;
629
630                             /* FIXME: BI_BITFIELDS not supported yet */
631                             for( y = 0; y < lines; y++) {
632                                 for( x = 0; x < srcwidth; x++ ) {
633                                     val = (DWORD)*srcbits++;
634                                     *dstbits++ = ((val << 3) & 0xf8) | ((val >> 2) & 0x07) |
635                                                  ((val << 6) & 0xf800) | ((val << 1) & 0x0700) |
636                                                  ((val << 9) & 0xf80000) | ((val << 4) & 0x070000);
637                                 }
638                                 dstbits=(LPDWORD)(dbits+=dstwidthb);
639                                 srcbits=(LPWORD)(sbits+=srcwidthb);
640                             }
641                         }
642                         break;
643
644                     case 24: /* 24 bpp srcDIB -> 32 bpp dstDIB */
645                         {
646                             LPBYTE srcbits = sbits;
647
648                             for( y = 0; y < lines; y++) {
649                                 for( x = 0; x < srcwidth; x++, srcbits+=3 )
650                                     *dstbits++ = ((DWORD)*srcbits) & 0x00ffffff;
651                                 dstbits=(LPDWORD)(dbits+=dstwidthb);
652                                 srcbits=(sbits+=srcwidthb);
653                             }
654                         }
655                         break;
656
657                     case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
658                         {
659                             /* FIXME: BI_BITFIELDS not supported yet */
660                             for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
661                                 memcpy(dbits, sbits, srcwidthb);
662                         }
663                         break;
664
665                     default: /* ? bit bmp -> 16 bit DIB */
666                         FIXME("15/16 bit DIB %d bit bitmap\n",
667                         bmp->bitmap.bmBitsPixel);
668                         break;
669                     }
670                 }
671                 break;
672
673             default: /* ? bit DIB */
674                 FIXME("Unsupported DIB depth %d\n", info->bmiHeader.biBitCount);
675                 break;
676             }
677         }
678         /* Otherwise, get bits from the XImage */
679         else
680         {
681             if (!bmp->funcs && !BITMAP_SetOwnerDC( hbitmap, dc )) lines = 0;
682             else
683             {
684                 if (bmp->funcs && bmp->funcs->pGetDIBits)
685                     lines = bmp->funcs->pGetDIBits( dc->physDev, hbitmap, startscan,
686                                                     lines, bits, info, coloruse );
687                 else
688                     lines = 0;  /* FIXME: should copy from bmp->bitmap.bmBits */
689             }
690         }
691     }
692     else if( info->bmiHeader.biSize >= sizeof(BITMAPINFOHEADER) )
693     {
694         /* fill in struct members */
695
696         if( info->bmiHeader.biBitCount == 0)
697         {
698             info->bmiHeader.biWidth = bmp->bitmap.bmWidth;
699             info->bmiHeader.biHeight = bmp->bitmap.bmHeight;
700             info->bmiHeader.biPlanes = 1;
701             info->bmiHeader.biBitCount = bmp->bitmap.bmBitsPixel;
702             info->bmiHeader.biSizeImage =
703                              DIB_GetDIBImageBytes( bmp->bitmap.bmWidth,
704                                                    bmp->bitmap.bmHeight,
705                                                    bmp->bitmap.bmBitsPixel );
706             info->bmiHeader.biCompression = 0;
707         }
708         else
709         {
710             info->bmiHeader.biSizeImage = DIB_GetDIBImageBytes(
711                                                info->bmiHeader.biWidth,
712                                                info->bmiHeader.biHeight,
713                                                info->bmiHeader.biBitCount );
714         }
715     }
716
717     TRACE("biSizeImage = %ld, biWidth = %ld, biHeight = %ld\n",
718           info->bmiHeader.biSizeImage, info->bmiHeader.biWidth,
719           info->bmiHeader.biHeight);
720
721     GDI_ReleaseObj( hdc );
722     GDI_ReleaseObj( hbitmap );
723
724     return lines;
725 }
726
727
728 /***********************************************************************
729  *           CreateDIBitmap    (GDI32.@)
730  */
731 HBITMAP WINAPI CreateDIBitmap( HDC hdc, const BITMAPINFOHEADER *header,
732                             DWORD init, LPCVOID bits, const BITMAPINFO *data,
733                             UINT coloruse )
734 {
735     HBITMAP handle;
736     BOOL fColor;
737     DWORD width;
738     int height;
739     WORD bpp;
740     WORD compr;
741     DC *dc;
742
743     if (DIB_GetBitmapInfo( header, &width, &height, &bpp, &compr ) == -1) return 0;
744     if (height < 0) height = -height;
745
746     /* Check if we should create a monochrome or color bitmap. */
747     /* We create a monochrome bitmap only if it has exactly 2  */
748     /* colors, which are black followed by white, nothing else.  */
749     /* In all other cases, we create a color bitmap.           */
750
751     if (bpp != 1) fColor = TRUE;
752     else if ((coloruse != DIB_RGB_COLORS) ||
753              (init != CBM_INIT) || !data) fColor = FALSE;
754     else
755     {
756         if (data->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
757         {
758             RGBQUAD *rgb = data->bmiColors;
759             DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
760
761             /* Check if the first color of the colormap is black */
762             if ((col == RGB(0,0,0)))
763             {
764                 rgb++;
765                 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
766                 /* If the second color is white, create a monochrome bitmap */
767                 fColor =  (col != RGB(0xff,0xff,0xff));
768             }
769             /* Note : If the first color of the colormap is white
770                followed by black, we have to create a color bitmap.
771                If we don't the white will be displayed in black later on!*/
772             else fColor = TRUE;
773         }
774         else if (data->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
775         {
776             RGBTRIPLE *rgb = ((BITMAPCOREINFO *)data)->bmciColors;
777             DWORD col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
778             if ((col == RGB(0,0,0)))
779             {
780                 rgb++;
781                 col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
782                 fColor = (col != RGB(0xff,0xff,0xff));
783             }
784             else fColor = TRUE;
785         }
786         else if (data->bmiHeader.biSize == sizeof(BITMAPV4HEADER))
787         { /* FIXME: correct ? */
788             RGBQUAD *rgb = data->bmiColors;
789             DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
790
791             /* Check if the first color of the colormap is black */
792             if ((col == RGB(0,0,0)))
793             {
794                 rgb++;
795                 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
796                 /* If the second color is white, create a monochrome bitmap */
797                 fColor =  (col != RGB(0xff,0xff,0xff));
798             }
799             /* Note : If the first color of the colormap is white
800                followed by black, we have to create a color bitmap.
801                If we don't the white will be displayed in black later on!*/
802             else fColor = TRUE;
803         }
804         else
805         {
806             ERR("(%ld): wrong/unknown size for data\n",
807                      data->bmiHeader.biSize );
808             return 0;
809         }
810     }
811
812     /* Now create the bitmap */
813
814     if (!(dc = DC_GetDCPtr( hdc ))) return 0;
815
816     if (fColor)
817         handle = CreateBitmap( width, height, GetDeviceCaps( hdc, PLANES ),
818                                GetDeviceCaps( hdc, BITSPIXEL ), NULL );
819     else handle = CreateBitmap( width, height, 1, 1, NULL );
820
821     if (handle)
822     {
823         if (init == CBM_INIT) SetDIBits( hdc, handle, 0, height, bits, data, coloruse );
824         else if (!BITMAP_SetOwnerDC( handle, dc ))
825         {
826             DeleteObject( handle );
827             handle = 0;
828         }
829     }
830
831     GDI_ReleaseObj( hdc );
832     return handle;
833 }
834
835 /***********************************************************************
836  *           CreateDIBSection    (GDI.489)
837  */
838 HBITMAP16 WINAPI CreateDIBSection16 (HDC16 hdc, BITMAPINFO *bmi, UINT16 usage,
839                                      SEGPTR *bits16, HANDLE section, DWORD offset)
840 {
841     LPVOID bits32;
842     HBITMAP hbitmap;
843
844     hbitmap = CreateDIBSection( HDC_32(hdc), bmi, usage, &bits32, section, offset );
845     if (hbitmap)
846     {
847         BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr(hbitmap, BITMAP_MAGIC);
848         if (bmp && bmp->dib && bits32)
849         {
850             BITMAPINFOHEADER *bi = &bmi->bmiHeader;
851             INT height = bi->biHeight >= 0 ? bi->biHeight : -bi->biHeight;
852             INT width_bytes = DIB_GetDIBWidthBytes(bi->biWidth, bi->biBitCount);
853             INT size  = (bi->biSizeImage && bi->biCompression != BI_RGB) ?
854                          bi->biSizeImage : width_bytes * height;
855
856             /* calculate number of sel's needed for size with 64K steps */
857             WORD count = (size + 0xffff) / 0x10000;
858             WORD sel = AllocSelectorArray16(count);
859             int i;
860
861             for (i = 0; i < count; i++)
862             {
863                 SetSelectorBase(sel + (i << __AHSHIFT), (DWORD)bits32 + i * 0x10000);
864                 SetSelectorLimit16(sel + (i << __AHSHIFT), size - 1); /* yep, limit is correct */
865                 size -= 0x10000;
866             }
867             bmp->segptr_bits = MAKESEGPTR( sel, 0 );
868             if (bits16) *bits16 = bmp->segptr_bits;
869         }
870         if (bmp) GDI_ReleaseObj( hbitmap );
871     }
872     return HBITMAP_16(hbitmap);
873 }
874
875 /***********************************************************************
876  *           DIB_CreateDIBSection
877  */
878 HBITMAP DIB_CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
879                              LPVOID *bits, HANDLE section,
880                              DWORD offset, DWORD ovr_pitch)
881 {
882     HBITMAP hbitmap = 0;
883     DC *dc;
884     BOOL bDesktopDC = FALSE;
885
886     /* If the reference hdc is null, take the desktop dc */
887     if (hdc == 0)
888     {
889         hdc = CreateCompatibleDC(0);
890         bDesktopDC = TRUE;
891     }
892
893     if ((dc = DC_GetDCPtr( hdc )))
894     {
895         hbitmap = dc->funcs->pCreateDIBSection(dc->physDev, bmi, usage, bits, section, offset, ovr_pitch);
896         GDI_ReleaseObj(hdc);
897     }
898
899     if (bDesktopDC)
900       DeleteDC(hdc);
901
902     return hbitmap;
903 }
904
905 /***********************************************************************
906  *           CreateDIBSection    (GDI32.@)
907  */
908 HBITMAP WINAPI CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
909                                 LPVOID *bits, HANDLE section,
910                                 DWORD offset)
911 {
912     return DIB_CreateDIBSection(hdc, bmi, usage, bits, section, offset, 0);
913 }
914
915 /***********************************************************************
916  *           DIB_CreateDIBFromBitmap
917  *  Allocates a packed DIB and copies the bitmap data into it.
918  */
919 HGLOBAL DIB_CreateDIBFromBitmap(HDC hdc, HBITMAP hBmp)
920 {
921     BITMAPOBJ *pBmp = NULL;
922     HGLOBAL hPackedDIB = 0;
923     LPBYTE pPackedDIB = NULL;
924     LPBITMAPINFOHEADER pbmiHeader = NULL;
925     unsigned int width, height, depth, cDataSize = 0, cPackedSize = 0,
926                  OffsetBits = 0, nLinesCopied = 0;
927
928     /* Get a pointer to the BITMAPOBJ structure */
929     pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
930     if (!pBmp) return hPackedDIB;
931
932     /* Get the bitmap dimensions */
933     width = pBmp->bitmap.bmWidth;
934     height = pBmp->bitmap.bmHeight;
935     depth = pBmp->bitmap.bmBitsPixel;
936
937     /*
938      * A packed DIB contains a BITMAPINFO structure followed immediately by
939      * an optional color palette and the pixel data.
940      */
941
942     /* Calculate the size of the packed DIB */
943     cDataSize = DIB_GetDIBImageBytes( width, height, depth );
944     cPackedSize = sizeof(BITMAPINFOHEADER)
945                   + ( (depth <= 8) ? (sizeof(RGBQUAD) * (1 << depth)) : 0 )
946                   + cDataSize;
947     /* Get the offset to the bits */
948     OffsetBits = cPackedSize - cDataSize;
949
950     /* Allocate the packed DIB */
951     TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
952     hPackedDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE /*| GMEM_ZEROINIT*/,
953                              cPackedSize );
954     if ( !hPackedDIB )
955     {
956         WARN("Could not allocate packed DIB!\n");
957         goto END;
958     }
959
960     /* A packed DIB starts with a BITMAPINFOHEADER */
961     pPackedDIB = (LPBYTE)GlobalLock(hPackedDIB);
962     pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;
963
964     /* Init the BITMAPINFOHEADER */
965     pbmiHeader->biSize = sizeof(BITMAPINFOHEADER);
966     pbmiHeader->biWidth = width;
967     pbmiHeader->biHeight = height;
968     pbmiHeader->biPlanes = 1;
969     pbmiHeader->biBitCount = depth;
970     pbmiHeader->biCompression = BI_RGB;
971     pbmiHeader->biSizeImage = 0;
972     pbmiHeader->biXPelsPerMeter = pbmiHeader->biYPelsPerMeter = 0;
973     pbmiHeader->biClrUsed = 0;
974     pbmiHeader->biClrImportant = 0;
975
976     /* Retrieve the DIB bits from the bitmap and fill in the
977      * DIB color table if present */
978
979     nLinesCopied = GetDIBits(hdc,                       /* Handle to device context */
980                              hBmp,                      /* Handle to bitmap */
981                              0,                         /* First scan line to set in dest bitmap */
982                              height,                    /* Number of scan lines to copy */
983                              pPackedDIB + OffsetBits,   /* [out] Address of array for bitmap bits */
984                              (LPBITMAPINFO) pbmiHeader, /* [out] Address of BITMAPINFO structure */
985                              0);                        /* RGB or palette index */
986     GlobalUnlock(hPackedDIB);
987
988     /* Cleanup if GetDIBits failed */
989     if (nLinesCopied != height)
990     {
991         TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, height);
992         GlobalFree(hPackedDIB);
993         hPackedDIB = 0;
994     }
995
996 END:
997     GDI_ReleaseObj( hBmp );
998     return hPackedDIB;
999 }