Porting fix.
[wine/multimedia.git] / graphics / x11drv / brush.c
blob7c0d802d157ea456471dacde1e334e8813b82561
1 /*
2 * X11DRV brush objects
4 * Copyright 1993, 1994 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
23 #include "ts_xlib.h"
25 #include <stdlib.h>
26 #include "bitmap.h"
27 #include "x11drv.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
32 #define NB_HATCH_STYLES (HS_DIAGCROSS+1)
34 static const char HatchBrushes[NB_HATCH_STYLES + 1][8] =
36 { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HS_HORIZONTAL */
37 { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HS_VERTICAL */
38 { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HS_FDIAGONAL */
39 { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HS_BDIAGONAL */
40 { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HS_CROSS */
41 { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 }, /* HS_DIAGCROSS */
42 { 0xee, 0xbb, 0xee, 0xbb, 0xee, 0xbb, 0xee, 0xbb } /* Hack for DKGRAY */
45 /* Levels of each primary for dithering */
46 #define PRIMARY_LEVELS 3
47 #define TOTAL_LEVELS (PRIMARY_LEVELS*PRIMARY_LEVELS*PRIMARY_LEVELS)
49 /* Dithering matrix size */
50 #define MATRIX_SIZE 8
51 #define MATRIX_SIZE_2 (MATRIX_SIZE*MATRIX_SIZE)
53 /* Total number of possible levels for a dithered primary color */
54 #define DITHER_LEVELS (MATRIX_SIZE_2 * (PRIMARY_LEVELS-1) + 1)
56 /* Dithering matrix */
57 static const int dither_matrix[MATRIX_SIZE_2] =
59 0, 32, 8, 40, 2, 34, 10, 42,
60 48, 16, 56, 24, 50, 18, 58, 26,
61 12, 44, 4, 36, 14, 46, 6, 38,
62 60, 28, 52, 20, 62, 30, 54, 22,
63 3, 35, 11, 43, 1, 33, 9, 41,
64 51, 19, 59, 27, 49, 17, 57, 25,
65 15, 47, 7, 39, 13, 45, 5, 37,
66 63, 31, 55, 23, 61, 29, 53, 21
69 /* Mapping between (R,G,B) triples and EGA colors */
70 static const int EGAmapping[TOTAL_LEVELS] =
72 0, /* 000000 -> 000000 */
73 4, /* 00007f -> 000080 */
74 12, /* 0000ff -> 0000ff */
75 2, /* 007f00 -> 008000 */
76 6, /* 007f7f -> 008080 */
77 6, /* 007fff -> 008080 */
78 10, /* 00ff00 -> 00ff00 */
79 6, /* 00ff7f -> 008080 */
80 14, /* 00ffff -> 00ffff */
81 1, /* 7f0000 -> 800000 */
82 5, /* 7f007f -> 800080 */
83 5, /* 7f00ff -> 800080 */
84 3, /* 7f7f00 -> 808000 */
85 8, /* 7f7f7f -> 808080 */
86 7, /* 7f7fff -> c0c0c0 */
87 3, /* 7fff00 -> 808000 */
88 7, /* 7fff7f -> c0c0c0 */
89 7, /* 7fffff -> c0c0c0 */
90 9, /* ff0000 -> ff0000 */
91 5, /* ff007f -> 800080 */
92 13, /* ff00ff -> ff00ff */
93 3, /* ff7f00 -> 808000 */
94 7, /* ff7f7f -> c0c0c0 */
95 7, /* ff7fff -> c0c0c0 */
96 11, /* ffff00 -> ffff00 */
97 7, /* ffff7f -> c0c0c0 */
98 15 /* ffffff -> ffffff */
101 #define PIXEL_VALUE(r,g,b) \
102 X11DRV_PALETTE_mapEGAPixel[EGAmapping[((r)*PRIMARY_LEVELS+(g))*PRIMARY_LEVELS+(b)]]
104 /* X image for building dithered pixmap */
105 static XImage *ditherImage = NULL;
108 /***********************************************************************
109 * BRUSH_DitherColor
111 static Pixmap BRUSH_DitherColor( DC *dc, COLORREF color )
113 static COLORREF prevColor = 0xffffffff;
114 unsigned int x, y;
115 Pixmap pixmap;
117 if (!ditherImage)
119 ditherImage = X11DRV_DIB_CreateXImage( MATRIX_SIZE, MATRIX_SIZE, screen_depth );
120 if (!ditherImage) return 0;
123 wine_tsx11_lock();
124 if (color != prevColor)
126 int r = GetRValue( color ) * DITHER_LEVELS;
127 int g = GetGValue( color ) * DITHER_LEVELS;
128 int b = GetBValue( color ) * DITHER_LEVELS;
129 const int *pmatrix = dither_matrix;
131 for (y = 0; y < MATRIX_SIZE; y++)
133 for (x = 0; x < MATRIX_SIZE; x++)
135 int d = *pmatrix++ * 256;
136 int dr = ((r + d) / MATRIX_SIZE_2) / 256;
137 int dg = ((g + d) / MATRIX_SIZE_2) / 256;
138 int db = ((b + d) / MATRIX_SIZE_2) / 256;
139 XPutPixel( ditherImage, x, y, PIXEL_VALUE(dr,dg,db) );
142 prevColor = color;
145 pixmap = XCreatePixmap( gdi_display, root_window, MATRIX_SIZE, MATRIX_SIZE, screen_depth );
146 XPutImage( gdi_display, pixmap, BITMAP_colorGC, ditherImage, 0, 0,
147 0, 0, MATRIX_SIZE, MATRIX_SIZE );
148 wine_tsx11_unlock();
149 return pixmap;
153 /***********************************************************************
154 * BRUSH_SelectSolidBrush
156 static void BRUSH_SelectSolidBrush( X11DRV_PDEVICE *physDev, COLORREF color )
158 DC *dc = physDev->dc;
160 if ((dc->bitsPerPixel > 1) && (screen_depth <= 8) && !X11DRV_IsSolidColor( color ))
162 /* Dithered brush */
163 physDev->brush.pixmap = BRUSH_DitherColor( dc, color );
164 physDev->brush.fillStyle = FillTiled;
165 physDev->brush.pixel = 0;
167 else
169 /* Solid brush */
170 physDev->brush.pixel = X11DRV_PALETTE_ToPhysical( physDev, color );
171 physDev->brush.fillStyle = FillSolid;
176 /***********************************************************************
177 * BRUSH_SelectPatternBrush
179 static BOOL BRUSH_SelectPatternBrush( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
181 BOOL ret = FALSE;
182 DC *dc = physDev->dc;
183 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
184 if (!bmp) return FALSE;
186 if(!bmp->physBitmap) goto done;
188 if ((dc->bitsPerPixel == 1) && (bmp->bitmap.bmBitsPixel != 1))
190 /* Special case: a color pattern on a monochrome DC */
191 physDev->brush.pixmap = TSXCreatePixmap( gdi_display, root_window, 8, 8, 1);
192 /* FIXME: should probably convert to monochrome instead */
193 TSXCopyPlane( gdi_display, (Pixmap)bmp->physBitmap, physDev->brush.pixmap,
194 BITMAP_monoGC, 0, 0, 8, 8, 0, 0, 1 );
196 else
198 physDev->brush.pixmap = TSXCreatePixmap( gdi_display, root_window,
199 8, 8, bmp->bitmap.bmBitsPixel );
200 TSXCopyArea( gdi_display, (Pixmap)bmp->physBitmap, physDev->brush.pixmap,
201 BITMAP_GC(bmp), 0, 0, 8, 8, 0, 0 );
204 if (bmp->bitmap.bmBitsPixel > 1)
206 physDev->brush.fillStyle = FillTiled;
207 physDev->brush.pixel = 0; /* Ignored */
209 else
211 physDev->brush.fillStyle = FillOpaqueStippled;
212 physDev->brush.pixel = -1; /* Special case (see DC_SetupGCForBrush) */
214 ret = TRUE;
215 done:
216 GDI_ReleaseObj( hbitmap );
217 return ret;
223 /***********************************************************************
224 * SelectBrush (X11DRV.@)
226 HBRUSH X11DRV_SelectBrush( X11DRV_PDEVICE *physDev, HBRUSH hbrush )
228 LOGBRUSH logbrush;
229 HBITMAP16 hBitmap;
230 BITMAPINFO * bmpInfo;
232 if (!GetObjectA( hbrush, sizeof(logbrush), &logbrush )) return 0;
234 TRACE("hdc=%04x hbrush=%04x\n", physDev->hdc,hbrush);
236 if (physDev->brush.pixmap)
238 TSXFreePixmap( gdi_display, physDev->brush.pixmap );
239 physDev->brush.pixmap = 0;
241 physDev->brush.style = logbrush.lbStyle;
243 switch(logbrush.lbStyle)
245 case BS_NULL:
246 TRACE("BS_NULL\n" );
247 break;
249 case BS_SOLID:
250 TRACE("BS_SOLID\n" );
251 BRUSH_SelectSolidBrush( physDev, logbrush.lbColor );
252 break;
254 case BS_HATCHED:
255 TRACE("BS_HATCHED\n" );
256 physDev->brush.pixel = X11DRV_PALETTE_ToPhysical( physDev, logbrush.lbColor );
257 physDev->brush.pixmap = TSXCreateBitmapFromData( gdi_display, root_window,
258 HatchBrushes[logbrush.lbHatch], 8, 8 );
259 physDev->brush.fillStyle = FillStippled;
260 break;
262 case BS_PATTERN:
263 TRACE("BS_PATTERN\n");
264 if (!BRUSH_SelectPatternBrush( physDev, (HBITMAP)logbrush.lbHatch )) return 0;
265 break;
267 case BS_DIBPATTERN:
268 TRACE("BS_DIBPATTERN\n");
269 if ((bmpInfo = (BITMAPINFO *) GlobalLock16( (HGLOBAL16)logbrush.lbHatch )))
271 int size = DIB_BitmapInfoSize( bmpInfo, logbrush.lbColor );
272 hBitmap = CreateDIBitmap( physDev->hdc, &bmpInfo->bmiHeader,
273 CBM_INIT, ((char *)bmpInfo) + size,
274 bmpInfo,
275 (WORD)logbrush.lbColor );
276 BRUSH_SelectPatternBrush( physDev, hBitmap );
277 DeleteObject( hBitmap );
278 GlobalUnlock16( (HGLOBAL16)logbrush.lbHatch );
281 break;
283 return hbrush;