Added LGPL standard comment, and copyright notices where necessary.
[wine/multimedia.git] / graphics / x11drv / brush.c
blob55c6f78e2e7b9e711088d09ca1f5fb46936c4676
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( DC *dc, COLORREF color )
159 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
161 if ((dc->bitsPerPixel > 1) && (screen_depth <= 8) && !COLOR_IsSolid( 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( dc, color );
172 physDev->brush.fillStyle = FillSolid;
177 /***********************************************************************
178 * BRUSH_SelectPatternBrush
180 static BOOL BRUSH_SelectPatternBrush( DC * dc, HBITMAP hbitmap )
182 BOOL ret = FALSE;
183 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
184 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
185 if (!bmp) return FALSE;
187 if(!bmp->physBitmap)
188 if(!X11DRV_CreateBitmap(hbitmap))
189 goto done;
191 if(bmp->funcs != dc->funcs) {
192 WARN("Trying to select non-X11 DDB into an X11 dc\n");
193 goto done;
196 if ((dc->bitsPerPixel == 1) && (bmp->bitmap.bmBitsPixel != 1))
198 /* Special case: a color pattern on a monochrome DC */
199 physDev->brush.pixmap = TSXCreatePixmap( gdi_display, root_window, 8, 8, 1);
200 /* FIXME: should probably convert to monochrome instead */
201 TSXCopyPlane( gdi_display, (Pixmap)bmp->physBitmap, physDev->brush.pixmap,
202 BITMAP_monoGC, 0, 0, 8, 8, 0, 0, 1 );
204 else
206 physDev->brush.pixmap = TSXCreatePixmap( gdi_display, root_window,
207 8, 8, bmp->bitmap.bmBitsPixel );
208 TSXCopyArea( gdi_display, (Pixmap)bmp->physBitmap, physDev->brush.pixmap,
209 BITMAP_GC(bmp), 0, 0, 8, 8, 0, 0 );
212 if (bmp->bitmap.bmBitsPixel > 1)
214 physDev->brush.fillStyle = FillTiled;
215 physDev->brush.pixel = 0; /* Ignored */
217 else
219 physDev->brush.fillStyle = FillOpaqueStippled;
220 physDev->brush.pixel = -1; /* Special case (see DC_SetupGCForBrush) */
222 ret = TRUE;
223 done:
224 GDI_ReleaseObj( hbitmap );
225 return ret;
231 /***********************************************************************
232 * BRUSH_SelectObject
234 HBRUSH X11DRV_BRUSH_SelectObject( DC * dc, HBRUSH hbrush )
236 LOGBRUSH logbrush;
237 HBITMAP16 hBitmap;
238 BITMAPINFO * bmpInfo;
239 HBRUSH16 prevHandle = dc->hBrush;
240 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
242 if (!GetObjectA( hbrush, sizeof(logbrush), &logbrush )) return 0;
244 TRACE("hdc=%04x hbrush=%04x\n", dc->hSelf,hbrush);
246 dc->hBrush = hbrush;
248 if (physDev->brush.pixmap)
250 TSXFreePixmap( gdi_display, physDev->brush.pixmap );
251 physDev->brush.pixmap = 0;
253 physDev->brush.style = logbrush.lbStyle;
255 switch(logbrush.lbStyle)
257 case BS_NULL:
258 TRACE("BS_NULL\n" );
259 break;
261 case BS_SOLID:
262 TRACE("BS_SOLID\n" );
263 BRUSH_SelectSolidBrush( dc, logbrush.lbColor );
264 break;
266 case BS_HATCHED:
267 TRACE("BS_HATCHED\n" );
268 physDev->brush.pixel = X11DRV_PALETTE_ToPhysical( dc, logbrush.lbColor );
269 physDev->brush.pixmap = TSXCreateBitmapFromData( gdi_display, root_window,
270 HatchBrushes[logbrush.lbHatch], 8, 8 );
271 physDev->brush.fillStyle = FillStippled;
272 break;
274 case BS_PATTERN:
275 TRACE("BS_PATTERN\n");
276 BRUSH_SelectPatternBrush( dc, (HBRUSH16)logbrush.lbHatch );
277 break;
279 case BS_DIBPATTERN:
280 TRACE("BS_DIBPATTERN\n");
281 if ((bmpInfo = (BITMAPINFO *) GlobalLock16( (HGLOBAL16)logbrush.lbHatch )))
283 int size = DIB_BitmapInfoSize( bmpInfo, logbrush.lbColor );
284 hBitmap = CreateDIBitmap( dc->hSelf, &bmpInfo->bmiHeader,
285 CBM_INIT, ((char *)bmpInfo) + size,
286 bmpInfo,
287 (WORD)logbrush.lbColor );
288 BRUSH_SelectPatternBrush( dc, hBitmap );
289 DeleteObject( hBitmap );
290 GlobalUnlock16( (HGLOBAL16)logbrush.lbHatch );
293 break;
296 return prevHandle;