Added a missing prototype.
[wine] / dlls / x11drv / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include <stdlib.h>
24
25 #include "gdi.h"
26 #include "x11drv.h"
27 #include "wine/debug.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
30
31 #define NB_HATCH_STYLES  (HS_DIAGCROSS+1)
32
33 static const char HatchBrushes[NB_HATCH_STYLES + 1][8] =
34 {
35     { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HS_HORIZONTAL */
36     { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HS_VERTICAL   */
37     { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HS_FDIAGONAL  */
38     { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HS_BDIAGONAL  */
39     { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HS_CROSS      */
40     { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 }, /* HS_DIAGCROSS  */
41     { 0xee, 0xbb, 0xee, 0xbb, 0xee, 0xbb, 0xee, 0xbb }  /* Hack for DKGRAY */
42 };
43
44   /* Levels of each primary for dithering */
45 #define PRIMARY_LEVELS  3
46 #define TOTAL_LEVELS    (PRIMARY_LEVELS*PRIMARY_LEVELS*PRIMARY_LEVELS)
47
48  /* Dithering matrix size  */
49 #define MATRIX_SIZE     8
50 #define MATRIX_SIZE_2   (MATRIX_SIZE*MATRIX_SIZE)
51
52   /* Total number of possible levels for a dithered primary color */
53 #define DITHER_LEVELS   (MATRIX_SIZE_2 * (PRIMARY_LEVELS-1) + 1)
54
55   /* Dithering matrix */
56 static const int dither_matrix[MATRIX_SIZE_2] =
57 {
58      0, 32,  8, 40,  2, 34, 10, 42,
59     48, 16, 56, 24, 50, 18, 58, 26,
60     12, 44,  4, 36, 14, 46,  6, 38,
61     60, 28, 52, 20, 62, 30, 54, 22,
62      3, 35, 11, 43,  1, 33,  9, 41,
63     51, 19, 59, 27, 49, 17, 57, 25,
64     15, 47,  7, 39, 13, 45,  5, 37,
65     63, 31, 55, 23, 61, 29, 53, 21
66 };
67
68   /* Mapping between (R,G,B) triples and EGA colors */
69 static const int EGAmapping[TOTAL_LEVELS] =
70 {
71     0,  /* 000000 -> 000000 */
72     4,  /* 00007f -> 000080 */
73     12, /* 0000ff -> 0000ff */
74     2,  /* 007f00 -> 008000 */
75     6,  /* 007f7f -> 008080 */
76     6,  /* 007fff -> 008080 */
77     10, /* 00ff00 -> 00ff00 */
78     6,  /* 00ff7f -> 008080 */
79     14, /* 00ffff -> 00ffff */
80     1,  /* 7f0000 -> 800000 */
81     5,  /* 7f007f -> 800080 */
82     5,  /* 7f00ff -> 800080 */
83     3,  /* 7f7f00 -> 808000 */
84     8,  /* 7f7f7f -> 808080 */
85     7,  /* 7f7fff -> c0c0c0 */
86     3,  /* 7fff00 -> 808000 */
87     7,  /* 7fff7f -> c0c0c0 */
88     7,  /* 7fffff -> c0c0c0 */
89     9,  /* ff0000 -> ff0000 */
90     5,  /* ff007f -> 800080 */
91     13, /* ff00ff -> ff00ff */
92     3,  /* ff7f00 -> 808000 */
93     7,  /* ff7f7f -> c0c0c0 */
94     7,  /* ff7fff -> c0c0c0 */
95     11, /* ffff00 -> ffff00 */
96     7,  /* ffff7f -> c0c0c0 */
97     15  /* ffffff -> ffffff */
98 };
99
100 #define PIXEL_VALUE(r,g,b) \
101     X11DRV_PALETTE_mapEGAPixel[EGAmapping[((r)*PRIMARY_LEVELS+(g))*PRIMARY_LEVELS+(b)]]
102
103   /* X image for building dithered pixmap */
104 static XImage *ditherImage = NULL;
105
106
107 /***********************************************************************
108  *           BRUSH_DitherColor
109  */
110 static Pixmap BRUSH_DitherColor( COLORREF color )
111 {
112     static COLORREF prevColor = 0xffffffff;
113     unsigned int x, y;
114     Pixmap pixmap;
115
116     if (!ditherImage)
117     {
118         ditherImage = X11DRV_DIB_CreateXImage( MATRIX_SIZE, MATRIX_SIZE, screen_depth );
119         if (!ditherImage) return 0;
120     }
121
122     wine_tsx11_lock();
123     if (color != prevColor)
124     {
125         int r = GetRValue( color ) * DITHER_LEVELS;
126         int g = GetGValue( color ) * DITHER_LEVELS;
127         int b = GetBValue( color ) * DITHER_LEVELS;
128         const int *pmatrix = dither_matrix;
129
130         for (y = 0; y < MATRIX_SIZE; y++)
131         {
132             for (x = 0; x < MATRIX_SIZE; x++)
133             {
134                 int d  = *pmatrix++ * 256;
135                 int dr = ((r + d) / MATRIX_SIZE_2) / 256;
136                 int dg = ((g + d) / MATRIX_SIZE_2) / 256;
137                 int db = ((b + d) / MATRIX_SIZE_2) / 256;
138                 XPutPixel( ditherImage, x, y, PIXEL_VALUE(dr,dg,db) );
139             }
140         }
141         prevColor = color;
142     }
143
144     pixmap = XCreatePixmap( gdi_display, root_window, MATRIX_SIZE, MATRIX_SIZE, screen_depth );
145     XPutImage( gdi_display, pixmap, BITMAP_colorGC, ditherImage, 0, 0,
146                0, 0, MATRIX_SIZE, MATRIX_SIZE );
147     wine_tsx11_unlock();
148     return pixmap;
149 }
150
151
152 /***********************************************************************
153  *           BRUSH_SelectSolidBrush
154  */
155 static void BRUSH_SelectSolidBrush( X11DRV_PDEVICE *physDev, COLORREF color )
156 {
157     if ((physDev->depth > 1) && (screen_depth <= 8) && !X11DRV_IsSolidColor( color ))
158     {
159           /* Dithered brush */
160         physDev->brush.pixmap = BRUSH_DitherColor( color );
161         physDev->brush.fillStyle = FillTiled;
162         physDev->brush.pixel = 0;
163     }
164     else
165     {
166           /* Solid brush */
167         physDev->brush.pixel = X11DRV_PALETTE_ToPhysical( physDev, color );
168         physDev->brush.fillStyle = FillSolid;
169     }
170 }
171
172
173 /***********************************************************************
174  *           BRUSH_SelectPatternBrush
175  */
176 static BOOL BRUSH_SelectPatternBrush( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
177 {
178     BOOL ret = FALSE;
179     BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
180     if (!bmp) return FALSE;
181
182     if(!bmp->physBitmap) goto done;
183
184     wine_tsx11_lock();
185     if ((physDev->depth == 1) && (bmp->bitmap.bmBitsPixel != 1))
186     {
187         /* Special case: a color pattern on a monochrome DC */
188         physDev->brush.pixmap = XCreatePixmap( gdi_display, root_window,
189                                                bmp->bitmap.bmWidth, bmp->bitmap.bmHeight, 1);
190         /* FIXME: should probably convert to monochrome instead */
191         XCopyPlane( gdi_display, (Pixmap)bmp->physBitmap, physDev->brush.pixmap,
192                     BITMAP_monoGC, 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight, 0, 0, 1 );
193     }
194     else
195     {
196         physDev->brush.pixmap = XCreatePixmap( gdi_display, root_window,
197                                                bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
198                                                bmp->bitmap.bmBitsPixel );
199         XCopyArea( gdi_display, (Pixmap)bmp->physBitmap, physDev->brush.pixmap,
200                    BITMAP_GC(bmp), 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight, 0, 0 );
201     }
202     wine_tsx11_unlock();
203
204     if (bmp->bitmap.bmBitsPixel > 1)
205     {
206         physDev->brush.fillStyle = FillTiled;
207         physDev->brush.pixel = 0;  /* Ignored */
208     }
209     else
210     {
211         physDev->brush.fillStyle = FillOpaqueStippled;
212         physDev->brush.pixel = -1;  /* Special case (see DC_SetupGCForBrush) */
213     }
214     ret = TRUE;
215  done:
216     GDI_ReleaseObj( hbitmap );
217     return ret;
218 }
219
220
221 /***********************************************************************
222  *           SelectBrush   (X11DRV.@)
223  */
224 HBRUSH X11DRV_SelectBrush( X11DRV_PDEVICE *physDev, HBRUSH hbrush )
225 {
226     LOGBRUSH logbrush;
227     HBITMAP hBitmap;
228     BITMAPINFO * bmpInfo;
229
230     if (!GetObjectA( hbrush, sizeof(logbrush), &logbrush )) return 0;
231
232     TRACE("hdc=%p hbrush=%p\n", physDev->hdc,hbrush);
233
234     if (physDev->brush.pixmap)
235     {
236         wine_tsx11_lock();
237         XFreePixmap( gdi_display, physDev->brush.pixmap );
238         wine_tsx11_unlock();
239         physDev->brush.pixmap = 0;
240     }
241     physDev->brush.style = logbrush.lbStyle;
242     if (hbrush == GetStockObject( DC_BRUSH ))
243         logbrush.lbColor = GetDCBrushColor( physDev->hdc );
244
245     switch(logbrush.lbStyle)
246     {
247       case BS_NULL:
248         TRACE("BS_NULL\n" );
249         break;
250
251       case BS_SOLID:
252         TRACE("BS_SOLID\n" );
253         BRUSH_SelectSolidBrush( physDev, logbrush.lbColor );
254         break;
255
256       case BS_HATCHED:
257         TRACE("BS_HATCHED\n" );
258         physDev->brush.pixel = X11DRV_PALETTE_ToPhysical( physDev, logbrush.lbColor );
259         wine_tsx11_lock();
260         physDev->brush.pixmap = XCreateBitmapFromData( gdi_display, root_window,
261                                                        HatchBrushes[logbrush.lbHatch], 8, 8 );
262         wine_tsx11_unlock();
263         physDev->brush.fillStyle = FillStippled;
264         break;
265
266       case BS_PATTERN:
267         TRACE("BS_PATTERN\n");
268         if (!BRUSH_SelectPatternBrush( physDev, (HBITMAP)logbrush.lbHatch )) return 0;
269         break;
270
271       case BS_DIBPATTERN:
272         TRACE("BS_DIBPATTERN\n");
273         if ((bmpInfo = (BITMAPINFO *) GlobalLock16( (HGLOBAL16)logbrush.lbHatch )))
274         {
275             int size = X11DRV_DIB_BitmapInfoSize( bmpInfo, logbrush.lbColor );
276             hBitmap = CreateDIBitmap( physDev->hdc, &bmpInfo->bmiHeader,
277                                         CBM_INIT, ((char *)bmpInfo) + size,
278                                         bmpInfo,
279                                         (WORD)logbrush.lbColor );
280             BRUSH_SelectPatternBrush( physDev, hBitmap );
281             DeleteObject( hBitmap );
282             GlobalUnlock16( (HGLOBAL16)logbrush.lbHatch );
283         }
284
285         break;
286     }
287     return hbrush;
288 }
289
290
291 /***********************************************************************
292  *           SetDCBrushColor (X11DRV.@)
293  */
294 COLORREF X11DRV_SetDCBrushColor( X11DRV_PDEVICE *physDev, COLORREF crColor )
295 {
296     if (GetCurrentObject(physDev->hdc, OBJ_BRUSH) == GetStockObject( DC_BRUSH ))
297         BRUSH_SelectSolidBrush( physDev, crColor );
298
299     return crColor;
300 }