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
->stride
= get_dib_stride( dib
->width
, dib
->bit_count
);
78 dib
->bits
.is_copy
= FALSE
;
79 dib
->bits
.free
= NULL
;
80 dib
->bits
.param
= NULL
;
83 if(dib
->height
< 0) /* top-down */
85 dib
->height
= -dib
->height
;
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
;
94 dib
->funcs
= &funcs_null
;
96 switch(dib
->bit_count
)
99 if(bi
->biCompression
== BI_RGB
)
100 bit_fields
= bit_fields_888
;
102 init_bit_fields(dib
, bit_fields
);
104 if(dib
->red_mask
== 0xff0000 && dib
->green_mask
== 0x00ff00 && dib
->blue_mask
== 0x0000ff)
105 dib
->funcs
= &funcs_8888
;
107 dib
->funcs
= &funcs_32
;
111 dib
->funcs
= &funcs_24
;
115 if(bi
->biCompression
== BI_RGB
)
116 bit_fields
= bit_fields_555
;
118 init_bit_fields(dib
, bit_fields
);
120 if(dib
->red_mask
== 0x7c00 && dib
->green_mask
== 0x03e0 && dib
->blue_mask
== 0x001f)
121 dib
->funcs
= &funcs_555
;
123 dib
->funcs
= &funcs_16
;
127 dib
->funcs
= &funcs_8
;
131 dib
->funcs
= &funcs_4
;
135 dib
->funcs
= &funcs_1
;
139 TRACE("bpp %d not supported, will forward to graphics driver.\n", dib
->bit_count
);
145 if (flags
& private_color_table
)
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]));
152 dib
->color_table
= color_table
;
153 dib
->color_table_size
= color_table_size
;
157 dib
->color_table
= NULL
;
158 dib
->color_table_size
= 0;
164 BOOL
init_dib_info_from_packed(dib_info
*dib
, const BITMAPINFOHEADER
*bi
, WORD usage
, HPALETTE palette
)
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
);
171 if(bi
->biCompression
== BI_BITFIELDS
)
173 masks
= (DWORD
*)ptr
;
174 ptr
+= 3 * sizeof(DWORD
);
179 if(usage
== DIB_PAL_COLORS
)
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
++)
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;
192 color_table
= pal_table
;
193 ptr
+= num_colors
* sizeof(WORD
);
197 color_table
= (RGBQUAD
*)ptr
;
198 ptr
+= num_colors
* sizeof(*color_table
);
202 return init_dib_info(dib
, bi
, masks
, color_table
, num_colors
, ptr
, private_color_table
);
205 BOOL
init_dib_info_from_bitmapinfo(dib_info
*dib
, const BITMAPINFO
*info
, void *bits
, enum dib_info_flags flags
)
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
;
211 return init_dib_info( dib
, &info
->bmiHeader
, bitfields
, colors
? colorptr
: NULL
, colors
, bits
, flags
);
214 BOOL
init_dib_info_from_bitmapobj(dib_info
*dib
, BITMAPOBJ
*bmp
, enum dib_info_flags flags
)
218 char buffer
[FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] )];
219 BITMAPINFO
*info
= (BITMAPINFO
*)buffer
;
221 get_ddb_bitmapinfo( bmp
, info
);
222 return init_dib_info_from_bitmapinfo( dib
, info
, bmp
->bitmap
.bmBits
,
223 flags
| private_color_table
);
225 return init_dib_info( dib
, &bmp
->dib
->dsBmih
, bmp
->dib
->dsBitfields
,
226 bmp
->color_table
, bmp
->nb_colors
, bmp
->dib
->dsBm
.bmBits
, flags
);
229 static void clear_dib_info(dib_info
*dib
)
231 dib
->color_table
= NULL
;
232 dib
->bits
.ptr
= NULL
;
233 dib
->bits
.free
= NULL
;
234 dib
->bits
.param
= NULL
;
237 /**********************************************************************
240 * Free the resources associated with a dib and optionally the bits
242 void free_dib_info(dib_info
*dib
)
244 if (dib
->flags
& private_color_table
)
245 HeapFree(GetProcessHeap(), 0, dib
->color_table
);
247 if (dib
->bits
.free
) dib
->bits
.free( &dib
->bits
);
248 clear_dib_info( dib
);
251 void copy_dib_color_info(dib_info
*dst
, const dib_info
*src
)
253 dst
->bit_count
= src
->bit_count
;
254 dst
->red_mask
= src
->red_mask
;
255 dst
->green_mask
= src
->green_mask
;
256 dst
->blue_mask
= src
->blue_mask
;
257 dst
->red_len
= src
->red_len
;
258 dst
->green_len
= src
->green_len
;
259 dst
->blue_len
= src
->blue_len
;
260 dst
->red_shift
= src
->red_shift
;
261 dst
->green_shift
= src
->green_shift
;
262 dst
->blue_shift
= src
->blue_shift
;
263 dst
->funcs
= src
->funcs
;
264 dst
->color_table_size
= src
->color_table_size
;
265 dst
->color_table
= NULL
;
266 dst
->flags
= src
->flags
;
267 if(dst
->color_table_size
)
269 int size
= dst
->color_table_size
* sizeof(dst
->color_table
[0]);
270 if (dst
->flags
& private_color_table
)
272 dst
->color_table
= HeapAlloc(GetProcessHeap(), 0, size
);
273 memcpy(dst
->color_table
, src
->color_table
, size
);
276 dst
->color_table
= src
->color_table
;
280 DWORD
convert_bitmapinfo( const BITMAPINFO
*src_info
, void *src_bits
, struct bitblt_coords
*src
,
281 const BITMAPINFO
*dst_info
, void *dst_bits
, BOOL add_alpha
)
283 dib_info src_dib
, dst_dib
;
286 if ( !init_dib_info_from_bitmapinfo( &src_dib
, src_info
, src_bits
, 0 ) )
287 return ERROR_BAD_FORMAT
;
288 if ( !init_dib_info_from_bitmapinfo( &dst_dib
, dst_info
, dst_bits
, 0 ) )
289 return ERROR_BAD_FORMAT
;
293 dst_dib
.funcs
->convert_to( &dst_dib
, &src_dib
, &src
->visrect
);
298 WARN( "invalid bits pointer %p\n", src_bits
);
303 /* We shared the color tables, so there's no need to free the dib_infos here */
304 if(!ret
) return ERROR_BAD_FORMAT
;
306 /* update coordinates, the destination rectangle is always stored at 0,0 */
307 src
->x
-= src
->visrect
.left
;
308 src
->y
-= src
->visrect
.top
;
309 offset_rect( &src
->visrect
, -src
->visrect
.left
, -src
->visrect
.top
);
311 if (add_alpha
&& dst_dib
.funcs
== &funcs_8888
&& src_dib
.funcs
!= &funcs_8888
)
313 DWORD
*pixel
= dst_dib
.bits
.ptr
;
316 for (y
= src
->visrect
.top
; y
< src
->visrect
.bottom
; y
++, pixel
+= dst_dib
.stride
/ 4)
317 for (x
= src
->visrect
.left
; x
< src
->visrect
.right
; x
++)
318 pixel
[x
] |= 0xff000000;
321 return ERROR_SUCCESS
;
324 static void update_fg_colors( dibdrv_physdev
*pdev
)
326 pdev
->pen_color
= get_pixel_color( pdev
, pdev
->pen_colorref
, TRUE
);
327 pdev
->brush_color
= get_pixel_color( pdev
, pdev
->brush_colorref
, TRUE
);
330 static void update_masks( dibdrv_physdev
*pdev
, INT rop
)
332 calc_and_xor_masks( rop
, pdev
->pen_color
, &pdev
->pen_and
, &pdev
->pen_xor
);
333 update_brush_rop( pdev
, rop
);
334 if( GetBkMode( pdev
->dev
.hdc
) == OPAQUE
)
335 calc_and_xor_masks( rop
, pdev
->bkgnd_color
, &pdev
->bkgnd_and
, &pdev
->bkgnd_xor
);
338 /***********************************************************************
339 * add_extra_clipping_region
341 * Temporarily add a region to the current clipping region.
342 * The returned region must be restored with restore_clipping_region.
344 HRGN
add_extra_clipping_region( dibdrv_physdev
*pdev
, HRGN rgn
)
348 if (!(clip
= CreateRectRgn( 0, 0, 0, 0 ))) return 0;
349 CombineRgn( clip
, pdev
->clip
, rgn
, RGN_AND
);
355 /***********************************************************************
356 * restore_clipping_region
358 void restore_clipping_region( dibdrv_physdev
*pdev
, HRGN rgn
)
361 DeleteObject( pdev
->clip
);
365 /**********************************************************************
368 static BOOL
dibdrv_CreateDC( PHYSDEV
*dev
, LPCWSTR driver
, LPCWSTR device
,
369 LPCWSTR output
, const DEVMODEW
*data
)
371 dibdrv_physdev
*pdev
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*pdev
) );
373 if (!pdev
) return FALSE
;
374 if (!(pdev
->clip
= CreateRectRgn(0, 0, 0, 0)))
376 HeapFree( GetProcessHeap(), 0, pdev
);
379 clear_dib_info(&pdev
->dib
);
380 clear_dib_info(&pdev
->brush_dib
);
381 push_dc_driver( dev
, &pdev
->dev
, &dib_driver
);
385 /***********************************************************************
388 static BOOL
dibdrv_DeleteDC( PHYSDEV dev
)
390 dibdrv_physdev
*pdev
= get_dibdrv_pdev(dev
);
391 TRACE("(%p)\n", dev
);
392 DeleteObject(pdev
->clip
);
393 free_pattern_brush(pdev
);
394 free_dib_info(&pdev
->dib
);
395 HeapFree( GetProcessHeap(), 0, pdev
);
399 /***********************************************************************
400 * dibdrv_SelectBitmap
402 static HBITMAP
dibdrv_SelectBitmap( PHYSDEV dev
, HBITMAP bitmap
)
404 PHYSDEV next
= GET_NEXT_PHYSDEV( dev
, pSelectBitmap
);
405 dibdrv_physdev
*pdev
= get_dibdrv_pdev(dev
);
406 BITMAPOBJ
*bmp
= GDI_GetObjPtr( bitmap
, OBJ_BITMAP
);
407 TRACE("(%p, %p)\n", dev
, bitmap
);
412 free_dib_info(&pdev
->dib
);
414 if(!init_dib_info_from_bitmapobj(&pdev
->dib
, bmp
, private_color_table
))
415 pdev
->defer
|= DEFER_FORMAT
;
417 GDI_ReleaseObj( bitmap
);
419 return next
->funcs
->pSelectBitmap( next
, bitmap
);
422 /***********************************************************************
425 static COLORREF
dibdrv_SetBkColor( PHYSDEV dev
, COLORREF color
)
427 PHYSDEV next
= GET_NEXT_PHYSDEV( dev
, pSetBkColor
);
428 dibdrv_physdev
*pdev
= get_dibdrv_pdev(dev
);
430 pdev
->bkgnd_color
= get_pixel_color( pdev
, color
, FALSE
);
432 if( GetBkMode(dev
->hdc
) == OPAQUE
)
433 calc_and_xor_masks( GetROP2(dev
->hdc
), pdev
->bkgnd_color
, &pdev
->bkgnd_and
, &pdev
->bkgnd_xor
);
436 pdev
->bkgnd_and
= ~0u;
440 update_fg_colors( pdev
); /* Only needed in the 1 bpp case */
442 return next
->funcs
->pSetBkColor( next
, color
);
445 /***********************************************************************
448 static INT
dibdrv_SetBkMode( PHYSDEV dev
, INT mode
)
450 PHYSDEV next
= GET_NEXT_PHYSDEV( dev
, pSetBkMode
);
451 dibdrv_physdev
*pdev
= get_dibdrv_pdev(dev
);
454 calc_and_xor_masks( GetROP2(dev
->hdc
), pdev
->bkgnd_color
, &pdev
->bkgnd_and
, &pdev
->bkgnd_xor
);
457 pdev
->bkgnd_and
= ~0u;
461 return next
->funcs
->pSetBkMode( next
, mode
);
464 /***********************************************************************
465 * dibdrv_SetDeviceClipping
467 static void dibdrv_SetDeviceClipping( PHYSDEV dev
, HRGN vis_rgn
, HRGN clip_rgn
)
469 PHYSDEV next
= GET_NEXT_PHYSDEV( dev
, pSetDeviceClipping
);
470 dibdrv_physdev
*pdev
= get_dibdrv_pdev(dev
);
471 TRACE("(%p, %p, %p)\n", dev
, vis_rgn
, clip_rgn
);
473 CombineRgn( pdev
->clip
, vis_rgn
, clip_rgn
, clip_rgn
? RGN_AND
: RGN_COPY
);
474 return next
->funcs
->pSetDeviceClipping( next
, vis_rgn
, clip_rgn
);
477 /***********************************************************************
478 * dibdrv_SetDIBColorTable
480 static UINT
dibdrv_SetDIBColorTable( PHYSDEV dev
, UINT pos
, UINT count
, const RGBQUAD
*colors
)
482 PHYSDEV next
= GET_NEXT_PHYSDEV( dev
, pSetDIBColorTable
);
483 dibdrv_physdev
*pdev
= get_dibdrv_pdev(dev
);
484 TRACE("(%p, %d, %d, %p)\n", dev
, pos
, count
, colors
);
486 if( pdev
->dib
.color_table
&& pos
< pdev
->dib
.color_table_size
)
488 if( pos
+ count
> pdev
->dib
.color_table_size
) count
= pdev
->dib
.color_table_size
- pos
;
489 memcpy( pdev
->dib
.color_table
+ pos
, colors
, count
* sizeof(RGBQUAD
) );
491 pdev
->bkgnd_color
= get_pixel_color( pdev
, GetBkColor( dev
->hdc
), FALSE
);
492 update_fg_colors( pdev
);
494 update_masks( pdev
, GetROP2( dev
->hdc
) );
496 return next
->funcs
->pSetDIBColorTable( next
, pos
, count
, colors
);
499 /***********************************************************************
502 static INT
dibdrv_SetROP2( PHYSDEV dev
, INT rop
)
504 PHYSDEV next
= GET_NEXT_PHYSDEV( dev
, pSetROP2
);
505 dibdrv_physdev
*pdev
= get_dibdrv_pdev(dev
);
507 update_masks( pdev
, rop
);
509 return next
->funcs
->pSetROP2( next
, rop
);
512 const struct gdi_dc_funcs dib_driver
=
514 NULL
, /* pAbortDoc */
515 NULL
, /* pAbortPath */
516 dibdrv_AlphaBlend
, /* pAlphaBlend */
517 NULL
, /* pAngleArc */
520 NULL
, /* pBeginPath */
521 dibdrv_BlendImage
, /* pBlendImage */
522 NULL
, /* pChoosePixelFormat */
524 NULL
, /* pCloseFigure */
525 NULL
, /* pCreateBitmap */
526 NULL
, /* pCreateCompatibleDC */
527 dibdrv_CreateDC
, /* pCreateDC */
528 NULL
, /* pCreateDIBSection */
529 NULL
, /* pDeleteBitmap */
530 dibdrv_DeleteDC
, /* pDeleteDC */
531 NULL
, /* pDeleteObject */
532 NULL
, /* pDescribePixelFormat */
533 NULL
, /* pDeviceCapabilities */
538 NULL
, /* pEnumDeviceFonts */
539 NULL
, /* pEnumICMProfiles */
540 NULL
, /* pExcludeClipRect */
541 NULL
, /* pExtDeviceMode */
542 NULL
, /* pExtEscape */
543 NULL
, /* pExtFloodFill */
544 NULL
, /* pExtSelectClipRgn */
545 NULL
, /* pExtTextOut */
546 NULL
, /* pFillPath */
548 NULL
, /* pFlattenPath */
549 NULL
, /* pFrameRgn */
550 NULL
, /* pGdiComment */
551 NULL
, /* pGetCharWidth */
552 NULL
, /* pGetDeviceCaps */
553 NULL
, /* pGetDeviceGammaRamp */
554 NULL
, /* pGetICMProfile */
555 dibdrv_GetImage
, /* pGetImage */
556 NULL
, /* pGetNearestColor */
557 NULL
, /* pGetPixel */
558 NULL
, /* pGetPixelFormat */
559 NULL
, /* pGetSystemPaletteEntries */
560 NULL
, /* pGetTextExtentExPoint */
561 NULL
, /* pGetTextMetrics */
562 NULL
, /* pIntersectClipRect */
563 NULL
, /* pInvertRgn */
564 dibdrv_LineTo
, /* pLineTo */
565 NULL
, /* pModifyWorldTransform */
567 NULL
, /* pOffsetClipRgn */
568 NULL
, /* pOffsetViewportOrg */
569 NULL
, /* pOffsetWindowOrg */
570 dibdrv_PaintRgn
, /* pPaintRgn */
571 dibdrv_PatBlt
, /* pPatBlt */
573 NULL
, /* pPolyBezier */
574 NULL
, /* pPolyBezierTo */
575 NULL
, /* pPolyDraw */
576 NULL
, /* pPolyPolygon */
577 dibdrv_PolyPolyline
, /* pPolyPolyline */
579 dibdrv_Polyline
, /* pPolyline */
580 NULL
, /* pPolylineTo */
581 dibdrv_PutImage
, /* pPutImage */
582 NULL
, /* pRealizeDefaultPalette */
583 NULL
, /* pRealizePalette */
584 dibdrv_Rectangle
, /* pRectangle */
586 NULL
, /* pRestoreDC */
587 NULL
, /* pRoundRect */
589 NULL
, /* pScaleViewportExt */
590 NULL
, /* pScaleWindowExt */
591 dibdrv_SelectBitmap
, /* pSelectBitmap */
592 dibdrv_SelectBrush
, /* pSelectBrush */
593 NULL
, /* pSelectClipPath */
594 NULL
, /* pSelectFont */
595 NULL
, /* pSelectPalette */
596 dibdrv_SelectPen
, /* pSelectPen */
597 NULL
, /* pSetArcDirection */
598 dibdrv_SetBkColor
, /* pSetBkColor */
599 dibdrv_SetBkMode
, /* pSetBkMode */
600 dibdrv_SetDCBrushColor
, /* pSetDCBrushColor */
601 dibdrv_SetDCPenColor
, /* pSetDCPenColor */
602 dibdrv_SetDIBColorTable
, /* pSetDIBColorTable */
603 NULL
, /* pSetDIBitsToDevice */
604 dibdrv_SetDeviceClipping
, /* pSetDeviceClipping */
605 NULL
, /* pSetDeviceGammaRamp */
606 NULL
, /* pSetLayout */
607 NULL
, /* pSetMapMode */
608 NULL
, /* pSetMapperFlags */
609 dibdrv_SetPixel
, /* pSetPixel */
610 NULL
, /* pSetPixelFormat */
611 NULL
, /* pSetPolyFillMode */
612 dibdrv_SetROP2
, /* pSetROP2 */
613 NULL
, /* pSetRelAbs */
614 NULL
, /* pSetStretchBltMode */
615 NULL
, /* pSetTextAlign */
616 NULL
, /* pSetTextCharacterExtra */
617 NULL
, /* pSetTextColor */
618 NULL
, /* pSetTextJustification */
619 NULL
, /* pSetViewportExt */
620 NULL
, /* pSetViewportOrg */
621 NULL
, /* pSetWindowExt */
622 NULL
, /* pSetWindowOrg */
623 NULL
, /* pSetWorldTransform */
624 NULL
, /* pStartDoc */
625 NULL
, /* pStartPage */
626 dibdrv_StretchBlt
, /* pStretchBlt */
627 NULL
, /* pStretchDIBits */
628 NULL
, /* pStrokeAndFillPath */
629 NULL
, /* pStrokePath */
630 NULL
, /* pSwapBuffers */
631 NULL
, /* pUnrealizePalette */
632 NULL
, /* pWidenPath */
633 NULL
, /* pwglCopyContext */
634 NULL
, /* pwglCreateContext */
635 NULL
, /* pwglCreateContextAttribsARB */
636 NULL
, /* pwglDeleteContext */
637 NULL
, /* pwglGetPbufferDCARB */
638 NULL
, /* pwglGetProcAddress */
639 NULL
, /* pwglMakeContextCurrentARB */
640 NULL
, /* pwglMakeCurrent */
641 NULL
, /* pwglSetPixelFormatWINE */
642 NULL
, /* pwglShareLists */
643 NULL
, /* pwglUseFontBitmapsA */
644 NULL
/* pwglUseFontBitmapsW */