gdi32: Add support for 8 bpp dibs.
[wine] / dlls / gdi32 / dibdrv / dc.c
1 /*
2  * DIB driver initialization and DC functions.
3  *
4  * Copyright 2011 Huw Davies
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <assert.h>
22
23 #include "gdi_private.h"
24 #include "dibdrv.h"
25
26 #include "wine/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(dib);
29
30 static void calc_shift_and_len(DWORD mask, int *shift, int *len)
31 {
32     int s, l;
33
34     if(!mask)
35     {
36         *shift = *len = 0;
37         return;
38     }
39
40     s = 0;
41     while ((mask & 1) == 0)
42     {
43         mask >>= 1;
44         s++;
45     }
46     l = 0;
47     while ((mask & 1) == 1)
48     {
49         mask >>= 1;
50         l++;
51     }
52     *shift = s;
53     *len = l;
54 }
55
56 static void init_bit_fields(dib_info *dib, const DWORD *bit_fields)
57 {
58     dib->red_mask    = bit_fields[0];
59     dib->green_mask  = bit_fields[1];
60     dib->blue_mask   = bit_fields[2];
61     calc_shift_and_len(dib->red_mask,   &dib->red_shift,   &dib->red_len);
62     calc_shift_and_len(dib->green_mask, &dib->green_shift, &dib->green_len);
63     calc_shift_and_len(dib->blue_mask,  &dib->blue_shift,  &dib->blue_len);
64 }
65
66 static BOOL init_dib_info(dib_info *dib, const BITMAPINFOHEADER *bi, const DWORD *bit_fields,
67                           RGBQUAD *color_table, int color_table_size, void *bits)
68 {
69     static const DWORD bit_fields_888[3] = {0xff0000, 0x00ff00, 0x0000ff};
70     static const DWORD bit_fields_555[3] = {0x7c00, 0x03e0, 0x001f};
71
72     dib->bit_count = bi->biBitCount;
73     dib->width     = bi->biWidth;
74     dib->height    = bi->biHeight;
75     dib->stride    = ((dib->width * dib->bit_count + 31) >> 3) & ~3;
76     dib->bits      = bits;
77
78     if(dib->height < 0) /* top-down */
79     {
80         dib->height = -dib->height;
81     }
82     else /* bottom-up */
83     {
84         /* bits always points to the top-left corner and the stride is -ve */
85         dib->bits    = (BYTE*)dib->bits + (dib->height - 1) * dib->stride;
86         dib->stride  = -dib->stride;
87     }
88
89     dib->funcs = &funcs_null;
90
91     switch(dib->bit_count)
92     {
93     case 32:
94         if(bi->biCompression == BI_RGB)
95             bit_fields = bit_fields_888;
96
97         init_bit_fields(dib, bit_fields);
98
99         if(dib->red_mask == 0xff0000 && dib->green_mask == 0x00ff00 && dib->blue_mask == 0x0000ff)
100             dib->funcs = &funcs_8888;
101         else
102             dib->funcs = &funcs_32;
103         break;
104
105     case 16:
106         if(bi->biCompression == BI_RGB)
107             bit_fields = bit_fields_555;
108
109         init_bit_fields(dib, bit_fields);
110
111         if(dib->red_mask == 0x7c00 && dib->green_mask == 0x03e0 && dib->blue_mask == 0x001f)
112             dib->funcs = &funcs_555;
113         else
114             dib->funcs = &funcs_16;
115         break;
116
117     case 8:
118         dib->funcs = &funcs_8;
119         break;
120
121     default:
122         TRACE("bpp %d not supported, will forward to graphics driver.\n", dib->bit_count);
123         return FALSE;
124     }
125
126     if(color_table)
127     {
128         dib->color_table = HeapAlloc(GetProcessHeap(), 0, color_table_size * sizeof(dib->color_table[0]));
129         if(!dib->color_table) return FALSE;
130         dib->color_table_size = color_table_size;
131         memcpy(dib->color_table, color_table, color_table_size * sizeof(color_table[0]));
132     }
133     else
134     {
135         dib->color_table = NULL;
136         dib->color_table_size = 0;
137     }
138
139     return TRUE;
140 }
141
142 BOOL init_dib_info_from_packed(dib_info *dib, const BITMAPINFOHEADER *bi, WORD usage, HPALETTE palette)
143 {
144     DWORD *masks = NULL;
145     RGBQUAD *color_table = NULL, pal_table[256];
146     BYTE *ptr = (BYTE*)bi + bi->biSize;
147     int num_colors = bi->biClrUsed;
148
149     if(bi->biCompression == BI_BITFIELDS)
150     {
151         masks = (DWORD *)ptr;
152         ptr += 3 * sizeof(DWORD);
153     }
154
155     if(!num_colors && bi->biBitCount <= 8) num_colors = 1 << bi->biBitCount;
156     if(num_colors)
157     {
158         if(usage == DIB_PAL_COLORS)
159         {
160             PALETTEENTRY entries[256];
161             const WORD *index = (const WORD*) ptr;
162             UINT i, count = GetPaletteEntries( palette, 0, num_colors, entries );
163             for (i = 0; i < num_colors; i++, index++)
164             {
165                 PALETTEENTRY *entry = &entries[*index % count];
166                 pal_table[i].rgbRed      = entry->peRed;
167                 pal_table[i].rgbGreen    = entry->peGreen;
168                 pal_table[i].rgbBlue     = entry->peBlue;
169                 pal_table[i].rgbReserved = 0;
170             }
171             color_table = pal_table;
172             ptr += num_colors * sizeof(WORD);
173         }
174         else
175         {
176             color_table = (RGBQUAD*)ptr;
177             ptr += num_colors * sizeof(*color_table);
178         }
179     }
180
181     return init_dib_info(dib, bi, masks, color_table, num_colors, ptr);
182 }
183
184 static void clear_dib_info(dib_info *dib)
185 {
186     dib->color_table = NULL;
187     dib->bits = NULL;
188 }
189
190 /**********************************************************************
191  *      free_dib_info
192  *
193  * Free the resources associated with a dib and optionally the bits
194  */
195 void free_dib_info(dib_info *dib, BOOL free_bits)
196 {
197     HeapFree(GetProcessHeap(), 0, dib->color_table);
198     dib->color_table = NULL;
199     if(free_bits)
200     {
201         HeapFree(GetProcessHeap(), 0, dib->bits);
202         dib->bits = NULL;
203     }
204 }
205
206 void copy_dib_color_info(dib_info *dst, const dib_info *src)
207 {
208     dst->bit_count        = src->bit_count;
209     dst->red_mask         = src->red_mask;
210     dst->green_mask       = src->green_mask;
211     dst->blue_mask        = src->blue_mask;
212     dst->red_len          = src->red_len;
213     dst->green_len        = src->green_len;
214     dst->blue_len         = src->blue_len;
215     dst->red_shift        = src->red_shift;
216     dst->green_shift      = src->green_shift;
217     dst->blue_shift       = src->blue_shift;
218     dst->funcs            = src->funcs;
219     dst->color_table_size = src->color_table_size;
220     dst->color_table      = NULL;
221     if(dst->color_table_size)
222     {
223         int size = dst->color_table_size * sizeof(dst->color_table[0]);
224         dst->color_table = HeapAlloc(GetProcessHeap(), 0, size);
225         memcpy(dst->color_table, src->color_table, size);
226     }
227 }
228
229 /**************************************************************
230  *            convert_dib
231  *
232  * Converts src into the format specified in dst.
233  *
234  * FIXME: At the moment this always creates a top-down dib,
235  * do we want to give the option of bottom-up?
236  */
237 BOOL convert_dib(dib_info *dst, const dib_info *src)
238 {
239     BOOL ret;
240     RECT src_rect;
241
242     dst->height = src->height;
243     dst->width = src->width;
244     dst->stride = ((dst->width * dst->bit_count + 31) >> 3) & ~3;
245     dst->bits = HeapAlloc(GetProcessHeap(), 0, dst->height * dst->stride);
246
247     src_rect.left = src_rect.top = 0;
248     src_rect.right = src->width;
249     src_rect.bottom = src->height;
250
251     ret = dst->funcs->convert_to(dst, src, &src_rect);
252
253     if(!ret) free_dib_info(dst, TRUE);
254     return ret;
255 }
256
257 /***********************************************************************
258  *           dibdrv_DeleteDC
259  */
260 static BOOL CDECL dibdrv_DeleteDC( PHYSDEV dev )
261 {
262     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
263     TRACE("(%p)\n", dev);
264     DeleteObject(pdev->clip);
265     free_pattern_brush(pdev);
266     free_dib_info(&pdev->dib, FALSE);
267     return 0;
268 }
269
270 /***********************************************************************
271  *           dibdrv_SelectBitmap
272  */
273 static HBITMAP CDECL dibdrv_SelectBitmap( PHYSDEV dev, HBITMAP bitmap )
274 {
275     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectBitmap );
276     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
277     BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
278     TRACE("(%p, %p)\n", dev, bitmap);
279
280     if (!bmp) return 0;
281     assert(bmp->dib);
282
283     pdev->clip = CreateRectRgn(0, 0, 0, 0);
284     pdev->defer = 0;
285
286     clear_dib_info(&pdev->dib);
287     clear_dib_info(&pdev->brush_dib);
288     pdev->brush_and_bits = pdev->brush_xor_bits = NULL;
289
290     if(!init_dib_info(&pdev->dib, &bmp->dib->dsBmih, bmp->dib->dsBitfields,
291                       bmp->color_table, bmp->nb_colors, bmp->dib->dsBm.bmBits))
292         pdev->defer |= DEFER_FORMAT;
293
294     GDI_ReleaseObj( bitmap );
295
296     return next->funcs->pSelectBitmap( next, bitmap );
297 }
298
299 /***********************************************************************
300  *           dibdrv_SetBkColor
301  */
302 static COLORREF CDECL dibdrv_SetBkColor( PHYSDEV dev, COLORREF color )
303 {
304     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetBkColor );
305     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
306
307     pdev->bkgnd_color = pdev->dib.funcs->colorref_to_pixel( &pdev->dib, color );
308
309     if( GetBkMode(dev->hdc) == OPAQUE )
310         calc_and_xor_masks( GetROP2(dev->hdc), pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
311     else
312     {
313         pdev->bkgnd_and = ~0u;
314         pdev->bkgnd_xor = 0;
315     }
316
317     return next->funcs->pSetBkColor( next, color );
318 }
319
320 /***********************************************************************
321  *           dibdrv_SetBkMode
322  */
323 static INT CDECL dibdrv_SetBkMode( PHYSDEV dev, INT mode )
324 {
325     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetBkMode );
326     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
327
328     if( mode == OPAQUE )
329         calc_and_xor_masks( GetROP2(dev->hdc), pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
330     else
331     {
332         pdev->bkgnd_and = ~0u;
333         pdev->bkgnd_xor = 0;
334     }
335
336     return next->funcs->pSetBkMode( next, mode );
337 }
338
339 /***********************************************************************
340  *           dibdrv_SetDeviceClipping
341  */
342 static void CDECL dibdrv_SetDeviceClipping( PHYSDEV dev, HRGN vis_rgn, HRGN clip_rgn )
343 {
344     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDeviceClipping );
345     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
346     TRACE("(%p, %p, %p)\n", dev, vis_rgn, clip_rgn);
347
348     CombineRgn( pdev->clip, vis_rgn, clip_rgn, clip_rgn ? RGN_AND : RGN_COPY );
349     return next->funcs->pSetDeviceClipping( next, vis_rgn, clip_rgn);
350 }
351
352 /***********************************************************************
353  *           dibdrv_SetROP2
354  */
355 static INT CDECL dibdrv_SetROP2( PHYSDEV dev, INT rop )
356 {
357     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetROP2 );
358     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
359
360     calc_and_xor_masks(rop, pdev->pen_color, &pdev->pen_and, &pdev->pen_xor);
361     update_brush_rop(pdev, rop);
362     if( GetBkMode(dev->hdc) == OPAQUE )
363         calc_and_xor_masks(rop, pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor);
364
365     return next->funcs->pSetROP2( next, rop );
366 }
367
368 const DC_FUNCTIONS dib_driver =
369 {
370     NULL,                               /* pAbortDoc */
371     NULL,                               /* pAbortPath */
372     NULL,                               /* pAlphaBlend */
373     NULL,                               /* pAngleArc */
374     NULL,                               /* pArc */
375     NULL,                               /* pArcTo */
376     NULL,                               /* pBeginPath */
377     NULL,                               /* pChoosePixelFormat */
378     NULL,                               /* pChord */
379     NULL,                               /* pCloseFigure */
380     NULL,                               /* pCreateBitmap */
381     NULL,                               /* pCreateDC */
382     NULL,                               /* pCreateDIBSection */
383     NULL,                               /* pDeleteBitmap */
384     dibdrv_DeleteDC,                    /* pDeleteDC */
385     NULL,                               /* pDeleteObject */
386     NULL,                               /* pDescribePixelFormat */
387     NULL,                               /* pDeviceCapabilities */
388     NULL,                               /* pEllipse */
389     NULL,                               /* pEndDoc */
390     NULL,                               /* pEndPage */
391     NULL,                               /* pEndPath */
392     NULL,                               /* pEnumDeviceFonts */
393     NULL,                               /* pEnumICMProfiles */
394     NULL,                               /* pExcludeClipRect */
395     NULL,                               /* pExtDeviceMode */
396     NULL,                               /* pExtEscape */
397     NULL,                               /* pExtFloodFill */
398     NULL,                               /* pExtSelectClipRgn */
399     NULL,                               /* pExtTextOut */
400     NULL,                               /* pFillPath */
401     NULL,                               /* pFillRgn */
402     NULL,                               /* pFlattenPath */
403     NULL,                               /* pFrameRgn */
404     NULL,                               /* pGdiComment */
405     NULL,                               /* pGetBitmapBits */
406     NULL,                               /* pGetCharWidth */
407     NULL,                               /* pGetDIBits */
408     NULL,                               /* pGetDeviceCaps */
409     NULL,                               /* pGetDeviceGammaRamp */
410     NULL,                               /* pGetICMProfile */
411     NULL,                               /* pGetNearestColor */
412     NULL,                               /* pGetPixel */
413     NULL,                               /* pGetPixelFormat */
414     NULL,                               /* pGetSystemPaletteEntries */
415     NULL,                               /* pGetTextExtentExPoint */
416     NULL,                               /* pGetTextMetrics */
417     NULL,                               /* pIntersectClipRect */
418     NULL,                               /* pInvertRgn */
419     dibdrv_LineTo,                      /* pLineTo */
420     NULL,                               /* pModifyWorldTransform */
421     NULL,                               /* pMoveTo */
422     NULL,                               /* pOffsetClipRgn */
423     NULL,                               /* pOffsetViewportOrg */
424     NULL,                               /* pOffsetWindowOrg */
425     dibdrv_PaintRgn,                    /* pPaintRgn */
426     dibdrv_PatBlt,                      /* pPatBlt */
427     NULL,                               /* pPie */
428     NULL,                               /* pPolyBezier */
429     NULL,                               /* pPolyBezierTo */
430     NULL,                               /* pPolyDraw */
431     NULL,                               /* pPolyPolygon */
432     NULL,                               /* pPolyPolyline */
433     NULL,                               /* pPolygon */
434     NULL,                               /* pPolyline */
435     NULL,                               /* pPolylineTo */
436     NULL,                               /* pRealizeDefaultPalette */
437     NULL,                               /* pRealizePalette */
438     dibdrv_Rectangle,                   /* pRectangle */
439     NULL,                               /* pResetDC */
440     NULL,                               /* pRestoreDC */
441     NULL,                               /* pRoundRect */
442     NULL,                               /* pSaveDC */
443     NULL,                               /* pScaleViewportExt */
444     NULL,                               /* pScaleWindowExt */
445     dibdrv_SelectBitmap,                /* pSelectBitmap */
446     dibdrv_SelectBrush,                 /* pSelectBrush */
447     NULL,                               /* pSelectClipPath */
448     NULL,                               /* pSelectFont */
449     NULL,                               /* pSelectPalette */
450     dibdrv_SelectPen,                   /* pSelectPen */
451     NULL,                               /* pSetArcDirection */
452     NULL,                               /* pSetBitmapBits */
453     dibdrv_SetBkColor,                  /* pSetBkColor */
454     dibdrv_SetBkMode,                   /* pSetBkMode */
455     dibdrv_SetDCBrushColor,             /* pSetDCBrushColor */
456     dibdrv_SetDCPenColor,               /* pSetDCPenColor */
457     NULL,                               /* pSetDIBColorTable */
458     NULL,                               /* pSetDIBits */
459     NULL,                               /* pSetDIBitsToDevice */
460     dibdrv_SetDeviceClipping,           /* pSetDeviceClipping */
461     NULL,                               /* pSetDeviceGammaRamp */
462     NULL,                               /* pSetLayout */
463     NULL,                               /* pSetMapMode */
464     NULL,                               /* pSetMapperFlags */
465     NULL,                               /* pSetPixel */
466     NULL,                               /* pSetPixelFormat */
467     NULL,                               /* pSetPolyFillMode */
468     dibdrv_SetROP2,                     /* pSetROP2 */
469     NULL,                               /* pSetRelAbs */
470     NULL,                               /* pSetStretchBltMode */
471     NULL,                               /* pSetTextAlign */
472     NULL,                               /* pSetTextCharacterExtra */
473     NULL,                               /* pSetTextColor */
474     NULL,                               /* pSetTextJustification */
475     NULL,                               /* pSetViewportExt */
476     NULL,                               /* pSetViewportOrg */
477     NULL,                               /* pSetWindowExt */
478     NULL,                               /* pSetWindowOrg */
479     NULL,                               /* pSetWorldTransform */
480     NULL,                               /* pStartDoc */
481     NULL,                               /* pStartPage */
482     NULL,                               /* pStretchBlt */
483     NULL,                               /* pStretchDIBits */
484     NULL,                               /* pStrokeAndFillPath */
485     NULL,                               /* pStrokePath */
486     NULL,                               /* pSwapBuffers */
487     NULL,                               /* pUnrealizePalette */
488     NULL,                               /* pWidenPath */
489     NULL,                               /* pwglCopyContext */
490     NULL,                               /* pwglCreateContext */
491     NULL,                               /* pwglCreateContextAttribsARB */
492     NULL,                               /* pwglDeleteContext */
493     NULL,                               /* pwglGetProcAddress */
494     NULL,                               /* pwglGetPbufferDCARB */
495     NULL,                               /* pwglMakeCurrent */
496     NULL,                               /* pwglMakeContextCurrentARB */
497     NULL,                               /* pwglSetPixelFormatWINE */
498     NULL,                               /* pwglShareLists */
499     NULL,                               /* pwglUseFontBitmapsA */
500     NULL                                /* pwglUseFontBitmapsW */
501 };