po: Update French translation.
[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 static void init_dib_info(dib_info *dib, const BITMAPINFOHEADER *bi, const DWORD *bit_fields,
71                           const RGBQUAD *color_table, void *bits)
72 {
73     dib->bit_count    = bi->biBitCount;
74     dib->width        = bi->biWidth;
75     dib->height       = bi->biHeight;
76     dib->rect.left    = 0;
77     dib->rect.top     = 0;
78     dib->rect.right   = bi->biWidth;
79     dib->rect.bottom  = abs( bi->biHeight );
80     dib->compression  = bi->biCompression;
81     dib->stride       = get_dib_stride( dib->width, dib->bit_count );
82     dib->bits.ptr     = bits;
83     dib->bits.is_copy = FALSE;
84     dib->bits.free    = NULL;
85     dib->bits.param   = NULL;
86
87     if(dib->height < 0) /* top-down */
88     {
89         dib->height = -dib->height;
90     }
91     else /* bottom-up */
92     {
93         /* bits always points to the top-left corner and the stride is -ve */
94         dib->bits.ptr = (BYTE*)dib->bits.ptr + (dib->height - 1) * dib->stride;
95         dib->stride  = -dib->stride;
96     }
97
98     dib->funcs = &funcs_null;
99
100     switch(dib->bit_count)
101     {
102     case 32:
103         if(bi->biCompression == BI_RGB)
104             bit_fields = bit_fields_888;
105
106         init_bit_fields(dib, bit_fields);
107
108         if(dib->red_mask == 0xff0000 && dib->green_mask == 0x00ff00 && dib->blue_mask == 0x0000ff)
109             dib->funcs = &funcs_8888;
110         else
111             dib->funcs = &funcs_32;
112         break;
113
114     case 24:
115         dib->funcs = &funcs_24;
116         break;
117
118     case 16:
119         if(bi->biCompression == BI_RGB)
120             bit_fields = bit_fields_555;
121
122         init_bit_fields(dib, bit_fields);
123
124         if(dib->red_mask == 0x7c00 && dib->green_mask == 0x03e0 && dib->blue_mask == 0x001f)
125             dib->funcs = &funcs_555;
126         else
127             dib->funcs = &funcs_16;
128         break;
129
130     case 8:
131         dib->funcs = &funcs_8;
132         break;
133
134     case 4:
135         dib->funcs = &funcs_4;
136         break;
137
138     case 1:
139         dib->funcs = &funcs_1;
140         break;
141     }
142
143     if (color_table && bi->biClrUsed)
144     {
145         dib->color_table = color_table;
146         dib->color_table_size = bi->biClrUsed;
147     }
148     else
149     {
150         dib->color_table = NULL;
151         dib->color_table_size = 0;
152     }
153 }
154
155 void init_dib_info_from_bitmapinfo(dib_info *dib, const BITMAPINFO *info, void *bits)
156 {
157     init_dib_info( dib, &info->bmiHeader, (const DWORD *)info->bmiColors, info->bmiColors, bits );
158 }
159
160 BOOL init_dib_info_from_bitmapobj(dib_info *dib, BITMAPOBJ *bmp)
161 {
162     if (!is_bitmapobj_dib( bmp ))
163     {
164         BITMAPINFO info;
165
166         get_ddb_bitmapinfo( bmp, &info );
167         if (!bmp->dib.dsBm.bmBits)
168         {
169             int width_bytes = get_dib_stride( bmp->dib.dsBm.bmWidth, bmp->dib.dsBm.bmBitsPixel );
170             bmp->dib.dsBm.bmBits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
171                                               bmp->dib.dsBm.bmHeight * width_bytes );
172             if (!bmp->dib.dsBm.bmBits) return FALSE;
173         }
174         init_dib_info_from_bitmapinfo( dib, &info, bmp->dib.dsBm.bmBits );
175     }
176     else init_dib_info( dib, &bmp->dib.dsBmih, bmp->dib.dsBitfields,
177                         bmp->color_table, bmp->dib.dsBm.bmBits );
178     return TRUE;
179 }
180
181 static void clear_dib_info(dib_info *dib)
182 {
183     dib->bits.ptr    = NULL;
184     dib->bits.free   = NULL;
185     dib->bits.param  = NULL;
186 }
187
188 /**********************************************************************
189  *      free_dib_info
190  *
191  * Free the resources associated with a dib and optionally the bits
192  */
193 void free_dib_info(dib_info *dib)
194 {
195     if (dib->bits.free) dib->bits.free( &dib->bits );
196     clear_dib_info( dib );
197 }
198
199 void copy_dib_color_info(dib_info *dst, const dib_info *src)
200 {
201     dst->bit_count        = src->bit_count;
202     dst->red_mask         = src->red_mask;
203     dst->green_mask       = src->green_mask;
204     dst->blue_mask        = src->blue_mask;
205     dst->red_len          = src->red_len;
206     dst->green_len        = src->green_len;
207     dst->blue_len         = src->blue_len;
208     dst->red_shift        = src->red_shift;
209     dst->green_shift      = src->green_shift;
210     dst->blue_shift       = src->blue_shift;
211     dst->funcs            = src->funcs;
212     dst->color_table_size = src->color_table_size;
213     dst->color_table      = src->color_table;
214 }
215
216 DWORD convert_bitmapinfo( const BITMAPINFO *src_info, void *src_bits, struct bitblt_coords *src,
217                           const BITMAPINFO *dst_info, void *dst_bits, BOOL add_alpha )
218 {
219     dib_info src_dib, dst_dib;
220     DWORD ret;
221
222     init_dib_info_from_bitmapinfo( &src_dib, src_info, src_bits );
223     init_dib_info_from_bitmapinfo( &dst_dib, dst_info, dst_bits );
224
225     __TRY
226     {
227         dst_dib.funcs->convert_to( &dst_dib, &src_dib, &src->visrect, FALSE );
228         ret = TRUE;
229     }
230     __EXCEPT_PAGE_FAULT
231     {
232         WARN( "invalid bits pointer %p\n", src_bits );
233         ret = FALSE;
234     }
235     __ENDTRY
236
237     /* We shared the color tables, so there's no need to free the dib_infos here */
238     if(!ret) return ERROR_BAD_FORMAT;
239
240     /* update coordinates, the destination rectangle is always stored at 0,0 */
241     src->x -= src->visrect.left;
242     src->y -= src->visrect.top;
243     offset_rect( &src->visrect, -src->visrect.left, -src->visrect.top );
244
245     if (add_alpha && dst_dib.funcs == &funcs_8888 && src_dib.funcs != &funcs_8888)
246     {
247         DWORD *pixel = dst_dib.bits.ptr;
248         int x, y;
249
250         for (y = src->visrect.top; y < src->visrect.bottom; y++, pixel += dst_dib.stride / 4)
251             for (x = src->visrect.left; x < src->visrect.right; x++)
252                 pixel[x] |= 0xff000000;
253     }
254
255     return ERROR_SUCCESS;
256 }
257
258 int get_clipped_rects( const dib_info *dib, const RECT *rc, HRGN clip, struct clipped_rects *clip_rects )
259 {
260     const WINEREGION *region;
261     RECT rect, *out = clip_rects->buffer;
262     int i;
263
264     init_clipped_rects( clip_rects );
265
266     rect.left   = 0;
267     rect.top    = 0;
268     rect.right  = dib->rect.right - dib->rect.left;
269     rect.bottom = dib->rect.bottom - dib->rect.top;
270     if (rc && !intersect_rect( &rect, &rect, rc )) return 0;
271
272     if (!clip)
273     {
274         *out = rect;
275         clip_rects->count = 1;
276         return 1;
277     }
278
279     if (!(region = get_wine_region( clip ))) return 0;
280
281     for (i = 0; i < region->numRects; i++)
282     {
283         if (region->rects[i].top >= rect.bottom) break;
284         if (!intersect_rect( out, &rect, &region->rects[i] )) continue;
285         out++;
286         if (out == &clip_rects->buffer[sizeof(clip_rects->buffer) / sizeof(RECT)])
287         {
288             clip_rects->rects = HeapAlloc( GetProcessHeap(), 0, region->numRects * sizeof(RECT) );
289             if (!clip_rects->rects) return 0;
290             memcpy( clip_rects->rects, clip_rects->buffer, (out - clip_rects->buffer) * sizeof(RECT) );
291             out = clip_rects->rects + (out - clip_rects->buffer);
292         }
293     }
294     release_wine_region( clip );
295     clip_rects->count = out - clip_rects->rects;
296     return clip_rects->count;
297 }
298
299 void add_clipped_bounds( dibdrv_physdev *dev, const RECT *rect, HRGN clip )
300 {
301     const WINEREGION *region;
302     RECT rc;
303
304     if (!dev->bounds) return;
305     if (clip)
306     {
307         if (!(region = get_wine_region( clip ))) return;
308         if (!rect) rc = region->extents;
309         else intersect_rect( &rc, rect, &region->extents );
310         release_wine_region( clip );
311     }
312     else rc = *rect;
313
314     if (is_rect_empty( &rc )) return;
315     offset_rect( &rc, dev->dib.rect.left, dev->dib.rect.top );
316     add_bounds_rect( dev->bounds, &rc );
317 }
318
319 /**********************************************************************
320  *           dibdrv_CreateDC
321  */
322 static BOOL dibdrv_CreateDC( PHYSDEV *dev, LPCWSTR driver, LPCWSTR device,
323                              LPCWSTR output, const DEVMODEW *data )
324 {
325     dibdrv_physdev *pdev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pdev) );
326
327     if (!pdev) return FALSE;
328     clear_dib_info(&pdev->dib);
329     clear_dib_info(&pdev->brush.dib);
330     clear_dib_info(&pdev->pen_brush.dib);
331     push_dc_driver( dev, &pdev->dev, &dib_driver );
332     return TRUE;
333 }
334
335 /***********************************************************************
336  *           dibdrv_DeleteDC
337  */
338 static BOOL dibdrv_DeleteDC( PHYSDEV dev )
339 {
340     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
341     TRACE("(%p)\n", dev);
342     free_pattern_brush( &pdev->brush );
343     free_pattern_brush( &pdev->pen_brush );
344     HeapFree( GetProcessHeap(), 0, pdev );
345     return TRUE;
346 }
347
348 /***********************************************************************
349  *           dibdrv_CopyBitmap
350  */
351 static BOOL dibdrv_CopyBitmap( HBITMAP src, HBITMAP dst )
352 {
353     return nulldrv_CopyBitmap( src, dst );
354 }
355
356 /***********************************************************************
357  *           dibdrv_DeleteBitmap
358  */
359 static BOOL dibdrv_DeleteBitmap( HBITMAP bitmap )
360 {
361     return TRUE;
362 }
363
364 /***********************************************************************
365  *           dibdrv_SelectBitmap
366  */
367 static HBITMAP dibdrv_SelectBitmap( PHYSDEV dev, HBITMAP bitmap )
368 {
369     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
370     BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
371     dib_info dib;
372
373     TRACE("(%p, %p)\n", dev, bitmap);
374
375     if (!bmp) return 0;
376
377     if (!init_dib_info_from_bitmapobj(&dib, bmp))
378     {
379         GDI_ReleaseObj( bitmap );
380         return 0;
381     }
382     pdev->dib = dib;
383     GDI_ReleaseObj( bitmap );
384
385     return bitmap;
386 }
387
388 /***********************************************************************
389  *           dibdrv_SetDeviceClipping
390  */
391 static void dibdrv_SetDeviceClipping( PHYSDEV dev, HRGN rgn )
392 {
393     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
394     TRACE("(%p, %p)\n", dev, rgn);
395
396     pdev->clip = rgn;
397 }
398
399 /***********************************************************************
400  *           dibdrv_SetBoundsRect
401  */
402 static UINT dibdrv_SetBoundsRect( PHYSDEV dev, RECT *rect, UINT flags )
403 {
404     dibdrv_physdev *pdev = get_dibdrv_pdev( dev );
405
406     if (flags & DCB_DISABLE) pdev->bounds = NULL;
407     else if (flags & DCB_ENABLE) pdev->bounds = rect;
408     return DCB_RESET;  /* we don't have device-specific bounds */
409 }
410
411 /***********************************************************************
412  *           dibdrv_GetDeviceGammaRamp
413  */
414 static BOOL dibdrv_GetDeviceGammaRamp( PHYSDEV dev, void *ramp )
415 {
416     SetLastError( ERROR_INVALID_PARAMETER );
417     return FALSE;
418 }
419
420 /***********************************************************************
421  *           dibdrv_SetDeviceGammaRamp
422  */
423 static BOOL dibdrv_SetDeviceGammaRamp( PHYSDEV dev, void *ramp )
424 {
425     SetLastError( ERROR_INVALID_PARAMETER );
426     return FALSE;
427 }
428
429 const struct gdi_dc_funcs dib_driver =
430 {
431     NULL,                               /* pAbortDoc */
432     NULL,                               /* pAbortPath */
433     dibdrv_AlphaBlend,                  /* pAlphaBlend */
434     NULL,                               /* pAngleArc */
435     dibdrv_Arc,                         /* pArc */
436     dibdrv_ArcTo,                       /* pArcTo */
437     NULL,                               /* pBeginPath */
438     dibdrv_BlendImage,                  /* pBlendImage */
439     NULL,                               /* pChoosePixelFormat */
440     dibdrv_Chord,                       /* pChord */
441     NULL,                               /* pCloseFigure */
442     dibdrv_CopyBitmap,                  /* pCopyBitmap */
443     NULL,                               /* pCreateBitmap */
444     NULL,                               /* pCreateCompatibleDC */
445     dibdrv_CreateDC,                    /* pCreateDC */
446     dibdrv_DeleteBitmap,                /* pDeleteBitmap */
447     dibdrv_DeleteDC,                    /* pDeleteDC */
448     NULL,                               /* pDeleteObject */
449     NULL,                               /* pDescribePixelFormat */
450     NULL,                               /* pDeviceCapabilities */
451     dibdrv_Ellipse,                     /* pEllipse */
452     NULL,                               /* pEndDoc */
453     NULL,                               /* pEndPage */
454     NULL,                               /* pEndPath */
455     NULL,                               /* pEnumFonts */
456     NULL,                               /* pEnumICMProfiles */
457     NULL,                               /* pExcludeClipRect */
458     NULL,                               /* pExtDeviceMode */
459     NULL,                               /* pExtEscape */
460     dibdrv_ExtFloodFill,                /* pExtFloodFill */
461     NULL,                               /* pExtSelectClipRgn */
462     dibdrv_ExtTextOut,                  /* pExtTextOut */
463     NULL,                               /* pFillPath */
464     NULL,                               /* pFillRgn */
465     NULL,                               /* pFlattenPath */
466     NULL,                               /* pFontIsLinked */
467     NULL,                               /* pFrameRgn */
468     NULL,                               /* pGdiComment */
469     NULL,                               /* pGdiRealizationInfo */
470     NULL,                               /* pGetBoundsRect */
471     NULL,                               /* pGetCharABCWidths */
472     NULL,                               /* pGetCharABCWidthsI */
473     NULL,                               /* pGetCharWidth */
474     NULL,                               /* pGetDeviceCaps */
475     dibdrv_GetDeviceGammaRamp,          /* pGetDeviceGammaRamp */
476     NULL,                               /* pGetFontData */
477     NULL,                               /* pGetFontUnicodeRanges */
478     NULL,                               /* pGetGlyphIndices */
479     NULL,                               /* pGetGlyphOutline */
480     NULL,                               /* pGetICMProfile */
481     dibdrv_GetImage,                    /* pGetImage */
482     NULL,                               /* pGetKerningPairs */
483     dibdrv_GetNearestColor,             /* pGetNearestColor */
484     NULL,                               /* pGetOutlineTextMetrics */
485     dibdrv_GetPixel,                    /* pGetPixel */
486     NULL,                               /* pGetPixelFormat */
487     NULL,                               /* pGetSystemPaletteEntries */
488     NULL,                               /* pGetTextCharsetInfo */
489     NULL,                               /* pGetTextExtentExPoint */
490     NULL,                               /* pGetTextExtentExPointI */
491     NULL,                               /* pGetTextFace */
492     NULL,                               /* pGetTextMetrics */
493     dibdrv_GradientFill,                /* pGradientFill */
494     NULL,                               /* pIntersectClipRect */
495     NULL,                               /* pInvertRgn */
496     dibdrv_LineTo,                      /* pLineTo */
497     NULL,                               /* pModifyWorldTransform */
498     NULL,                               /* pMoveTo */
499     NULL,                               /* pOffsetClipRgn */
500     NULL,                               /* pOffsetViewportOrg */
501     NULL,                               /* pOffsetWindowOrg */
502     dibdrv_PaintRgn,                    /* pPaintRgn */
503     dibdrv_PatBlt,                      /* pPatBlt */
504     dibdrv_Pie,                         /* pPie */
505     NULL,                               /* pPolyBezier */
506     NULL,                               /* pPolyBezierTo */
507     NULL,                               /* pPolyDraw */
508     dibdrv_PolyPolygon,                 /* pPolyPolygon */
509     dibdrv_PolyPolyline,                /* pPolyPolyline */
510     dibdrv_Polygon,                     /* pPolygon */
511     dibdrv_Polyline,                    /* pPolyline */
512     NULL,                               /* pPolylineTo */
513     dibdrv_PutImage,                    /* pPutImage */
514     NULL,                               /* pRealizeDefaultPalette */
515     NULL,                               /* pRealizePalette */
516     dibdrv_Rectangle,                   /* pRectangle */
517     NULL,                               /* pResetDC */
518     NULL,                               /* pRestoreDC */
519     dibdrv_RoundRect,                   /* pRoundRect */
520     NULL,                               /* pSaveDC */
521     NULL,                               /* pScaleViewportExt */
522     NULL,                               /* pScaleWindowExt */
523     dibdrv_SelectBitmap,                /* pSelectBitmap */
524     dibdrv_SelectBrush,                 /* pSelectBrush */
525     NULL,                               /* pSelectClipPath */
526     NULL,                               /* pSelectFont */
527     NULL,                               /* pSelectPalette */
528     dibdrv_SelectPen,                   /* pSelectPen */
529     NULL,                               /* pSetArcDirection */
530     NULL,                               /* pSetBkColor */
531     NULL,                               /* pSetBkMode */
532     dibdrv_SetBoundsRect,               /* pSetBoundsRect */
533     dibdrv_SetDCBrushColor,             /* pSetDCBrushColor */
534     dibdrv_SetDCPenColor,               /* pSetDCPenColor */
535     NULL,                               /* pSetDIBitsToDevice */
536     dibdrv_SetDeviceClipping,           /* pSetDeviceClipping */
537     dibdrv_SetDeviceGammaRamp,          /* pSetDeviceGammaRamp */
538     NULL,                               /* pSetLayout */
539     NULL,                               /* pSetMapMode */
540     NULL,                               /* pSetMapperFlags */
541     dibdrv_SetPixel,                    /* pSetPixel */
542     NULL,                               /* pSetPixelFormat */
543     NULL,                               /* pSetPolyFillMode */
544     NULL,                               /* pSetROP2 */
545     NULL,                               /* pSetRelAbs */
546     NULL,                               /* pSetStretchBltMode */
547     NULL,                               /* pSetTextAlign */
548     NULL,                               /* pSetTextCharacterExtra */
549     NULL,                               /* pSetTextColor */
550     NULL,                               /* pSetTextJustification */
551     NULL,                               /* pSetViewportExt */
552     NULL,                               /* pSetViewportOrg */
553     NULL,                               /* pSetWindowExt */
554     NULL,                               /* pSetWindowOrg */
555     NULL,                               /* pSetWorldTransform */
556     NULL,                               /* pStartDoc */
557     NULL,                               /* pStartPage */
558     dibdrv_StretchBlt,                  /* pStretchBlt */
559     NULL,                               /* pStretchDIBits */
560     NULL,                               /* pStrokeAndFillPath */
561     NULL,                               /* pStrokePath */
562     NULL,                               /* pSwapBuffers */
563     NULL,                               /* pUnrealizePalette */
564     NULL,                               /* pWidenPath */
565     NULL,                               /* pwglCopyContext */
566     NULL,                               /* pwglCreateContext */
567     NULL,                               /* pwglCreateContextAttribsARB */
568     NULL,                               /* pwglDeleteContext */
569     NULL,                               /* pwglGetProcAddress */
570     NULL,                               /* pwglMakeContextCurrentARB */
571     NULL,                               /* pwglMakeCurrent */
572     NULL,                               /* pwglSetPixelFormatWINE */
573     NULL,                               /* pwglShareLists */
574     NULL,                               /* pwglUseFontBitmapsA */
575     NULL,                               /* pwglUseFontBitmapsW */
576     GDI_PRIORITY_DIB_DRV                /* priority */
577 };