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)
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;
150 dib->color_table = NULL;
151 dib->color_table_size = 0;
155 void init_dib_info_from_bitmapinfo(dib_info *dib, const BITMAPINFO *info, void *bits)
157 init_dib_info( dib, &info->bmiHeader, (const DWORD *)info->bmiColors, info->bmiColors, bits );
160 BOOL init_dib_info_from_bitmapobj(dib_info *dib, BITMAPOBJ *bmp)
162 if (!is_bitmapobj_dib( bmp ))
166 get_ddb_bitmapinfo( bmp, &info );
167 if (!bmp->dib.dsBm.bmBits)
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;
174 init_dib_info_from_bitmapinfo( dib, &info, bmp->dib.dsBm.bmBits );
176 else init_dib_info( dib, &bmp->dib.dsBmih, bmp->dib.dsBitfields,
177 bmp->color_table, bmp->dib.dsBm.bmBits );
181 static void clear_dib_info(dib_info *dib)
183 dib->bits.ptr = NULL;
184 dib->bits.free = NULL;
185 dib->bits.param = NULL;
188 /**********************************************************************
191 * Free the resources associated with a dib and optionally the bits
193 void free_dib_info(dib_info *dib)
195 if (dib->bits.free) dib->bits.free( &dib->bits );
196 clear_dib_info( dib );
199 void copy_dib_color_info(dib_info *dst, const dib_info *src)
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;
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 )
219 dib_info src_dib, dst_dib;
222 init_dib_info_from_bitmapinfo( &src_dib, src_info, src_bits );
223 init_dib_info_from_bitmapinfo( &dst_dib, dst_info, dst_bits );
227 dst_dib.funcs->convert_to( &dst_dib, &src_dib, &src->visrect, FALSE );
232 WARN( "invalid bits pointer %p\n", src_bits );
237 /* We shared the color tables, so there's no need to free the dib_infos here */
238 if(!ret) return ERROR_BAD_FORMAT;
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 );
245 if (add_alpha && dst_dib.funcs == &funcs_8888 && src_dib.funcs != &funcs_8888)
247 DWORD *pixel = dst_dib.bits.ptr;
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;
255 return ERROR_SUCCESS;
258 int get_clipped_rects( const dib_info *dib, const RECT *rc, HRGN clip, struct clipped_rects *clip_rects )
260 const WINEREGION *region;
261 RECT rect, *out = clip_rects->buffer;
264 init_clipped_rects( clip_rects );
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;
275 clip_rects->count = 1;
279 if (!(region = get_wine_region( clip ))) return 0;
281 for (i = 0; i < region->numRects; i++)
283 if (region->rects[i].top >= rect.bottom) break;
284 if (!intersect_rect( out, &rect, ®ion->rects[i] )) continue;
286 if (out == &clip_rects->buffer[sizeof(clip_rects->buffer) / sizeof(RECT)])
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);
294 release_wine_region( clip );
295 clip_rects->count = out - clip_rects->rects;
296 return clip_rects->count;
299 void add_clipped_bounds( dibdrv_physdev *dev, const RECT *rect, HRGN clip )
301 const WINEREGION *region;
304 if (!dev->bounds) return;
307 if (!(region = get_wine_region( clip ))) return;
308 if (!rect) rc = region->extents;
309 else intersect_rect( &rc, rect, ®ion->extents );
310 release_wine_region( clip );
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 );
319 /**********************************************************************
322 static BOOL dibdrv_CreateDC( PHYSDEV *dev, LPCWSTR driver, LPCWSTR device,
323 LPCWSTR output, const DEVMODEW *data )
325 dibdrv_physdev *pdev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pdev) );
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 );
335 /***********************************************************************
338 static BOOL dibdrv_DeleteDC( PHYSDEV dev )
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 );
348 /***********************************************************************
351 static BOOL dibdrv_CopyBitmap( HBITMAP src, HBITMAP dst )
353 return nulldrv_CopyBitmap( src, dst );
356 /***********************************************************************
357 * dibdrv_DeleteBitmap
359 static BOOL dibdrv_DeleteBitmap( HBITMAP bitmap )
364 /***********************************************************************
365 * dibdrv_SelectBitmap
367 static HBITMAP dibdrv_SelectBitmap( PHYSDEV dev, HBITMAP bitmap )
369 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
370 BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
373 TRACE("(%p, %p)\n", dev, bitmap);
377 if (!init_dib_info_from_bitmapobj(&dib, bmp))
379 GDI_ReleaseObj( bitmap );
383 GDI_ReleaseObj( bitmap );
388 /***********************************************************************
389 * dibdrv_SetDeviceClipping
391 static void dibdrv_SetDeviceClipping( PHYSDEV dev, HRGN rgn )
393 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
394 TRACE("(%p, %p)\n", dev, rgn);
399 /***********************************************************************
400 * dibdrv_SetBoundsRect
402 static UINT dibdrv_SetBoundsRect( PHYSDEV dev, RECT *rect, UINT flags )
404 dibdrv_physdev *pdev = get_dibdrv_pdev( dev );
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 */
411 /***********************************************************************
412 * dibdrv_GetDeviceGammaRamp
414 static BOOL dibdrv_GetDeviceGammaRamp( PHYSDEV dev, void *ramp )
416 SetLastError( ERROR_INVALID_PARAMETER );
420 /***********************************************************************
421 * dibdrv_SetDeviceGammaRamp
423 static BOOL dibdrv_SetDeviceGammaRamp( PHYSDEV dev, void *ramp )
425 SetLastError( ERROR_INVALID_PARAMETER );
429 const struct gdi_dc_funcs dib_driver =
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 */
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 */
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 */
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 */
518 NULL, /* pRestoreDC */
519 dibdrv_RoundRect, /* pRoundRect */
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 */
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 */