Release 970629
[wine/multimedia.git] / graphics / x11drv / brush.c
blob1fe88daf5d352d1c14c89589bb9970ef88e97421
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 "stddebug.h"
13 #include "debug.h"
15 static const char HatchBrushes[NB_HATCH_STYLES + 1][8] =
17 { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HS_HORIZONTAL */
18 { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HS_VERTICAL */
19 { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HS_FDIAGONAL */
20 { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HS_BDIAGONAL */
21 { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HS_CROSS */
22 { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 }, /* HS_DIAGCROSS */
23 { 0xee, 0xbb, 0xee, 0xbb, 0xee, 0xbb, 0xee, 0xbb } /* Hack for DKGRAY */
26 /* Levels of each primary for dithering */
27 #define PRIMARY_LEVELS 3
28 #define TOTAL_LEVELS (PRIMARY_LEVELS*PRIMARY_LEVELS*PRIMARY_LEVELS)
30 /* Dithering matrix size */
31 #define MATRIX_SIZE 8
32 #define MATRIX_SIZE_2 (MATRIX_SIZE*MATRIX_SIZE)
34 /* Total number of possible levels for a dithered primary color */
35 #define DITHER_LEVELS (MATRIX_SIZE_2 * (PRIMARY_LEVELS-1) + 1)
37 /* Dithering matrix */
38 static const int dither_matrix[MATRIX_SIZE_2] =
40 0, 32, 8, 40, 2, 34, 10, 42,
41 48, 16, 56, 24, 50, 18, 58, 26,
42 12, 44, 4, 36, 14, 46, 6, 38,
43 60, 28, 52, 20, 62, 30, 54, 22,
44 3, 35, 11, 43, 1, 33, 9, 41,
45 51, 19, 59, 27, 49, 17, 57, 25,
46 15, 47, 7, 39, 13, 45, 5, 37,
47 63, 31, 55, 23, 61, 29, 53, 21
50 /* Mapping between (R,G,B) triples and EGA colors */
51 static const int EGAmapping[TOTAL_LEVELS] =
53 0, /* 000000 -> 000000 */
54 4, /* 00007f -> 000080 */
55 12, /* 0000ff -> 0000ff */
56 2, /* 007f00 -> 008000 */
57 6, /* 007f7f -> 008080 */
58 6, /* 007fff -> 008080 */
59 10, /* 00ff00 -> 00ff00 */
60 6, /* 00ff7f -> 008080 */
61 14, /* 00ffff -> 00ffff */
62 1, /* 7f0000 -> 800000 */
63 5, /* 7f007f -> 800080 */
64 5, /* 7f00ff -> 800080 */
65 3, /* 7f7f00 -> 808000 */
66 8, /* 7f7f7f -> 808080 */
67 7, /* 7f7fff -> c0c0c0 */
68 3, /* 7fff00 -> 808000 */
69 7, /* 7fff7f -> c0c0c0 */
70 7, /* 7fffff -> c0c0c0 */
71 9, /* ff0000 -> ff0000 */
72 5, /* ff007f -> 800080 */
73 13, /* ff00ff -> ff00ff */
74 3, /* ff7f00 -> 808000 */
75 7, /* ff7f7f -> c0c0c0 */
76 7, /* ff7fff -> c0c0c0 */
77 11, /* ffff00 -> ffff00 */
78 7, /* ffff7f -> c0c0c0 */
79 15 /* ffffff -> ffffff */
82 #define PIXEL_VALUE(r,g,b) \
83 COLOR_mapEGAPixel[EGAmapping[((r)*PRIMARY_LEVELS+(g))*PRIMARY_LEVELS+(b)]]
85 /* X image for building dithered pixmap */
86 static XImage *ditherImage = NULL;
89 /***********************************************************************
90 * BRUSH_Init
92 * Create the X image used for dithering.
94 BOOL32 X11DRV_BRUSH_Init(void)
96 XCREATEIMAGE( ditherImage, MATRIX_SIZE, MATRIX_SIZE, screenDepth );
97 return (ditherImage != NULL);
101 /***********************************************************************
102 * BRUSH_DitherColor
104 static Pixmap BRUSH_DitherColor( DC *dc, COLORREF color )
106 static COLORREF prevColor = 0xffffffff;
107 unsigned int x, y;
108 Pixmap pixmap;
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 return pixmap;
139 /***********************************************************************
140 * BRUSH_SelectSolidBrush
142 static void BRUSH_SelectSolidBrush( DC *dc, COLORREF color )
144 if ((dc->w.bitsPerPixel > 1) && (screenDepth <= 8) && !COLOR_IsSolid( color ))
146 /* Dithered brush */
147 dc->u.x.brush.pixmap = BRUSH_DitherColor( dc, color );
148 dc->u.x.brush.fillStyle = FillTiled;
149 dc->u.x.brush.pixel = 0;
151 else
153 /* Solid brush */
154 dc->u.x.brush.pixel = COLOR_ToPhysical( dc, color );
155 dc->u.x.brush.fillStyle = FillSolid;
160 /***********************************************************************
161 * BRUSH_SelectPatternBrush
163 static BOOL32 BRUSH_SelectPatternBrush( DC * dc, HBITMAP32 hbitmap )
165 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
166 if (!bmp) return FALSE;
167 dc->u.x.brush.pixmap = XCreatePixmap( display, rootWindow,
168 8, 8, bmp->bitmap.bmBitsPixel );
169 XCopyArea( display, bmp->pixmap, dc->u.x.brush.pixmap,
170 BITMAP_GC(bmp), 0, 0, 8, 8, 0, 0 );
172 if (bmp->bitmap.bmBitsPixel > 1)
174 dc->u.x.brush.fillStyle = FillTiled;
175 dc->u.x.brush.pixel = 0; /* Ignored */
177 else
179 dc->u.x.brush.fillStyle = FillOpaqueStippled;
180 dc->u.x.brush.pixel = -1; /* Special case (see DC_SetupGCForBrush) */
182 return TRUE;
188 /***********************************************************************
189 * BRUSH_SelectObject
191 HBRUSH32 X11DRV_BRUSH_SelectObject( DC * dc, HBRUSH32 hbrush, BRUSHOBJ * brush )
193 HBITMAP16 hBitmap;
194 BITMAPINFO * bmpInfo;
195 HBRUSH16 prevHandle = dc->w.hBrush;
197 dprintf_gdi(stddeb, "Brush_SelectObject: hdc=%04x hbrush=%04x\n",
198 dc->hSelf,hbrush);
199 #ifdef NOTDEF
200 if (dc->header.wMagic == METAFILE_DC_MAGIC)
202 LOGBRUSH16 logbrush = { brush->logbrush.lbStyle,
203 brush->logbrush.lbColor,
204 brush->logbrush.lbHatch };
205 switch (brush->logbrush.lbStyle)
207 case BS_SOLID:
208 case BS_HATCHED:
209 case BS_HOLLOW:
210 if (!MF_CreateBrushIndirect( dc, hbrush, &logbrush )) return 0;
211 break;
212 case BS_PATTERN:
213 case BS_DIBPATTERN:
214 if (!MF_CreatePatternBrush( dc, hbrush, &logbrush )) return 0;
215 break;
217 return 1; /* FIXME? */
219 #endif
220 dc->w.hBrush = hbrush;
222 if (dc->u.x.brush.pixmap)
224 XFreePixmap( display, dc->u.x.brush.pixmap );
225 dc->u.x.brush.pixmap = 0;
227 dc->u.x.brush.style = brush->logbrush.lbStyle;
229 switch(brush->logbrush.lbStyle)
231 case BS_NULL:
232 dprintf_gdi( stddeb,"BS_NULL\n" );
233 break;
235 case BS_SOLID:
236 dprintf_gdi( stddeb,"BS_SOLID\n" );
237 BRUSH_SelectSolidBrush( dc, brush->logbrush.lbColor );
238 break;
240 case BS_HATCHED:
241 dprintf_gdi( stddeb, "BS_HATCHED\n" );
242 dc->u.x.brush.pixel = COLOR_ToPhysical( dc, brush->logbrush.lbColor );
243 dc->u.x.brush.pixmap = XCreateBitmapFromData( display, rootWindow,
244 HatchBrushes[brush->logbrush.lbHatch], 8, 8 );
245 dc->u.x.brush.fillStyle = FillStippled;
246 break;
248 case BS_PATTERN:
249 dprintf_gdi( stddeb, "BS_PATTERN\n");
250 BRUSH_SelectPatternBrush( dc, (HBRUSH16)brush->logbrush.lbHatch );
251 break;
253 case BS_DIBPATTERN:
254 dprintf_gdi( stddeb, "BS_DIBPATTERN\n");
255 if ((bmpInfo = (BITMAPINFO *) GlobalLock16( (HGLOBAL16)brush->logbrush.lbHatch )))
257 int size = DIB_BitmapInfoSize( bmpInfo, brush->logbrush.lbColor );
258 hBitmap = CreateDIBitmap32( dc->hSelf, &bmpInfo->bmiHeader,
259 CBM_INIT, ((char *)bmpInfo) + size,
260 bmpInfo,
261 (WORD)brush->logbrush.lbColor );
262 BRUSH_SelectPatternBrush( dc, hBitmap );
263 DeleteObject16( hBitmap );
264 GlobalUnlock16( (HGLOBAL16)brush->logbrush.lbHatch );
267 break;
270 return prevHandle;