comctl32: Fix a logical operator typo.
[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/exception.h"
27 #include "wine/debug.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(dib);
30
31 static const DWORD bit_fields_888[3] = {0xff0000, 0x00ff00, 0x0000ff};
32 static const DWORD bit_fields_555[3] = {0x7c00, 0x03e0, 0x001f};
33
34 static void calc_shift_and_len(DWORD mask, int *shift, int *len)
35 {
36     int s, l;
37
38     if(!mask)
39     {
40         *shift = *len = 0;
41         return;
42     }
43
44     s = 0;
45     while ((mask & 1) == 0)
46     {
47         mask >>= 1;
48         s++;
49     }
50     l = 0;
51     while ((mask & 1) == 1)
52     {
53         mask >>= 1;
54         l++;
55     }
56     *shift = s;
57     *len = l;
58 }
59
60 static void init_bit_fields(dib_info *dib, const DWORD *bit_fields)
61 {
62     dib->red_mask    = bit_fields[0];
63     dib->green_mask  = bit_fields[1];
64     dib->blue_mask   = bit_fields[2];
65     calc_shift_and_len(dib->red_mask,   &dib->red_shift,   &dib->red_len);
66     calc_shift_and_len(dib->green_mask, &dib->green_shift, &dib->green_len);
67     calc_shift_and_len(dib->blue_mask,  &dib->blue_shift,  &dib->blue_len);
68 }
69
70 BOOL init_dib_info(dib_info *dib, const BITMAPINFOHEADER *bi, const DWORD *bit_fields,
71                    RGBQUAD *color_table, int color_table_size, void *bits, enum dib_info_flags flags)
72 {
73     dib->bit_count    = bi->biBitCount;
74     dib->width        = bi->biWidth;
75     dib->height       = bi->biHeight;
76     dib->stride       = get_dib_stride( dib->width, dib->bit_count );
77     dib->bits.ptr     = bits;
78     dib->bits.is_copy = FALSE;
79     dib->bits.free    = NULL;
80     dib->bits.param   = NULL;
81     dib->flags        = flags;
82
83     if(dib->height < 0) /* top-down */
84     {
85         dib->height = -dib->height;
86     }
87     else /* bottom-up */
88     {
89         /* bits always points to the top-left corner and the stride is -ve */
90         dib->bits.ptr = (BYTE*)dib->bits.ptr + (dib->height - 1) * dib->stride;
91         dib->stride  = -dib->stride;
92     }
93
94     dib->funcs = &funcs_null;
95
96     switch(dib->bit_count)
97     {
98     case 32:
99         if(bi->biCompression == BI_RGB)
100             bit_fields = bit_fields_888;
101
102         init_bit_fields(dib, bit_fields);
103
104         if(dib->red_mask == 0xff0000 && dib->green_mask == 0x00ff00 && dib->blue_mask == 0x0000ff)
105             dib->funcs = &funcs_8888;
106         else
107             dib->funcs = &funcs_32;
108         break;
109
110     case 24:
111         dib->funcs = &funcs_24;
112         break;
113
114     case 16:
115         if(bi->biCompression == BI_RGB)
116             bit_fields = bit_fields_555;
117
118         init_bit_fields(dib, bit_fields);
119
120         if(dib->red_mask == 0x7c00 && dib->green_mask == 0x03e0 && dib->blue_mask == 0x001f)
121             dib->funcs = &funcs_555;
122         else
123             dib->funcs = &funcs_16;
124         break;
125
126     case 8:
127         dib->funcs = &funcs_8;
128         break;
129
130     case 4:
131         dib->funcs = &funcs_4;
132         break;
133
134     case 1:
135         dib->funcs = &funcs_1;
136         break;
137
138     default:
139         TRACE("bpp %d not supported, will forward to graphics driver.\n", dib->bit_count);
140         return FALSE;
141     }
142
143     if(color_table)
144     {
145         if (flags & private_color_table)
146         {
147             dib->color_table = HeapAlloc(GetProcessHeap(), 0, color_table_size * sizeof(dib->color_table[0]));
148             if(!dib->color_table) return FALSE;
149             memcpy(dib->color_table, color_table, color_table_size * sizeof(color_table[0]));
150         }
151         else
152             dib->color_table = color_table;
153         dib->color_table_size = color_table_size;
154     }
155     else
156     {
157         dib->color_table = NULL;
158         dib->color_table_size = 0;
159     }
160
161     return TRUE;
162 }
163
164 BOOL init_dib_info_from_packed(dib_info *dib, const BITMAPINFOHEADER *bi, WORD usage, HPALETTE palette)
165 {
166     DWORD *masks = NULL;
167     RGBQUAD *color_table = NULL, pal_table[256];
168     BYTE *ptr = (BYTE*)bi + bi->biSize;
169     int num_colors = get_dib_num_of_colors( (const BITMAPINFO *)bi );
170
171     if(bi->biCompression == BI_BITFIELDS)
172     {
173         masks = (DWORD *)ptr;
174         ptr += 3 * sizeof(DWORD);
175     }
176
177     if(num_colors)
178     {
179         if(usage == DIB_PAL_COLORS)
180         {
181             PALETTEENTRY entries[256];
182             const WORD *index = (const WORD*) ptr;
183             UINT i, count = GetPaletteEntries( palette, 0, num_colors, entries );
184             for (i = 0; i < num_colors; i++, index++)
185             {
186                 PALETTEENTRY *entry = &entries[*index % count];
187                 pal_table[i].rgbRed      = entry->peRed;
188                 pal_table[i].rgbGreen    = entry->peGreen;
189                 pal_table[i].rgbBlue     = entry->peBlue;
190                 pal_table[i].rgbReserved = 0;
191             }
192             color_table = pal_table;
193             ptr += num_colors * sizeof(WORD);
194         }
195         else
196         {
197             color_table = (RGBQUAD*)ptr;
198             ptr += num_colors * sizeof(*color_table);
199         }
200     }
201
202     return init_dib_info(dib, bi, masks, color_table, num_colors, ptr, private_color_table);
203 }
204
205 BOOL init_dib_info_from_bitmapinfo(dib_info *dib, const BITMAPINFO *info, void *bits, enum dib_info_flags flags)
206 {
207     unsigned int colors = get_dib_num_of_colors( info );
208     void *colorptr = (char *)&info->bmiHeader + info->bmiHeader.biSize;
209     const DWORD *bitfields = (info->bmiHeader.biCompression == BI_BITFIELDS) ? (DWORD *)colorptr : NULL;
210
211     return init_dib_info( dib, &info->bmiHeader, bitfields, colors ? colorptr : NULL, colors, bits, flags );
212 }
213
214 static void clear_dib_info(dib_info *dib)
215 {
216     dib->color_table = NULL;
217     dib->bits.ptr    = NULL;
218     dib->bits.free   = NULL;
219     dib->bits.param  = NULL;
220 }
221
222 /**********************************************************************
223  *      free_dib_info
224  *
225  * Free the resources associated with a dib and optionally the bits
226  */
227 void free_dib_info(dib_info *dib)
228 {
229     if (dib->flags & private_color_table)
230         HeapFree(GetProcessHeap(), 0, dib->color_table);
231
232     if (dib->bits.free) dib->bits.free( &dib->bits );
233     clear_dib_info( dib );
234 }
235
236 void copy_dib_color_info(dib_info *dst, const dib_info *src)
237 {
238     dst->bit_count        = src->bit_count;
239     dst->red_mask         = src->red_mask;
240     dst->green_mask       = src->green_mask;
241     dst->blue_mask        = src->blue_mask;
242     dst->red_len          = src->red_len;
243     dst->green_len        = src->green_len;
244     dst->blue_len         = src->blue_len;
245     dst->red_shift        = src->red_shift;
246     dst->green_shift      = src->green_shift;
247     dst->blue_shift       = src->blue_shift;
248     dst->funcs            = src->funcs;
249     dst->color_table_size = src->color_table_size;
250     dst->color_table      = NULL;
251     dst->flags            = src->flags;
252     if(dst->color_table_size)
253     {
254         int size = dst->color_table_size * sizeof(dst->color_table[0]);
255         if (dst->flags & private_color_table)
256         {
257             dst->color_table = HeapAlloc(GetProcessHeap(), 0, size);
258             memcpy(dst->color_table, src->color_table, size);
259         }
260         else
261             dst->color_table = src->color_table;
262     }
263 }
264
265 DWORD convert_bitmapinfo( const BITMAPINFO *src_info, void *src_bits, struct bitblt_coords *src,
266                           const BITMAPINFO *dst_info, void *dst_bits )
267 {
268     dib_info src_dib, dst_dib;
269     DWORD ret;
270
271     if ( !init_dib_info_from_bitmapinfo( &src_dib, src_info, src_bits, 0 ) )
272         return ERROR_BAD_FORMAT;
273     if ( !init_dib_info_from_bitmapinfo( &dst_dib, dst_info, dst_bits, 0 ) )
274         return ERROR_BAD_FORMAT;
275
276     __TRY
277     {
278         ret = dst_dib.funcs->convert_to( &dst_dib, &src_dib, &src->visrect );
279     }
280     __EXCEPT_PAGE_FAULT
281     {
282         WARN( "invalid bits pointer %p\n", src_bits );
283         ret = FALSE;
284     }
285     __ENDTRY
286
287     /* We shared the color tables, so there's no need to free the dib_infos here */
288     if(!ret) return ERROR_BAD_FORMAT;
289
290     /* update coordinates, the destination rectangle is always stored at 0,0 */
291     src->x -= src->visrect.left;
292     src->y -= src->visrect.top;
293     offset_rect( &src->visrect, -src->visrect.left, -src->visrect.top );
294     return ERROR_SUCCESS;
295 }
296
297 static void update_fg_colors( dibdrv_physdev *pdev )
298 {
299     pdev->pen_color   = get_fg_color( pdev, pdev->pen_colorref );
300     pdev->brush_color = get_fg_color( pdev, pdev->brush_colorref );
301 }
302
303 static void update_masks( dibdrv_physdev *pdev, INT rop )
304 {
305     calc_and_xor_masks( rop, pdev->pen_color, &pdev->pen_and, &pdev->pen_xor );
306     update_brush_rop( pdev, rop );
307     if( GetBkMode( pdev->dev.hdc ) == OPAQUE )
308         calc_and_xor_masks( rop, pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
309 }
310
311  /***********************************************************************
312  *           add_extra_clipping_region
313  *
314  * Temporarily add a region to the current clipping region.
315  * The returned region must be restored with restore_clipping_region.
316  */
317 HRGN add_extra_clipping_region( dibdrv_physdev *pdev, HRGN rgn )
318 {
319     HRGN ret, clip;
320
321     if (!(clip = CreateRectRgn( 0, 0, 0, 0 ))) return 0;
322     CombineRgn( clip, pdev->clip, rgn, RGN_AND );
323     ret = pdev->clip;
324     pdev->clip = clip;
325     return ret;
326 }
327
328 /***********************************************************************
329  *           restore_clipping_region
330  */
331 void restore_clipping_region( dibdrv_physdev *pdev, HRGN rgn )
332 {
333     if (!rgn) return;
334     DeleteObject( pdev->clip );
335     pdev->clip = rgn;
336 }
337
338 /***********************************************************************
339  *           dibdrv_DeleteDC
340  */
341 static BOOL dibdrv_DeleteDC( PHYSDEV dev )
342 {
343     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
344     TRACE("(%p)\n", dev);
345     DeleteObject(pdev->clip);
346     free_pattern_brush(pdev);
347     free_dib_info(&pdev->dib);
348     return 0;
349 }
350
351 /***********************************************************************
352  *           dibdrv_SelectBitmap
353  */
354 static HBITMAP dibdrv_SelectBitmap( PHYSDEV dev, HBITMAP bitmap )
355 {
356     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectBitmap );
357     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
358     BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
359     TRACE("(%p, %p)\n", dev, bitmap);
360
361     if (!bmp) return 0;
362     assert(bmp->dib);
363
364     pdev->clip = CreateRectRgn(0, 0, 0, 0);
365     pdev->defer = 0;
366
367     clear_dib_info(&pdev->dib);
368     clear_dib_info(&pdev->brush_dib);
369     pdev->brush_and_bits = pdev->brush_xor_bits = NULL;
370
371     if(!init_dib_info(&pdev->dib, &bmp->dib->dsBmih, bmp->dib->dsBitfields,
372                       bmp->color_table, bmp->nb_colors, bmp->dib->dsBm.bmBits, private_color_table))
373         pdev->defer |= DEFER_FORMAT;
374
375     GDI_ReleaseObj( bitmap );
376
377     return next->funcs->pSelectBitmap( next, bitmap );
378 }
379
380 /***********************************************************************
381  *           dibdrv_SetBkColor
382  */
383 static COLORREF dibdrv_SetBkColor( PHYSDEV dev, COLORREF color )
384 {
385     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetBkColor );
386     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
387
388     pdev->bkgnd_color = pdev->dib.funcs->colorref_to_pixel( &pdev->dib, color );
389
390     if( GetBkMode(dev->hdc) == OPAQUE )
391         calc_and_xor_masks( GetROP2(dev->hdc), pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
392     else
393     {
394         pdev->bkgnd_and = ~0u;
395         pdev->bkgnd_xor = 0;
396     }
397
398     update_fg_colors( pdev ); /* Only needed in the 1 bpp case */
399
400     return next->funcs->pSetBkColor( next, color );
401 }
402
403 /***********************************************************************
404  *           dibdrv_SetBkMode
405  */
406 static INT dibdrv_SetBkMode( PHYSDEV dev, INT mode )
407 {
408     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetBkMode );
409     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
410
411     if( mode == OPAQUE )
412         calc_and_xor_masks( GetROP2(dev->hdc), pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
413     else
414     {
415         pdev->bkgnd_and = ~0u;
416         pdev->bkgnd_xor = 0;
417     }
418
419     return next->funcs->pSetBkMode( next, mode );
420 }
421
422 /***********************************************************************
423  *           dibdrv_SetDeviceClipping
424  */
425 static void dibdrv_SetDeviceClipping( PHYSDEV dev, HRGN vis_rgn, HRGN clip_rgn )
426 {
427     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDeviceClipping );
428     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
429     TRACE("(%p, %p, %p)\n", dev, vis_rgn, clip_rgn);
430
431     CombineRgn( pdev->clip, vis_rgn, clip_rgn, clip_rgn ? RGN_AND : RGN_COPY );
432     return next->funcs->pSetDeviceClipping( next, vis_rgn, clip_rgn);
433 }
434
435 /***********************************************************************
436  *           dibdrv_SetDIBColorTable
437  */
438 static UINT dibdrv_SetDIBColorTable( PHYSDEV dev, UINT pos, UINT count, const RGBQUAD *colors )
439 {
440     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDIBColorTable );
441     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
442     TRACE("(%p, %d, %d, %p)\n", dev, pos, count, colors);
443
444     if( pdev->dib.color_table && pos < pdev->dib.color_table_size )
445     {
446         if( pos + count > pdev->dib.color_table_size ) count = pdev->dib.color_table_size - pos;
447         memcpy( pdev->dib.color_table + pos, colors, count * sizeof(RGBQUAD) );
448
449         pdev->bkgnd_color = pdev->dib.funcs->colorref_to_pixel( &pdev->dib, GetBkColor( dev->hdc ) );
450         update_fg_colors( pdev );
451
452         update_masks( pdev, GetROP2( dev->hdc ) );
453     }
454     return next->funcs->pSetDIBColorTable( next, pos, count, colors );
455 }
456
457 /***********************************************************************
458  *           dibdrv_SetROP2
459  */
460 static INT dibdrv_SetROP2( PHYSDEV dev, INT rop )
461 {
462     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetROP2 );
463     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
464
465     update_masks( pdev, rop );
466
467     return next->funcs->pSetROP2( next, rop );
468 }
469
470 const DC_FUNCTIONS dib_driver =
471 {
472     NULL,                               /* pAbortDoc */
473     NULL,                               /* pAbortPath */
474     NULL,                               /* pAlphaBlend */
475     NULL,                               /* pAngleArc */
476     NULL,                               /* pArc */
477     NULL,                               /* pArcTo */
478     NULL,                               /* pBeginPath */
479     NULL,                               /* pChoosePixelFormat */
480     NULL,                               /* pChord */
481     NULL,                               /* pCloseFigure */
482     NULL,                               /* pCreateBitmap */
483     NULL,                               /* pCreateDC */
484     NULL,                               /* pCreateDIBSection */
485     NULL,                               /* pDeleteBitmap */
486     dibdrv_DeleteDC,                    /* pDeleteDC */
487     NULL,                               /* pDeleteObject */
488     NULL,                               /* pDescribePixelFormat */
489     NULL,                               /* pDeviceCapabilities */
490     NULL,                               /* pEllipse */
491     NULL,                               /* pEndDoc */
492     NULL,                               /* pEndPage */
493     NULL,                               /* pEndPath */
494     NULL,                               /* pEnumDeviceFonts */
495     NULL,                               /* pEnumICMProfiles */
496     NULL,                               /* pExcludeClipRect */
497     NULL,                               /* pExtDeviceMode */
498     NULL,                               /* pExtEscape */
499     NULL,                               /* pExtFloodFill */
500     NULL,                               /* pExtSelectClipRgn */
501     NULL,                               /* pExtTextOut */
502     NULL,                               /* pFillPath */
503     NULL,                               /* pFillRgn */
504     NULL,                               /* pFlattenPath */
505     NULL,                               /* pFrameRgn */
506     NULL,                               /* pGdiComment */
507     NULL,                               /* pGetCharWidth */
508     NULL,                               /* pGetDeviceCaps */
509     NULL,                               /* pGetDeviceGammaRamp */
510     NULL,                               /* pGetICMProfile */
511     dibdrv_GetImage,                    /* pGetImage */
512     NULL,                               /* pGetNearestColor */
513     NULL,                               /* pGetPixel */
514     NULL,                               /* pGetPixelFormat */
515     NULL,                               /* pGetSystemPaletteEntries */
516     NULL,                               /* pGetTextExtentExPoint */
517     NULL,                               /* pGetTextMetrics */
518     NULL,                               /* pIntersectClipRect */
519     NULL,                               /* pInvertRgn */
520     dibdrv_LineTo,                      /* pLineTo */
521     NULL,                               /* pModifyWorldTransform */
522     NULL,                               /* pMoveTo */
523     NULL,                               /* pOffsetClipRgn */
524     NULL,                               /* pOffsetViewportOrg */
525     NULL,                               /* pOffsetWindowOrg */
526     dibdrv_PaintRgn,                    /* pPaintRgn */
527     dibdrv_PatBlt,                      /* pPatBlt */
528     NULL,                               /* pPie */
529     NULL,                               /* pPolyBezier */
530     NULL,                               /* pPolyBezierTo */
531     NULL,                               /* pPolyDraw */
532     NULL,                               /* pPolyPolygon */
533     dibdrv_PolyPolyline,                /* pPolyPolyline */
534     NULL,                               /* pPolygon */
535     dibdrv_Polyline,                    /* pPolyline */
536     NULL,                               /* pPolylineTo */
537     dibdrv_PutImage,                    /* pPutImage */
538     NULL,                               /* pRealizeDefaultPalette */
539     NULL,                               /* pRealizePalette */
540     dibdrv_Rectangle,                   /* pRectangle */
541     NULL,                               /* pResetDC */
542     NULL,                               /* pRestoreDC */
543     NULL,                               /* pRoundRect */
544     NULL,                               /* pSaveDC */
545     NULL,                               /* pScaleViewportExt */
546     NULL,                               /* pScaleWindowExt */
547     dibdrv_SelectBitmap,                /* pSelectBitmap */
548     dibdrv_SelectBrush,                 /* pSelectBrush */
549     NULL,                               /* pSelectClipPath */
550     NULL,                               /* pSelectFont */
551     NULL,                               /* pSelectPalette */
552     dibdrv_SelectPen,                   /* pSelectPen */
553     NULL,                               /* pSetArcDirection */
554     dibdrv_SetBkColor,                  /* pSetBkColor */
555     dibdrv_SetBkMode,                   /* pSetBkMode */
556     dibdrv_SetDCBrushColor,             /* pSetDCBrushColor */
557     dibdrv_SetDCPenColor,               /* pSetDCPenColor */
558     dibdrv_SetDIBColorTable,            /* pSetDIBColorTable */
559     NULL,                               /* pSetDIBitsToDevice */
560     dibdrv_SetDeviceClipping,           /* pSetDeviceClipping */
561     NULL,                               /* pSetDeviceGammaRamp */
562     NULL,                               /* pSetLayout */
563     NULL,                               /* pSetMapMode */
564     NULL,                               /* pSetMapperFlags */
565     NULL,                               /* pSetPixel */
566     NULL,                               /* pSetPixelFormat */
567     NULL,                               /* pSetPolyFillMode */
568     dibdrv_SetROP2,                     /* pSetROP2 */
569     NULL,                               /* pSetRelAbs */
570     NULL,                               /* pSetStretchBltMode */
571     NULL,                               /* pSetTextAlign */
572     NULL,                               /* pSetTextCharacterExtra */
573     NULL,                               /* pSetTextColor */
574     NULL,                               /* pSetTextJustification */
575     NULL,                               /* pSetViewportExt */
576     NULL,                               /* pSetViewportOrg */
577     NULL,                               /* pSetWindowExt */
578     NULL,                               /* pSetWindowOrg */
579     NULL,                               /* pSetWorldTransform */
580     NULL,                               /* pStartDoc */
581     NULL,                               /* pStartPage */
582     NULL,                               /* pStretchBlt */
583     NULL,                               /* pStretchDIBits */
584     NULL,                               /* pStrokeAndFillPath */
585     NULL,                               /* pStrokePath */
586     NULL,                               /* pSwapBuffers */
587     NULL,                               /* pUnrealizePalette */
588     NULL,                               /* pWidenPath */
589     NULL,                               /* pwglCopyContext */
590     NULL,                               /* pwglCreateContext */
591     NULL,                               /* pwglCreateContextAttribsARB */
592     NULL,                               /* pwglDeleteContext */
593     NULL,                               /* pwglGetPbufferDCARB */
594     NULL,                               /* pwglGetProcAddress */
595     NULL,                               /* pwglMakeContextCurrentARB */
596     NULL,                               /* pwglMakeCurrent */
597     NULL,                               /* pwglSetPixelFormatWINE */
598     NULL,                               /* pwglShareLists */
599     NULL,                               /* pwglUseFontBitmapsA */
600     NULL                                /* pwglUseFontBitmapsW */
601 };