shell32: Indentation fix.
[wine] / dlls / winex11.drv / brush.c
1 /*
2  * X11DRV brush objects
3  *
4  * Copyright 1993, 1994  Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22
23 #include <stdlib.h>
24
25 #include "x11drv.h"
26 #include "wine/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
29
30 static const char HatchBrushes[][8] =
31 {
32     { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HS_HORIZONTAL */
33     { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HS_VERTICAL   */
34     { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HS_FDIAGONAL  */
35     { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HS_BDIAGONAL  */
36     { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HS_CROSS      */
37     { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 }, /* HS_DIAGCROSS  */
38 };
39
40   /* Levels of each primary for dithering */
41 #define PRIMARY_LEVELS  3
42 #define TOTAL_LEVELS    (PRIMARY_LEVELS*PRIMARY_LEVELS*PRIMARY_LEVELS)
43
44  /* Dithering matrix size  */
45 #define MATRIX_SIZE     8
46 #define MATRIX_SIZE_2   (MATRIX_SIZE*MATRIX_SIZE)
47
48   /* Total number of possible levels for a dithered primary color */
49 #define DITHER_LEVELS   (MATRIX_SIZE_2 * (PRIMARY_LEVELS-1) + 1)
50
51   /* Dithering matrix */
52 static const int dither_matrix[MATRIX_SIZE_2] =
53 {
54      0, 32,  8, 40,  2, 34, 10, 42,
55     48, 16, 56, 24, 50, 18, 58, 26,
56     12, 44,  4, 36, 14, 46,  6, 38,
57     60, 28, 52, 20, 62, 30, 54, 22,
58      3, 35, 11, 43,  1, 33,  9, 41,
59     51, 19, 59, 27, 49, 17, 57, 25,
60     15, 47,  7, 39, 13, 45,  5, 37,
61     63, 31, 55, 23, 61, 29, 53, 21
62 };
63
64   /* Mapping between (R,G,B) triples and EGA colors */
65 static const int EGAmapping[TOTAL_LEVELS] =
66 {
67     0,  /* 000000 -> 000000 */
68     4,  /* 00007f -> 000080 */
69     12, /* 0000ff -> 0000ff */
70     2,  /* 007f00 -> 008000 */
71     6,  /* 007f7f -> 008080 */
72     6,  /* 007fff -> 008080 */
73     10, /* 00ff00 -> 00ff00 */
74     6,  /* 00ff7f -> 008080 */
75     14, /* 00ffff -> 00ffff */
76     1,  /* 7f0000 -> 800000 */
77     5,  /* 7f007f -> 800080 */
78     5,  /* 7f00ff -> 800080 */
79     3,  /* 7f7f00 -> 808000 */
80     8,  /* 7f7f7f -> 808080 */
81     7,  /* 7f7fff -> c0c0c0 */
82     3,  /* 7fff00 -> 808000 */
83     7,  /* 7fff7f -> c0c0c0 */
84     7,  /* 7fffff -> c0c0c0 */
85     9,  /* ff0000 -> ff0000 */
86     5,  /* ff007f -> 800080 */
87     13, /* ff00ff -> ff00ff */
88     3,  /* ff7f00 -> 808000 */
89     7,  /* ff7f7f -> c0c0c0 */
90     7,  /* ff7fff -> c0c0c0 */
91     11, /* ffff00 -> ffff00 */
92     7,  /* ffff7f -> c0c0c0 */
93     15  /* ffffff -> ffffff */
94 };
95
96 #define PIXEL_VALUE(r,g,b) \
97     X11DRV_PALETTE_mapEGAPixel[EGAmapping[((r)*PRIMARY_LEVELS+(g))*PRIMARY_LEVELS+(b)]]
98
99 static const COLORREF BLACK = RGB(0, 0, 0);
100 static const COLORREF WHITE = RGB(0xff, 0xff, 0xff);
101
102 /***********************************************************************
103  *           BRUSH_DitherColor
104  */
105 static Pixmap BRUSH_DitherColor( COLORREF color, int depth)
106 {
107     /* X image for building dithered pixmap */
108     static XImage *ditherImage = NULL;
109     static COLORREF prevColor = 0xffffffff;
110     unsigned int x, y;
111     Pixmap pixmap;
112     GC gc;
113
114     XLockDisplay( gdi_display );
115     if (!ditherImage)
116     {
117         ditherImage = XCreateImage( gdi_display, default_visual.visual, depth, ZPixmap, 0,
118                                     NULL, MATRIX_SIZE, MATRIX_SIZE, 32, 0 );
119         if (!ditherImage)
120         {
121             ERR("Could not create dither image\n");
122             XUnlockDisplay( gdi_display );
123             return 0;
124         }
125         ditherImage->data = HeapAlloc( GetProcessHeap(), 0,
126                                        ditherImage->height * ditherImage->bytes_per_line );
127     }
128
129     if (color != prevColor)
130     {
131         int r = GetRValue( color ) * DITHER_LEVELS;
132         int g = GetGValue( color ) * DITHER_LEVELS;
133         int b = GetBValue( color ) * DITHER_LEVELS;
134         const int *pmatrix = dither_matrix;
135
136         for (y = 0; y < MATRIX_SIZE; y++)
137         {
138             for (x = 0; x < MATRIX_SIZE; x++)
139             {
140                 int d  = *pmatrix++ * 256;
141                 int dr = ((r + d) / MATRIX_SIZE_2) / 256;
142                 int dg = ((g + d) / MATRIX_SIZE_2) / 256;
143                 int db = ((b + d) / MATRIX_SIZE_2) / 256;
144                 XPutPixel( ditherImage, x, y, PIXEL_VALUE(dr,dg,db) );
145             }
146         }
147         prevColor = color;
148     }
149
150     pixmap = XCreatePixmap( gdi_display, root_window, MATRIX_SIZE, MATRIX_SIZE, depth );
151     gc = XCreateGC( gdi_display, pixmap, 0, NULL );
152     XPutImage( gdi_display, pixmap, gc, ditherImage, 0, 0, 0, 0, MATRIX_SIZE, MATRIX_SIZE );
153     XFreeGC( gdi_display, gc );
154     XUnlockDisplay( gdi_display );
155
156     return pixmap;
157 }
158
159
160 /***********************************************************************
161  *           BRUSH_DitherMono
162  */
163 static Pixmap BRUSH_DitherMono( COLORREF color )
164 {
165     /* This makes the spray work in Win 3.11 pbrush.exe */
166     /* FIXME. Extend this basic selection of dither patterns */
167     static const char gray_dither[][2] = {{ 0x1, 0x0 }, /* DKGRAY */
168                                           { 0x2, 0x1 }, /* GRAY */
169                                           { 0x1, 0x3 }, /* LTGRAY */
170     };                                      
171     int gray = (30 * GetRValue(color) + 59 * GetGValue(color) + 11 * GetBValue(color)) / 100;
172     int idx = gray * (sizeof gray_dither/sizeof gray_dither[0] + 1)/256 - 1;
173
174     TRACE("color=%06x -> gray=%x\n", color, gray);
175     return XCreateBitmapFromData( gdi_display, root_window, gray_dither[idx], 2, 2 );
176 }
177
178 /***********************************************************************
179  *           BRUSH_SelectSolidBrush
180  */
181 static void BRUSH_SelectSolidBrush( X11DRV_PDEVICE *physDev, COLORREF color )
182 {
183     COLORREF colorRGB = X11DRV_PALETTE_GetColor( physDev, color );
184     if ((physDev->depth > 1) && (default_visual.depth <= 8) && !X11DRV_IsSolidColor( color ))
185     {
186           /* Dithered brush */
187         physDev->brush.pixmap = BRUSH_DitherColor( colorRGB, physDev->depth );
188         physDev->brush.fillStyle = FillTiled;
189         physDev->brush.pixel = 0;
190     }
191     else if (physDev->depth == 1 && colorRGB != WHITE && colorRGB != BLACK)
192     {
193         physDev->brush.pixel = 0;
194         physDev->brush.pixmap = BRUSH_DitherMono( colorRGB );
195         physDev->brush.fillStyle = FillTiled;
196     }
197     else
198     {
199           /* Solid brush */
200         physDev->brush.pixel = X11DRV_PALETTE_ToPhysical( physDev, color );
201         physDev->brush.fillStyle = FillSolid;
202     }
203 }
204
205
206 static BOOL select_pattern_brush( X11DRV_PDEVICE *physdev, const struct brush_pattern *pattern )
207 {
208     XVisualInfo vis = default_visual;
209     Pixmap pixmap;
210     const BITMAPINFO *info = pattern->info;
211
212     if (physdev->depth == 1 || info->bmiHeader.biBitCount == 1) vis.depth = 1;
213
214     pixmap = create_pixmap_from_image( physdev->dev.hdc, &vis, info, &pattern->bits, pattern->usage );
215     if (!pixmap) return FALSE;
216
217     if (physdev->brush.pixmap) XFreePixmap( gdi_display, physdev->brush.pixmap );
218     physdev->brush.pixmap = pixmap;
219
220     if (vis.depth == 1)
221     {
222         physdev->brush.fillStyle = FillOpaqueStippled;
223         physdev->brush.pixel = -1;  /* Special case (see DC_SetupGCForBrush) */
224     }
225     else
226     {
227         physdev->brush.fillStyle = FillTiled;
228         physdev->brush.pixel = 0;  /* Ignored */
229     }
230     return TRUE;
231 }
232
233 /***********************************************************************
234  *           SelectBrush   (X11DRV.@)
235  */
236 HBRUSH X11DRV_SelectBrush( PHYSDEV dev, HBRUSH hbrush, const struct brush_pattern *pattern )
237 {
238     X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
239     LOGBRUSH logbrush;
240
241     if (pattern)  /* pattern brush */
242     {
243         if (!select_pattern_brush( physDev, pattern )) return 0;
244         TRACE("BS_PATTERN\n");
245         physDev->brush.style = BS_PATTERN;
246         return hbrush;
247     }
248
249     if (!GetObjectA( hbrush, sizeof(logbrush), &logbrush )) return 0;
250
251     TRACE("hdc=%p hbrush=%p\n", dev->hdc, hbrush);
252
253     if (physDev->brush.pixmap)
254     {
255         XFreePixmap( gdi_display, physDev->brush.pixmap );
256         physDev->brush.pixmap = 0;
257     }
258     physDev->brush.style = logbrush.lbStyle;
259     if (hbrush == GetStockObject( DC_BRUSH ))
260         logbrush.lbColor = GetDCBrushColor( dev->hdc );
261
262     switch(logbrush.lbStyle)
263     {
264       case BS_NULL:
265         TRACE("BS_NULL\n" );
266         break;
267
268       case BS_SOLID:
269         TRACE("BS_SOLID\n" );
270         BRUSH_SelectSolidBrush( physDev, logbrush.lbColor );
271         break;
272
273       case BS_HATCHED:
274         TRACE("BS_HATCHED\n" );
275         physDev->brush.pixel = X11DRV_PALETTE_ToPhysical( physDev, logbrush.lbColor );
276         physDev->brush.pixmap = XCreateBitmapFromData( gdi_display, root_window,
277                                                        HatchBrushes[logbrush.lbHatch], 8, 8 );
278         physDev->brush.fillStyle = FillStippled;
279         break;
280     }
281     return hbrush;
282 }
283
284
285 /***********************************************************************
286  *           SetDCBrushColor (X11DRV.@)
287  */
288 COLORREF X11DRV_SetDCBrushColor( PHYSDEV dev, COLORREF crColor )
289 {
290     X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
291
292     if (GetCurrentObject(dev->hdc, OBJ_BRUSH) == GetStockObject( DC_BRUSH ))
293         BRUSH_SelectSolidBrush( physDev, crColor );
294
295     return crColor;
296 }