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 = get_dib_stride( dib->width, dib->bit_count );
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 BOOL init_dib_info_from_bitmapinfo(dib_info *dib, const BITMAPINFO *info, void *bits, enum dib_info_flags flags)
205 unsigned int colors = 0;
206 void *colorptr = (char *)&info->bmiHeader + info->bmiHeader.biSize;
207 const DWORD *bitfields = (info->bmiHeader.biCompression == BI_BITFIELDS) ? (DWORD *)colorptr : NULL;
209 if (info->bmiHeader.biBitCount <= 8)
211 colors = 1 << info->bmiHeader.biBitCount;
212 if (info->bmiHeader.biClrUsed) colors = info->bmiHeader.biClrUsed;
214 return init_dib_info( dib, &info->bmiHeader, bitfields, colors ? colorptr : NULL, colors, bits, flags );
217 static void clear_dib_info(dib_info *dib)
219 dib->color_table = NULL;
221 dib->ptr_to_free = NULL;
224 /**********************************************************************
227 * Free the resources associated with a dib and optionally the bits
229 void free_dib_info(dib_info *dib)
231 if (dib->flags & private_color_table)
232 HeapFree(GetProcessHeap(), 0, dib->color_table);
233 dib->color_table = NULL;
235 HeapFree(GetProcessHeap(), 0, dib->ptr_to_free);
236 dib->ptr_to_free = NULL;
240 void copy_dib_color_info(dib_info *dst, const dib_info *src)
242 dst->bit_count = src->bit_count;
243 dst->red_mask = src->red_mask;
244 dst->green_mask = src->green_mask;
245 dst->blue_mask = src->blue_mask;
246 dst->red_len = src->red_len;
247 dst->green_len = src->green_len;
248 dst->blue_len = src->blue_len;
249 dst->red_shift = src->red_shift;
250 dst->green_shift = src->green_shift;
251 dst->blue_shift = src->blue_shift;
252 dst->funcs = src->funcs;
253 dst->color_table_size = src->color_table_size;
254 dst->color_table = NULL;
255 dst->flags = src->flags;
256 if(dst->color_table_size)
258 int size = dst->color_table_size * sizeof(dst->color_table[0]);
259 if (dst->flags & private_color_table)
261 dst->color_table = HeapAlloc(GetProcessHeap(), 0, size);
262 memcpy(dst->color_table, src->color_table, size);
265 dst->color_table = src->color_table;
269 DWORD convert_bitmapinfo( const BITMAPINFO *src_info, void *src_bits, const RECT *src_rect,
270 const BITMAPINFO *dst_info, void *dst_bits )
272 dib_info src_dib, dst_dib;
275 if ( !init_dib_info_from_bitmapinfo( &src_dib, src_info, src_bits, 0 ) )
276 return ERROR_BAD_FORMAT;
277 if ( !init_dib_info_from_bitmapinfo( &dst_dib, dst_info, dst_bits, 0 ) )
278 return ERROR_BAD_FORMAT;
280 ret = dst_dib.funcs->convert_to( &dst_dib, &src_dib, src_rect );
282 /* We shared the color tables, so there's no need to free the dib_infos here */
284 if(!ret) return ERROR_BAD_FORMAT;
285 return ERROR_SUCCESS;
288 static void update_fg_colors( dibdrv_physdev *pdev )
290 pdev->pen_color = get_fg_color( pdev, pdev->pen_colorref );
291 pdev->brush_color = get_fg_color( pdev, pdev->brush_colorref );
294 static void update_masks( dibdrv_physdev *pdev, INT rop )
296 calc_and_xor_masks( rop, pdev->pen_color, &pdev->pen_and, &pdev->pen_xor );
297 update_brush_rop( pdev, rop );
298 if( GetBkMode( pdev->dev.hdc ) == OPAQUE )
299 calc_and_xor_masks( rop, pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
302 /***********************************************************************
305 static BOOL dibdrv_DeleteDC( PHYSDEV dev )
307 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
308 TRACE("(%p)\n", dev);
309 DeleteObject(pdev->clip);
310 free_pattern_brush(pdev);
311 free_dib_info(&pdev->dib);
315 /***********************************************************************
316 * dibdrv_SelectBitmap
318 static HBITMAP dibdrv_SelectBitmap( PHYSDEV dev, HBITMAP bitmap )
320 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectBitmap );
321 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
322 BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
323 TRACE("(%p, %p)\n", dev, bitmap);
328 pdev->clip = CreateRectRgn(0, 0, 0, 0);
331 clear_dib_info(&pdev->dib);
332 clear_dib_info(&pdev->brush_dib);
333 pdev->brush_and_bits = pdev->brush_xor_bits = NULL;
335 if(!init_dib_info(&pdev->dib, &bmp->dib->dsBmih, bmp->dib->dsBitfields,
336 bmp->color_table, bmp->nb_colors, bmp->dib->dsBm.bmBits, private_color_table))
337 pdev->defer |= DEFER_FORMAT;
339 GDI_ReleaseObj( bitmap );
341 return next->funcs->pSelectBitmap( next, bitmap );
344 /***********************************************************************
347 static COLORREF dibdrv_SetBkColor( PHYSDEV dev, COLORREF color )
349 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetBkColor );
350 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
352 pdev->bkgnd_color = pdev->dib.funcs->colorref_to_pixel( &pdev->dib, color );
354 if( GetBkMode(dev->hdc) == OPAQUE )
355 calc_and_xor_masks( GetROP2(dev->hdc), pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
358 pdev->bkgnd_and = ~0u;
362 update_fg_colors( pdev ); /* Only needed in the 1 bpp case */
364 return next->funcs->pSetBkColor( next, color );
367 /***********************************************************************
370 static INT dibdrv_SetBkMode( PHYSDEV dev, INT mode )
372 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetBkMode );
373 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
376 calc_and_xor_masks( GetROP2(dev->hdc), pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
379 pdev->bkgnd_and = ~0u;
383 return next->funcs->pSetBkMode( next, mode );
386 /***********************************************************************
387 * dibdrv_SetDeviceClipping
389 static void dibdrv_SetDeviceClipping( PHYSDEV dev, HRGN vis_rgn, HRGN clip_rgn )
391 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDeviceClipping );
392 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
393 TRACE("(%p, %p, %p)\n", dev, vis_rgn, clip_rgn);
395 CombineRgn( pdev->clip, vis_rgn, clip_rgn, clip_rgn ? RGN_AND : RGN_COPY );
396 return next->funcs->pSetDeviceClipping( next, vis_rgn, clip_rgn);
399 /***********************************************************************
400 * dibdrv_SetDIBColorTable
402 static UINT dibdrv_SetDIBColorTable( PHYSDEV dev, UINT pos, UINT count, const RGBQUAD *colors )
404 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDIBColorTable );
405 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
406 TRACE("(%p, %d, %d, %p)\n", dev, pos, count, colors);
408 if( pdev->dib.color_table && pos < pdev->dib.color_table_size )
410 if( pos + count > pdev->dib.color_table_size ) count = pdev->dib.color_table_size - pos;
411 memcpy( pdev->dib.color_table + pos, colors, count * sizeof(RGBQUAD) );
413 pdev->bkgnd_color = pdev->dib.funcs->colorref_to_pixel( &pdev->dib, GetBkColor( dev->hdc ) );
414 update_fg_colors( pdev );
416 update_masks( pdev, GetROP2( dev->hdc ) );
418 return next->funcs->pSetDIBColorTable( next, pos, count, colors );
421 /***********************************************************************
424 static INT dibdrv_SetROP2( PHYSDEV dev, INT rop )
426 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetROP2 );
427 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
429 update_masks( pdev, rop );
431 return next->funcs->pSetROP2( next, rop );
434 const DC_FUNCTIONS dib_driver =
436 NULL, /* pAbortDoc */
437 NULL, /* pAbortPath */
438 NULL, /* pAlphaBlend */
439 NULL, /* pAngleArc */
442 NULL, /* pBeginPath */
443 NULL, /* pChoosePixelFormat */
445 NULL, /* pCloseFigure */
446 NULL, /* pCreateBitmap */
447 NULL, /* pCreateDC */
448 NULL, /* pCreateDIBSection */
449 NULL, /* pDeleteBitmap */
450 dibdrv_DeleteDC, /* pDeleteDC */
451 NULL, /* pDeleteObject */
452 NULL, /* pDescribePixelFormat */
453 NULL, /* pDeviceCapabilities */
458 NULL, /* pEnumDeviceFonts */
459 NULL, /* pEnumICMProfiles */
460 NULL, /* pExcludeClipRect */
461 NULL, /* pExtDeviceMode */
462 NULL, /* pExtEscape */
463 NULL, /* pExtFloodFill */
464 NULL, /* pExtSelectClipRgn */
465 NULL, /* pExtTextOut */
466 NULL, /* pFillPath */
468 NULL, /* pFlattenPath */
469 NULL, /* pFrameRgn */
470 NULL, /* pGdiComment */
471 NULL, /* pGetBitmapBits */
472 NULL, /* pGetCharWidth */
473 NULL, /* pGetDeviceCaps */
474 NULL, /* pGetDeviceGammaRamp */
475 NULL, /* pGetICMProfile */
476 NULL, /* pGetImage */
477 NULL, /* pGetNearestColor */
478 NULL, /* pGetPixel */
479 NULL, /* pGetPixelFormat */
480 NULL, /* pGetSystemPaletteEntries */
481 NULL, /* pGetTextExtentExPoint */
482 NULL, /* pGetTextMetrics */
483 NULL, /* pIntersectClipRect */
484 NULL, /* pInvertRgn */
485 dibdrv_LineTo, /* pLineTo */
486 NULL, /* pModifyWorldTransform */
488 NULL, /* pOffsetClipRgn */
489 NULL, /* pOffsetViewportOrg */
490 NULL, /* pOffsetWindowOrg */
491 dibdrv_PaintRgn, /* pPaintRgn */
492 dibdrv_PatBlt, /* pPatBlt */
494 NULL, /* pPolyBezier */
495 NULL, /* pPolyBezierTo */
496 NULL, /* pPolyDraw */
497 NULL, /* pPolyPolygon */
498 NULL, /* pPolyPolyline */
500 NULL, /* pPolyline */
501 NULL, /* pPolylineTo */
502 NULL, /* pPutImage */
503 NULL, /* pRealizeDefaultPalette */
504 NULL, /* pRealizePalette */
505 dibdrv_Rectangle, /* pRectangle */
507 NULL, /* pRestoreDC */
508 NULL, /* pRoundRect */
510 NULL, /* pScaleViewportExt */
511 NULL, /* pScaleWindowExt */
512 dibdrv_SelectBitmap, /* pSelectBitmap */
513 dibdrv_SelectBrush, /* pSelectBrush */
514 NULL, /* pSelectClipPath */
515 NULL, /* pSelectFont */
516 NULL, /* pSelectPalette */
517 dibdrv_SelectPen, /* pSelectPen */
518 NULL, /* pSetArcDirection */
519 NULL, /* pSetBitmapBits */
520 dibdrv_SetBkColor, /* pSetBkColor */
521 dibdrv_SetBkMode, /* pSetBkMode */
522 dibdrv_SetDCBrushColor, /* pSetDCBrushColor */
523 dibdrv_SetDCPenColor, /* pSetDCPenColor */
524 dibdrv_SetDIBColorTable, /* pSetDIBColorTable */
525 NULL, /* pSetDIBitsToDevice */
526 dibdrv_SetDeviceClipping, /* pSetDeviceClipping */
527 NULL, /* pSetDeviceGammaRamp */
528 NULL, /* pSetLayout */
529 NULL, /* pSetMapMode */
530 NULL, /* pSetMapperFlags */
531 NULL, /* pSetPixel */
532 NULL, /* pSetPixelFormat */
533 NULL, /* pSetPolyFillMode */
534 dibdrv_SetROP2, /* pSetROP2 */
535 NULL, /* pSetRelAbs */
536 NULL, /* pSetStretchBltMode */
537 NULL, /* pSetTextAlign */
538 NULL, /* pSetTextCharacterExtra */
539 NULL, /* pSetTextColor */
540 NULL, /* pSetTextJustification */
541 NULL, /* pSetViewportExt */
542 NULL, /* pSetViewportOrg */
543 NULL, /* pSetWindowExt */
544 NULL, /* pSetWindowOrg */
545 NULL, /* pSetWorldTransform */
546 NULL, /* pStartDoc */
547 NULL, /* pStartPage */
548 NULL, /* pStretchBlt */
549 NULL, /* pStretchDIBits */
550 NULL, /* pStrokeAndFillPath */
551 NULL, /* pStrokePath */
552 NULL, /* pSwapBuffers */
553 NULL, /* pUnrealizePalette */
554 NULL, /* pWidenPath */
555 NULL, /* pwglCopyContext */
556 NULL, /* pwglCreateContext */
557 NULL, /* pwglCreateContextAttribsARB */
558 NULL, /* pwglDeleteContext */
559 NULL, /* pwglGetPbufferDCARB */
560 NULL, /* pwglGetProcAddress */
561 NULL, /* pwglMakeContextCurrentARB */
562 NULL, /* pwglMakeCurrent */
563 NULL, /* pwglSetPixelFormatWINE */
564 NULL, /* pwglShareLists */
565 NULL, /* pwglUseFontBitmapsA */
566 NULL /* pwglUseFontBitmapsW */