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;
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 );
83 dib->bits.is_copy = FALSE;
84 dib->bits.free = NULL;
85 dib->bits.param = NULL;
87 if(dib->height < 0) /* top-down */
89 dib->height = -dib->height;
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;
98 dib->funcs = &funcs_null;
100 switch(dib->bit_count)
103 if(bi->biCompression == BI_RGB)
104 bit_fields = bit_fields_888;
106 init_bit_fields(dib, bit_fields);
108 if(dib->red_mask == 0xff0000 && dib->green_mask == 0x00ff00 && dib->blue_mask == 0x0000ff)
109 dib->funcs = &funcs_8888;
111 dib->funcs = &funcs_32;
115 dib->funcs = &funcs_24;
119 if(bi->biCompression == BI_RGB)
120 bit_fields = bit_fields_555;
122 init_bit_fields(dib, bit_fields);
124 if(dib->red_mask == 0x7c00 && dib->green_mask == 0x03e0 && dib->blue_mask == 0x001f)
125 dib->funcs = &funcs_555;
127 dib->funcs = &funcs_16;
131 dib->funcs = &funcs_8;
135 dib->funcs = &funcs_4;
139 dib->funcs = &funcs_1;
143 if (color_table && bi->biClrUsed)
145 dib->color_table = color_table;
146 dib->color_table_size = bi->biClrUsed;
148 else if (flags & default_color_table)
150 dib->color_table = get_default_color_table( dib->bit_count );
151 dib->color_table_size = dib->color_table ? (1 << dib->bit_count) : 0;
155 dib->color_table = NULL;
156 dib->color_table_size = 0;
160 void init_dib_info_from_bitmapinfo(dib_info *dib, const BITMAPINFO *info, void *bits, enum dib_info_flags flags)
162 init_dib_info( dib, &info->bmiHeader, (const DWORD *)info->bmiColors, info->bmiColors, bits, flags );
165 BOOL init_dib_info_from_bitmapobj(dib_info *dib, BITMAPOBJ *bmp, enum dib_info_flags flags)
167 if (!is_bitmapobj_dib( bmp ))
171 get_ddb_bitmapinfo( bmp, &info );
172 if (!bmp->dib.dsBm.bmBits)
174 int width_bytes = get_dib_stride( bmp->dib.dsBm.bmWidth, bmp->dib.dsBm.bmBitsPixel );
175 bmp->dib.dsBm.bmBits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
176 bmp->dib.dsBm.bmHeight * width_bytes );
177 if (!bmp->dib.dsBm.bmBits) return FALSE;
179 init_dib_info_from_bitmapinfo( dib, &info, bmp->dib.dsBm.bmBits, flags );
181 else init_dib_info( dib, &bmp->dib.dsBmih, bmp->dib.dsBitfields,
182 bmp->color_table, bmp->dib.dsBm.bmBits, flags );
186 static void clear_dib_info(dib_info *dib)
188 dib->color_table = NULL;
189 dib->bits.ptr = NULL;
190 dib->bits.free = NULL;
191 dib->bits.param = NULL;
194 /**********************************************************************
197 * Free the resources associated with a dib and optionally the bits
199 void free_dib_info(dib_info *dib)
201 if (dib->bits.free) dib->bits.free( &dib->bits );
202 clear_dib_info( dib );
205 void copy_dib_color_info(dib_info *dst, const dib_info *src)
207 dst->bit_count = src->bit_count;
208 dst->red_mask = src->red_mask;
209 dst->green_mask = src->green_mask;
210 dst->blue_mask = src->blue_mask;
211 dst->red_len = src->red_len;
212 dst->green_len = src->green_len;
213 dst->blue_len = src->blue_len;
214 dst->red_shift = src->red_shift;
215 dst->green_shift = src->green_shift;
216 dst->blue_shift = src->blue_shift;
217 dst->funcs = src->funcs;
218 dst->color_table_size = src->color_table_size;
219 dst->color_table = src->color_table;
222 DWORD convert_bitmapinfo( const BITMAPINFO *src_info, void *src_bits, struct bitblt_coords *src,
223 const BITMAPINFO *dst_info, void *dst_bits, BOOL add_alpha )
225 dib_info src_dib, dst_dib;
228 init_dib_info_from_bitmapinfo( &src_dib, src_info, src_bits, default_color_table );
229 init_dib_info_from_bitmapinfo( &dst_dib, dst_info, dst_bits, default_color_table );
233 dst_dib.funcs->convert_to( &dst_dib, &src_dib, &src->visrect );
238 WARN( "invalid bits pointer %p\n", src_bits );
243 /* We shared the color tables, so there's no need to free the dib_infos here */
244 if(!ret) return ERROR_BAD_FORMAT;
246 /* update coordinates, the destination rectangle is always stored at 0,0 */
247 src->x -= src->visrect.left;
248 src->y -= src->visrect.top;
249 offset_rect( &src->visrect, -src->visrect.left, -src->visrect.top );
251 if (add_alpha && dst_dib.funcs == &funcs_8888 && src_dib.funcs != &funcs_8888)
253 DWORD *pixel = dst_dib.bits.ptr;
256 for (y = src->visrect.top; y < src->visrect.bottom; y++, pixel += dst_dib.stride / 4)
257 for (x = src->visrect.left; x < src->visrect.right; x++)
258 pixel[x] |= 0xff000000;
261 return ERROR_SUCCESS;
264 int get_clipped_rects( const dib_info *dib, const RECT *rc, HRGN clip, struct clipped_rects *clip_rects )
266 const WINEREGION *region;
267 RECT rect, *out = clip_rects->buffer;
270 init_clipped_rects( clip_rects );
274 rect.right = dib->rect.right - dib->rect.left;
275 rect.bottom = dib->rect.bottom - dib->rect.top;
276 if (rc && !intersect_rect( &rect, &rect, rc )) return 0;
281 clip_rects->count = 1;
285 if (!(region = get_wine_region( clip ))) return 0;
287 for (i = 0; i < region->numRects; i++)
289 if (region->rects[i].top >= rect.bottom) break;
290 if (!intersect_rect( out, &rect, ®ion->rects[i] )) continue;
292 if (out == &clip_rects->buffer[sizeof(clip_rects->buffer) / sizeof(RECT)])
294 clip_rects->rects = HeapAlloc( GetProcessHeap(), 0, region->numRects * sizeof(RECT) );
295 if (!clip_rects->rects) return 0;
296 memcpy( clip_rects->rects, clip_rects->buffer, (out - clip_rects->buffer) * sizeof(RECT) );
297 out = clip_rects->rects + (out - clip_rects->buffer);
300 release_wine_region( clip );
301 clip_rects->count = out - clip_rects->rects;
302 return clip_rects->count;
305 void add_clipped_bounds( dibdrv_physdev *dev, const RECT *rect, HRGN clip )
307 const WINEREGION *region;
310 if (!dev->bounds) return;
313 if (!(region = get_wine_region( clip ))) return;
314 if (!rect) rc = region->extents;
315 else intersect_rect( &rc, rect, ®ion->extents );
316 release_wine_region( clip );
320 if (is_rect_empty( &rc )) return;
321 offset_rect( &rc, dev->dib.rect.left, dev->dib.rect.top );
322 add_bounds_rect( dev->bounds, &rc );
325 /**********************************************************************
328 static BOOL dibdrv_CreateDC( PHYSDEV *dev, LPCWSTR driver, LPCWSTR device,
329 LPCWSTR output, const DEVMODEW *data )
331 dibdrv_physdev *pdev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pdev) );
333 if (!pdev) return FALSE;
334 clear_dib_info(&pdev->dib);
335 clear_dib_info(&pdev->brush.dib);
336 clear_dib_info(&pdev->pen_brush.dib);
337 push_dc_driver( dev, &pdev->dev, &dib_driver );
341 /***********************************************************************
344 static BOOL dibdrv_DeleteDC( PHYSDEV dev )
346 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
347 TRACE("(%p)\n", dev);
348 free_pattern_brush( &pdev->brush );
349 free_pattern_brush( &pdev->pen_brush );
350 HeapFree( GetProcessHeap(), 0, pdev );
354 /***********************************************************************
357 static BOOL dibdrv_CopyBitmap( HBITMAP src, HBITMAP dst )
359 return nulldrv_CopyBitmap( src, dst );
362 /***********************************************************************
363 * dibdrv_DeleteBitmap
365 static BOOL dibdrv_DeleteBitmap( HBITMAP bitmap )
370 /***********************************************************************
371 * dibdrv_SelectBitmap
373 static HBITMAP dibdrv_SelectBitmap( PHYSDEV dev, HBITMAP bitmap )
375 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
376 BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
379 TRACE("(%p, %p)\n", dev, bitmap);
383 if(!init_dib_info_from_bitmapobj(&dib, bmp, default_color_table))
385 GDI_ReleaseObj( bitmap );
389 GDI_ReleaseObj( bitmap );
394 /***********************************************************************
395 * dibdrv_SetDeviceClipping
397 static void dibdrv_SetDeviceClipping( PHYSDEV dev, HRGN rgn )
399 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
400 TRACE("(%p, %p)\n", dev, rgn);
405 /***********************************************************************
406 * dibdrv_SetBoundsRect
408 static UINT dibdrv_SetBoundsRect( PHYSDEV dev, RECT *rect, UINT flags )
410 dibdrv_physdev *pdev = get_dibdrv_pdev( dev );
412 if (flags & DCB_DISABLE) pdev->bounds = NULL;
413 else if (flags & DCB_ENABLE) pdev->bounds = rect;
414 return DCB_RESET; /* we don't have device-specific bounds */
417 /***********************************************************************
418 * dibdrv_GetDeviceGammaRamp
420 static BOOL dibdrv_GetDeviceGammaRamp( PHYSDEV dev, void *ramp )
422 SetLastError( ERROR_INVALID_PARAMETER );
426 /***********************************************************************
427 * dibdrv_SetDeviceGammaRamp
429 static BOOL dibdrv_SetDeviceGammaRamp( PHYSDEV dev, void *ramp )
431 SetLastError( ERROR_INVALID_PARAMETER );
435 const struct gdi_dc_funcs dib_driver =
437 NULL, /* pAbortDoc */
438 NULL, /* pAbortPath */
439 dibdrv_AlphaBlend, /* pAlphaBlend */
440 NULL, /* pAngleArc */
441 dibdrv_Arc, /* pArc */
442 dibdrv_ArcTo, /* pArcTo */
443 NULL, /* pBeginPath */
444 dibdrv_BlendImage, /* pBlendImage */
445 NULL, /* pChoosePixelFormat */
446 dibdrv_Chord, /* pChord */
447 NULL, /* pCloseFigure */
448 dibdrv_CopyBitmap, /* pCopyBitmap */
449 NULL, /* pCreateBitmap */
450 NULL, /* pCreateCompatibleDC */
451 dibdrv_CreateDC, /* pCreateDC */
452 dibdrv_DeleteBitmap, /* pDeleteBitmap */
453 dibdrv_DeleteDC, /* pDeleteDC */
454 NULL, /* pDeleteObject */
455 NULL, /* pDescribePixelFormat */
456 NULL, /* pDeviceCapabilities */
457 dibdrv_Ellipse, /* pEllipse */
461 NULL, /* pEnumFonts */
462 NULL, /* pEnumICMProfiles */
463 NULL, /* pExcludeClipRect */
464 NULL, /* pExtDeviceMode */
465 NULL, /* pExtEscape */
466 dibdrv_ExtFloodFill, /* pExtFloodFill */
467 NULL, /* pExtSelectClipRgn */
468 dibdrv_ExtTextOut, /* pExtTextOut */
469 NULL, /* pFillPath */
471 NULL, /* pFlattenPath */
472 NULL, /* pFontIsLinked */
473 NULL, /* pFrameRgn */
474 NULL, /* pGdiComment */
475 NULL, /* pGdiRealizationInfo */
476 NULL, /* pGetBoundsRect */
477 NULL, /* pGetCharABCWidths */
478 NULL, /* pGetCharABCWidthsI */
479 NULL, /* pGetCharWidth */
480 NULL, /* pGetDeviceCaps */
481 dibdrv_GetDeviceGammaRamp, /* pGetDeviceGammaRamp */
482 NULL, /* pGetFontData */
483 NULL, /* pGetFontUnicodeRanges */
484 NULL, /* pGetGlyphIndices */
485 NULL, /* pGetGlyphOutline */
486 NULL, /* pGetICMProfile */
487 dibdrv_GetImage, /* pGetImage */
488 NULL, /* pGetKerningPairs */
489 dibdrv_GetNearestColor, /* pGetNearestColor */
490 NULL, /* pGetOutlineTextMetrics */
491 dibdrv_GetPixel, /* pGetPixel */
492 NULL, /* pGetPixelFormat */
493 NULL, /* pGetSystemPaletteEntries */
494 NULL, /* pGetTextCharsetInfo */
495 NULL, /* pGetTextExtentExPoint */
496 NULL, /* pGetTextExtentExPointI */
497 NULL, /* pGetTextFace */
498 NULL, /* pGetTextMetrics */
499 dibdrv_GradientFill, /* pGradientFill */
500 NULL, /* pIntersectClipRect */
501 NULL, /* pInvertRgn */
502 dibdrv_LineTo, /* pLineTo */
503 NULL, /* pModifyWorldTransform */
505 NULL, /* pOffsetClipRgn */
506 NULL, /* pOffsetViewportOrg */
507 NULL, /* pOffsetWindowOrg */
508 dibdrv_PaintRgn, /* pPaintRgn */
509 dibdrv_PatBlt, /* pPatBlt */
510 dibdrv_Pie, /* pPie */
511 NULL, /* pPolyBezier */
512 NULL, /* pPolyBezierTo */
513 NULL, /* pPolyDraw */
514 dibdrv_PolyPolygon, /* pPolyPolygon */
515 dibdrv_PolyPolyline, /* pPolyPolyline */
516 dibdrv_Polygon, /* pPolygon */
517 dibdrv_Polyline, /* pPolyline */
518 NULL, /* pPolylineTo */
519 dibdrv_PutImage, /* pPutImage */
520 NULL, /* pRealizeDefaultPalette */
521 NULL, /* pRealizePalette */
522 dibdrv_Rectangle, /* pRectangle */
524 NULL, /* pRestoreDC */
525 dibdrv_RoundRect, /* pRoundRect */
527 NULL, /* pScaleViewportExt */
528 NULL, /* pScaleWindowExt */
529 dibdrv_SelectBitmap, /* pSelectBitmap */
530 dibdrv_SelectBrush, /* pSelectBrush */
531 NULL, /* pSelectClipPath */
532 NULL, /* pSelectFont */
533 NULL, /* pSelectPalette */
534 dibdrv_SelectPen, /* pSelectPen */
535 NULL, /* pSetArcDirection */
536 NULL, /* pSetBkColor */
537 NULL, /* pSetBkMode */
538 dibdrv_SetBoundsRect, /* pSetBoundsRect */
539 dibdrv_SetDCBrushColor, /* pSetDCBrushColor */
540 dibdrv_SetDCPenColor, /* pSetDCPenColor */
541 NULL, /* pSetDIBitsToDevice */
542 dibdrv_SetDeviceClipping, /* pSetDeviceClipping */
543 dibdrv_SetDeviceGammaRamp, /* pSetDeviceGammaRamp */
544 NULL, /* pSetLayout */
545 NULL, /* pSetMapMode */
546 NULL, /* pSetMapperFlags */
547 dibdrv_SetPixel, /* pSetPixel */
548 NULL, /* pSetPixelFormat */
549 NULL, /* pSetPolyFillMode */
551 NULL, /* pSetRelAbs */
552 NULL, /* pSetStretchBltMode */
553 NULL, /* pSetTextAlign */
554 NULL, /* pSetTextCharacterExtra */
555 NULL, /* pSetTextColor */
556 NULL, /* pSetTextJustification */
557 NULL, /* pSetViewportExt */
558 NULL, /* pSetViewportOrg */
559 NULL, /* pSetWindowExt */
560 NULL, /* pSetWindowOrg */
561 NULL, /* pSetWorldTransform */
562 NULL, /* pStartDoc */
563 NULL, /* pStartPage */
564 dibdrv_StretchBlt, /* pStretchBlt */
565 NULL, /* pStretchDIBits */
566 NULL, /* pStrokeAndFillPath */
567 NULL, /* pStrokePath */
568 NULL, /* pSwapBuffers */
569 NULL, /* pUnrealizePalette */
570 NULL, /* pWidenPath */
571 NULL, /* pwglCopyContext */
572 NULL, /* pwglCreateContext */
573 NULL, /* pwglCreateContextAttribsARB */
574 NULL, /* pwglDeleteContext */
575 NULL, /* pwglGetProcAddress */
576 NULL, /* pwglMakeContextCurrentARB */
577 NULL, /* pwglMakeCurrent */
578 NULL, /* pwglSetPixelFormatWINE */
579 NULL, /* pwglShareLists */
580 NULL, /* pwglUseFontBitmapsA */
581 NULL, /* pwglUseFontBitmapsW */
582 GDI_PRIORITY_DIB_DRV /* priority */