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