Undefined CHAR_MAX and CHAR_MIN (#defined by NetBSD's system headers).
[wine/multimedia.git] / graphics / x11drv / brush.c
blob379857eb1625ca3d265c0e5b865d4a46f3a69921
1 /*
2 * X11DRV 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 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
147 if ((dc->w.bitsPerPixel > 1) && (screenDepth <= 8) && !COLOR_IsSolid( color ))
149 /* Dithered brush */
150 physDev->brush.pixmap = BRUSH_DitherColor( dc, color );
151 physDev->brush.fillStyle = FillTiled;
152 physDev->brush.pixel = 0;
154 else
156 /* Solid brush */
157 physDev->brush.pixel = COLOR_ToPhysical( dc, color );
158 physDev->brush.fillStyle = FillSolid;
163 /***********************************************************************
164 * BRUSH_SelectPatternBrush
166 static BOOL32 BRUSH_SelectPatternBrush( DC * dc, HBITMAP32 hbitmap )
168 X11DRV_PHYSBITMAP *pbitmap;
169 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
170 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
171 if (!bmp) return FALSE;
173 if(!bmp->DDBitmap)
174 if(!X11DRV_CreateBitmap(hbitmap))
175 return 0;
177 if(bmp->DDBitmap->funcs != dc->funcs) {
178 WARN(bitmap, "Trying to select non-X11 DDB into an X11 dc\n");
179 return 0;
182 pbitmap = bmp->DDBitmap->physBitmap;
184 if ((dc->w.bitsPerPixel == 1) && (bmp->bitmap.bmBitsPixel != 1))
186 /* Special case: a color pattern on a monochrome DC */
187 physDev->brush.pixmap = TSXCreatePixmap( display, rootWindow, 8, 8, 1);
188 /* FIXME: should probably convert to monochrome instead */
189 TSXCopyPlane( display, pbitmap->pixmap, physDev->brush.pixmap,
190 BITMAP_monoGC, 0, 0, 8, 8, 0, 0, 1 );
192 else
194 physDev->brush.pixmap = TSXCreatePixmap( display, rootWindow,
195 8, 8, bmp->bitmap.bmBitsPixel );
196 TSXCopyArea( display, pbitmap->pixmap, physDev->brush.pixmap,
197 BITMAP_GC(bmp), 0, 0, 8, 8, 0, 0 );
200 if (bmp->bitmap.bmBitsPixel > 1)
202 physDev->brush.fillStyle = FillTiled;
203 physDev->brush.pixel = 0; /* Ignored */
205 else
207 physDev->brush.fillStyle = FillOpaqueStippled;
208 physDev->brush.pixel = -1; /* Special case (see DC_SetupGCForBrush) */
210 GDI_HEAP_UNLOCK( hbitmap );
211 return TRUE;
217 /***********************************************************************
218 * BRUSH_SelectObject
220 HBRUSH32 X11DRV_BRUSH_SelectObject( DC * dc, HBRUSH32 hbrush, BRUSHOBJ * brush )
222 HBITMAP16 hBitmap;
223 BITMAPINFO * bmpInfo;
224 HBRUSH16 prevHandle = dc->w.hBrush;
225 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
227 TRACE(gdi, "hdc=%04x hbrush=%04x\n",
228 dc->hSelf,hbrush);
230 dc->w.hBrush = hbrush;
232 if (physDev->brush.pixmap)
234 TSXFreePixmap( display, physDev->brush.pixmap );
235 physDev->brush.pixmap = 0;
237 physDev->brush.style = brush->logbrush.lbStyle;
239 switch(brush->logbrush.lbStyle)
241 case BS_NULL:
242 TRACE(gdi,"BS_NULL\n" );
243 break;
245 case BS_SOLID:
246 TRACE(gdi,"BS_SOLID\n" );
247 BRUSH_SelectSolidBrush( dc, brush->logbrush.lbColor );
248 break;
250 case BS_HATCHED:
251 TRACE(gdi, "BS_HATCHED\n" );
252 physDev->brush.pixel = COLOR_ToPhysical( dc, brush->logbrush.lbColor );
253 physDev->brush.pixmap = TSXCreateBitmapFromData( display, rootWindow,
254 HatchBrushes[brush->logbrush.lbHatch], 8, 8 );
255 physDev->brush.fillStyle = FillStippled;
256 break;
258 case BS_PATTERN:
259 TRACE(gdi, "BS_PATTERN\n");
260 BRUSH_SelectPatternBrush( dc, (HBRUSH16)brush->logbrush.lbHatch );
261 break;
263 case BS_DIBPATTERN:
264 TRACE(gdi, "BS_DIBPATTERN\n");
265 if ((bmpInfo = (BITMAPINFO *) GlobalLock16( (HGLOBAL16)brush->logbrush.lbHatch )))
267 int size = DIB_BitmapInfoSize( bmpInfo, brush->logbrush.lbColor );
268 hBitmap = CreateDIBitmap32( dc->hSelf, &bmpInfo->bmiHeader,
269 CBM_INIT, ((char *)bmpInfo) + size,
270 bmpInfo,
271 (WORD)brush->logbrush.lbColor );
272 BRUSH_SelectPatternBrush( dc, hBitmap );
273 DeleteObject16( hBitmap );
274 GlobalUnlock16( (HGLOBAL16)brush->logbrush.lbHatch );
277 break;
280 return prevHandle;