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