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 static void init_dib_info(dib_info
*dib
, const BITMAPINFOHEADER
*bi
, const DWORD
*bit_fields
,
71 const RGBQUAD
*color_table
, 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
;
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 if (color_table
&& bi
->biClrUsed
)
141 dib
->color_table
= color_table
;
142 dib
->color_table_size
= bi
->biClrUsed
;
144 else if (flags
& default_color_table
)
146 dib
->color_table
= get_default_color_table( dib
->bit_count
);
147 dib
->color_table_size
= dib
->color_table
? (1 << dib
->bit_count
) : 0;
151 dib
->color_table
= NULL
;
152 dib
->color_table_size
= 0;
156 void init_dib_info_from_bitmapinfo(dib_info
*dib
, const BITMAPINFO
*info
, void *bits
, enum dib_info_flags flags
)
158 init_dib_info( dib
, &info
->bmiHeader
, (const DWORD
*)info
->bmiColors
, info
->bmiColors
, bits
, flags
);
161 BOOL
init_dib_info_from_bitmapobj(dib_info
*dib
, BITMAPOBJ
*bmp
, enum dib_info_flags flags
)
167 get_ddb_bitmapinfo( bmp
, &info
);
168 if (!bmp
->bitmap
.bmBits
)
170 int width_bytes
= get_dib_stride( bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmBitsPixel
);
171 bmp
->bitmap
.bmBits
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
172 bmp
->bitmap
.bmHeight
* width_bytes
);
173 if (!bmp
->bitmap
.bmBits
) return FALSE
;
175 init_dib_info_from_bitmapinfo( dib
, &info
, bmp
->bitmap
.bmBits
, flags
);
177 else init_dib_info( dib
, &bmp
->dib
->dsBmih
, bmp
->dib
->dsBitfields
,
178 bmp
->color_table
, bmp
->dib
->dsBm
.bmBits
, flags
);
182 static void clear_dib_info(dib_info
*dib
)
184 dib
->color_table
= NULL
;
185 dib
->bits
.ptr
= NULL
;
186 dib
->bits
.free
= NULL
;
187 dib
->bits
.param
= NULL
;
190 /**********************************************************************
193 * Free the resources associated with a dib and optionally the bits
195 void free_dib_info(dib_info
*dib
)
197 if (dib
->bits
.free
) dib
->bits
.free( &dib
->bits
);
198 clear_dib_info( dib
);
201 void copy_dib_color_info(dib_info
*dst
, const dib_info
*src
)
203 dst
->bit_count
= src
->bit_count
;
204 dst
->red_mask
= src
->red_mask
;
205 dst
->green_mask
= src
->green_mask
;
206 dst
->blue_mask
= src
->blue_mask
;
207 dst
->red_len
= src
->red_len
;
208 dst
->green_len
= src
->green_len
;
209 dst
->blue_len
= src
->blue_len
;
210 dst
->red_shift
= src
->red_shift
;
211 dst
->green_shift
= src
->green_shift
;
212 dst
->blue_shift
= src
->blue_shift
;
213 dst
->funcs
= src
->funcs
;
214 dst
->color_table_size
= src
->color_table_size
;
215 dst
->color_table
= src
->color_table
;
218 DWORD
convert_bitmapinfo( const BITMAPINFO
*src_info
, void *src_bits
, struct bitblt_coords
*src
,
219 const BITMAPINFO
*dst_info
, void *dst_bits
, BOOL add_alpha
)
221 dib_info src_dib
, dst_dib
;
224 init_dib_info_from_bitmapinfo( &src_dib
, src_info
, src_bits
, default_color_table
);
225 init_dib_info_from_bitmapinfo( &dst_dib
, dst_info
, dst_bits
, default_color_table
);
229 dst_dib
.funcs
->convert_to( &dst_dib
, &src_dib
, &src
->visrect
);
234 WARN( "invalid bits pointer %p\n", src_bits
);
239 /* We shared the color tables, so there's no need to free the dib_infos here */
240 if(!ret
) return ERROR_BAD_FORMAT
;
242 /* update coordinates, the destination rectangle is always stored at 0,0 */
243 src
->x
-= src
->visrect
.left
;
244 src
->y
-= src
->visrect
.top
;
245 offset_rect( &src
->visrect
, -src
->visrect
.left
, -src
->visrect
.top
);
247 if (add_alpha
&& dst_dib
.funcs
== &funcs_8888
&& src_dib
.funcs
!= &funcs_8888
)
249 DWORD
*pixel
= dst_dib
.bits
.ptr
;
252 for (y
= src
->visrect
.top
; y
< src
->visrect
.bottom
; y
++, pixel
+= dst_dib
.stride
/ 4)
253 for (x
= src
->visrect
.left
; x
< src
->visrect
.right
; x
++)
254 pixel
[x
] |= 0xff000000;
257 return ERROR_SUCCESS
;
260 static void update_fg_colors( dibdrv_physdev
*pdev
)
262 pdev
->text_color
= get_pixel_color( pdev
, GetTextColor( pdev
->dev
.hdc
), TRUE
);
265 /***********************************************************************
266 * add_extra_clipping_region
268 * Temporarily add a region to the current clipping region.
269 * The returned region must be restored with restore_clipping_region.
271 HRGN
add_extra_clipping_region( dibdrv_physdev
*pdev
, HRGN rgn
)
275 if (!(clip
= CreateRectRgn( 0, 0, 0, 0 ))) return 0;
276 CombineRgn( clip
, pdev
->clip
, rgn
, RGN_AND
);
282 /***********************************************************************
283 * restore_clipping_region
285 void restore_clipping_region( dibdrv_physdev
*pdev
, HRGN rgn
)
288 DeleteObject( pdev
->clip
);
292 /**********************************************************************
295 static BOOL
dibdrv_CreateDC( PHYSDEV
*dev
, LPCWSTR driver
, LPCWSTR device
,
296 LPCWSTR output
, const DEVMODEW
*data
)
298 dibdrv_physdev
*pdev
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*pdev
) );
300 if (!pdev
) return FALSE
;
301 if (!(pdev
->clip
= CreateRectRgn(0, 0, 0, 0)))
303 HeapFree( GetProcessHeap(), 0, pdev
);
306 clear_dib_info(&pdev
->dib
);
307 clear_dib_info(&pdev
->brush_dib
);
308 push_dc_driver( dev
, &pdev
->dev
, &dib_driver
);
312 /***********************************************************************
315 static BOOL
dibdrv_DeleteDC( PHYSDEV dev
)
317 dibdrv_physdev
*pdev
= get_dibdrv_pdev(dev
);
318 TRACE("(%p)\n", dev
);
319 DeleteObject(pdev
->clip
);
320 free_pattern_brush(pdev
);
321 HeapFree( GetProcessHeap(), 0, pdev
);
325 /***********************************************************************
328 static BOOL
dibdrv_CopyBitmap( HBITMAP src
, HBITMAP dst
)
330 return nulldrv_CopyBitmap( src
, dst
);
333 /***********************************************************************
334 * dibdrv_DeleteBitmap
336 static BOOL
dibdrv_DeleteBitmap( HBITMAP bitmap
)
341 /***********************************************************************
342 * dibdrv_SelectBitmap
344 static HBITMAP
dibdrv_SelectBitmap( PHYSDEV dev
, HBITMAP bitmap
)
346 PHYSDEV next
= GET_NEXT_PHYSDEV( dev
, pSelectBitmap
);
347 dibdrv_physdev
*pdev
= get_dibdrv_pdev(dev
);
348 BITMAPOBJ
*bmp
= GDI_GetObjPtr( bitmap
, OBJ_BITMAP
);
351 TRACE("(%p, %p)\n", dev
, bitmap
);
355 if(!init_dib_info_from_bitmapobj(&dib
, bmp
, default_color_table
))
357 GDI_ReleaseObj( bitmap
);
362 GDI_ReleaseObj( bitmap
);
364 return next
->funcs
->pSelectBitmap( next
, bitmap
);
367 /***********************************************************************
370 static COLORREF
dibdrv_SetBkColor( PHYSDEV dev
, COLORREF color
)
372 PHYSDEV next
= GET_NEXT_PHYSDEV( dev
, pSetBkColor
);
373 dibdrv_physdev
*pdev
= get_dibdrv_pdev(dev
);
375 update_fg_colors( pdev
); /* Only needed in the 1 bpp case */
377 return next
->funcs
->pSetBkColor( next
, color
);
380 /***********************************************************************
381 * dibdrv_SetDeviceClipping
383 static void dibdrv_SetDeviceClipping( PHYSDEV dev
, HRGN rgn
)
385 PHYSDEV next
= GET_NEXT_PHYSDEV( dev
, pSetDeviceClipping
);
386 dibdrv_physdev
*pdev
= get_dibdrv_pdev(dev
);
387 TRACE("(%p, %p)\n", dev
, rgn
);
389 SetRectRgn( pdev
->clip
, 0, 0, pdev
->dib
.width
, pdev
->dib
.height
);
390 if (rgn
) CombineRgn( pdev
->clip
, pdev
->clip
, rgn
, RGN_AND
);
391 return next
->funcs
->pSetDeviceClipping( next
, rgn
);
394 /***********************************************************************
395 * dibdrv_SetDIBColorTable
397 static UINT
dibdrv_SetDIBColorTable( PHYSDEV dev
, UINT pos
, UINT count
, const RGBQUAD
*colors
)
399 PHYSDEV next
= GET_NEXT_PHYSDEV( dev
, pSetDIBColorTable
);
400 dibdrv_physdev
*pdev
= get_dibdrv_pdev(dev
);
401 TRACE("(%p, %d, %d, %p)\n", dev
, pos
, count
, colors
);
403 if (pdev
->dib
.color_table
)
405 update_fg_colors( pdev
);
406 update_brush_rop( pdev
, GetROP2( dev
->hdc
) );
408 return next
->funcs
->pSetDIBColorTable( next
, pos
, count
, colors
);
411 /***********************************************************************
414 static INT
dibdrv_SetROP2( PHYSDEV dev
, INT rop
)
416 PHYSDEV next
= GET_NEXT_PHYSDEV( dev
, pSetROP2
);
417 dibdrv_physdev
*pdev
= get_dibdrv_pdev(dev
);
419 update_brush_rop( pdev
, rop
);
421 return next
->funcs
->pSetROP2( next
, rop
);
424 /***********************************************************************
425 * dibdrv_SetTextColor
427 static COLORREF
dibdrv_SetTextColor( PHYSDEV dev
, COLORREF color
)
429 PHYSDEV next
= GET_NEXT_PHYSDEV( dev
, pSetTextColor
);
430 dibdrv_physdev
*pdev
= get_dibdrv_pdev(dev
);
432 pdev
->text_color
= get_pixel_color( pdev
, color
, TRUE
);
433 update_aa_ranges( pdev
);
435 return next
->funcs
->pSetTextColor( next
, color
);
438 const struct gdi_dc_funcs dib_driver
=
440 NULL
, /* pAbortDoc */
441 NULL
, /* pAbortPath */
442 dibdrv_AlphaBlend
, /* pAlphaBlend */
443 NULL
, /* pAngleArc */
446 NULL
, /* pBeginPath */
447 dibdrv_BlendImage
, /* pBlendImage */
448 NULL
, /* pChoosePixelFormat */
450 NULL
, /* pCloseFigure */
451 dibdrv_CopyBitmap
, /* pCopyBitmap */
452 NULL
, /* pCreateBitmap */
453 NULL
, /* pCreateCompatibleDC */
454 dibdrv_CreateDC
, /* pCreateDC */
455 NULL
, /* pCreateDIBSection */
456 dibdrv_DeleteBitmap
, /* pDeleteBitmap */
457 dibdrv_DeleteDC
, /* pDeleteDC */
458 NULL
, /* pDeleteObject */
459 NULL
, /* pDescribePixelFormat */
460 NULL
, /* pDeviceCapabilities */
465 NULL
, /* pEnumFonts */
466 NULL
, /* pEnumICMProfiles */
467 NULL
, /* pExcludeClipRect */
468 NULL
, /* pExtDeviceMode */
469 NULL
, /* pExtEscape */
470 NULL
, /* pExtFloodFill */
471 NULL
, /* pExtSelectClipRgn */
472 dibdrv_ExtTextOut
, /* pExtTextOut */
473 NULL
, /* pFillPath */
475 NULL
, /* pFlattenPath */
476 NULL
, /* pFontIsLinked */
477 NULL
, /* pFrameRgn */
478 NULL
, /* pGdiComment */
479 NULL
, /* pGdiRealizationInfo */
480 NULL
, /* pGetCharABCWidths */
481 NULL
, /* pGetCharABCWidthsI */
482 NULL
, /* pGetCharWidth */
483 NULL
, /* pGetDeviceCaps */
484 NULL
, /* pGetDeviceGammaRamp */
485 NULL
, /* pGetFontData */
486 NULL
, /* pGetFontUnicodeRanges */
487 NULL
, /* pGetGlyphIndices */
488 NULL
, /* pGetGlyphOutline */
489 NULL
, /* pGetICMProfile */
490 dibdrv_GetImage
, /* pGetImage */
491 NULL
, /* pGetKerningPairs */
492 dibdrv_GetNearestColor
, /* pGetNearestColor */
493 NULL
, /* pGetOutlineTextMetrics */
494 dibdrv_GetPixel
, /* pGetPixel */
495 NULL
, /* pGetPixelFormat */
496 NULL
, /* pGetSystemPaletteEntries */
497 NULL
, /* pGetTextCharsetInfo */
498 NULL
, /* pGetTextExtentExPoint */
499 NULL
, /* pGetTextExtentExPointI */
500 NULL
, /* pGetTextFace */
501 NULL
, /* pGetTextMetrics */
502 dibdrv_GradientFill
, /* pGradientFill */
503 NULL
, /* pIntersectClipRect */
504 NULL
, /* pInvertRgn */
505 dibdrv_LineTo
, /* pLineTo */
506 NULL
, /* pModifyWorldTransform */
508 NULL
, /* pOffsetClipRgn */
509 NULL
, /* pOffsetViewportOrg */
510 NULL
, /* pOffsetWindowOrg */
511 dibdrv_PaintRgn
, /* pPaintRgn */
512 dibdrv_PatBlt
, /* pPatBlt */
514 NULL
, /* pPolyBezier */
515 NULL
, /* pPolyBezierTo */
516 NULL
, /* pPolyDraw */
517 NULL
, /* pPolyPolygon */
518 dibdrv_PolyPolyline
, /* pPolyPolyline */
520 dibdrv_Polyline
, /* pPolyline */
521 NULL
, /* pPolylineTo */
522 dibdrv_PutImage
, /* pPutImage */
523 NULL
, /* pRealizeDefaultPalette */
524 NULL
, /* pRealizePalette */
525 dibdrv_Rectangle
, /* pRectangle */
527 NULL
, /* pRestoreDC */
528 NULL
, /* pRoundRect */
530 NULL
, /* pScaleViewportExt */
531 NULL
, /* pScaleWindowExt */
532 dibdrv_SelectBitmap
, /* pSelectBitmap */
533 dibdrv_SelectBrush
, /* pSelectBrush */
534 NULL
, /* pSelectClipPath */
535 NULL
, /* pSelectFont */
536 NULL
, /* pSelectPalette */
537 dibdrv_SelectPen
, /* pSelectPen */
538 NULL
, /* pSetArcDirection */
539 dibdrv_SetBkColor
, /* pSetBkColor */
540 NULL
, /* pSetBkMode */
541 dibdrv_SetDCBrushColor
, /* pSetDCBrushColor */
542 dibdrv_SetDCPenColor
, /* pSetDCPenColor */
543 dibdrv_SetDIBColorTable
, /* pSetDIBColorTable */
544 NULL
, /* pSetDIBitsToDevice */
545 dibdrv_SetDeviceClipping
, /* pSetDeviceClipping */
546 NULL
, /* pSetDeviceGammaRamp */
547 NULL
, /* pSetLayout */
548 NULL
, /* pSetMapMode */
549 NULL
, /* pSetMapperFlags */
550 dibdrv_SetPixel
, /* pSetPixel */
551 NULL
, /* pSetPixelFormat */
552 NULL
, /* pSetPolyFillMode */
553 dibdrv_SetROP2
, /* pSetROP2 */
554 NULL
, /* pSetRelAbs */
555 NULL
, /* pSetStretchBltMode */
556 NULL
, /* pSetTextAlign */
557 NULL
, /* pSetTextCharacterExtra */
558 dibdrv_SetTextColor
, /* pSetTextColor */
559 NULL
, /* pSetTextJustification */
560 NULL
, /* pSetViewportExt */
561 NULL
, /* pSetViewportOrg */
562 NULL
, /* pSetWindowExt */
563 NULL
, /* pSetWindowOrg */
564 NULL
, /* pSetWorldTransform */
565 NULL
, /* pStartDoc */
566 NULL
, /* pStartPage */
567 dibdrv_StretchBlt
, /* pStretchBlt */
568 NULL
, /* pStretchDIBits */
569 NULL
, /* pStrokeAndFillPath */
570 NULL
, /* pStrokePath */
571 NULL
, /* pSwapBuffers */
572 NULL
, /* pUnrealizePalette */
573 NULL
, /* pWidenPath */
574 NULL
, /* pwglCopyContext */
575 NULL
, /* pwglCreateContext */
576 NULL
, /* pwglCreateContextAttribsARB */
577 NULL
, /* pwglDeleteContext */
578 NULL
, /* pwglGetPbufferDCARB */
579 NULL
, /* pwglGetProcAddress */
580 NULL
, /* pwglMakeContextCurrentARB */
581 NULL
, /* pwglMakeCurrent */
582 NULL
, /* pwglSetPixelFormatWINE */
583 NULL
, /* pwglShareLists */
584 NULL
, /* pwglUseFontBitmapsA */
585 NULL
/* pwglUseFontBitmapsW */