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