Release 980301
[wine/hacks.git] / graphics / x11drv / brush.c
blob90ff60aacd2917477b6fb4e36a9031d7a3b1a4bb
1 /*
2 * GDI brush objects
4 * Copyright 1993, 1994 Alexandre Julliard
5 */
7 #include <stdlib.h>
8 #include "brush.h"
9 #include "bitmap.h"
10 #include "color.h"
11 #include "x11drv.h"
12 #include "debug.h"
14 static const char HatchBrushes[NB_HATCH_STYLES + 1][8] =
16 { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HS_HORIZONTAL */
17 { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HS_VERTICAL */
18 { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HS_FDIAGONAL */
19 { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HS_BDIAGONAL */
20 { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HS_CROSS */
21 { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 }, /* HS_DIAGCROSS */
22 { 0xee, 0xbb, 0xee, 0xbb, 0xee, 0xbb, 0xee, 0xbb } /* Hack for DKGRAY */
25 /* Levels of each primary for dithering */
26 #define PRIMARY_LEVELS 3
27 #define TOTAL_LEVELS (PRIMARY_LEVELS*PRIMARY_LEVELS*PRIMARY_LEVELS)
29 /* Dithering matrix size */
30 #define MATRIX_SIZE 8
31 #define MATRIX_SIZE_2 (MATRIX_SIZE*MATRIX_SIZE)
33 /* Total number of possible levels for a dithered primary color */
34 #define DITHER_LEVELS (MATRIX_SIZE_2 * (PRIMARY_LEVELS-1) + 1)
36 /* Dithering matrix */
37 static const int dither_matrix[MATRIX_SIZE_2] =
39 0, 32, 8, 40, 2, 34, 10, 42,
40 48, 16, 56, 24, 50, 18, 58, 26,
41 12, 44, 4, 36, 14, 46, 6, 38,
42 60, 28, 52, 20, 62, 30, 54, 22,
43 3, 35, 11, 43, 1, 33, 9, 41,
44 51, 19, 59, 27, 49, 17, 57, 25,
45 15, 47, 7, 39, 13, 45, 5, 37,
46 63, 31, 55, 23, 61, 29, 53, 21
49 /* Mapping between (R,G,B) triples and EGA colors */
50 static const int EGAmapping[TOTAL_LEVELS] =
52 0, /* 000000 -> 000000 */
53 4, /* 00007f -> 000080 */
54 12, /* 0000ff -> 0000ff */
55 2, /* 007f00 -> 008000 */
56 6, /* 007f7f -> 008080 */
57 6, /* 007fff -> 008080 */
58 10, /* 00ff00 -> 00ff00 */
59 6, /* 00ff7f -> 008080 */
60 14, /* 00ffff -> 00ffff */
61 1, /* 7f0000 -> 800000 */
62 5, /* 7f007f -> 800080 */
63 5, /* 7f00ff -> 800080 */
64 3, /* 7f7f00 -> 808000 */
65 8, /* 7f7f7f -> 808080 */
66 7, /* 7f7fff -> c0c0c0 */
67 3, /* 7fff00 -> 808000 */
68 7, /* 7fff7f -> c0c0c0 */
69 7, /* 7fffff -> c0c0c0 */
70 9, /* ff0000 -> ff0000 */
71 5, /* ff007f -> 800080 */
72 13, /* ff00ff -> ff00ff */
73 3, /* ff7f00 -> 808000 */
74 7, /* ff7f7f -> c0c0c0 */
75 7, /* ff7fff -> c0c0c0 */
76 11, /* ffff00 -> ffff00 */
77 7, /* ffff7f -> c0c0c0 */
78 15 /* ffffff -> ffffff */
81 #define PIXEL_VALUE(r,g,b) \
82 COLOR_mapEGAPixel[EGAmapping[((r)*PRIMARY_LEVELS+(g))*PRIMARY_LEVELS+(b)]]
84 /* X image for building dithered pixmap */
85 static XImage *ditherImage = NULL;
88 /***********************************************************************
89 * BRUSH_Init
91 * Create the X image used for dithering.
93 BOOL32 X11DRV_BRUSH_Init(void)
95 XCREATEIMAGE( ditherImage, MATRIX_SIZE, MATRIX_SIZE, screenDepth );
96 return (ditherImage != NULL);
100 /***********************************************************************
101 * BRUSH_DitherColor
103 static Pixmap BRUSH_DitherColor( DC *dc, COLORREF color )
105 static COLORREF prevColor = 0xffffffff;
106 unsigned int x, y;
107 Pixmap pixmap;
109 EnterCriticalSection( &X11DRV_CritSection );
110 if (color != prevColor)
112 int r = GetRValue( color ) * DITHER_LEVELS;
113 int g = GetGValue( color ) * DITHER_LEVELS;
114 int b = GetBValue( color ) * DITHER_LEVELS;
115 const int *pmatrix = dither_matrix;
117 for (y = 0; y < MATRIX_SIZE; y++)
119 for (x = 0; x < MATRIX_SIZE; x++)
121 int d = *pmatrix++ * 256;
122 int dr = ((r + d) / MATRIX_SIZE_2) / 256;
123 int dg = ((g + d) / MATRIX_SIZE_2) / 256;
124 int db = ((b + d) / MATRIX_SIZE_2) / 256;
125 XPutPixel( ditherImage, x, y, PIXEL_VALUE(dr,dg,db) );
128 prevColor = color;
131 pixmap = XCreatePixmap( display, rootWindow,
132 MATRIX_SIZE, MATRIX_SIZE, screenDepth );
133 XPutImage( display, pixmap, BITMAP_colorGC, ditherImage, 0, 0,
134 0, 0, MATRIX_SIZE, MATRIX_SIZE );
135 LeaveCriticalSection( &X11DRV_CritSection );
136 return pixmap;
140 /***********************************************************************
141 * BRUSH_SelectSolidBrush
143 static void BRUSH_SelectSolidBrush( DC *dc, COLORREF color )
145 if ((dc->w.bitsPerPixel > 1) && (screenDepth <= 8) && !COLOR_IsSolid( color ))
147 /* Dithered brush */
148 dc->u.x.brush.pixmap = BRUSH_DitherColor( dc, color );
149 dc->u.x.brush.fillStyle = FillTiled;
150 dc->u.x.brush.pixel = 0;
152 else
154 /* Solid brush */
155 dc->u.x.brush.pixel = COLOR_ToPhysical( dc, color );
156 dc->u.x.brush.fillStyle = FillSolid;
161 /***********************************************************************
162 * BRUSH_SelectPatternBrush
164 static BOOL32 BRUSH_SelectPatternBrush( DC * dc, HBITMAP32 hbitmap )
166 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
167 if (!bmp) return FALSE;
169 if ((dc->w.bitsPerPixel == 1) && (bmp->bitmap.bmBitsPixel != 1))
171 /* Special case: a color pattern on a monochrome DC */
172 dc->u.x.brush.pixmap = TSXCreatePixmap( display, rootWindow, 8, 8, 1 );
173 /* FIXME: should probably convert to monochrome instead */
174 TSXCopyPlane( display, bmp->pixmap, dc->u.x.brush.pixmap,
175 BITMAP_monoGC, 0, 0, 8, 8, 0, 0, 1 );
177 else
179 dc->u.x.brush.pixmap = TSXCreatePixmap( display, rootWindow,
180 8, 8, bmp->bitmap.bmBitsPixel );
181 TSXCopyArea( display, bmp->pixmap, dc->u.x.brush.pixmap,
182 BITMAP_GC(bmp), 0, 0, 8, 8, 0, 0 );
185 if (bmp->bitmap.bmBitsPixel > 1)
187 dc->u.x.brush.fillStyle = FillTiled;
188 dc->u.x.brush.pixel = 0; /* Ignored */
190 else
192 dc->u.x.brush.fillStyle = FillOpaqueStippled;
193 dc->u.x.brush.pixel = -1; /* Special case (see DC_SetupGCForBrush) */
195 GDI_HEAP_UNLOCK( hbitmap );
196 return TRUE;
202 /***********************************************************************
203 * BRUSH_SelectObject
205 HBRUSH32 X11DRV_BRUSH_SelectObject( DC * dc, HBRUSH32 hbrush, BRUSHOBJ * brush )
207 HBITMAP16 hBitmap;
208 BITMAPINFO * bmpInfo;
209 HBRUSH16 prevHandle = dc->w.hBrush;
211 dprintf_info(gdi, "Brush_SelectObject: hdc=%04x hbrush=%04x\n",
212 dc->hSelf,hbrush);
213 #ifdef NOTDEF
214 if (dc->header.wMagic == METAFILE_DC_MAGIC)
216 LOGBRUSH16 logbrush = { brush->logbrush.lbStyle,
217 brush->logbrush.lbColor,
218 brush->logbrush.lbHatch };
219 switch (brush->logbrush.lbStyle)
221 case BS_SOLID:
222 case BS_HATCHED:
223 case BS_HOLLOW:
224 if (!MF_CreateBrushIndirect( dc, hbrush, &logbrush )) return 0;
225 break;
226 case BS_PATTERN:
227 case BS_DIBPATTERN:
228 if (!MF_CreatePatternBrush( dc, hbrush, &logbrush )) return 0;
229 break;
231 return 1; /* FIXME? */
233 #endif
234 dc->w.hBrush = hbrush;
236 if (dc->u.x.brush.pixmap)
238 TSXFreePixmap( display, dc->u.x.brush.pixmap );
239 dc->u.x.brush.pixmap = 0;
241 dc->u.x.brush.style = brush->logbrush.lbStyle;
243 switch(brush->logbrush.lbStyle)
245 case BS_NULL:
246 dprintf_info(gdi,"BS_NULL\n" );
247 break;
249 case BS_SOLID:
250 dprintf_info(gdi,"BS_SOLID\n" );
251 BRUSH_SelectSolidBrush( dc, brush->logbrush.lbColor );
252 break;
254 case BS_HATCHED:
255 dprintf_info(gdi, "BS_HATCHED\n" );
256 dc->u.x.brush.pixel = COLOR_ToPhysical( dc, brush->logbrush.lbColor );
257 dc->u.x.brush.pixmap = TSXCreateBitmapFromData( display, rootWindow,
258 HatchBrushes[brush->logbrush.lbHatch], 8, 8 );
259 dc->u.x.brush.fillStyle = FillStippled;
260 break;
262 case BS_PATTERN:
263 dprintf_info(gdi, "BS_PATTERN\n");
264 BRUSH_SelectPatternBrush( dc, (HBRUSH16)brush->logbrush.lbHatch );
265 break;
267 case BS_DIBPATTERN:
268 dprintf_info(gdi, "BS_DIBPATTERN\n");
269 if ((bmpInfo = (BITMAPINFO *) GlobalLock16( (HGLOBAL16)brush->logbrush.lbHatch )))
271 int size = DIB_BitmapInfoSize( bmpInfo, brush->logbrush.lbColor );
272 hBitmap = CreateDIBitmap32( dc->hSelf, &bmpInfo->bmiHeader,
273 CBM_INIT, ((char *)bmpInfo) + size,
274 bmpInfo,
275 (WORD)brush->logbrush.lbColor );
276 BRUSH_SelectPatternBrush( dc, hBitmap );
277 DeleteObject16( hBitmap );
278 GlobalUnlock16( (HGLOBAL16)brush->logbrush.lbHatch );
281 break;
284 return prevHandle;