2 * DIB driver initialization and DC functions.
4 * Copyright 2011 Huw Davies
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.
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.
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
23 #include "gdi_private.h"
26 #include "wine/exception.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(dib);
31 static const DWORD bit_fields_888[3] = {0xff0000, 0x00ff00, 0x0000ff};
32 static const DWORD bit_fields_555[3] = {0x7c00, 0x03e0, 0x001f};
34 static void calc_shift_and_len(DWORD mask, int *shift, int *len)
45 while ((mask & 1) == 0)
51 while ((mask & 1) == 1)
60 static void init_bit_fields(dib_info *dib, const DWORD *bit_fields)
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);
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)
73 dib->bit_count = bi->biBitCount;
74 dib->width = bi->biWidth;
75 dib->height = bi->biHeight;
76 dib->compression = bi->biCompression;
77 dib->stride = get_dib_stride( dib->width, dib->bit_count );
79 dib->bits.is_copy = FALSE;
80 dib->bits.free = NULL;
81 dib->bits.param = NULL;
84 if(dib->height < 0) /* top-down */
86 dib->height = -dib->height;
90 /* bits always points to the top-left corner and the stride is -ve */
91 dib->bits.ptr = (BYTE*)dib->bits.ptr + (dib->height - 1) * dib->stride;
92 dib->stride = -dib->stride;
95 dib->funcs = &funcs_null;
97 switch(dib->bit_count)
100 if(bi->biCompression == BI_RGB)
101 bit_fields = bit_fields_888;
103 init_bit_fields(dib, bit_fields);
105 if(dib->red_mask == 0xff0000 && dib->green_mask == 0x00ff00 && dib->blue_mask == 0x0000ff)
106 dib->funcs = &funcs_8888;
108 dib->funcs = &funcs_32;
112 dib->funcs = &funcs_24;
116 if(bi->biCompression == BI_RGB)
117 bit_fields = bit_fields_555;
119 init_bit_fields(dib, bit_fields);
121 if(dib->red_mask == 0x7c00 && dib->green_mask == 0x03e0 && dib->blue_mask == 0x001f)
122 dib->funcs = &funcs_555;
124 dib->funcs = &funcs_16;
128 dib->funcs = &funcs_8;
132 dib->funcs = &funcs_4;
136 dib->funcs = &funcs_1;
140 TRACE("bpp %d not supported, will forward to graphics driver.\n", dib->bit_count);
146 if (flags & private_color_table)
148 dib->color_table = HeapAlloc(GetProcessHeap(), 0, color_table_size * sizeof(dib->color_table[0]));
149 if(!dib->color_table) return FALSE;
150 memcpy(dib->color_table, color_table, color_table_size * sizeof(color_table[0]));
153 dib->color_table = color_table;
154 dib->color_table_size = color_table_size;
158 dib->color_table = NULL;
159 dib->color_table_size = 0;
165 BOOL init_dib_info_from_brush(dib_info *dib, const BITMAPINFO *bi, void *bits, UINT usage, HPALETTE palette)
167 DWORD *masks = (bi->bmiHeader.biCompression == BI_BITFIELDS) ? (DWORD *)bi->bmiColors : NULL;
168 RGBQUAD *color_table = NULL, pal_table[256];
169 int num_colors = get_dib_num_of_colors( bi );
173 if(usage == DIB_PAL_COLORS)
175 PALETTEENTRY entries[256];
176 const WORD *index = (const WORD *)bi->bmiColors;
177 UINT i, count = GetPaletteEntries( palette, 0, num_colors, entries );
178 for (i = 0; i < num_colors; i++, index++)
180 PALETTEENTRY *entry = &entries[*index % count];
181 pal_table[i].rgbRed = entry->peRed;
182 pal_table[i].rgbGreen = entry->peGreen;
183 pal_table[i].rgbBlue = entry->peBlue;
184 pal_table[i].rgbReserved = 0;
186 color_table = pal_table;
188 else color_table = (RGBQUAD *)bi->bmiColors;
190 return init_dib_info(dib, &bi->bmiHeader, masks, color_table, num_colors, bits, private_color_table);
193 BOOL init_dib_info_from_bitmapinfo(dib_info *dib, const BITMAPINFO *info, void *bits, enum dib_info_flags flags)
195 unsigned int colors = get_dib_num_of_colors( info );
196 void *colorptr = (char *)&info->bmiHeader + info->bmiHeader.biSize;
197 const DWORD *bitfields = (info->bmiHeader.biCompression == BI_BITFIELDS) ? (DWORD *)colorptr : NULL;
199 return init_dib_info( dib, &info->bmiHeader, bitfields, colors ? colorptr : NULL, colors, bits, flags );
202 BOOL init_dib_info_from_bitmapobj(dib_info *dib, BITMAPOBJ *bmp, enum dib_info_flags flags)
206 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
207 BITMAPINFO *info = (BITMAPINFO *)buffer;
209 get_ddb_bitmapinfo( bmp, info );
210 if (!bmp->bitmap.bmBits)
212 int width_bytes = get_dib_stride( bmp->bitmap.bmWidth, bmp->bitmap.bmBitsPixel );
213 bmp->bitmap.bmBits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
214 bmp->bitmap.bmHeight * width_bytes );
215 if (!bmp->bitmap.bmBits) return FALSE;
217 return init_dib_info_from_bitmapinfo( dib, info, bmp->bitmap.bmBits,
218 flags | private_color_table );
220 return init_dib_info( dib, &bmp->dib->dsBmih, bmp->dib->dsBitfields,
221 bmp->color_table, bmp->nb_colors, bmp->dib->dsBm.bmBits, flags );
224 static void clear_dib_info(dib_info *dib)
226 dib->color_table = NULL;
227 dib->bits.ptr = NULL;
228 dib->bits.free = NULL;
229 dib->bits.param = NULL;
232 /**********************************************************************
235 * Free the resources associated with a dib and optionally the bits
237 void free_dib_info(dib_info *dib)
239 if (dib->flags & private_color_table)
240 HeapFree(GetProcessHeap(), 0, dib->color_table);
242 if (dib->bits.free) dib->bits.free( &dib->bits );
243 clear_dib_info( dib );
246 void copy_dib_color_info(dib_info *dst, const dib_info *src)
248 dst->bit_count = src->bit_count;
249 dst->red_mask = src->red_mask;
250 dst->green_mask = src->green_mask;
251 dst->blue_mask = src->blue_mask;
252 dst->red_len = src->red_len;
253 dst->green_len = src->green_len;
254 dst->blue_len = src->blue_len;
255 dst->red_shift = src->red_shift;
256 dst->green_shift = src->green_shift;
257 dst->blue_shift = src->blue_shift;
258 dst->funcs = src->funcs;
259 dst->color_table_size = src->color_table_size;
260 dst->color_table = NULL;
261 dst->flags = src->flags;
262 if(dst->color_table_size)
264 int size = dst->color_table_size * sizeof(dst->color_table[0]);
265 if (dst->flags & private_color_table)
267 dst->color_table = HeapAlloc(GetProcessHeap(), 0, size);
268 memcpy(dst->color_table, src->color_table, size);
271 dst->color_table = src->color_table;
275 DWORD convert_bitmapinfo( const BITMAPINFO *src_info, void *src_bits, struct bitblt_coords *src,
276 const BITMAPINFO *dst_info, void *dst_bits, BOOL add_alpha )
278 dib_info src_dib, dst_dib;
281 if ( !init_dib_info_from_bitmapinfo( &src_dib, src_info, src_bits, 0 ) )
282 return ERROR_BAD_FORMAT;
283 if ( !init_dib_info_from_bitmapinfo( &dst_dib, dst_info, dst_bits, 0 ) )
284 return ERROR_BAD_FORMAT;
288 dst_dib.funcs->convert_to( &dst_dib, &src_dib, &src->visrect );
293 WARN( "invalid bits pointer %p\n", src_bits );
298 /* We shared the color tables, so there's no need to free the dib_infos here */
299 if(!ret) return ERROR_BAD_FORMAT;
301 /* update coordinates, the destination rectangle is always stored at 0,0 */
302 src->x -= src->visrect.left;
303 src->y -= src->visrect.top;
304 offset_rect( &src->visrect, -src->visrect.left, -src->visrect.top );
306 if (add_alpha && dst_dib.funcs == &funcs_8888 && src_dib.funcs != &funcs_8888)
308 DWORD *pixel = dst_dib.bits.ptr;
311 for (y = src->visrect.top; y < src->visrect.bottom; y++, pixel += dst_dib.stride / 4)
312 for (x = src->visrect.left; x < src->visrect.right; x++)
313 pixel[x] |= 0xff000000;
316 return ERROR_SUCCESS;
319 static void update_fg_colors( dibdrv_physdev *pdev )
321 pdev->pen_color = get_pixel_color( pdev, pdev->pen_colorref, TRUE );
322 pdev->brush_color = get_pixel_color( pdev, pdev->brush_colorref, TRUE );
323 pdev->text_color = get_pixel_color( pdev, GetTextColor( pdev->dev.hdc ), TRUE );
326 static void update_masks( dibdrv_physdev *pdev, INT rop )
328 calc_and_xor_masks( rop, pdev->pen_color, &pdev->pen_and, &pdev->pen_xor );
329 update_brush_rop( pdev, rop );
330 if( GetBkMode( pdev->dev.hdc ) == OPAQUE )
331 calc_and_xor_masks( rop, pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
334 /***********************************************************************
335 * add_extra_clipping_region
337 * Temporarily add a region to the current clipping region.
338 * The returned region must be restored with restore_clipping_region.
340 HRGN add_extra_clipping_region( dibdrv_physdev *pdev, HRGN rgn )
344 if (!(clip = CreateRectRgn( 0, 0, 0, 0 ))) return 0;
345 CombineRgn( clip, pdev->clip, rgn, RGN_AND );
351 /***********************************************************************
352 * restore_clipping_region
354 void restore_clipping_region( dibdrv_physdev *pdev, HRGN rgn )
357 DeleteObject( pdev->clip );
361 /**********************************************************************
364 static BOOL dibdrv_CreateDC( PHYSDEV *dev, LPCWSTR driver, LPCWSTR device,
365 LPCWSTR output, const DEVMODEW *data )
367 dibdrv_physdev *pdev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pdev) );
369 if (!pdev) return FALSE;
370 if (!(pdev->clip = CreateRectRgn(0, 0, 0, 0)))
372 HeapFree( GetProcessHeap(), 0, pdev );
375 clear_dib_info(&pdev->dib);
376 clear_dib_info(&pdev->brush_dib);
377 push_dc_driver( dev, &pdev->dev, &dib_driver );
381 /***********************************************************************
384 static BOOL dibdrv_DeleteDC( PHYSDEV dev )
386 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
387 TRACE("(%p)\n", dev);
388 DeleteObject(pdev->clip);
389 free_pattern_brush(pdev);
390 free_dib_info(&pdev->dib);
391 HeapFree( GetProcessHeap(), 0, pdev );
395 /***********************************************************************
398 static BOOL dibdrv_CopyBitmap( HBITMAP src, HBITMAP dst )
400 return nulldrv_CopyBitmap( src, dst );
403 /***********************************************************************
404 * dibdrv_DeleteBitmap
406 static BOOL dibdrv_DeleteBitmap( HBITMAP bitmap )
411 /***********************************************************************
412 * dibdrv_SelectBitmap
414 static HBITMAP dibdrv_SelectBitmap( PHYSDEV dev, HBITMAP bitmap )
416 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectBitmap );
417 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
418 BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
419 TRACE("(%p, %p)\n", dev, bitmap);
423 free_dib_info(&pdev->dib);
425 if(!init_dib_info_from_bitmapobj(&pdev->dib, bmp, private_color_table))
426 pdev->defer |= DEFER_FORMAT;
428 GDI_ReleaseObj( bitmap );
430 return next->funcs->pSelectBitmap( next, bitmap );
433 /***********************************************************************
436 static COLORREF dibdrv_SetBkColor( PHYSDEV dev, COLORREF color )
438 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetBkColor );
439 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
441 pdev->bkgnd_color = get_pixel_color( pdev, color, FALSE );
443 if( GetBkMode(dev->hdc) == OPAQUE )
444 calc_and_xor_masks( GetROP2(dev->hdc), pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
447 pdev->bkgnd_and = ~0u;
451 update_fg_colors( pdev ); /* Only needed in the 1 bpp case */
453 return next->funcs->pSetBkColor( next, color );
456 /***********************************************************************
459 static INT dibdrv_SetBkMode( PHYSDEV dev, INT mode )
461 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetBkMode );
462 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
465 calc_and_xor_masks( GetROP2(dev->hdc), pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
468 pdev->bkgnd_and = ~0u;
472 return next->funcs->pSetBkMode( next, mode );
475 /***********************************************************************
476 * dibdrv_SetDeviceClipping
478 static void dibdrv_SetDeviceClipping( PHYSDEV dev, HRGN vis_rgn, HRGN clip_rgn )
480 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDeviceClipping );
481 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
482 TRACE("(%p, %p, %p)\n", dev, vis_rgn, clip_rgn);
484 CombineRgn( pdev->clip, vis_rgn, clip_rgn, clip_rgn ? RGN_AND : RGN_COPY );
485 return next->funcs->pSetDeviceClipping( next, vis_rgn, clip_rgn);
488 /***********************************************************************
489 * dibdrv_SetDIBColorTable
491 static UINT dibdrv_SetDIBColorTable( PHYSDEV dev, UINT pos, UINT count, const RGBQUAD *colors )
493 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDIBColorTable );
494 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
495 TRACE("(%p, %d, %d, %p)\n", dev, pos, count, colors);
497 if( pdev->dib.color_table && pos < pdev->dib.color_table_size )
499 if( pos + count > pdev->dib.color_table_size ) count = pdev->dib.color_table_size - pos;
500 memcpy( pdev->dib.color_table + pos, colors, count * sizeof(RGBQUAD) );
502 pdev->bkgnd_color = get_pixel_color( pdev, GetBkColor( dev->hdc ), FALSE );
503 update_fg_colors( pdev );
505 update_masks( pdev, GetROP2( dev->hdc ) );
507 return next->funcs->pSetDIBColorTable( next, pos, count, colors );
510 /***********************************************************************
513 static INT dibdrv_SetROP2( PHYSDEV dev, INT rop )
515 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetROP2 );
516 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
518 update_masks( pdev, rop );
520 return next->funcs->pSetROP2( next, rop );
523 /***********************************************************************
524 * dibdrv_SetTextColor
526 static COLORREF dibdrv_SetTextColor( PHYSDEV dev, COLORREF color )
528 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetTextColor );
529 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
531 pdev->text_color = get_pixel_color( pdev, color, TRUE );
532 update_aa_ranges( pdev );
534 return next->funcs->pSetTextColor( next, color );
537 const struct gdi_dc_funcs dib_driver =
539 NULL, /* pAbortDoc */
540 NULL, /* pAbortPath */
541 dibdrv_AlphaBlend, /* pAlphaBlend */
542 NULL, /* pAngleArc */
545 NULL, /* pBeginPath */
546 dibdrv_BlendImage, /* pBlendImage */
547 NULL, /* pChoosePixelFormat */
549 NULL, /* pCloseFigure */
550 dibdrv_CopyBitmap, /* pCopyBitmap */
551 NULL, /* pCreateBitmap */
552 NULL, /* pCreateCompatibleDC */
553 dibdrv_CreateDC, /* pCreateDC */
554 NULL, /* pCreateDIBSection */
555 dibdrv_DeleteBitmap, /* pDeleteBitmap */
556 dibdrv_DeleteDC, /* pDeleteDC */
557 NULL, /* pDeleteObject */
558 NULL, /* pDescribePixelFormat */
559 NULL, /* pDeviceCapabilities */
564 NULL, /* pEnumFonts */
565 NULL, /* pEnumICMProfiles */
566 NULL, /* pExcludeClipRect */
567 NULL, /* pExtDeviceMode */
568 NULL, /* pExtEscape */
569 NULL, /* pExtFloodFill */
570 NULL, /* pExtSelectClipRgn */
571 dibdrv_ExtTextOut, /* pExtTextOut */
572 NULL, /* pFillPath */
574 NULL, /* pFlattenPath */
575 NULL, /* pFontIsLinked */
576 NULL, /* pFrameRgn */
577 NULL, /* pGdiComment */
578 NULL, /* pGdiRealizationInfo */
579 NULL, /* pGetCharABCWidths */
580 NULL, /* pGetCharABCWidthsI */
581 NULL, /* pGetCharWidth */
582 NULL, /* pGetDeviceCaps */
583 NULL, /* pGetDeviceGammaRamp */
584 NULL, /* pGetFontData */
585 NULL, /* pGetFontUnicodeRanges */
586 NULL, /* pGetGlyphIndices */
587 NULL, /* pGetGlyphOutline */
588 NULL, /* pGetICMProfile */
589 dibdrv_GetImage, /* pGetImage */
590 NULL, /* pGetKerningPairs */
591 NULL, /* pGetNearestColor */
592 NULL, /* pGetOutlineTextMetrics */
593 dibdrv_GetPixel, /* pGetPixel */
594 NULL, /* pGetPixelFormat */
595 NULL, /* pGetSystemPaletteEntries */
596 NULL, /* pGetTextCharsetInfo */
597 NULL, /* pGetTextExtentExPoint */
598 NULL, /* pGetTextExtentExPointI */
599 NULL, /* pGetTextFace */
600 NULL, /* pGetTextMetrics */
601 dibdrv_GradientFill, /* pGradientFill */
602 NULL, /* pIntersectClipRect */
603 NULL, /* pInvertRgn */
604 dibdrv_LineTo, /* pLineTo */
605 NULL, /* pModifyWorldTransform */
607 NULL, /* pOffsetClipRgn */
608 NULL, /* pOffsetViewportOrg */
609 NULL, /* pOffsetWindowOrg */
610 dibdrv_PaintRgn, /* pPaintRgn */
611 dibdrv_PatBlt, /* pPatBlt */
613 NULL, /* pPolyBezier */
614 NULL, /* pPolyBezierTo */
615 NULL, /* pPolyDraw */
616 NULL, /* pPolyPolygon */
617 dibdrv_PolyPolyline, /* pPolyPolyline */
619 dibdrv_Polyline, /* pPolyline */
620 NULL, /* pPolylineTo */
621 dibdrv_PutImage, /* pPutImage */
622 NULL, /* pRealizeDefaultPalette */
623 NULL, /* pRealizePalette */
624 dibdrv_Rectangle, /* pRectangle */
626 NULL, /* pRestoreDC */
627 NULL, /* pRoundRect */
629 NULL, /* pScaleViewportExt */
630 NULL, /* pScaleWindowExt */
631 dibdrv_SelectBitmap, /* pSelectBitmap */
632 dibdrv_SelectBrush, /* pSelectBrush */
633 NULL, /* pSelectClipPath */
634 NULL, /* pSelectFont */
635 NULL, /* pSelectPalette */
636 dibdrv_SelectPen, /* pSelectPen */
637 NULL, /* pSetArcDirection */
638 dibdrv_SetBkColor, /* pSetBkColor */
639 dibdrv_SetBkMode, /* pSetBkMode */
640 dibdrv_SetDCBrushColor, /* pSetDCBrushColor */
641 dibdrv_SetDCPenColor, /* pSetDCPenColor */
642 dibdrv_SetDIBColorTable, /* pSetDIBColorTable */
643 NULL, /* pSetDIBitsToDevice */
644 dibdrv_SetDeviceClipping, /* pSetDeviceClipping */
645 NULL, /* pSetDeviceGammaRamp */
646 NULL, /* pSetLayout */
647 NULL, /* pSetMapMode */
648 NULL, /* pSetMapperFlags */
649 dibdrv_SetPixel, /* pSetPixel */
650 NULL, /* pSetPixelFormat */
651 NULL, /* pSetPolyFillMode */
652 dibdrv_SetROP2, /* pSetROP2 */
653 NULL, /* pSetRelAbs */
654 NULL, /* pSetStretchBltMode */
655 NULL, /* pSetTextAlign */
656 NULL, /* pSetTextCharacterExtra */
657 dibdrv_SetTextColor, /* pSetTextColor */
658 NULL, /* pSetTextJustification */
659 NULL, /* pSetViewportExt */
660 NULL, /* pSetViewportOrg */
661 NULL, /* pSetWindowExt */
662 NULL, /* pSetWindowOrg */
663 NULL, /* pSetWorldTransform */
664 NULL, /* pStartDoc */
665 NULL, /* pStartPage */
666 dibdrv_StretchBlt, /* pStretchBlt */
667 NULL, /* pStretchDIBits */
668 NULL, /* pStrokeAndFillPath */
669 NULL, /* pStrokePath */
670 NULL, /* pSwapBuffers */
671 NULL, /* pUnrealizePalette */
672 NULL, /* pWidenPath */
673 NULL, /* pwglCopyContext */
674 NULL, /* pwglCreateContext */
675 NULL, /* pwglCreateContextAttribsARB */
676 NULL, /* pwglDeleteContext */
677 NULL, /* pwglGetPbufferDCARB */
678 NULL, /* pwglGetProcAddress */
679 NULL, /* pwglMakeContextCurrentARB */
680 NULL, /* pwglMakeCurrent */
681 NULL, /* pwglSetPixelFormatWINE */
682 NULL, /* pwglShareLists */
683 NULL, /* pwglUseFontBitmapsA */
684 NULL /* pwglUseFontBitmapsW */