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