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