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 dib->funcs = &funcs_24;
110 if(bi->biCompression == BI_RGB)
111 bit_fields = bit_fields_555;
113 init_bit_fields(dib, bit_fields);
115 if(dib->red_mask == 0x7c00 && dib->green_mask == 0x03e0 && dib->blue_mask == 0x001f)
116 dib->funcs = &funcs_555;
118 dib->funcs = &funcs_16;
122 dib->funcs = &funcs_8;
126 dib->funcs = &funcs_4;
130 dib->funcs = &funcs_1;
134 TRACE("bpp %d not supported, will forward to graphics driver.\n", dib->bit_count);
140 dib->color_table = HeapAlloc(GetProcessHeap(), 0, color_table_size * sizeof(dib->color_table[0]));
141 if(!dib->color_table) return FALSE;
142 dib->color_table_size = color_table_size;
143 memcpy(dib->color_table, color_table, color_table_size * sizeof(color_table[0]));
147 dib->color_table = NULL;
148 dib->color_table_size = 0;
154 BOOL init_dib_info_from_packed(dib_info *dib, const BITMAPINFOHEADER *bi, WORD usage, HPALETTE palette)
157 RGBQUAD *color_table = NULL, pal_table[256];
158 BYTE *ptr = (BYTE*)bi + bi->biSize;
159 int num_colors = bi->biClrUsed;
161 if(bi->biCompression == BI_BITFIELDS)
163 masks = (DWORD *)ptr;
164 ptr += 3 * sizeof(DWORD);
167 if(!num_colors && bi->biBitCount <= 8) num_colors = 1 << bi->biBitCount;
170 if(usage == DIB_PAL_COLORS)
172 PALETTEENTRY entries[256];
173 const WORD *index = (const WORD*) ptr;
174 UINT i, count = GetPaletteEntries( palette, 0, num_colors, entries );
175 for (i = 0; i < num_colors; i++, index++)
177 PALETTEENTRY *entry = &entries[*index % count];
178 pal_table[i].rgbRed = entry->peRed;
179 pal_table[i].rgbGreen = entry->peGreen;
180 pal_table[i].rgbBlue = entry->peBlue;
181 pal_table[i].rgbReserved = 0;
183 color_table = pal_table;
184 ptr += num_colors * sizeof(WORD);
188 color_table = (RGBQUAD*)ptr;
189 ptr += num_colors * sizeof(*color_table);
193 return init_dib_info(dib, bi, masks, color_table, num_colors, ptr);
196 static void clear_dib_info(dib_info *dib)
198 dib->color_table = NULL;
202 /**********************************************************************
205 * Free the resources associated with a dib and optionally the bits
207 void free_dib_info(dib_info *dib, BOOL free_bits)
209 HeapFree(GetProcessHeap(), 0, dib->color_table);
210 dib->color_table = NULL;
213 HeapFree(GetProcessHeap(), 0, dib->bits);
218 void copy_dib_color_info(dib_info *dst, const dib_info *src)
220 dst->bit_count = src->bit_count;
221 dst->red_mask = src->red_mask;
222 dst->green_mask = src->green_mask;
223 dst->blue_mask = src->blue_mask;
224 dst->red_len = src->red_len;
225 dst->green_len = src->green_len;
226 dst->blue_len = src->blue_len;
227 dst->red_shift = src->red_shift;
228 dst->green_shift = src->green_shift;
229 dst->blue_shift = src->blue_shift;
230 dst->funcs = src->funcs;
231 dst->color_table_size = src->color_table_size;
232 dst->color_table = NULL;
233 if(dst->color_table_size)
235 int size = dst->color_table_size * sizeof(dst->color_table[0]);
236 dst->color_table = HeapAlloc(GetProcessHeap(), 0, size);
237 memcpy(dst->color_table, src->color_table, size);
241 /**************************************************************
244 * Converts src into the format specified in dst.
246 * FIXME: At the moment this always creates a top-down dib,
247 * do we want to give the option of bottom-up?
249 BOOL convert_dib(dib_info *dst, const dib_info *src)
254 dst->height = src->height;
255 dst->width = src->width;
256 dst->stride = ((dst->width * dst->bit_count + 31) >> 3) & ~3;
257 dst->bits = HeapAlloc(GetProcessHeap(), 0, dst->height * dst->stride);
259 src_rect.left = src_rect.top = 0;
260 src_rect.right = src->width;
261 src_rect.bottom = src->height;
263 ret = dst->funcs->convert_to(dst, src, &src_rect);
265 if(!ret) free_dib_info(dst, TRUE);
269 static void update_fg_colors( dibdrv_physdev *pdev )
271 pdev->pen_color = get_fg_color( pdev, pdev->pen_colorref );
272 pdev->brush_color = get_fg_color( pdev, pdev->brush_colorref );
275 static void update_masks( dibdrv_physdev *pdev, INT rop )
277 calc_and_xor_masks( rop, pdev->pen_color, &pdev->pen_and, &pdev->pen_xor );
278 update_brush_rop( pdev, rop );
279 if( GetBkMode( pdev->dev.hdc ) == OPAQUE )
280 calc_and_xor_masks( rop, pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
283 /***********************************************************************
286 static BOOL CDECL dibdrv_DeleteDC( PHYSDEV dev )
288 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
289 TRACE("(%p)\n", dev);
290 DeleteObject(pdev->clip);
291 free_pattern_brush(pdev);
292 free_dib_info(&pdev->dib, FALSE);
296 /***********************************************************************
297 * dibdrv_SelectBitmap
299 static HBITMAP CDECL dibdrv_SelectBitmap( PHYSDEV dev, HBITMAP bitmap )
301 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectBitmap );
302 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
303 BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
304 TRACE("(%p, %p)\n", dev, bitmap);
309 pdev->clip = CreateRectRgn(0, 0, 0, 0);
312 clear_dib_info(&pdev->dib);
313 clear_dib_info(&pdev->brush_dib);
314 pdev->brush_and_bits = pdev->brush_xor_bits = NULL;
316 if(!init_dib_info(&pdev->dib, &bmp->dib->dsBmih, bmp->dib->dsBitfields,
317 bmp->color_table, bmp->nb_colors, bmp->dib->dsBm.bmBits))
318 pdev->defer |= DEFER_FORMAT;
320 GDI_ReleaseObj( bitmap );
322 return next->funcs->pSelectBitmap( next, bitmap );
325 /***********************************************************************
328 static COLORREF CDECL dibdrv_SetBkColor( PHYSDEV dev, COLORREF color )
330 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetBkColor );
331 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
333 pdev->bkgnd_color = pdev->dib.funcs->colorref_to_pixel( &pdev->dib, color );
335 if( GetBkMode(dev->hdc) == OPAQUE )
336 calc_and_xor_masks( GetROP2(dev->hdc), pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
339 pdev->bkgnd_and = ~0u;
343 update_fg_colors( pdev ); /* Only needed in the 1 bpp case */
345 return next->funcs->pSetBkColor( next, color );
348 /***********************************************************************
351 static INT CDECL dibdrv_SetBkMode( PHYSDEV dev, INT mode )
353 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetBkMode );
354 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
357 calc_and_xor_masks( GetROP2(dev->hdc), pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
360 pdev->bkgnd_and = ~0u;
364 return next->funcs->pSetBkMode( next, mode );
367 /***********************************************************************
368 * dibdrv_SetDeviceClipping
370 static void CDECL dibdrv_SetDeviceClipping( PHYSDEV dev, HRGN vis_rgn, HRGN clip_rgn )
372 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDeviceClipping );
373 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
374 TRACE("(%p, %p, %p)\n", dev, vis_rgn, clip_rgn);
376 CombineRgn( pdev->clip, vis_rgn, clip_rgn, clip_rgn ? RGN_AND : RGN_COPY );
377 return next->funcs->pSetDeviceClipping( next, vis_rgn, clip_rgn);
380 /***********************************************************************
381 * dibdrv_SetDIBColorTable
383 static UINT CDECL dibdrv_SetDIBColorTable( PHYSDEV dev, UINT pos, UINT count, const RGBQUAD *colors )
385 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDIBColorTable );
386 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
387 TRACE("(%p, %d, %d, %p)\n", dev, pos, count, colors);
389 if( pdev->dib.color_table && pos < pdev->dib.color_table_size )
391 if( pos + count > pdev->dib.color_table_size ) count = pdev->dib.color_table_size - pos;
392 memcpy( pdev->dib.color_table + pos, colors, count * sizeof(RGBQUAD) );
394 pdev->bkgnd_color = pdev->dib.funcs->colorref_to_pixel( &pdev->dib, GetBkColor( dev->hdc ) );
395 update_fg_colors( pdev );
397 update_masks( pdev, GetROP2( dev->hdc ) );
399 return next->funcs->pSetDIBColorTable( next, pos, count, colors );
402 /***********************************************************************
405 static INT CDECL dibdrv_SetROP2( PHYSDEV dev, INT rop )
407 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetROP2 );
408 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
410 update_masks( pdev, rop );
412 return next->funcs->pSetROP2( next, rop );
415 const DC_FUNCTIONS dib_driver =
417 NULL, /* pAbortDoc */
418 NULL, /* pAbortPath */
419 NULL, /* pAlphaBlend */
420 NULL, /* pAngleArc */
423 NULL, /* pBeginPath */
424 NULL, /* pChoosePixelFormat */
426 NULL, /* pCloseFigure */
427 NULL, /* pCreateBitmap */
428 NULL, /* pCreateDC */
429 NULL, /* pCreateDIBSection */
430 NULL, /* pDeleteBitmap */
431 dibdrv_DeleteDC, /* pDeleteDC */
432 NULL, /* pDeleteObject */
433 NULL, /* pDescribePixelFormat */
434 NULL, /* pDeviceCapabilities */
439 NULL, /* pEnumDeviceFonts */
440 NULL, /* pEnumICMProfiles */
441 NULL, /* pExcludeClipRect */
442 NULL, /* pExtDeviceMode */
443 NULL, /* pExtEscape */
444 NULL, /* pExtFloodFill */
445 NULL, /* pExtSelectClipRgn */
446 NULL, /* pExtTextOut */
447 NULL, /* pFillPath */
449 NULL, /* pFlattenPath */
450 NULL, /* pFrameRgn */
451 NULL, /* pGdiComment */
452 NULL, /* pGetBitmapBits */
453 NULL, /* pGetCharWidth */
454 NULL, /* pGetDIBits */
455 NULL, /* pGetDeviceCaps */
456 NULL, /* pGetDeviceGammaRamp */
457 NULL, /* pGetICMProfile */
458 NULL, /* pGetNearestColor */
459 NULL, /* pGetPixel */
460 NULL, /* pGetPixelFormat */
461 NULL, /* pGetSystemPaletteEntries */
462 NULL, /* pGetTextExtentExPoint */
463 NULL, /* pGetTextMetrics */
464 NULL, /* pIntersectClipRect */
465 NULL, /* pInvertRgn */
466 dibdrv_LineTo, /* pLineTo */
467 NULL, /* pModifyWorldTransform */
469 NULL, /* pOffsetClipRgn */
470 NULL, /* pOffsetViewportOrg */
471 NULL, /* pOffsetWindowOrg */
472 dibdrv_PaintRgn, /* pPaintRgn */
473 dibdrv_PatBlt, /* pPatBlt */
475 NULL, /* pPolyBezier */
476 NULL, /* pPolyBezierTo */
477 NULL, /* pPolyDraw */
478 NULL, /* pPolyPolygon */
479 NULL, /* pPolyPolyline */
481 NULL, /* pPolyline */
482 NULL, /* pPolylineTo */
483 NULL, /* pRealizeDefaultPalette */
484 NULL, /* pRealizePalette */
485 dibdrv_Rectangle, /* pRectangle */
487 NULL, /* pRestoreDC */
488 NULL, /* pRoundRect */
490 NULL, /* pScaleViewportExt */
491 NULL, /* pScaleWindowExt */
492 dibdrv_SelectBitmap, /* pSelectBitmap */
493 dibdrv_SelectBrush, /* pSelectBrush */
494 NULL, /* pSelectClipPath */
495 NULL, /* pSelectFont */
496 NULL, /* pSelectPalette */
497 dibdrv_SelectPen, /* pSelectPen */
498 NULL, /* pSetArcDirection */
499 NULL, /* pSetBitmapBits */
500 dibdrv_SetBkColor, /* pSetBkColor */
501 dibdrv_SetBkMode, /* pSetBkMode */
502 dibdrv_SetDCBrushColor, /* pSetDCBrushColor */
503 dibdrv_SetDCPenColor, /* pSetDCPenColor */
504 dibdrv_SetDIBColorTable, /* pSetDIBColorTable */
505 NULL, /* pSetDIBits */
506 NULL, /* pSetDIBitsToDevice */
507 dibdrv_SetDeviceClipping, /* pSetDeviceClipping */
508 NULL, /* pSetDeviceGammaRamp */
509 NULL, /* pSetLayout */
510 NULL, /* pSetMapMode */
511 NULL, /* pSetMapperFlags */
512 NULL, /* pSetPixel */
513 NULL, /* pSetPixelFormat */
514 NULL, /* pSetPolyFillMode */
515 dibdrv_SetROP2, /* pSetROP2 */
516 NULL, /* pSetRelAbs */
517 NULL, /* pSetStretchBltMode */
518 NULL, /* pSetTextAlign */
519 NULL, /* pSetTextCharacterExtra */
520 NULL, /* pSetTextColor */
521 NULL, /* pSetTextJustification */
522 NULL, /* pSetViewportExt */
523 NULL, /* pSetViewportOrg */
524 NULL, /* pSetWindowExt */
525 NULL, /* pSetWindowOrg */
526 NULL, /* pSetWorldTransform */
527 NULL, /* pStartDoc */
528 NULL, /* pStartPage */
529 NULL, /* pStretchBlt */
530 NULL, /* pStretchDIBits */
531 NULL, /* pStrokeAndFillPath */
532 NULL, /* pStrokePath */
533 NULL, /* pSwapBuffers */
534 NULL, /* pUnrealizePalette */
535 NULL, /* pWidenPath */
536 NULL, /* pwglCopyContext */
537 NULL, /* pwglCreateContext */
538 NULL, /* pwglCreateContextAttribsARB */
539 NULL, /* pwglDeleteContext */
540 NULL, /* pwglGetProcAddress */
541 NULL, /* pwglGetPbufferDCARB */
542 NULL, /* pwglMakeCurrent */
543 NULL, /* pwglMakeContextCurrentARB */
544 NULL, /* pwglSetPixelFormatWINE */
545 NULL, /* pwglShareLists */
546 NULL, /* pwglUseFontBitmapsA */
547 NULL /* pwglUseFontBitmapsW */