Added pow() forward.
[wine.git] / graphics / x11drv / brush.c
blobee17da104420ef92901c4f17cab1ea20d413a757
1 /*
2 * X11DRV brush objects
4 * Copyright 1993, 1994 Alexandre Julliard
5 */
7 #include "config.h"
9 #include "ts_xlib.h"
11 #include <stdlib.h>
12 #include "brush.h"
13 #include "bitmap.h"
14 #include "color.h"
15 #include "x11drv.h"
16 #include "debugtools.h"
17 #include "local.h"
19 DEFAULT_DEBUG_CHANNEL(gdi);
21 static const char HatchBrushes[NB_HATCH_STYLES + 1][8] =
23 { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HS_HORIZONTAL */
24 { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HS_VERTICAL */
25 { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HS_FDIAGONAL */
26 { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HS_BDIAGONAL */
27 { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HS_CROSS */
28 { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 }, /* HS_DIAGCROSS */
29 { 0xee, 0xbb, 0xee, 0xbb, 0xee, 0xbb, 0xee, 0xbb } /* Hack for DKGRAY */
32 /* Levels of each primary for dithering */
33 #define PRIMARY_LEVELS 3
34 #define TOTAL_LEVELS (PRIMARY_LEVELS*PRIMARY_LEVELS*PRIMARY_LEVELS)
36 /* Dithering matrix size */
37 #define MATRIX_SIZE 8
38 #define MATRIX_SIZE_2 (MATRIX_SIZE*MATRIX_SIZE)
40 /* Total number of possible levels for a dithered primary color */
41 #define DITHER_LEVELS (MATRIX_SIZE_2 * (PRIMARY_LEVELS-1) + 1)
43 /* Dithering matrix */
44 static const int dither_matrix[MATRIX_SIZE_2] =
46 0, 32, 8, 40, 2, 34, 10, 42,
47 48, 16, 56, 24, 50, 18, 58, 26,
48 12, 44, 4, 36, 14, 46, 6, 38,
49 60, 28, 52, 20, 62, 30, 54, 22,
50 3, 35, 11, 43, 1, 33, 9, 41,
51 51, 19, 59, 27, 49, 17, 57, 25,
52 15, 47, 7, 39, 13, 45, 5, 37,
53 63, 31, 55, 23, 61, 29, 53, 21
56 /* Mapping between (R,G,B) triples and EGA colors */
57 static const int EGAmapping[TOTAL_LEVELS] =
59 0, /* 000000 -> 000000 */
60 4, /* 00007f -> 000080 */
61 12, /* 0000ff -> 0000ff */
62 2, /* 007f00 -> 008000 */
63 6, /* 007f7f -> 008080 */
64 6, /* 007fff -> 008080 */
65 10, /* 00ff00 -> 00ff00 */
66 6, /* 00ff7f -> 008080 */
67 14, /* 00ffff -> 00ffff */
68 1, /* 7f0000 -> 800000 */
69 5, /* 7f007f -> 800080 */
70 5, /* 7f00ff -> 800080 */
71 3, /* 7f7f00 -> 808000 */
72 8, /* 7f7f7f -> 808080 */
73 7, /* 7f7fff -> c0c0c0 */
74 3, /* 7fff00 -> 808000 */
75 7, /* 7fff7f -> c0c0c0 */
76 7, /* 7fffff -> c0c0c0 */
77 9, /* ff0000 -> ff0000 */
78 5, /* ff007f -> 800080 */
79 13, /* ff00ff -> ff00ff */
80 3, /* ff7f00 -> 808000 */
81 7, /* ff7f7f -> c0c0c0 */
82 7, /* ff7fff -> c0c0c0 */
83 11, /* ffff00 -> ffff00 */
84 7, /* ffff7f -> c0c0c0 */
85 15 /* ffffff -> ffffff */
88 #define PIXEL_VALUE(r,g,b) \
89 X11DRV_PALETTE_mapEGAPixel[EGAmapping[((r)*PRIMARY_LEVELS+(g))*PRIMARY_LEVELS+(b)]]
91 /* X image for building dithered pixmap */
92 static XImage *ditherImage = NULL;
95 /***********************************************************************
96 * BRUSH_Init
98 * Create the X image used for dithering.
100 BOOL X11DRV_BRUSH_Init(void)
102 XCREATEIMAGE( ditherImage, MATRIX_SIZE, MATRIX_SIZE, X11DRV_GetDepth() );
103 return (ditherImage != NULL);
107 /***********************************************************************
108 * BRUSH_DitherColor
110 static Pixmap BRUSH_DitherColor( DC *dc, COLORREF color )
112 static COLORREF prevColor = 0xffffffff;
113 unsigned int x, y;
114 Pixmap pixmap;
116 EnterCriticalSection( &X11DRV_CritSection );
117 if (color != prevColor)
119 int r = GetRValue( color ) * DITHER_LEVELS;
120 int g = GetGValue( color ) * DITHER_LEVELS;
121 int b = GetBValue( color ) * DITHER_LEVELS;
122 const int *pmatrix = dither_matrix;
124 for (y = 0; y < MATRIX_SIZE; y++)
126 for (x = 0; x < MATRIX_SIZE; x++)
128 int d = *pmatrix++ * 256;
129 int dr = ((r + d) / MATRIX_SIZE_2) / 256;
130 int dg = ((g + d) / MATRIX_SIZE_2) / 256;
131 int db = ((b + d) / MATRIX_SIZE_2) / 256;
132 XPutPixel( ditherImage, x, y, PIXEL_VALUE(dr,dg,db) );
135 prevColor = color;
138 pixmap = XCreatePixmap( display, X11DRV_GetXRootWindow(),
139 MATRIX_SIZE, MATRIX_SIZE, X11DRV_GetDepth() );
140 XPutImage( display, pixmap, BITMAP_colorGC, ditherImage, 0, 0,
141 0, 0, MATRIX_SIZE, MATRIX_SIZE );
142 LeaveCriticalSection( &X11DRV_CritSection );
143 return pixmap;
147 /***********************************************************************
148 * BRUSH_SelectSolidBrush
150 static void BRUSH_SelectSolidBrush( DC *dc, COLORREF color )
152 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
154 if ((dc->bitsPerPixel > 1) && (X11DRV_GetDepth() <= 8) && !COLOR_IsSolid( color ))
156 /* Dithered brush */
157 physDev->brush.pixmap = BRUSH_DitherColor( dc, color );
158 physDev->brush.fillStyle = FillTiled;
159 physDev->brush.pixel = 0;
161 else
163 /* Solid brush */
164 physDev->brush.pixel = X11DRV_PALETTE_ToPhysical( dc, color );
165 physDev->brush.fillStyle = FillSolid;
170 /***********************************************************************
171 * BRUSH_SelectPatternBrush
173 static BOOL BRUSH_SelectPatternBrush( DC * dc, HBITMAP hbitmap )
175 BOOL ret = FALSE;
176 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
177 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
178 if (!bmp) return FALSE;
180 if(!bmp->physBitmap)
181 if(!X11DRV_CreateBitmap(hbitmap))
182 goto done;
184 if(bmp->funcs != dc->funcs) {
185 WARN("Trying to select non-X11 DDB into an X11 dc\n");
186 goto done;
189 if ((dc->bitsPerPixel == 1) && (bmp->bitmap.bmBitsPixel != 1))
191 /* Special case: a color pattern on a monochrome DC */
192 physDev->brush.pixmap = TSXCreatePixmap( display, X11DRV_GetXRootWindow(), 8, 8, 1);
193 /* FIXME: should probably convert to monochrome instead */
194 TSXCopyPlane( display, (Pixmap)bmp->physBitmap, physDev->brush.pixmap,
195 BITMAP_monoGC, 0, 0, 8, 8, 0, 0, 1 );
197 else
199 physDev->brush.pixmap = TSXCreatePixmap( display, X11DRV_GetXRootWindow(),
200 8, 8, bmp->bitmap.bmBitsPixel );
201 TSXCopyArea( display, (Pixmap)bmp->physBitmap, physDev->brush.pixmap,
202 BITMAP_GC(bmp), 0, 0, 8, 8, 0, 0 );
205 if (bmp->bitmap.bmBitsPixel > 1)
207 physDev->brush.fillStyle = FillTiled;
208 physDev->brush.pixel = 0; /* Ignored */
210 else
212 physDev->brush.fillStyle = FillOpaqueStippled;
213 physDev->brush.pixel = -1; /* Special case (see DC_SetupGCForBrush) */
215 ret = TRUE;
216 done:
217 GDI_ReleaseObj( hbitmap );
218 return ret;
224 /***********************************************************************
225 * BRUSH_SelectObject
227 HBRUSH X11DRV_BRUSH_SelectObject( DC * dc, HBRUSH hbrush, BRUSHOBJ * brush )
229 HBITMAP16 hBitmap;
230 BITMAPINFO * bmpInfo;
231 HBRUSH16 prevHandle = dc->hBrush;
232 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
234 TRACE("hdc=%04x hbrush=%04x\n",
235 dc->hSelf,hbrush);
237 dc->hBrush = hbrush;
239 if (physDev->brush.pixmap)
241 TSXFreePixmap( display, physDev->brush.pixmap );
242 physDev->brush.pixmap = 0;
244 physDev->brush.style = brush->logbrush.lbStyle;
246 switch(brush->logbrush.lbStyle)
248 case BS_NULL:
249 TRACE("BS_NULL\n" );
250 break;
252 case BS_SOLID:
253 TRACE("BS_SOLID\n" );
254 BRUSH_SelectSolidBrush( dc, brush->logbrush.lbColor );
255 break;
257 case BS_HATCHED:
258 TRACE("BS_HATCHED\n" );
259 physDev->brush.pixel = X11DRV_PALETTE_ToPhysical( dc, brush->logbrush.lbColor );
260 physDev->brush.pixmap = TSXCreateBitmapFromData( display, X11DRV_GetXRootWindow(),
261 HatchBrushes[brush->logbrush.lbHatch], 8, 8 );
262 physDev->brush.fillStyle = FillStippled;
263 break;
265 case BS_PATTERN:
266 TRACE("BS_PATTERN\n");
267 BRUSH_SelectPatternBrush( dc, (HBRUSH16)brush->logbrush.lbHatch );
268 break;
270 case BS_DIBPATTERN:
271 TRACE("BS_DIBPATTERN\n");
272 if ((bmpInfo = (BITMAPINFO *) GlobalLock16( (HGLOBAL16)brush->logbrush.lbHatch )))
274 int size = DIB_BitmapInfoSize( bmpInfo, brush->logbrush.lbColor );
275 hBitmap = CreateDIBitmap( dc->hSelf, &bmpInfo->bmiHeader,
276 CBM_INIT, ((char *)bmpInfo) + size,
277 bmpInfo,
278 (WORD)brush->logbrush.lbColor );
279 BRUSH_SelectPatternBrush( dc, hBitmap );
280 DeleteObject16( hBitmap );
281 GlobalUnlock16( (HGLOBAL16)brush->logbrush.lbHatch );
284 break;
287 return prevHandle;