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, enum dib_info_flags flags)
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;
77 dib->ptr_to_free = NULL;
80 if(dib->height < 0) /* top-down */
82 dib->height = -dib->height;
86 /* bits always points to the top-left corner and the stride is -ve */
87 dib->bits = (BYTE*)dib->bits + (dib->height - 1) * dib->stride;
88 dib->stride = -dib->stride;
91 dib->funcs = &funcs_null;
93 switch(dib->bit_count)
96 if(bi->biCompression == BI_RGB)
97 bit_fields = bit_fields_888;
99 init_bit_fields(dib, bit_fields);
101 if(dib->red_mask == 0xff0000 && dib->green_mask == 0x00ff00 && dib->blue_mask == 0x0000ff)
102 dib->funcs = &funcs_8888;
104 dib->funcs = &funcs_32;
108 dib->funcs = &funcs_24;
112 if(bi->biCompression == BI_RGB)
113 bit_fields = bit_fields_555;
115 init_bit_fields(dib, bit_fields);
117 if(dib->red_mask == 0x7c00 && dib->green_mask == 0x03e0 && dib->blue_mask == 0x001f)
118 dib->funcs = &funcs_555;
120 dib->funcs = &funcs_16;
124 dib->funcs = &funcs_8;
128 dib->funcs = &funcs_4;
132 dib->funcs = &funcs_1;
136 TRACE("bpp %d not supported, will forward to graphics driver.\n", dib->bit_count);
142 if (flags & private_color_table)
144 dib->color_table = HeapAlloc(GetProcessHeap(), 0, color_table_size * sizeof(dib->color_table[0]));
145 if(!dib->color_table) return FALSE;
146 memcpy(dib->color_table, color_table, color_table_size * sizeof(color_table[0]));
149 dib->color_table = color_table;
150 dib->color_table_size = color_table_size;
154 dib->color_table = NULL;
155 dib->color_table_size = 0;
161 BOOL init_dib_info_from_packed(dib_info *dib, const BITMAPINFOHEADER *bi, WORD usage, HPALETTE palette)
164 RGBQUAD *color_table = NULL, pal_table[256];
165 BYTE *ptr = (BYTE*)bi + bi->biSize;
166 int num_colors = bi->biClrUsed;
168 if(bi->biCompression == BI_BITFIELDS)
170 masks = (DWORD *)ptr;
171 ptr += 3 * sizeof(DWORD);
174 if(!num_colors && bi->biBitCount <= 8) num_colors = 1 << bi->biBitCount;
177 if(usage == DIB_PAL_COLORS)
179 PALETTEENTRY entries[256];
180 const WORD *index = (const WORD*) ptr;
181 UINT i, count = GetPaletteEntries( palette, 0, num_colors, entries );
182 for (i = 0; i < num_colors; i++, index++)
184 PALETTEENTRY *entry = &entries[*index % count];
185 pal_table[i].rgbRed = entry->peRed;
186 pal_table[i].rgbGreen = entry->peGreen;
187 pal_table[i].rgbBlue = entry->peBlue;
188 pal_table[i].rgbReserved = 0;
190 color_table = pal_table;
191 ptr += num_colors * sizeof(WORD);
195 color_table = (RGBQUAD*)ptr;
196 ptr += num_colors * sizeof(*color_table);
200 return init_dib_info(dib, bi, masks, color_table, num_colors, ptr, private_color_table);
203 static void clear_dib_info(dib_info *dib)
205 dib->color_table = NULL;
207 dib->ptr_to_free = NULL;
210 /**********************************************************************
213 * Free the resources associated with a dib and optionally the bits
215 void free_dib_info(dib_info *dib)
217 if (dib->flags & private_color_table)
218 HeapFree(GetProcessHeap(), 0, dib->color_table);
219 dib->color_table = NULL;
221 HeapFree(GetProcessHeap(), 0, dib->ptr_to_free);
222 dib->ptr_to_free = NULL;
226 void copy_dib_color_info(dib_info *dst, const dib_info *src)
228 dst->bit_count = src->bit_count;
229 dst->red_mask = src->red_mask;
230 dst->green_mask = src->green_mask;
231 dst->blue_mask = src->blue_mask;
232 dst->red_len = src->red_len;
233 dst->green_len = src->green_len;
234 dst->blue_len = src->blue_len;
235 dst->red_shift = src->red_shift;
236 dst->green_shift = src->green_shift;
237 dst->blue_shift = src->blue_shift;
238 dst->funcs = src->funcs;
239 dst->color_table_size = src->color_table_size;
240 dst->color_table = NULL;
241 dst->flags = src->flags;
242 if(dst->color_table_size)
244 int size = dst->color_table_size * sizeof(dst->color_table[0]);
245 if (dst->flags & private_color_table)
247 dst->color_table = HeapAlloc(GetProcessHeap(), 0, size);
248 memcpy(dst->color_table, src->color_table, size);
251 dst->color_table = src->color_table;
255 /**************************************************************
258 * Converts src into the format specified in dst.
260 * FIXME: At the moment this always creates a top-down dib,
261 * do we want to give the option of bottom-up?
263 BOOL convert_dib(dib_info *dst, const dib_info *src)
268 dst->height = src->height;
269 dst->width = src->width;
270 dst->stride = ((dst->width * dst->bit_count + 31) >> 3) & ~3;
271 dst->ptr_to_free = dst->bits = HeapAlloc(GetProcessHeap(), 0, dst->height * dst->stride);
273 src_rect.left = src_rect.top = 0;
274 src_rect.right = src->width;
275 src_rect.bottom = src->height;
277 ret = dst->funcs->convert_to(dst, src, &src_rect);
279 if(!ret) free_dib_info(dst);
283 static void update_fg_colors( dibdrv_physdev *pdev )
285 pdev->pen_color = get_fg_color( pdev, pdev->pen_colorref );
286 pdev->brush_color = get_fg_color( pdev, pdev->brush_colorref );
289 static void update_masks( dibdrv_physdev *pdev, INT rop )
291 calc_and_xor_masks( rop, pdev->pen_color, &pdev->pen_and, &pdev->pen_xor );
292 update_brush_rop( pdev, rop );
293 if( GetBkMode( pdev->dev.hdc ) == OPAQUE )
294 calc_and_xor_masks( rop, pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
297 /***********************************************************************
300 static BOOL dibdrv_DeleteDC( PHYSDEV dev )
302 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
303 TRACE("(%p)\n", dev);
304 DeleteObject(pdev->clip);
305 free_pattern_brush(pdev);
306 free_dib_info(&pdev->dib);
310 /***********************************************************************
311 * dibdrv_SelectBitmap
313 static HBITMAP dibdrv_SelectBitmap( PHYSDEV dev, HBITMAP bitmap )
315 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectBitmap );
316 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
317 BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
318 TRACE("(%p, %p)\n", dev, bitmap);
323 pdev->clip = CreateRectRgn(0, 0, 0, 0);
326 clear_dib_info(&pdev->dib);
327 clear_dib_info(&pdev->brush_dib);
328 pdev->brush_and_bits = pdev->brush_xor_bits = NULL;
330 if(!init_dib_info(&pdev->dib, &bmp->dib->dsBmih, bmp->dib->dsBitfields,
331 bmp->color_table, bmp->nb_colors, bmp->dib->dsBm.bmBits, private_color_table))
332 pdev->defer |= DEFER_FORMAT;
334 GDI_ReleaseObj( bitmap );
336 return next->funcs->pSelectBitmap( next, bitmap );
339 /***********************************************************************
342 static COLORREF dibdrv_SetBkColor( PHYSDEV dev, COLORREF color )
344 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetBkColor );
345 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
347 pdev->bkgnd_color = pdev->dib.funcs->colorref_to_pixel( &pdev->dib, color );
349 if( GetBkMode(dev->hdc) == OPAQUE )
350 calc_and_xor_masks( GetROP2(dev->hdc), pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
353 pdev->bkgnd_and = ~0u;
357 update_fg_colors( pdev ); /* Only needed in the 1 bpp case */
359 return next->funcs->pSetBkColor( next, color );
362 /***********************************************************************
365 static INT dibdrv_SetBkMode( PHYSDEV dev, INT mode )
367 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetBkMode );
368 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
371 calc_and_xor_masks( GetROP2(dev->hdc), pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
374 pdev->bkgnd_and = ~0u;
378 return next->funcs->pSetBkMode( next, mode );
381 /***********************************************************************
382 * dibdrv_SetDeviceClipping
384 static void dibdrv_SetDeviceClipping( PHYSDEV dev, HRGN vis_rgn, HRGN clip_rgn )
386 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDeviceClipping );
387 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
388 TRACE("(%p, %p, %p)\n", dev, vis_rgn, clip_rgn);
390 CombineRgn( pdev->clip, vis_rgn, clip_rgn, clip_rgn ? RGN_AND : RGN_COPY );
391 return next->funcs->pSetDeviceClipping( next, vis_rgn, clip_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 && pos < pdev->dib.color_table_size )
405 if( pos + count > pdev->dib.color_table_size ) count = pdev->dib.color_table_size - pos;
406 memcpy( pdev->dib.color_table + pos, colors, count * sizeof(RGBQUAD) );
408 pdev->bkgnd_color = pdev->dib.funcs->colorref_to_pixel( &pdev->dib, GetBkColor( dev->hdc ) );
409 update_fg_colors( pdev );
411 update_masks( pdev, GetROP2( dev->hdc ) );
413 return next->funcs->pSetDIBColorTable( next, pos, count, colors );
416 /***********************************************************************
419 static INT dibdrv_SetROP2( PHYSDEV dev, INT rop )
421 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetROP2 );
422 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
424 update_masks( pdev, rop );
426 return next->funcs->pSetROP2( next, rop );
429 const DC_FUNCTIONS dib_driver =
431 NULL, /* pAbortDoc */
432 NULL, /* pAbortPath */
433 NULL, /* pAlphaBlend */
434 NULL, /* pAngleArc */
437 NULL, /* pBeginPath */
438 NULL, /* pChoosePixelFormat */
440 NULL, /* pCloseFigure */
441 NULL, /* pCreateBitmap */
442 NULL, /* pCreateDC */
443 NULL, /* pCreateDIBSection */
444 NULL, /* pDeleteBitmap */
445 dibdrv_DeleteDC, /* pDeleteDC */
446 NULL, /* pDeleteObject */
447 NULL, /* pDescribePixelFormat */
448 NULL, /* pDeviceCapabilities */
453 NULL, /* pEnumDeviceFonts */
454 NULL, /* pEnumICMProfiles */
455 NULL, /* pExcludeClipRect */
456 NULL, /* pExtDeviceMode */
457 NULL, /* pExtEscape */
458 NULL, /* pExtFloodFill */
459 NULL, /* pExtSelectClipRgn */
460 NULL, /* pExtTextOut */
461 NULL, /* pFillPath */
463 NULL, /* pFlattenPath */
464 NULL, /* pFrameRgn */
465 NULL, /* pGdiComment */
466 NULL, /* pGetBitmapBits */
467 NULL, /* pGetCharWidth */
468 NULL, /* pGetDIBits */
469 NULL, /* pGetDeviceCaps */
470 NULL, /* pGetDeviceGammaRamp */
471 NULL, /* pGetICMProfile */
472 NULL, /* pGetImage */
473 NULL, /* pGetNearestColor */
474 NULL, /* pGetPixel */
475 NULL, /* pGetPixelFormat */
476 NULL, /* pGetSystemPaletteEntries */
477 NULL, /* pGetTextExtentExPoint */
478 NULL, /* pGetTextMetrics */
479 NULL, /* pIntersectClipRect */
480 NULL, /* pInvertRgn */
481 dibdrv_LineTo, /* pLineTo */
482 NULL, /* pModifyWorldTransform */
484 NULL, /* pOffsetClipRgn */
485 NULL, /* pOffsetViewportOrg */
486 NULL, /* pOffsetWindowOrg */
487 dibdrv_PaintRgn, /* pPaintRgn */
488 dibdrv_PatBlt, /* pPatBlt */
490 NULL, /* pPolyBezier */
491 NULL, /* pPolyBezierTo */
492 NULL, /* pPolyDraw */
493 NULL, /* pPolyPolygon */
494 NULL, /* pPolyPolyline */
496 NULL, /* pPolyline */
497 NULL, /* pPolylineTo */
498 NULL, /* pPutImage */
499 NULL, /* pRealizeDefaultPalette */
500 NULL, /* pRealizePalette */
501 dibdrv_Rectangle, /* pRectangle */
503 NULL, /* pRestoreDC */
504 NULL, /* pRoundRect */
506 NULL, /* pScaleViewportExt */
507 NULL, /* pScaleWindowExt */
508 dibdrv_SelectBitmap, /* pSelectBitmap */
509 dibdrv_SelectBrush, /* pSelectBrush */
510 NULL, /* pSelectClipPath */
511 NULL, /* pSelectFont */
512 NULL, /* pSelectPalette */
513 dibdrv_SelectPen, /* pSelectPen */
514 NULL, /* pSetArcDirection */
515 NULL, /* pSetBitmapBits */
516 dibdrv_SetBkColor, /* pSetBkColor */
517 dibdrv_SetBkMode, /* pSetBkMode */
518 dibdrv_SetDCBrushColor, /* pSetDCBrushColor */
519 dibdrv_SetDCPenColor, /* pSetDCPenColor */
520 dibdrv_SetDIBColorTable, /* pSetDIBColorTable */
521 NULL, /* pSetDIBits */
522 NULL, /* pSetDIBitsToDevice */
523 dibdrv_SetDeviceClipping, /* pSetDeviceClipping */
524 NULL, /* pSetDeviceGammaRamp */
525 NULL, /* pSetLayout */
526 NULL, /* pSetMapMode */
527 NULL, /* pSetMapperFlags */
528 NULL, /* pSetPixel */
529 NULL, /* pSetPixelFormat */
530 NULL, /* pSetPolyFillMode */
531 dibdrv_SetROP2, /* pSetROP2 */
532 NULL, /* pSetRelAbs */
533 NULL, /* pSetStretchBltMode */
534 NULL, /* pSetTextAlign */
535 NULL, /* pSetTextCharacterExtra */
536 NULL, /* pSetTextColor */
537 NULL, /* pSetTextJustification */
538 NULL, /* pSetViewportExt */
539 NULL, /* pSetViewportOrg */
540 NULL, /* pSetWindowExt */
541 NULL, /* pSetWindowOrg */
542 NULL, /* pSetWorldTransform */
543 NULL, /* pStartDoc */
544 NULL, /* pStartPage */
545 NULL, /* pStretchBlt */
546 NULL, /* pStretchDIBits */
547 NULL, /* pStrokeAndFillPath */
548 NULL, /* pStrokePath */
549 NULL, /* pSwapBuffers */
550 NULL, /* pUnrealizePalette */
551 NULL, /* pWidenPath */
552 NULL, /* pwglCopyContext */
553 NULL, /* pwglCreateContext */
554 NULL, /* pwglCreateContextAttribsARB */
555 NULL, /* pwglDeleteContext */
556 NULL, /* pwglGetPbufferDCARB */
557 NULL, /* pwglGetProcAddress */
558 NULL, /* pwglMakeContextCurrentARB */
559 NULL, /* pwglMakeCurrent */
560 NULL, /* pwglSetPixelFormatWINE */
561 NULL, /* pwglShareLists */
562 NULL, /* pwglUseFontBitmapsA */
563 NULL /* pwglUseFontBitmapsW */