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