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