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/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(dib);
30 static void calc_shift_and_len(DWORD mask, int *shift, int *len)
41 while ((mask & 1) == 0)
47 while ((mask & 1) == 1)
56 static void init_bit_fields(dib_info *dib, const DWORD *bit_fields)
58 dib->red_mask = bit_fields[0];
59 dib->green_mask = bit_fields[1];
60 dib->blue_mask = bit_fields[2];
61 calc_shift_and_len(dib->red_mask, &dib->red_shift, &dib->red_len);
62 calc_shift_and_len(dib->green_mask, &dib->green_shift, &dib->green_len);
63 calc_shift_and_len(dib->blue_mask, &dib->blue_shift, &dib->blue_len);
66 static BOOL init_dib_info(dib_info *dib, const BITMAPINFOHEADER *bi, const DWORD *bit_fields,
67 RGBQUAD *color_table, int color_table_size, void *bits)
69 static const DWORD bit_fields_888[3] = {0xff0000, 0x00ff00, 0x0000ff};
70 static const DWORD bit_fields_555[3] = {0x7c00, 0x03e0, 0x001f};
72 dib->bit_count = bi->biBitCount;
73 dib->width = bi->biWidth;
74 dib->height = bi->biHeight;
75 dib->stride = ((dib->width * dib->bit_count + 31) >> 3) & ~3;
78 if(dib->height < 0) /* top-down */
80 dib->height = -dib->height;
84 /* bits always points to the top-left corner and the stride is -ve */
85 dib->bits = (BYTE*)dib->bits + (dib->height - 1) * dib->stride;
86 dib->stride = -dib->stride;
89 dib->funcs = &funcs_null;
91 switch(dib->bit_count)
94 if(bi->biCompression == BI_RGB)
95 bit_fields = bit_fields_888;
97 init_bit_fields(dib, bit_fields);
99 if(dib->red_mask == 0xff0000 && dib->green_mask == 0x00ff00 && dib->blue_mask == 0x0000ff)
100 dib->funcs = &funcs_8888;
102 dib->funcs = &funcs_32;
106 if(bi->biCompression == BI_RGB)
107 bit_fields = bit_fields_555;
109 init_bit_fields(dib, bit_fields);
111 if(dib->red_mask == 0x7c00 && dib->green_mask == 0x03e0 && dib->blue_mask == 0x001f)
112 dib->funcs = &funcs_555;
114 dib->funcs = &funcs_16;
118 dib->funcs = &funcs_8;
122 TRACE("bpp %d not supported, will forward to graphics driver.\n", dib->bit_count);
128 dib->color_table = HeapAlloc(GetProcessHeap(), 0, color_table_size * sizeof(dib->color_table[0]));
129 if(!dib->color_table) return FALSE;
130 dib->color_table_size = color_table_size;
131 memcpy(dib->color_table, color_table, color_table_size * sizeof(color_table[0]));
135 dib->color_table = NULL;
136 dib->color_table_size = 0;
142 BOOL init_dib_info_from_packed(dib_info *dib, const BITMAPINFOHEADER *bi, WORD usage, HPALETTE palette)
145 RGBQUAD *color_table = NULL, pal_table[256];
146 BYTE *ptr = (BYTE*)bi + bi->biSize;
147 int num_colors = bi->biClrUsed;
149 if(bi->biCompression == BI_BITFIELDS)
151 masks = (DWORD *)ptr;
152 ptr += 3 * sizeof(DWORD);
155 if(!num_colors && bi->biBitCount <= 8) num_colors = 1 << bi->biBitCount;
158 if(usage == DIB_PAL_COLORS)
160 PALETTEENTRY entries[256];
161 const WORD *index = (const WORD*) ptr;
162 UINT i, count = GetPaletteEntries( palette, 0, num_colors, entries );
163 for (i = 0; i < num_colors; i++, index++)
165 PALETTEENTRY *entry = &entries[*index % count];
166 pal_table[i].rgbRed = entry->peRed;
167 pal_table[i].rgbGreen = entry->peGreen;
168 pal_table[i].rgbBlue = entry->peBlue;
169 pal_table[i].rgbReserved = 0;
171 color_table = pal_table;
172 ptr += num_colors * sizeof(WORD);
176 color_table = (RGBQUAD*)ptr;
177 ptr += num_colors * sizeof(*color_table);
181 return init_dib_info(dib, bi, masks, color_table, num_colors, ptr);
184 static void clear_dib_info(dib_info *dib)
186 dib->color_table = NULL;
190 /**********************************************************************
193 * Free the resources associated with a dib and optionally the bits
195 void free_dib_info(dib_info *dib, BOOL free_bits)
197 HeapFree(GetProcessHeap(), 0, dib->color_table);
198 dib->color_table = NULL;
201 HeapFree(GetProcessHeap(), 0, dib->bits);
206 void copy_dib_color_info(dib_info *dst, const dib_info *src)
208 dst->bit_count = src->bit_count;
209 dst->red_mask = src->red_mask;
210 dst->green_mask = src->green_mask;
211 dst->blue_mask = src->blue_mask;
212 dst->red_len = src->red_len;
213 dst->green_len = src->green_len;
214 dst->blue_len = src->blue_len;
215 dst->red_shift = src->red_shift;
216 dst->green_shift = src->green_shift;
217 dst->blue_shift = src->blue_shift;
218 dst->funcs = src->funcs;
219 dst->color_table_size = src->color_table_size;
220 dst->color_table = NULL;
221 if(dst->color_table_size)
223 int size = dst->color_table_size * sizeof(dst->color_table[0]);
224 dst->color_table = HeapAlloc(GetProcessHeap(), 0, size);
225 memcpy(dst->color_table, src->color_table, size);
229 /**************************************************************
232 * Converts src into the format specified in dst.
234 * FIXME: At the moment this always creates a top-down dib,
235 * do we want to give the option of bottom-up?
237 BOOL convert_dib(dib_info *dst, const dib_info *src)
242 dst->height = src->height;
243 dst->width = src->width;
244 dst->stride = ((dst->width * dst->bit_count + 31) >> 3) & ~3;
245 dst->bits = HeapAlloc(GetProcessHeap(), 0, dst->height * dst->stride);
247 src_rect.left = src_rect.top = 0;
248 src_rect.right = src->width;
249 src_rect.bottom = src->height;
251 ret = dst->funcs->convert_to(dst, src, &src_rect);
253 if(!ret) free_dib_info(dst, TRUE);
257 /***********************************************************************
260 static BOOL CDECL dibdrv_DeleteDC( PHYSDEV dev )
262 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
263 TRACE("(%p)\n", dev);
264 DeleteObject(pdev->clip);
265 free_pattern_brush(pdev);
266 free_dib_info(&pdev->dib, FALSE);
270 /***********************************************************************
271 * dibdrv_SelectBitmap
273 static HBITMAP CDECL dibdrv_SelectBitmap( PHYSDEV dev, HBITMAP bitmap )
275 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectBitmap );
276 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
277 BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
278 TRACE("(%p, %p)\n", dev, bitmap);
283 pdev->clip = CreateRectRgn(0, 0, 0, 0);
286 clear_dib_info(&pdev->dib);
287 clear_dib_info(&pdev->brush_dib);
288 pdev->brush_and_bits = pdev->brush_xor_bits = NULL;
290 if(!init_dib_info(&pdev->dib, &bmp->dib->dsBmih, bmp->dib->dsBitfields,
291 bmp->color_table, bmp->nb_colors, bmp->dib->dsBm.bmBits))
292 pdev->defer |= DEFER_FORMAT;
294 GDI_ReleaseObj( bitmap );
296 return next->funcs->pSelectBitmap( next, bitmap );
299 /***********************************************************************
302 static COLORREF CDECL dibdrv_SetBkColor( PHYSDEV dev, COLORREF color )
304 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetBkColor );
305 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
307 pdev->bkgnd_color = pdev->dib.funcs->colorref_to_pixel( &pdev->dib, color );
309 if( GetBkMode(dev->hdc) == OPAQUE )
310 calc_and_xor_masks( GetROP2(dev->hdc), pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
313 pdev->bkgnd_and = ~0u;
317 return next->funcs->pSetBkColor( next, color );
320 /***********************************************************************
323 static INT CDECL dibdrv_SetBkMode( PHYSDEV dev, INT mode )
325 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetBkMode );
326 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
329 calc_and_xor_masks( GetROP2(dev->hdc), pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
332 pdev->bkgnd_and = ~0u;
336 return next->funcs->pSetBkMode( next, mode );
339 /***********************************************************************
340 * dibdrv_SetDeviceClipping
342 static void CDECL dibdrv_SetDeviceClipping( PHYSDEV dev, HRGN vis_rgn, HRGN clip_rgn )
344 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDeviceClipping );
345 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
346 TRACE("(%p, %p, %p)\n", dev, vis_rgn, clip_rgn);
348 CombineRgn( pdev->clip, vis_rgn, clip_rgn, clip_rgn ? RGN_AND : RGN_COPY );
349 return next->funcs->pSetDeviceClipping( next, vis_rgn, clip_rgn);
352 /***********************************************************************
355 static INT CDECL dibdrv_SetROP2( PHYSDEV dev, INT rop )
357 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetROP2 );
358 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
360 calc_and_xor_masks(rop, pdev->pen_color, &pdev->pen_and, &pdev->pen_xor);
361 update_brush_rop(pdev, rop);
362 if( GetBkMode(dev->hdc) == OPAQUE )
363 calc_and_xor_masks(rop, pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor);
365 return next->funcs->pSetROP2( next, rop );
368 const DC_FUNCTIONS dib_driver =
370 NULL, /* pAbortDoc */
371 NULL, /* pAbortPath */
372 NULL, /* pAlphaBlend */
373 NULL, /* pAngleArc */
376 NULL, /* pBeginPath */
377 NULL, /* pChoosePixelFormat */
379 NULL, /* pCloseFigure */
380 NULL, /* pCreateBitmap */
381 NULL, /* pCreateDC */
382 NULL, /* pCreateDIBSection */
383 NULL, /* pDeleteBitmap */
384 dibdrv_DeleteDC, /* pDeleteDC */
385 NULL, /* pDeleteObject */
386 NULL, /* pDescribePixelFormat */
387 NULL, /* pDeviceCapabilities */
392 NULL, /* pEnumDeviceFonts */
393 NULL, /* pEnumICMProfiles */
394 NULL, /* pExcludeClipRect */
395 NULL, /* pExtDeviceMode */
396 NULL, /* pExtEscape */
397 NULL, /* pExtFloodFill */
398 NULL, /* pExtSelectClipRgn */
399 NULL, /* pExtTextOut */
400 NULL, /* pFillPath */
402 NULL, /* pFlattenPath */
403 NULL, /* pFrameRgn */
404 NULL, /* pGdiComment */
405 NULL, /* pGetBitmapBits */
406 NULL, /* pGetCharWidth */
407 NULL, /* pGetDIBits */
408 NULL, /* pGetDeviceCaps */
409 NULL, /* pGetDeviceGammaRamp */
410 NULL, /* pGetICMProfile */
411 NULL, /* pGetNearestColor */
412 NULL, /* pGetPixel */
413 NULL, /* pGetPixelFormat */
414 NULL, /* pGetSystemPaletteEntries */
415 NULL, /* pGetTextExtentExPoint */
416 NULL, /* pGetTextMetrics */
417 NULL, /* pIntersectClipRect */
418 NULL, /* pInvertRgn */
419 dibdrv_LineTo, /* pLineTo */
420 NULL, /* pModifyWorldTransform */
422 NULL, /* pOffsetClipRgn */
423 NULL, /* pOffsetViewportOrg */
424 NULL, /* pOffsetWindowOrg */
425 dibdrv_PaintRgn, /* pPaintRgn */
426 dibdrv_PatBlt, /* pPatBlt */
428 NULL, /* pPolyBezier */
429 NULL, /* pPolyBezierTo */
430 NULL, /* pPolyDraw */
431 NULL, /* pPolyPolygon */
432 NULL, /* pPolyPolyline */
434 NULL, /* pPolyline */
435 NULL, /* pPolylineTo */
436 NULL, /* pRealizeDefaultPalette */
437 NULL, /* pRealizePalette */
438 dibdrv_Rectangle, /* pRectangle */
440 NULL, /* pRestoreDC */
441 NULL, /* pRoundRect */
443 NULL, /* pScaleViewportExt */
444 NULL, /* pScaleWindowExt */
445 dibdrv_SelectBitmap, /* pSelectBitmap */
446 dibdrv_SelectBrush, /* pSelectBrush */
447 NULL, /* pSelectClipPath */
448 NULL, /* pSelectFont */
449 NULL, /* pSelectPalette */
450 dibdrv_SelectPen, /* pSelectPen */
451 NULL, /* pSetArcDirection */
452 NULL, /* pSetBitmapBits */
453 dibdrv_SetBkColor, /* pSetBkColor */
454 dibdrv_SetBkMode, /* pSetBkMode */
455 dibdrv_SetDCBrushColor, /* pSetDCBrushColor */
456 dibdrv_SetDCPenColor, /* pSetDCPenColor */
457 NULL, /* pSetDIBColorTable */
458 NULL, /* pSetDIBits */
459 NULL, /* pSetDIBitsToDevice */
460 dibdrv_SetDeviceClipping, /* pSetDeviceClipping */
461 NULL, /* pSetDeviceGammaRamp */
462 NULL, /* pSetLayout */
463 NULL, /* pSetMapMode */
464 NULL, /* pSetMapperFlags */
465 NULL, /* pSetPixel */
466 NULL, /* pSetPixelFormat */
467 NULL, /* pSetPolyFillMode */
468 dibdrv_SetROP2, /* pSetROP2 */
469 NULL, /* pSetRelAbs */
470 NULL, /* pSetStretchBltMode */
471 NULL, /* pSetTextAlign */
472 NULL, /* pSetTextCharacterExtra */
473 NULL, /* pSetTextColor */
474 NULL, /* pSetTextJustification */
475 NULL, /* pSetViewportExt */
476 NULL, /* pSetViewportOrg */
477 NULL, /* pSetWindowExt */
478 NULL, /* pSetWindowOrg */
479 NULL, /* pSetWorldTransform */
480 NULL, /* pStartDoc */
481 NULL, /* pStartPage */
482 NULL, /* pStretchBlt */
483 NULL, /* pStretchDIBits */
484 NULL, /* pStrokeAndFillPath */
485 NULL, /* pStrokePath */
486 NULL, /* pSwapBuffers */
487 NULL, /* pUnrealizePalette */
488 NULL, /* pWidenPath */
489 NULL, /* pwglCopyContext */
490 NULL, /* pwglCreateContext */
491 NULL, /* pwglCreateContextAttribsARB */
492 NULL, /* pwglDeleteContext */
493 NULL, /* pwglGetProcAddress */
494 NULL, /* pwglGetPbufferDCARB */
495 NULL, /* pwglMakeCurrent */
496 NULL, /* pwglMakeContextCurrentARB */
497 NULL, /* pwglSetPixelFormatWINE */
498 NULL, /* pwglShareLists */
499 NULL, /* pwglUseFontBitmapsA */
500 NULL /* pwglUseFontBitmapsW */