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