urlmon: Fixed handling empty URIs.
[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 24:
106         dib->funcs = &funcs_24;
107         break;
108
109     case 16:
110         if(bi->biCompression == BI_RGB)
111             bit_fields = bit_fields_555;
112
113         init_bit_fields(dib, bit_fields);
114
115         if(dib->red_mask == 0x7c00 && dib->green_mask == 0x03e0 && dib->blue_mask == 0x001f)
116             dib->funcs = &funcs_555;
117         else
118             dib->funcs = &funcs_16;
119         break;
120
121     case 8:
122         dib->funcs = &funcs_8;
123         break;
124
125     case 4:
126         dib->funcs = &funcs_4;
127         break;
128
129     case 1:
130         dib->funcs = &funcs_1;
131         break;
132
133     default:
134         TRACE("bpp %d not supported, will forward to graphics driver.\n", dib->bit_count);
135         return FALSE;
136     }
137
138     if(color_table)
139     {
140         dib->color_table = HeapAlloc(GetProcessHeap(), 0, color_table_size * sizeof(dib->color_table[0]));
141         if(!dib->color_table) return FALSE;
142         dib->color_table_size = color_table_size;
143         memcpy(dib->color_table, color_table, color_table_size * sizeof(color_table[0]));
144     }
145     else
146     {
147         dib->color_table = NULL;
148         dib->color_table_size = 0;
149     }
150
151     return TRUE;
152 }
153
154 BOOL init_dib_info_from_packed(dib_info *dib, const BITMAPINFOHEADER *bi, WORD usage, HPALETTE palette)
155 {
156     DWORD *masks = NULL;
157     RGBQUAD *color_table = NULL, pal_table[256];
158     BYTE *ptr = (BYTE*)bi + bi->biSize;
159     int num_colors = bi->biClrUsed;
160
161     if(bi->biCompression == BI_BITFIELDS)
162     {
163         masks = (DWORD *)ptr;
164         ptr += 3 * sizeof(DWORD);
165     }
166
167     if(!num_colors && bi->biBitCount <= 8) num_colors = 1 << bi->biBitCount;
168     if(num_colors)
169     {
170         if(usage == DIB_PAL_COLORS)
171         {
172             PALETTEENTRY entries[256];
173             const WORD *index = (const WORD*) ptr;
174             UINT i, count = GetPaletteEntries( palette, 0, num_colors, entries );
175             for (i = 0; i < num_colors; i++, index++)
176             {
177                 PALETTEENTRY *entry = &entries[*index % count];
178                 pal_table[i].rgbRed      = entry->peRed;
179                 pal_table[i].rgbGreen    = entry->peGreen;
180                 pal_table[i].rgbBlue     = entry->peBlue;
181                 pal_table[i].rgbReserved = 0;
182             }
183             color_table = pal_table;
184             ptr += num_colors * sizeof(WORD);
185         }
186         else
187         {
188             color_table = (RGBQUAD*)ptr;
189             ptr += num_colors * sizeof(*color_table);
190         }
191     }
192
193     return init_dib_info(dib, bi, masks, color_table, num_colors, ptr);
194 }
195
196 static void clear_dib_info(dib_info *dib)
197 {
198     dib->color_table = NULL;
199     dib->bits = NULL;
200 }
201
202 /**********************************************************************
203  *      free_dib_info
204  *
205  * Free the resources associated with a dib and optionally the bits
206  */
207 void free_dib_info(dib_info *dib, BOOL free_bits)
208 {
209     HeapFree(GetProcessHeap(), 0, dib->color_table);
210     dib->color_table = NULL;
211     if(free_bits)
212     {
213         HeapFree(GetProcessHeap(), 0, dib->bits);
214         dib->bits = NULL;
215     }
216 }
217
218 void copy_dib_color_info(dib_info *dst, const dib_info *src)
219 {
220     dst->bit_count        = src->bit_count;
221     dst->red_mask         = src->red_mask;
222     dst->green_mask       = src->green_mask;
223     dst->blue_mask        = src->blue_mask;
224     dst->red_len          = src->red_len;
225     dst->green_len        = src->green_len;
226     dst->blue_len         = src->blue_len;
227     dst->red_shift        = src->red_shift;
228     dst->green_shift      = src->green_shift;
229     dst->blue_shift       = src->blue_shift;
230     dst->funcs            = src->funcs;
231     dst->color_table_size = src->color_table_size;
232     dst->color_table      = NULL;
233     if(dst->color_table_size)
234     {
235         int size = dst->color_table_size * sizeof(dst->color_table[0]);
236         dst->color_table = HeapAlloc(GetProcessHeap(), 0, size);
237         memcpy(dst->color_table, src->color_table, size);
238     }
239 }
240
241 /**************************************************************
242  *            convert_dib
243  *
244  * Converts src into the format specified in dst.
245  *
246  * FIXME: At the moment this always creates a top-down dib,
247  * do we want to give the option of bottom-up?
248  */
249 BOOL convert_dib(dib_info *dst, const dib_info *src)
250 {
251     BOOL ret;
252     RECT src_rect;
253
254     dst->height = src->height;
255     dst->width = src->width;
256     dst->stride = ((dst->width * dst->bit_count + 31) >> 3) & ~3;
257     dst->bits = HeapAlloc(GetProcessHeap(), 0, dst->height * dst->stride);
258
259     src_rect.left = src_rect.top = 0;
260     src_rect.right = src->width;
261     src_rect.bottom = src->height;
262
263     ret = dst->funcs->convert_to(dst, src, &src_rect);
264
265     if(!ret) free_dib_info(dst, TRUE);
266     return ret;
267 }
268
269 static void update_fg_colors( dibdrv_physdev *pdev )
270 {
271     pdev->pen_color   = get_fg_color( pdev, pdev->pen_colorref );
272     pdev->brush_color = get_fg_color( pdev, pdev->brush_colorref );
273 }
274
275 static void update_masks( dibdrv_physdev *pdev, INT rop )
276 {
277     calc_and_xor_masks( rop, pdev->pen_color, &pdev->pen_and, &pdev->pen_xor );
278     update_brush_rop( pdev, rop );
279     if( GetBkMode( pdev->dev.hdc ) == OPAQUE )
280         calc_and_xor_masks( rop, pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
281 }
282
283 /***********************************************************************
284  *           dibdrv_DeleteDC
285  */
286 static BOOL CDECL dibdrv_DeleteDC( PHYSDEV dev )
287 {
288     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
289     TRACE("(%p)\n", dev);
290     DeleteObject(pdev->clip);
291     free_pattern_brush(pdev);
292     free_dib_info(&pdev->dib, FALSE);
293     return 0;
294 }
295
296 /***********************************************************************
297  *           dibdrv_SelectBitmap
298  */
299 static HBITMAP CDECL dibdrv_SelectBitmap( PHYSDEV dev, HBITMAP bitmap )
300 {
301     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectBitmap );
302     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
303     BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
304     TRACE("(%p, %p)\n", dev, bitmap);
305
306     if (!bmp) return 0;
307     assert(bmp->dib);
308
309     pdev->clip = CreateRectRgn(0, 0, 0, 0);
310     pdev->defer = 0;
311
312     clear_dib_info(&pdev->dib);
313     clear_dib_info(&pdev->brush_dib);
314     pdev->brush_and_bits = pdev->brush_xor_bits = NULL;
315
316     if(!init_dib_info(&pdev->dib, &bmp->dib->dsBmih, bmp->dib->dsBitfields,
317                       bmp->color_table, bmp->nb_colors, bmp->dib->dsBm.bmBits))
318         pdev->defer |= DEFER_FORMAT;
319
320     GDI_ReleaseObj( bitmap );
321
322     return next->funcs->pSelectBitmap( next, bitmap );
323 }
324
325 /***********************************************************************
326  *           dibdrv_SetBkColor
327  */
328 static COLORREF CDECL dibdrv_SetBkColor( PHYSDEV dev, COLORREF color )
329 {
330     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetBkColor );
331     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
332
333     pdev->bkgnd_color = pdev->dib.funcs->colorref_to_pixel( &pdev->dib, color );
334
335     if( GetBkMode(dev->hdc) == OPAQUE )
336         calc_and_xor_masks( GetROP2(dev->hdc), pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
337     else
338     {
339         pdev->bkgnd_and = ~0u;
340         pdev->bkgnd_xor = 0;
341     }
342
343     update_fg_colors( pdev ); /* Only needed in the 1 bpp case */
344
345     return next->funcs->pSetBkColor( next, color );
346 }
347
348 /***********************************************************************
349  *           dibdrv_SetBkMode
350  */
351 static INT CDECL dibdrv_SetBkMode( PHYSDEV dev, INT mode )
352 {
353     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetBkMode );
354     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
355
356     if( mode == OPAQUE )
357         calc_and_xor_masks( GetROP2(dev->hdc), pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
358     else
359     {
360         pdev->bkgnd_and = ~0u;
361         pdev->bkgnd_xor = 0;
362     }
363
364     return next->funcs->pSetBkMode( next, mode );
365 }
366
367 /***********************************************************************
368  *           dibdrv_SetDeviceClipping
369  */
370 static void CDECL dibdrv_SetDeviceClipping( PHYSDEV dev, HRGN vis_rgn, HRGN clip_rgn )
371 {
372     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDeviceClipping );
373     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
374     TRACE("(%p, %p, %p)\n", dev, vis_rgn, clip_rgn);
375
376     CombineRgn( pdev->clip, vis_rgn, clip_rgn, clip_rgn ? RGN_AND : RGN_COPY );
377     return next->funcs->pSetDeviceClipping( next, vis_rgn, clip_rgn);
378 }
379
380 /***********************************************************************
381  *           dibdrv_SetDIBColorTable
382  */
383 static UINT CDECL dibdrv_SetDIBColorTable( PHYSDEV dev, UINT pos, UINT count, const RGBQUAD *colors )
384 {
385     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDIBColorTable );
386     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
387     TRACE("(%p, %d, %d, %p)\n", dev, pos, count, colors);
388
389     if( pdev->dib.color_table && pos < pdev->dib.color_table_size )
390     {
391         if( pos + count > pdev->dib.color_table_size ) count = pdev->dib.color_table_size - pos;
392         memcpy( pdev->dib.color_table + pos, colors, count * sizeof(RGBQUAD) );
393
394         pdev->bkgnd_color = pdev->dib.funcs->colorref_to_pixel( &pdev->dib, GetBkColor( dev->hdc ) );
395         update_fg_colors( pdev );
396
397         update_masks( pdev, GetROP2( dev->hdc ) );
398     }
399     return next->funcs->pSetDIBColorTable( next, pos, count, colors );
400 }
401
402 /***********************************************************************
403  *           dibdrv_SetROP2
404  */
405 static INT CDECL dibdrv_SetROP2( PHYSDEV dev, INT rop )
406 {
407     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetROP2 );
408     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
409
410     update_masks( pdev, rop );
411
412     return next->funcs->pSetROP2( next, rop );
413 }
414
415 const DC_FUNCTIONS dib_driver =
416 {
417     NULL,                               /* pAbortDoc */
418     NULL,                               /* pAbortPath */
419     NULL,                               /* pAlphaBlend */
420     NULL,                               /* pAngleArc */
421     NULL,                               /* pArc */
422     NULL,                               /* pArcTo */
423     NULL,                               /* pBeginPath */
424     NULL,                               /* pChoosePixelFormat */
425     NULL,                               /* pChord */
426     NULL,                               /* pCloseFigure */
427     NULL,                               /* pCreateBitmap */
428     NULL,                               /* pCreateDC */
429     NULL,                               /* pCreateDIBSection */
430     NULL,                               /* pDeleteBitmap */
431     dibdrv_DeleteDC,                    /* pDeleteDC */
432     NULL,                               /* pDeleteObject */
433     NULL,                               /* pDescribePixelFormat */
434     NULL,                               /* pDeviceCapabilities */
435     NULL,                               /* pEllipse */
436     NULL,                               /* pEndDoc */
437     NULL,                               /* pEndPage */
438     NULL,                               /* pEndPath */
439     NULL,                               /* pEnumDeviceFonts */
440     NULL,                               /* pEnumICMProfiles */
441     NULL,                               /* pExcludeClipRect */
442     NULL,                               /* pExtDeviceMode */
443     NULL,                               /* pExtEscape */
444     NULL,                               /* pExtFloodFill */
445     NULL,                               /* pExtSelectClipRgn */
446     NULL,                               /* pExtTextOut */
447     NULL,                               /* pFillPath */
448     NULL,                               /* pFillRgn */
449     NULL,                               /* pFlattenPath */
450     NULL,                               /* pFrameRgn */
451     NULL,                               /* pGdiComment */
452     NULL,                               /* pGetBitmapBits */
453     NULL,                               /* pGetCharWidth */
454     NULL,                               /* pGetDIBits */
455     NULL,                               /* pGetDeviceCaps */
456     NULL,                               /* pGetDeviceGammaRamp */
457     NULL,                               /* pGetICMProfile */
458     NULL,                               /* pGetNearestColor */
459     NULL,                               /* pGetPixel */
460     NULL,                               /* pGetPixelFormat */
461     NULL,                               /* pGetSystemPaletteEntries */
462     NULL,                               /* pGetTextExtentExPoint */
463     NULL,                               /* pGetTextMetrics */
464     NULL,                               /* pIntersectClipRect */
465     NULL,                               /* pInvertRgn */
466     dibdrv_LineTo,                      /* pLineTo */
467     NULL,                               /* pModifyWorldTransform */
468     NULL,                               /* pMoveTo */
469     NULL,                               /* pOffsetClipRgn */
470     NULL,                               /* pOffsetViewportOrg */
471     NULL,                               /* pOffsetWindowOrg */
472     dibdrv_PaintRgn,                    /* pPaintRgn */
473     dibdrv_PatBlt,                      /* pPatBlt */
474     NULL,                               /* pPie */
475     NULL,                               /* pPolyBezier */
476     NULL,                               /* pPolyBezierTo */
477     NULL,                               /* pPolyDraw */
478     NULL,                               /* pPolyPolygon */
479     NULL,                               /* pPolyPolyline */
480     NULL,                               /* pPolygon */
481     NULL,                               /* pPolyline */
482     NULL,                               /* pPolylineTo */
483     NULL,                               /* pRealizeDefaultPalette */
484     NULL,                               /* pRealizePalette */
485     dibdrv_Rectangle,                   /* pRectangle */
486     NULL,                               /* pResetDC */
487     NULL,                               /* pRestoreDC */
488     NULL,                               /* pRoundRect */
489     NULL,                               /* pSaveDC */
490     NULL,                               /* pScaleViewportExt */
491     NULL,                               /* pScaleWindowExt */
492     dibdrv_SelectBitmap,                /* pSelectBitmap */
493     dibdrv_SelectBrush,                 /* pSelectBrush */
494     NULL,                               /* pSelectClipPath */
495     NULL,                               /* pSelectFont */
496     NULL,                               /* pSelectPalette */
497     dibdrv_SelectPen,                   /* pSelectPen */
498     NULL,                               /* pSetArcDirection */
499     NULL,                               /* pSetBitmapBits */
500     dibdrv_SetBkColor,                  /* pSetBkColor */
501     dibdrv_SetBkMode,                   /* pSetBkMode */
502     dibdrv_SetDCBrushColor,             /* pSetDCBrushColor */
503     dibdrv_SetDCPenColor,               /* pSetDCPenColor */
504     dibdrv_SetDIBColorTable,            /* pSetDIBColorTable */
505     NULL,                               /* pSetDIBits */
506     NULL,                               /* pSetDIBitsToDevice */
507     dibdrv_SetDeviceClipping,           /* pSetDeviceClipping */
508     NULL,                               /* pSetDeviceGammaRamp */
509     NULL,                               /* pSetLayout */
510     NULL,                               /* pSetMapMode */
511     NULL,                               /* pSetMapperFlags */
512     NULL,                               /* pSetPixel */
513     NULL,                               /* pSetPixelFormat */
514     NULL,                               /* pSetPolyFillMode */
515     dibdrv_SetROP2,                     /* pSetROP2 */
516     NULL,                               /* pSetRelAbs */
517     NULL,                               /* pSetStretchBltMode */
518     NULL,                               /* pSetTextAlign */
519     NULL,                               /* pSetTextCharacterExtra */
520     NULL,                               /* pSetTextColor */
521     NULL,                               /* pSetTextJustification */
522     NULL,                               /* pSetViewportExt */
523     NULL,                               /* pSetViewportOrg */
524     NULL,                               /* pSetWindowExt */
525     NULL,                               /* pSetWindowOrg */
526     NULL,                               /* pSetWorldTransform */
527     NULL,                               /* pStartDoc */
528     NULL,                               /* pStartPage */
529     NULL,                               /* pStretchBlt */
530     NULL,                               /* pStretchDIBits */
531     NULL,                               /* pStrokeAndFillPath */
532     NULL,                               /* pStrokePath */
533     NULL,                               /* pSwapBuffers */
534     NULL,                               /* pUnrealizePalette */
535     NULL,                               /* pWidenPath */
536     NULL,                               /* pwglCopyContext */
537     NULL,                               /* pwglCreateContext */
538     NULL,                               /* pwglCreateContextAttribsARB */
539     NULL,                               /* pwglDeleteContext */
540     NULL,                               /* pwglGetProcAddress */
541     NULL,                               /* pwglGetPbufferDCARB */
542     NULL,                               /* pwglMakeCurrent */
543     NULL,                               /* pwglMakeContextCurrentARB */
544     NULL,                               /* pwglSetPixelFormatWINE */
545     NULL,                               /* pwglShareLists */
546     NULL,                               /* pwglUseFontBitmapsA */
547     NULL                                /* pwglUseFontBitmapsW */
548 };