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 const DWORD bit_fields_888[3] = {0xff0000, 0x00ff00, 0x0000ff};
31 static const DWORD bit_fields_555[3] = {0x7c00, 0x03e0, 0x001f};
33 static void calc_shift_and_len(DWORD mask, int *shift, int *len)
44 while ((mask & 1) == 0)
50 while ((mask & 1) == 1)
59 static void init_bit_fields(dib_info *dib, const DWORD *bit_fields)
61 dib->red_mask = bit_fields[0];
62 dib->green_mask = bit_fields[1];
63 dib->blue_mask = bit_fields[2];
64 calc_shift_and_len(dib->red_mask, &dib->red_shift, &dib->red_len);
65 calc_shift_and_len(dib->green_mask, &dib->green_shift, &dib->green_len);
66 calc_shift_and_len(dib->blue_mask, &dib->blue_shift, &dib->blue_len);
69 static BOOL init_dib_info(dib_info *dib, const BITMAPINFOHEADER *bi, const DWORD *bit_fields,
70 RGBQUAD *color_table, int color_table_size, void *bits, enum dib_info_flags flags)
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 = get_dib_num_of_colors( (const BITMAPINFO *)bi );
168 if(bi->biCompression == BI_BITFIELDS)
170 masks = (DWORD *)ptr;
171 ptr += 3 * sizeof(DWORD);
176 if(usage == DIB_PAL_COLORS)
178 PALETTEENTRY entries[256];
179 const WORD *index = (const WORD*) ptr;
180 UINT i, count = GetPaletteEntries( palette, 0, num_colors, entries );
181 for (i = 0; i < num_colors; i++, index++)
183 PALETTEENTRY *entry = &entries[*index % count];
184 pal_table[i].rgbRed = entry->peRed;
185 pal_table[i].rgbGreen = entry->peGreen;
186 pal_table[i].rgbBlue = entry->peBlue;
187 pal_table[i].rgbReserved = 0;
189 color_table = pal_table;
190 ptr += num_colors * sizeof(WORD);
194 color_table = (RGBQUAD*)ptr;
195 ptr += num_colors * sizeof(*color_table);
199 return init_dib_info(dib, bi, masks, color_table, num_colors, ptr, private_color_table);
202 BOOL init_dib_info_from_bitmapinfo(dib_info *dib, const BITMAPINFO *info, void *bits, enum dib_info_flags flags)
204 unsigned int colors = get_dib_num_of_colors( info );
205 void *colorptr = (char *)&info->bmiHeader + info->bmiHeader.biSize;
206 const DWORD *bitfields = (info->bmiHeader.biCompression == BI_BITFIELDS) ? (DWORD *)colorptr : NULL;
208 return init_dib_info( dib, &info->bmiHeader, bitfields, colors ? colorptr : NULL, colors, bits, flags );
211 static void clear_dib_info(dib_info *dib)
213 dib->color_table = NULL;
215 dib->ptr_to_free = NULL;
218 /**********************************************************************
221 * Free the resources associated with a dib and optionally the bits
223 void free_dib_info(dib_info *dib)
225 if (dib->flags & private_color_table)
226 HeapFree(GetProcessHeap(), 0, dib->color_table);
227 dib->color_table = NULL;
229 HeapFree(GetProcessHeap(), 0, dib->ptr_to_free);
230 dib->ptr_to_free = NULL;
234 void copy_dib_color_info(dib_info *dst, const dib_info *src)
236 dst->bit_count = src->bit_count;
237 dst->red_mask = src->red_mask;
238 dst->green_mask = src->green_mask;
239 dst->blue_mask = src->blue_mask;
240 dst->red_len = src->red_len;
241 dst->green_len = src->green_len;
242 dst->blue_len = src->blue_len;
243 dst->red_shift = src->red_shift;
244 dst->green_shift = src->green_shift;
245 dst->blue_shift = src->blue_shift;
246 dst->funcs = src->funcs;
247 dst->color_table_size = src->color_table_size;
248 dst->color_table = NULL;
249 dst->flags = src->flags;
250 if(dst->color_table_size)
252 int size = dst->color_table_size * sizeof(dst->color_table[0]);
253 if (dst->flags & private_color_table)
255 dst->color_table = HeapAlloc(GetProcessHeap(), 0, size);
256 memcpy(dst->color_table, src->color_table, size);
259 dst->color_table = src->color_table;
263 DWORD convert_bitmapinfo( const BITMAPINFO *src_info, void *src_bits, const RECT *src_rect,
264 const BITMAPINFO *dst_info, void *dst_bits )
266 dib_info src_dib, dst_dib;
269 if ( !init_dib_info_from_bitmapinfo( &src_dib, src_info, src_bits, 0 ) )
270 return ERROR_BAD_FORMAT;
271 if ( !init_dib_info_from_bitmapinfo( &dst_dib, dst_info, dst_bits, 0 ) )
272 return ERROR_BAD_FORMAT;
274 ret = dst_dib.funcs->convert_to( &dst_dib, &src_dib, src_rect );
276 /* We shared the color tables, so there's no need to free the dib_infos here */
278 if(!ret) return ERROR_BAD_FORMAT;
279 return ERROR_SUCCESS;
282 static void update_fg_colors( dibdrv_physdev *pdev )
284 pdev->pen_color = get_fg_color( pdev, pdev->pen_colorref );
285 pdev->brush_color = get_fg_color( pdev, pdev->brush_colorref );
288 static void update_masks( dibdrv_physdev *pdev, INT rop )
290 calc_and_xor_masks( rop, pdev->pen_color, &pdev->pen_and, &pdev->pen_xor );
291 update_brush_rop( pdev, rop );
292 if( GetBkMode( pdev->dev.hdc ) == OPAQUE )
293 calc_and_xor_masks( rop, pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
296 /***********************************************************************
299 static BOOL dibdrv_DeleteDC( PHYSDEV dev )
301 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
302 TRACE("(%p)\n", dev);
303 DeleteObject(pdev->clip);
304 free_pattern_brush(pdev);
305 free_dib_info(&pdev->dib);
309 /***********************************************************************
312 static DWORD dibdrv_GetImage( PHYSDEV dev, HBITMAP hbitmap, BITMAPINFO *info,
313 struct gdi_image_bits *bits, struct bitblt_coords *src )
315 TRACE( "%p %p %p\n", dev, hbitmap, info );
317 info->bmiHeader.biSize = sizeof(info->bmiHeader);
318 info->bmiHeader.biPlanes = 1;
319 info->bmiHeader.biCompression = BI_RGB;
320 info->bmiHeader.biXPelsPerMeter = 0;
321 info->bmiHeader.biYPelsPerMeter = 0;
322 info->bmiHeader.biClrUsed = 0;
323 info->bmiHeader.biClrImportant = 0;
327 BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
329 if (!bmp) return ERROR_INVALID_HANDLE;
332 info->bmiHeader.biWidth = bmp->dib->dsBmih.biWidth;
333 info->bmiHeader.biHeight = bmp->dib->dsBmih.biHeight;
334 info->bmiHeader.biBitCount = bmp->dib->dsBmih.biBitCount;
335 info->bmiHeader.biSizeImage = get_dib_image_size( (BITMAPINFO *)&bmp->dib->dsBmih );
337 switch (info->bmiHeader.biBitCount)
342 if (bmp->color_table)
344 info->bmiHeader.biClrUsed = min( bmp->nb_colors, 1 << info->bmiHeader.biBitCount );
345 memcpy( info->bmiColors, bmp->color_table, info->bmiHeader.biClrUsed * sizeof(RGBQUAD) );
349 if (bmp->dib->dsBmih.biCompression == BI_BITFIELDS &&
350 memcmp( bmp->dib->dsBitfields, bit_fields_555, sizeof(bmp->dib->dsBitfields) ))
352 info->bmiHeader.biCompression = BI_BITFIELDS;
353 memcpy( info->bmiColors, bmp->dib->dsBitfields, sizeof(bmp->dib->dsBitfields) );
357 if (bmp->dib->dsBmih.biCompression == BI_BITFIELDS &&
358 memcmp( bmp->dib->dsBitfields, bit_fields_888, sizeof(bmp->dib->dsBitfields) ))
360 info->bmiHeader.biCompression = BI_BITFIELDS;
361 memcpy( info->bmiColors, bmp->dib->dsBitfields, sizeof(bmp->dib->dsBitfields) );
367 bits->ptr = bmp->dib->dsBm.bmBits;
368 bits->is_copy = FALSE;
371 GDI_ReleaseObj( hbitmap );
375 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
377 info->bmiHeader.biWidth = pdev->dib.width;
378 info->bmiHeader.biHeight = pdev->dib.stride > 0 ? -pdev->dib.height : pdev->dib.height;
379 info->bmiHeader.biBitCount = pdev->dib.bit_count;
380 info->bmiHeader.biSizeImage = pdev->dib.height * abs(pdev->dib.stride);
382 switch (info->bmiHeader.biBitCount)
387 if (pdev->dib.color_table)
389 info->bmiHeader.biClrUsed = min( pdev->dib.color_table_size, 1 << pdev->dib.bit_count );
390 memcpy( info->bmiColors, pdev->dib.color_table,
391 info->bmiHeader.biClrUsed * sizeof(RGBQUAD) );
395 if (pdev->dib.funcs != &funcs_555)
397 DWORD *masks = (DWORD *)info->bmiColors;
398 masks[0] = pdev->dib.red_mask;
399 masks[1] = pdev->dib.green_mask;
400 masks[2] = pdev->dib.blue_mask;
401 info->bmiHeader.biCompression = BI_BITFIELDS;
405 if (pdev->dib.funcs != &funcs_8888)
407 DWORD *masks = (DWORD *)info->bmiColors;
408 masks[0] = pdev->dib.red_mask;
409 masks[1] = pdev->dib.green_mask;
410 masks[2] = pdev->dib.blue_mask;
411 info->bmiHeader.biCompression = BI_BITFIELDS;
417 bits->ptr = pdev->dib.bits;
418 if (pdev->dib.stride < 0)
419 bits->ptr = (char *)bits->ptr + (pdev->dib.height - 1) * pdev->dib.stride;
420 bits->is_copy = FALSE;
424 return ERROR_SUCCESS;
427 /***********************************************************************
428 * dibdrv_SelectBitmap
430 static HBITMAP dibdrv_SelectBitmap( PHYSDEV dev, HBITMAP bitmap )
432 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectBitmap );
433 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
434 BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
435 TRACE("(%p, %p)\n", dev, bitmap);
440 pdev->clip = CreateRectRgn(0, 0, 0, 0);
443 clear_dib_info(&pdev->dib);
444 clear_dib_info(&pdev->brush_dib);
445 pdev->brush_and_bits = pdev->brush_xor_bits = NULL;
447 if(!init_dib_info(&pdev->dib, &bmp->dib->dsBmih, bmp->dib->dsBitfields,
448 bmp->color_table, bmp->nb_colors, bmp->dib->dsBm.bmBits, private_color_table))
449 pdev->defer |= DEFER_FORMAT;
451 GDI_ReleaseObj( bitmap );
453 return next->funcs->pSelectBitmap( next, bitmap );
456 /***********************************************************************
459 static COLORREF dibdrv_SetBkColor( PHYSDEV dev, COLORREF color )
461 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetBkColor );
462 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
464 pdev->bkgnd_color = pdev->dib.funcs->colorref_to_pixel( &pdev->dib, color );
466 if( GetBkMode(dev->hdc) == OPAQUE )
467 calc_and_xor_masks( GetROP2(dev->hdc), pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
470 pdev->bkgnd_and = ~0u;
474 update_fg_colors( pdev ); /* Only needed in the 1 bpp case */
476 return next->funcs->pSetBkColor( next, color );
479 /***********************************************************************
482 static INT dibdrv_SetBkMode( PHYSDEV dev, INT mode )
484 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetBkMode );
485 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
488 calc_and_xor_masks( GetROP2(dev->hdc), pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
491 pdev->bkgnd_and = ~0u;
495 return next->funcs->pSetBkMode( next, mode );
498 /***********************************************************************
499 * dibdrv_SetDeviceClipping
501 static void dibdrv_SetDeviceClipping( PHYSDEV dev, HRGN vis_rgn, HRGN clip_rgn )
503 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDeviceClipping );
504 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
505 TRACE("(%p, %p, %p)\n", dev, vis_rgn, clip_rgn);
507 CombineRgn( pdev->clip, vis_rgn, clip_rgn, clip_rgn ? RGN_AND : RGN_COPY );
508 return next->funcs->pSetDeviceClipping( next, vis_rgn, clip_rgn);
511 /***********************************************************************
512 * dibdrv_SetDIBColorTable
514 static UINT dibdrv_SetDIBColorTable( PHYSDEV dev, UINT pos, UINT count, const RGBQUAD *colors )
516 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDIBColorTable );
517 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
518 TRACE("(%p, %d, %d, %p)\n", dev, pos, count, colors);
520 if( pdev->dib.color_table && pos < pdev->dib.color_table_size )
522 if( pos + count > pdev->dib.color_table_size ) count = pdev->dib.color_table_size - pos;
523 memcpy( pdev->dib.color_table + pos, colors, count * sizeof(RGBQUAD) );
525 pdev->bkgnd_color = pdev->dib.funcs->colorref_to_pixel( &pdev->dib, GetBkColor( dev->hdc ) );
526 update_fg_colors( pdev );
528 update_masks( pdev, GetROP2( dev->hdc ) );
530 return next->funcs->pSetDIBColorTable( next, pos, count, colors );
533 /***********************************************************************
536 static INT dibdrv_SetROP2( PHYSDEV dev, INT rop )
538 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetROP2 );
539 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
541 update_masks( pdev, rop );
543 return next->funcs->pSetROP2( next, rop );
546 const DC_FUNCTIONS dib_driver =
548 NULL, /* pAbortDoc */
549 NULL, /* pAbortPath */
550 NULL, /* pAlphaBlend */
551 NULL, /* pAngleArc */
554 NULL, /* pBeginPath */
555 NULL, /* pChoosePixelFormat */
557 NULL, /* pCloseFigure */
558 NULL, /* pCreateBitmap */
559 NULL, /* pCreateDC */
560 NULL, /* pCreateDIBSection */
561 NULL, /* pDeleteBitmap */
562 dibdrv_DeleteDC, /* pDeleteDC */
563 NULL, /* pDeleteObject */
564 NULL, /* pDescribePixelFormat */
565 NULL, /* pDeviceCapabilities */
570 NULL, /* pEnumDeviceFonts */
571 NULL, /* pEnumICMProfiles */
572 NULL, /* pExcludeClipRect */
573 NULL, /* pExtDeviceMode */
574 NULL, /* pExtEscape */
575 NULL, /* pExtFloodFill */
576 NULL, /* pExtSelectClipRgn */
577 NULL, /* pExtTextOut */
578 NULL, /* pFillPath */
580 NULL, /* pFlattenPath */
581 NULL, /* pFrameRgn */
582 NULL, /* pGdiComment */
583 NULL, /* pGetCharWidth */
584 NULL, /* pGetDeviceCaps */
585 NULL, /* pGetDeviceGammaRamp */
586 NULL, /* pGetICMProfile */
587 dibdrv_GetImage, /* pGetImage */
588 NULL, /* pGetNearestColor */
589 NULL, /* pGetPixel */
590 NULL, /* pGetPixelFormat */
591 NULL, /* pGetSystemPaletteEntries */
592 NULL, /* pGetTextExtentExPoint */
593 NULL, /* pGetTextMetrics */
594 NULL, /* pIntersectClipRect */
595 NULL, /* pInvertRgn */
596 dibdrv_LineTo, /* pLineTo */
597 NULL, /* pModifyWorldTransform */
599 NULL, /* pOffsetClipRgn */
600 NULL, /* pOffsetViewportOrg */
601 NULL, /* pOffsetWindowOrg */
602 dibdrv_PaintRgn, /* pPaintRgn */
603 dibdrv_PatBlt, /* pPatBlt */
605 NULL, /* pPolyBezier */
606 NULL, /* pPolyBezierTo */
607 NULL, /* pPolyDraw */
608 NULL, /* pPolyPolygon */
609 NULL, /* pPolyPolyline */
611 NULL, /* pPolyline */
612 NULL, /* pPolylineTo */
613 NULL, /* pPutImage */
614 NULL, /* pRealizeDefaultPalette */
615 NULL, /* pRealizePalette */
616 dibdrv_Rectangle, /* pRectangle */
618 NULL, /* pRestoreDC */
619 NULL, /* pRoundRect */
621 NULL, /* pScaleViewportExt */
622 NULL, /* pScaleWindowExt */
623 dibdrv_SelectBitmap, /* pSelectBitmap */
624 dibdrv_SelectBrush, /* pSelectBrush */
625 NULL, /* pSelectClipPath */
626 NULL, /* pSelectFont */
627 NULL, /* pSelectPalette */
628 dibdrv_SelectPen, /* pSelectPen */
629 NULL, /* pSetArcDirection */
630 NULL, /* pSetBitmapBits */
631 dibdrv_SetBkColor, /* pSetBkColor */
632 dibdrv_SetBkMode, /* pSetBkMode */
633 dibdrv_SetDCBrushColor, /* pSetDCBrushColor */
634 dibdrv_SetDCPenColor, /* pSetDCPenColor */
635 dibdrv_SetDIBColorTable, /* pSetDIBColorTable */
636 NULL, /* pSetDIBitsToDevice */
637 dibdrv_SetDeviceClipping, /* pSetDeviceClipping */
638 NULL, /* pSetDeviceGammaRamp */
639 NULL, /* pSetLayout */
640 NULL, /* pSetMapMode */
641 NULL, /* pSetMapperFlags */
642 NULL, /* pSetPixel */
643 NULL, /* pSetPixelFormat */
644 NULL, /* pSetPolyFillMode */
645 dibdrv_SetROP2, /* pSetROP2 */
646 NULL, /* pSetRelAbs */
647 NULL, /* pSetStretchBltMode */
648 NULL, /* pSetTextAlign */
649 NULL, /* pSetTextCharacterExtra */
650 NULL, /* pSetTextColor */
651 NULL, /* pSetTextJustification */
652 NULL, /* pSetViewportExt */
653 NULL, /* pSetViewportOrg */
654 NULL, /* pSetWindowExt */
655 NULL, /* pSetWindowOrg */
656 NULL, /* pSetWorldTransform */
657 NULL, /* pStartDoc */
658 NULL, /* pStartPage */
659 NULL, /* pStretchBlt */
660 NULL, /* pStretchDIBits */
661 NULL, /* pStrokeAndFillPath */
662 NULL, /* pStrokePath */
663 NULL, /* pSwapBuffers */
664 NULL, /* pUnrealizePalette */
665 NULL, /* pWidenPath */
666 NULL, /* pwglCopyContext */
667 NULL, /* pwglCreateContext */
668 NULL, /* pwglCreateContextAttribsARB */
669 NULL, /* pwglDeleteContext */
670 NULL, /* pwglGetPbufferDCARB */
671 NULL, /* pwglGetProcAddress */
672 NULL, /* pwglMakeContextCurrentARB */
673 NULL, /* pwglMakeCurrent */
674 NULL, /* pwglSetPixelFormatWINE */
675 NULL, /* pwglShareLists */
676 NULL, /* pwglUseFontBitmapsA */
677 NULL /* pwglUseFontBitmapsW */