DOSFS_ToDosFCBFormat: fail if extension longer than 3 characters.
[wine/gsoc-2012-control.git] / graphics / x11drv / brush.c
blob7abe1d823fe25a6e9146125e502f1ba1984ad311
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 "local.h"
21 DEFAULT_DEBUG_CHANNEL(gdi);
23 static const char HatchBrushes[NB_HATCH_STYLES + 1][8] =
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 */
34 /* Levels of each primary for dithering */
35 #define PRIMARY_LEVELS 3
36 #define TOTAL_LEVELS (PRIMARY_LEVELS*PRIMARY_LEVELS*PRIMARY_LEVELS)
38 /* Dithering matrix size */
39 #define MATRIX_SIZE 8
40 #define MATRIX_SIZE_2 (MATRIX_SIZE*MATRIX_SIZE)
42 /* Total number of possible levels for a dithered primary color */
43 #define DITHER_LEVELS (MATRIX_SIZE_2 * (PRIMARY_LEVELS-1) + 1)
45 /* Dithering matrix */
46 static const int dither_matrix[MATRIX_SIZE_2] =
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
58 /* Mapping between (R,G,B) triples and EGA colors */
59 static const int EGAmapping[TOTAL_LEVELS] =
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 */
90 #define PIXEL_VALUE(r,g,b) \
91 X11DRV_PALETTE_mapEGAPixel[EGAmapping[((r)*PRIMARY_LEVELS+(g))*PRIMARY_LEVELS+(b)]]
93 /* X image for building dithered pixmap */
94 static XImage *ditherImage = NULL;
97 /***********************************************************************
98 * BRUSH_Init
100 * Create the X image used for dithering.
102 BOOL X11DRV_BRUSH_Init(void)
104 XCREATEIMAGE( ditherImage, MATRIX_SIZE, MATRIX_SIZE, X11DRV_GetDepth() );
105 return (ditherImage != NULL);
109 /***********************************************************************
110 * BRUSH_DitherColor
112 static Pixmap BRUSH_DitherColor( DC *dc, COLORREF color )
114 static COLORREF prevColor = 0xffffffff;
115 unsigned int x, y;
116 Pixmap pixmap;
118 EnterCriticalSection( &X11DRV_CritSection );
119 if (color != prevColor)
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;
126 for (y = 0; y < MATRIX_SIZE; y++)
128 for (x = 0; x < MATRIX_SIZE; x++)
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) );
137 prevColor = color;
140 pixmap = XCreatePixmap( display, X11DRV_GetXRootWindow(),
141 MATRIX_SIZE, MATRIX_SIZE, X11DRV_GetDepth() );
142 XPutImage( display, pixmap, BITMAP_colorGC, ditherImage, 0, 0,
143 0, 0, MATRIX_SIZE, MATRIX_SIZE );
144 LeaveCriticalSection( &X11DRV_CritSection );
145 return pixmap;
149 /***********************************************************************
150 * BRUSH_SelectSolidBrush
152 static void BRUSH_SelectSolidBrush( DC *dc, COLORREF color )
154 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
156 if ((dc->w.bitsPerPixel > 1) && (X11DRV_GetDepth() <= 8) && !COLOR_IsSolid( color ))
158 /* Dithered brush */
159 physDev->brush.pixmap = BRUSH_DitherColor( dc, color );
160 physDev->brush.fillStyle = FillTiled;
161 physDev->brush.pixel = 0;
163 else
165 /* Solid brush */
166 physDev->brush.pixel = X11DRV_PALETTE_ToPhysical( dc, color );
167 physDev->brush.fillStyle = FillSolid;
172 /***********************************************************************
173 * BRUSH_SelectPatternBrush
175 static BOOL BRUSH_SelectPatternBrush( DC * dc, HBITMAP hbitmap )
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;
182 if(!bmp->DDBitmap)
183 if(!X11DRV_CreateBitmap(hbitmap))
184 return 0;
186 if(bmp->DDBitmap->funcs != dc->funcs) {
187 WARN("Trying to select non-X11 DDB into an X11 dc\n");
188 return 0;
191 pbitmap = bmp->DDBitmap->physBitmap;
193 if ((dc->w.bitsPerPixel == 1) && (bmp->bitmap.bmBitsPixel != 1))
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 );
201 else
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 );
209 if (bmp->bitmap.bmBitsPixel > 1)
211 physDev->brush.fillStyle = FillTiled;
212 physDev->brush.pixel = 0; /* Ignored */
214 else
216 physDev->brush.fillStyle = FillOpaqueStippled;
217 physDev->brush.pixel = -1; /* Special case (see DC_SetupGCForBrush) */
219 GDI_HEAP_UNLOCK( hbitmap );
220 return TRUE;
226 /***********************************************************************
227 * BRUSH_SelectObject
229 HBRUSH X11DRV_BRUSH_SelectObject( DC * dc, HBRUSH hbrush, BRUSHOBJ * brush )
231 HBITMAP16 hBitmap;
232 BITMAPINFO * bmpInfo;
233 HBRUSH16 prevHandle = dc->w.hBrush;
234 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
236 TRACE("hdc=%04x hbrush=%04x\n",
237 dc->hSelf,hbrush);
239 dc->w.hBrush = hbrush;
241 if (physDev->brush.pixmap)
243 TSXFreePixmap( display, physDev->brush.pixmap );
244 physDev->brush.pixmap = 0;
246 physDev->brush.style = brush->logbrush.lbStyle;
248 switch(brush->logbrush.lbStyle)
250 case BS_NULL:
251 TRACE("BS_NULL\n" );
252 break;
254 case BS_SOLID:
255 TRACE("BS_SOLID\n" );
256 BRUSH_SelectSolidBrush( dc, brush->logbrush.lbColor );
257 break;
259 case BS_HATCHED:
260 TRACE("BS_HATCHED\n" );
261 physDev->brush.pixel = X11DRV_PALETTE_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;
267 case BS_PATTERN:
268 TRACE("BS_PATTERN\n");
269 BRUSH_SelectPatternBrush( dc, (HBRUSH16)brush->logbrush.lbHatch );
270 break;
272 case BS_DIBPATTERN:
273 TRACE("BS_DIBPATTERN\n");
274 if ((bmpInfo = (BITMAPINFO *) GlobalLock16( (HGLOBAL16)brush->logbrush.lbHatch )))
276 int size = DIB_BitmapInfoSize( bmpInfo, brush->logbrush.lbColor );
277 hBitmap = CreateDIBitmap( 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 );
286 break;
289 return prevHandle;
292 #endif /* !defined(X_DISPLAY_MISSING) */