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