gdiplus: First pen implementation.
[wine.git] / dlls / winex11.drv / brush.c
blob20f0e5263b65eb6dd4614d81e5b41c90dd4a2ad6
1 /*
2 * X11DRV brush objects
4 * Copyright 1993, 1994 Alexandre Julliard
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
21 #include "config.h"
23 #include <stdlib.h>
25 #include "wine/winbase16.h"
26 #include "x11drv.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
31 static const char HatchBrushes[][8] =
33 { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HS_HORIZONTAL */
34 { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HS_VERTICAL */
35 { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HS_FDIAGONAL */
36 { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HS_BDIAGONAL */
37 { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HS_CROSS */
38 { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 }, /* HS_DIAGCROSS */
41 /* Levels of each primary for dithering */
42 #define PRIMARY_LEVELS 3
43 #define TOTAL_LEVELS (PRIMARY_LEVELS*PRIMARY_LEVELS*PRIMARY_LEVELS)
45 /* Dithering matrix size */
46 #define MATRIX_SIZE 8
47 #define MATRIX_SIZE_2 (MATRIX_SIZE*MATRIX_SIZE)
49 /* Total number of possible levels for a dithered primary color */
50 #define DITHER_LEVELS (MATRIX_SIZE_2 * (PRIMARY_LEVELS-1) + 1)
52 /* Dithering matrix */
53 static const int dither_matrix[MATRIX_SIZE_2] =
55 0, 32, 8, 40, 2, 34, 10, 42,
56 48, 16, 56, 24, 50, 18, 58, 26,
57 12, 44, 4, 36, 14, 46, 6, 38,
58 60, 28, 52, 20, 62, 30, 54, 22,
59 3, 35, 11, 43, 1, 33, 9, 41,
60 51, 19, 59, 27, 49, 17, 57, 25,
61 15, 47, 7, 39, 13, 45, 5, 37,
62 63, 31, 55, 23, 61, 29, 53, 21
65 /* Mapping between (R,G,B) triples and EGA colors */
66 static const int EGAmapping[TOTAL_LEVELS] =
68 0, /* 000000 -> 000000 */
69 4, /* 00007f -> 000080 */
70 12, /* 0000ff -> 0000ff */
71 2, /* 007f00 -> 008000 */
72 6, /* 007f7f -> 008080 */
73 6, /* 007fff -> 008080 */
74 10, /* 00ff00 -> 00ff00 */
75 6, /* 00ff7f -> 008080 */
76 14, /* 00ffff -> 00ffff */
77 1, /* 7f0000 -> 800000 */
78 5, /* 7f007f -> 800080 */
79 5, /* 7f00ff -> 800080 */
80 3, /* 7f7f00 -> 808000 */
81 8, /* 7f7f7f -> 808080 */
82 7, /* 7f7fff -> c0c0c0 */
83 3, /* 7fff00 -> 808000 */
84 7, /* 7fff7f -> c0c0c0 */
85 7, /* 7fffff -> c0c0c0 */
86 9, /* ff0000 -> ff0000 */
87 5, /* ff007f -> 800080 */
88 13, /* ff00ff -> ff00ff */
89 3, /* ff7f00 -> 808000 */
90 7, /* ff7f7f -> c0c0c0 */
91 7, /* ff7fff -> c0c0c0 */
92 11, /* ffff00 -> ffff00 */
93 7, /* ffff7f -> c0c0c0 */
94 15 /* ffffff -> ffffff */
97 #define PIXEL_VALUE(r,g,b) \
98 X11DRV_PALETTE_mapEGAPixel[EGAmapping[((r)*PRIMARY_LEVELS+(g))*PRIMARY_LEVELS+(b)]]
100 static const COLORREF BLACK = RGB(0, 0, 0);
101 static const COLORREF WHITE = RGB(0xff, 0xff, 0xff);
103 /***********************************************************************
104 * BRUSH_DitherColor
106 static Pixmap BRUSH_DitherColor( COLORREF color )
108 /* X image for building dithered pixmap */
109 static XImage *ditherImage = NULL;
110 static COLORREF prevColor = 0xffffffff;
111 unsigned int x, y;
112 Pixmap pixmap;
114 if (!ditherImage)
116 ditherImage = X11DRV_DIB_CreateXImage( MATRIX_SIZE, MATRIX_SIZE, screen_depth );
117 if (!ditherImage)
119 ERR("Could not create dither image\n");
120 return 0;
124 wine_tsx11_lock();
125 if (color != prevColor)
127 int r = GetRValue( color ) * DITHER_LEVELS;
128 int g = GetGValue( color ) * DITHER_LEVELS;
129 int b = GetBValue( color ) * DITHER_LEVELS;
130 const int *pmatrix = dither_matrix;
132 for (y = 0; y < MATRIX_SIZE; y++)
134 for (x = 0; x < MATRIX_SIZE; x++)
136 int d = *pmatrix++ * 256;
137 int dr = ((r + d) / MATRIX_SIZE_2) / 256;
138 int dg = ((g + d) / MATRIX_SIZE_2) / 256;
139 int db = ((b + d) / MATRIX_SIZE_2) / 256;
140 XPutPixel( ditherImage, x, y, PIXEL_VALUE(dr,dg,db) );
143 prevColor = color;
146 pixmap = XCreatePixmap( gdi_display, root_window, MATRIX_SIZE, MATRIX_SIZE, screen_depth );
147 XPutImage( gdi_display, pixmap, BITMAP_colorGC, ditherImage, 0, 0,
148 0, 0, MATRIX_SIZE, MATRIX_SIZE );
149 wine_tsx11_unlock();
151 return pixmap;
155 /***********************************************************************
156 * BRUSH_DitherMono
158 static Pixmap BRUSH_DitherMono( COLORREF color )
160 /* This makes the spray work in Win 3.11 pbrush.exe */
161 /* FIXME. Extend this basic selection of dither patterns */
162 static const char gray_dither[][2] = {{ 0x1, 0x0 }, /* DKGRAY */
163 { 0x2, 0x1 }, /* GRAY */
164 { 0x1, 0x3 }, /* LTGRAY */
166 int gray = (30 * GetRValue(color) + 59 * GetGValue(color) + 11 * GetBValue(color)) / 100;
167 int idx = gray * (sizeof gray_dither/sizeof gray_dither[0] + 1)/256 - 1;
168 Pixmap pixmap;
170 TRACE("color=%06x -> gray=%x\n", color, gray);
172 wine_tsx11_lock();
173 pixmap = XCreateBitmapFromData( gdi_display, root_window,
174 gray_dither[idx],
175 2, 2 );
176 wine_tsx11_unlock();
177 return pixmap;
180 /***********************************************************************
181 * BRUSH_SelectSolidBrush
183 static void BRUSH_SelectSolidBrush( X11DRV_PDEVICE *physDev, COLORREF color )
185 if ((physDev->depth > 1) && (screen_depth <= 8) && !X11DRV_IsSolidColor( color ))
187 /* Dithered brush */
188 physDev->brush.pixmap = BRUSH_DitherColor( color );
189 physDev->brush.fillStyle = FillTiled;
190 physDev->brush.pixel = 0;
192 else if (physDev->depth == 1 && color != WHITE && color != BLACK)
194 physDev->brush.pixel = 0;
195 physDev->brush.pixmap = BRUSH_DitherMono( color );
196 physDev->brush.fillStyle = FillTiled;
198 else
200 /* Solid brush */
201 physDev->brush.pixel = X11DRV_PALETTE_ToPhysical( physDev, color );
202 physDev->brush.fillStyle = FillSolid;
207 /***********************************************************************
208 * BRUSH_SelectPatternBrush
210 static BOOL BRUSH_SelectPatternBrush( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
212 BITMAP bitmap;
213 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
215 if (!physBitmap || !GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return FALSE;
217 wine_tsx11_lock();
218 if ((physDev->depth == 1) && (physBitmap->pixmap_depth != 1))
220 /* Special case: a color pattern on a monochrome DC */
221 physDev->brush.pixmap = XCreatePixmap( gdi_display, root_window,
222 bitmap.bmWidth, bitmap.bmHeight, 1);
223 /* FIXME: should probably convert to monochrome instead */
224 XCopyPlane( gdi_display, physBitmap->pixmap, physDev->brush.pixmap,
225 BITMAP_monoGC, 0, 0, bitmap.bmWidth, bitmap.bmHeight, 0, 0, 1 );
227 else
229 physDev->brush.pixmap = XCreatePixmap( gdi_display, root_window,
230 bitmap.bmWidth, bitmap.bmHeight,
231 physBitmap->pixmap_depth );
232 XCopyArea( gdi_display, physBitmap->pixmap, physDev->brush.pixmap,
233 BITMAP_GC(physBitmap), 0, 0, bitmap.bmWidth, bitmap.bmHeight, 0, 0 );
235 wine_tsx11_unlock();
237 if (physBitmap->pixmap_depth > 1)
239 physDev->brush.fillStyle = FillTiled;
240 physDev->brush.pixel = 0; /* Ignored */
242 else
244 physDev->brush.fillStyle = FillOpaqueStippled;
245 physDev->brush.pixel = -1; /* Special case (see DC_SetupGCForBrush) */
247 return TRUE;
251 /***********************************************************************
252 * SelectBrush (X11DRV.@)
254 HBRUSH X11DRV_SelectBrush( X11DRV_PDEVICE *physDev, HBRUSH hbrush )
256 LOGBRUSH logbrush;
257 HBITMAP hBitmap;
258 BITMAPINFO * bmpInfo;
260 if (!GetObjectA( hbrush, sizeof(logbrush), &logbrush )) return 0;
262 TRACE("hdc=%p hbrush=%p\n", physDev->hdc,hbrush);
264 if (physDev->brush.pixmap)
266 wine_tsx11_lock();
267 XFreePixmap( gdi_display, physDev->brush.pixmap );
268 wine_tsx11_unlock();
269 physDev->brush.pixmap = 0;
271 physDev->brush.style = logbrush.lbStyle;
272 if (hbrush == GetStockObject( DC_BRUSH ))
273 logbrush.lbColor = GetDCBrushColor( physDev->hdc );
275 switch(logbrush.lbStyle)
277 case BS_NULL:
278 TRACE("BS_NULL\n" );
279 break;
281 case BS_SOLID:
282 TRACE("BS_SOLID\n" );
283 BRUSH_SelectSolidBrush( physDev, logbrush.lbColor );
284 break;
286 case BS_HATCHED:
287 TRACE("BS_HATCHED\n" );
288 physDev->brush.pixel = X11DRV_PALETTE_ToPhysical( physDev, logbrush.lbColor );
289 wine_tsx11_lock();
290 physDev->brush.pixmap = XCreateBitmapFromData( gdi_display, root_window,
291 HatchBrushes[logbrush.lbHatch], 8, 8 );
292 wine_tsx11_unlock();
293 physDev->brush.fillStyle = FillStippled;
294 break;
296 case BS_PATTERN:
297 TRACE("BS_PATTERN\n");
298 if (!BRUSH_SelectPatternBrush( physDev, (HBITMAP)logbrush.lbHatch )) return 0;
299 break;
301 case BS_DIBPATTERN:
302 TRACE("BS_DIBPATTERN\n");
303 if ((bmpInfo = (BITMAPINFO *) GlobalLock16( (HGLOBAL16)logbrush.lbHatch )))
305 int size = X11DRV_DIB_BitmapInfoSize( bmpInfo, logbrush.lbColor );
306 hBitmap = CreateDIBitmap( physDev->hdc, &bmpInfo->bmiHeader,
307 CBM_INIT, ((char *)bmpInfo) + size,
308 bmpInfo,
309 (WORD)logbrush.lbColor );
310 BRUSH_SelectPatternBrush( physDev, hBitmap );
311 DeleteObject( hBitmap );
312 GlobalUnlock16( (HGLOBAL16)logbrush.lbHatch );
315 break;
317 return hbrush;
321 /***********************************************************************
322 * SetDCBrushColor (X11DRV.@)
324 COLORREF X11DRV_SetDCBrushColor( X11DRV_PDEVICE *physDev, COLORREF crColor )
326 if (GetCurrentObject(physDev->hdc, OBJ_BRUSH) == GetStockObject( DC_BRUSH ))
327 BRUSH_SelectSolidBrush( physDev, crColor );
329 return crColor;