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