Added function table to GDI objects for better encapsulation.
[wine.git] / graphics / x11drv / brush.c
blobabe72d609e569d4bd41858f7add38c67005696fb
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 "color.h"
28 #include "x11drv.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
33 #define NB_HATCH_STYLES (HS_DIAGCROSS+1)
35 static const char HatchBrushes[NB_HATCH_STYLES + 1][8] =
37 { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HS_HORIZONTAL */
38 { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HS_VERTICAL */
39 { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HS_FDIAGONAL */
40 { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HS_BDIAGONAL */
41 { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HS_CROSS */
42 { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 }, /* HS_DIAGCROSS */
43 { 0xee, 0xbb, 0xee, 0xbb, 0xee, 0xbb, 0xee, 0xbb } /* Hack for DKGRAY */
46 /* Levels of each primary for dithering */
47 #define PRIMARY_LEVELS 3
48 #define TOTAL_LEVELS (PRIMARY_LEVELS*PRIMARY_LEVELS*PRIMARY_LEVELS)
50 /* Dithering matrix size */
51 #define MATRIX_SIZE 8
52 #define MATRIX_SIZE_2 (MATRIX_SIZE*MATRIX_SIZE)
54 /* Total number of possible levels for a dithered primary color */
55 #define DITHER_LEVELS (MATRIX_SIZE_2 * (PRIMARY_LEVELS-1) + 1)
57 /* Dithering matrix */
58 static const int dither_matrix[MATRIX_SIZE_2] =
60 0, 32, 8, 40, 2, 34, 10, 42,
61 48, 16, 56, 24, 50, 18, 58, 26,
62 12, 44, 4, 36, 14, 46, 6, 38,
63 60, 28, 52, 20, 62, 30, 54, 22,
64 3, 35, 11, 43, 1, 33, 9, 41,
65 51, 19, 59, 27, 49, 17, 57, 25,
66 15, 47, 7, 39, 13, 45, 5, 37,
67 63, 31, 55, 23, 61, 29, 53, 21
70 /* Mapping between (R,G,B) triples and EGA colors */
71 static const int EGAmapping[TOTAL_LEVELS] =
73 0, /* 000000 -> 000000 */
74 4, /* 00007f -> 000080 */
75 12, /* 0000ff -> 0000ff */
76 2, /* 007f00 -> 008000 */
77 6, /* 007f7f -> 008080 */
78 6, /* 007fff -> 008080 */
79 10, /* 00ff00 -> 00ff00 */
80 6, /* 00ff7f -> 008080 */
81 14, /* 00ffff -> 00ffff */
82 1, /* 7f0000 -> 800000 */
83 5, /* 7f007f -> 800080 */
84 5, /* 7f00ff -> 800080 */
85 3, /* 7f7f00 -> 808000 */
86 8, /* 7f7f7f -> 808080 */
87 7, /* 7f7fff -> c0c0c0 */
88 3, /* 7fff00 -> 808000 */
89 7, /* 7fff7f -> c0c0c0 */
90 7, /* 7fffff -> c0c0c0 */
91 9, /* ff0000 -> ff0000 */
92 5, /* ff007f -> 800080 */
93 13, /* ff00ff -> ff00ff */
94 3, /* ff7f00 -> 808000 */
95 7, /* ff7f7f -> c0c0c0 */
96 7, /* ff7fff -> c0c0c0 */
97 11, /* ffff00 -> ffff00 */
98 7, /* ffff7f -> c0c0c0 */
99 15 /* ffffff -> ffffff */
102 #define PIXEL_VALUE(r,g,b) \
103 X11DRV_PALETTE_mapEGAPixel[EGAmapping[((r)*PRIMARY_LEVELS+(g))*PRIMARY_LEVELS+(b)]]
105 /* X image for building dithered pixmap */
106 static XImage *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 if (!ditherImage)
120 ditherImage = X11DRV_DIB_CreateXImage( MATRIX_SIZE, MATRIX_SIZE, screen_depth );
121 if (!ditherImage) return 0;
124 wine_tsx11_lock();
125 if (color != prevColor)
127 int r = GetRValue( color ) * DITHER_LEVELS;
128 int g = GetGValue( color ) * DITHER_LEVELS;
129 int b = GetBValue( color ) * DITHER_LEVELS;
130 const int *pmatrix = dither_matrix;
132 for (y = 0; y < MATRIX_SIZE; y++)
134 for (x = 0; x < MATRIX_SIZE; x++)
136 int d = *pmatrix++ * 256;
137 int dr = ((r + d) / MATRIX_SIZE_2) / 256;
138 int dg = ((g + d) / MATRIX_SIZE_2) / 256;
139 int db = ((b + d) / MATRIX_SIZE_2) / 256;
140 XPutPixel( ditherImage, x, y, PIXEL_VALUE(dr,dg,db) );
143 prevColor = color;
146 pixmap = XCreatePixmap( gdi_display, root_window, MATRIX_SIZE, MATRIX_SIZE, screen_depth );
147 XPutImage( gdi_display, pixmap, BITMAP_colorGC, ditherImage, 0, 0,
148 0, 0, MATRIX_SIZE, MATRIX_SIZE );
149 wine_tsx11_unlock();
150 return pixmap;
154 /***********************************************************************
155 * BRUSH_SelectSolidBrush
157 static void BRUSH_SelectSolidBrush( X11DRV_PDEVICE *physDev, COLORREF color )
159 DC *dc = physDev->dc;
161 if ((dc->bitsPerPixel > 1) && (screen_depth <= 8) && !X11DRV_IsSolidColor( color ))
163 /* Dithered brush */
164 physDev->brush.pixmap = BRUSH_DitherColor( dc, color );
165 physDev->brush.fillStyle = FillTiled;
166 physDev->brush.pixel = 0;
168 else
170 /* Solid brush */
171 physDev->brush.pixel = X11DRV_PALETTE_ToPhysical( physDev, color );
172 physDev->brush.fillStyle = FillSolid;
177 /***********************************************************************
178 * BRUSH_SelectPatternBrush
180 static BOOL BRUSH_SelectPatternBrush( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
182 BOOL ret = FALSE;
183 DC *dc = physDev->dc;
184 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
185 if (!bmp) return FALSE;
187 if(!bmp->physBitmap) goto done;
189 if ((dc->bitsPerPixel == 1) && (bmp->bitmap.bmBitsPixel != 1))
191 /* Special case: a color pattern on a monochrome DC */
192 physDev->brush.pixmap = TSXCreatePixmap( gdi_display, root_window, 8, 8, 1);
193 /* FIXME: should probably convert to monochrome instead */
194 TSXCopyPlane( gdi_display, (Pixmap)bmp->physBitmap, physDev->brush.pixmap,
195 BITMAP_monoGC, 0, 0, 8, 8, 0, 0, 1 );
197 else
199 physDev->brush.pixmap = TSXCreatePixmap( gdi_display, root_window,
200 8, 8, bmp->bitmap.bmBitsPixel );
201 TSXCopyArea( gdi_display, (Pixmap)bmp->physBitmap, physDev->brush.pixmap,
202 BITMAP_GC(bmp), 0, 0, 8, 8, 0, 0 );
205 if (bmp->bitmap.bmBitsPixel > 1)
207 physDev->brush.fillStyle = FillTiled;
208 physDev->brush.pixel = 0; /* Ignored */
210 else
212 physDev->brush.fillStyle = FillOpaqueStippled;
213 physDev->brush.pixel = -1; /* Special case (see DC_SetupGCForBrush) */
215 ret = TRUE;
216 done:
217 GDI_ReleaseObj( hbitmap );
218 return ret;
224 /***********************************************************************
225 * SelectBrush (X11DRV.@)
227 HBRUSH X11DRV_SelectBrush( X11DRV_PDEVICE *physDev, HBRUSH hbrush )
229 LOGBRUSH logbrush;
230 HBITMAP16 hBitmap;
231 BITMAPINFO * bmpInfo;
233 if (!GetObjectA( hbrush, sizeof(logbrush), &logbrush )) return 0;
235 TRACE("hdc=%04x hbrush=%04x\n", physDev->hdc,hbrush);
237 if (physDev->brush.pixmap)
239 TSXFreePixmap( gdi_display, physDev->brush.pixmap );
240 physDev->brush.pixmap = 0;
242 physDev->brush.style = logbrush.lbStyle;
244 switch(logbrush.lbStyle)
246 case BS_NULL:
247 TRACE("BS_NULL\n" );
248 break;
250 case BS_SOLID:
251 TRACE("BS_SOLID\n" );
252 BRUSH_SelectSolidBrush( physDev, logbrush.lbColor );
253 break;
255 case BS_HATCHED:
256 TRACE("BS_HATCHED\n" );
257 physDev->brush.pixel = X11DRV_PALETTE_ToPhysical( physDev, logbrush.lbColor );
258 physDev->brush.pixmap = TSXCreateBitmapFromData( gdi_display, root_window,
259 HatchBrushes[logbrush.lbHatch], 8, 8 );
260 physDev->brush.fillStyle = FillStippled;
261 break;
263 case BS_PATTERN:
264 TRACE("BS_PATTERN\n");
265 if (!BRUSH_SelectPatternBrush( physDev, (HBITMAP)logbrush.lbHatch )) return 0;
266 break;
268 case BS_DIBPATTERN:
269 TRACE("BS_DIBPATTERN\n");
270 if ((bmpInfo = (BITMAPINFO *) GlobalLock16( (HGLOBAL16)logbrush.lbHatch )))
272 int size = DIB_BitmapInfoSize( bmpInfo, logbrush.lbColor );
273 hBitmap = CreateDIBitmap( physDev->hdc, &bmpInfo->bmiHeader,
274 CBM_INIT, ((char *)bmpInfo) + size,
275 bmpInfo,
276 (WORD)logbrush.lbColor );
277 BRUSH_SelectPatternBrush( physDev, hBitmap );
278 DeleteObject( hBitmap );
279 GlobalUnlock16( (HGLOBAL16)logbrush.lbHatch );
282 break;
284 return hbrush;