vbscript: Fix memory leak in owned safearray iterator.
[wine.git] / dlls / winex11.drv / brush.c
blob9ecb5ec5eda7532710200a8d8e91283144cad955
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #if 0
22 #pragma makedep unix
23 #endif
25 #include "config.h"
27 #include <stdlib.h>
29 #include "x11drv.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
34 static const char HatchBrushes[][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 */
44 /* Levels of each primary for dithering */
45 #define PRIMARY_LEVELS 3
46 #define TOTAL_LEVELS (PRIMARY_LEVELS*PRIMARY_LEVELS*PRIMARY_LEVELS)
48 /* Dithering matrix size */
49 #define MATRIX_SIZE 8
50 #define MATRIX_SIZE_2 (MATRIX_SIZE*MATRIX_SIZE)
52 /* Total number of possible levels for a dithered primary color */
53 #define DITHER_LEVELS (MATRIX_SIZE_2 * (PRIMARY_LEVELS-1) + 1)
55 /* Dithering matrix */
56 static const int dither_matrix[MATRIX_SIZE_2] =
58 0, 32, 8, 40, 2, 34, 10, 42,
59 48, 16, 56, 24, 50, 18, 58, 26,
60 12, 44, 4, 36, 14, 46, 6, 38,
61 60, 28, 52, 20, 62, 30, 54, 22,
62 3, 35, 11, 43, 1, 33, 9, 41,
63 51, 19, 59, 27, 49, 17, 57, 25,
64 15, 47, 7, 39, 13, 45, 5, 37,
65 63, 31, 55, 23, 61, 29, 53, 21
68 /* Mapping between (R,G,B) triples and EGA colors */
69 static const int EGAmapping[TOTAL_LEVELS] =
71 0, /* 000000 -> 000000 */
72 4, /* 00007f -> 000080 */
73 12, /* 0000ff -> 0000ff */
74 2, /* 007f00 -> 008000 */
75 6, /* 007f7f -> 008080 */
76 6, /* 007fff -> 008080 */
77 10, /* 00ff00 -> 00ff00 */
78 6, /* 00ff7f -> 008080 */
79 14, /* 00ffff -> 00ffff */
80 1, /* 7f0000 -> 800000 */
81 5, /* 7f007f -> 800080 */
82 5, /* 7f00ff -> 800080 */
83 3, /* 7f7f00 -> 808000 */
84 8, /* 7f7f7f -> 808080 */
85 7, /* 7f7fff -> c0c0c0 */
86 3, /* 7fff00 -> 808000 */
87 7, /* 7fff7f -> c0c0c0 */
88 7, /* 7fffff -> c0c0c0 */
89 9, /* ff0000 -> ff0000 */
90 5, /* ff007f -> 800080 */
91 13, /* ff00ff -> ff00ff */
92 3, /* ff7f00 -> 808000 */
93 7, /* ff7f7f -> c0c0c0 */
94 7, /* ff7fff -> c0c0c0 */
95 11, /* ffff00 -> ffff00 */
96 7, /* ffff7f -> c0c0c0 */
97 15 /* ffffff -> ffffff */
100 #define PIXEL_VALUE(r,g,b) \
101 X11DRV_PALETTE_mapEGAPixel[EGAmapping[((r)*PRIMARY_LEVELS+(g))*PRIMARY_LEVELS+(b)]]
103 static const COLORREF BLACK = RGB(0, 0, 0);
104 static const COLORREF WHITE = RGB(0xff, 0xff, 0xff);
106 /***********************************************************************
107 * BRUSH_DitherColor
109 static Pixmap BRUSH_DitherColor( COLORREF color, int depth)
111 /* X image for building dithered pixmap */
112 static XImage *ditherImage = NULL;
113 static COLORREF prevColor = 0xffffffff;
114 unsigned int x, y;
115 Pixmap pixmap;
116 GC gc;
118 XLockDisplay( gdi_display );
119 if (!ditherImage)
121 ditherImage = XCreateImage( gdi_display, default_visual.visual, depth, ZPixmap, 0,
122 NULL, MATRIX_SIZE, MATRIX_SIZE, 32, 0 );
123 if (!ditherImage)
125 ERR("Could not create dither image\n");
126 XUnlockDisplay( gdi_display );
127 return 0;
129 ditherImage->data = malloc( ditherImage->height * ditherImage->bytes_per_line );
132 if (color != prevColor)
134 int r = GetRValue( color ) * DITHER_LEVELS;
135 int g = GetGValue( color ) * DITHER_LEVELS;
136 int b = GetBValue( color ) * DITHER_LEVELS;
137 const int *pmatrix = dither_matrix;
139 for (y = 0; y < MATRIX_SIZE; y++)
141 for (x = 0; x < MATRIX_SIZE; x++)
143 int d = *pmatrix++ * 256;
144 int dr = ((r + d) / MATRIX_SIZE_2) / 256;
145 int dg = ((g + d) / MATRIX_SIZE_2) / 256;
146 int db = ((b + d) / MATRIX_SIZE_2) / 256;
147 XPutPixel( ditherImage, x, y, PIXEL_VALUE(dr,dg,db) );
150 prevColor = color;
153 pixmap = XCreatePixmap( gdi_display, root_window, MATRIX_SIZE, MATRIX_SIZE, depth );
154 gc = XCreateGC( gdi_display, pixmap, 0, NULL );
155 XPutImage( gdi_display, pixmap, gc, ditherImage, 0, 0, 0, 0, MATRIX_SIZE, MATRIX_SIZE );
156 XFreeGC( gdi_display, gc );
157 XUnlockDisplay( gdi_display );
159 return pixmap;
163 /***********************************************************************
164 * BRUSH_DitherMono
166 static Pixmap BRUSH_DitherMono( COLORREF color )
168 /* This makes the spray work in Win 3.11 pbrush.exe */
169 /* FIXME. Extend this basic selection of dither patterns */
170 static const char gray_dither[][2] = {{ 0x1, 0x0 }, /* DKGRAY */
171 { 0x2, 0x1 }, /* GRAY */
172 { 0x1, 0x3 }, /* LTGRAY */
174 int gray = (30 * GetRValue(color) + 59 * GetGValue(color) + 11 * GetBValue(color)) / 100;
175 int idx = gray * (ARRAY_SIZE( gray_dither ) + 1)/256 - 1;
177 TRACE("color=%s -> gray=%x\n", debugstr_color(color), gray);
178 return XCreateBitmapFromData( gdi_display, root_window, gray_dither[idx], 2, 2 );
181 /***********************************************************************
182 * BRUSH_SelectSolidBrush
184 static void BRUSH_SelectSolidBrush( X11DRV_PDEVICE *physDev, COLORREF color )
186 COLORREF colorRGB = X11DRV_PALETTE_GetColor( physDev, color );
187 if ((physDev->depth > 1) && (default_visual.depth <= 8) && !X11DRV_IsSolidColor( color ))
189 /* Dithered brush */
190 physDev->brush.pixmap = BRUSH_DitherColor( colorRGB, physDev->depth );
191 physDev->brush.fillStyle = FillTiled;
192 physDev->brush.pixel = 0;
194 else if (physDev->depth == 1 && colorRGB != WHITE && colorRGB != BLACK)
196 physDev->brush.pixel = 0;
197 physDev->brush.pixmap = BRUSH_DitherMono( colorRGB );
198 physDev->brush.fillStyle = FillTiled;
200 else
202 /* Solid brush */
203 physDev->brush.pixel = X11DRV_PALETTE_ToPhysical( physDev, color );
204 physDev->brush.fillStyle = FillSolid;
209 static BOOL select_pattern_brush( X11DRV_PDEVICE *physdev, const struct brush_pattern *pattern )
211 XVisualInfo vis = default_visual;
212 Pixmap pixmap;
213 const BITMAPINFO *info = pattern->info;
215 if (physdev->depth == 1 || info->bmiHeader.biBitCount == 1) vis.depth = 1;
217 pixmap = create_pixmap_from_image( physdev->dev.hdc, &vis, info, &pattern->bits, pattern->usage );
218 if (!pixmap) return FALSE;
220 if (physdev->brush.pixmap) XFreePixmap( gdi_display, physdev->brush.pixmap );
221 physdev->brush.pixmap = pixmap;
223 if (vis.depth == 1)
225 physdev->brush.fillStyle = FillOpaqueStippled;
226 physdev->brush.pixel = -1; /* Special case (see DC_SetupGCForBrush) */
228 else
230 physdev->brush.fillStyle = FillTiled;
231 physdev->brush.pixel = 0; /* Ignored */
233 return TRUE;
236 /***********************************************************************
237 * SelectBrush (X11DRV.@)
239 HBRUSH CDECL X11DRV_SelectBrush( PHYSDEV dev, HBRUSH hbrush, const struct brush_pattern *pattern )
241 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
242 LOGBRUSH logbrush;
244 if (pattern) /* pattern brush */
246 if (!select_pattern_brush( physDev, pattern )) return 0;
247 TRACE("BS_PATTERN\n");
248 physDev->brush.style = BS_PATTERN;
249 return hbrush;
252 if (!NtGdiExtGetObjectW( hbrush, sizeof(logbrush), &logbrush )) return 0;
254 TRACE("hdc=%p hbrush=%p\n", dev->hdc, hbrush);
256 if (physDev->brush.pixmap)
258 XFreePixmap( gdi_display, physDev->brush.pixmap );
259 physDev->brush.pixmap = 0;
261 physDev->brush.style = logbrush.lbStyle;
262 if (hbrush == GetStockObject( DC_BRUSH ))
263 NtGdiGetDCDword( dev->hdc, NtGdiGetDCBrushColor, &logbrush.lbColor );
265 switch(logbrush.lbStyle)
267 case BS_NULL:
268 TRACE("BS_NULL\n" );
269 break;
271 case BS_SOLID:
272 TRACE("BS_SOLID\n" );
273 BRUSH_SelectSolidBrush( physDev, logbrush.lbColor );
274 break;
276 case BS_HATCHED:
277 TRACE("BS_HATCHED\n" );
278 physDev->brush.pixel = X11DRV_PALETTE_ToPhysical( physDev, logbrush.lbColor );
279 physDev->brush.pixmap = XCreateBitmapFromData( gdi_display, root_window,
280 HatchBrushes[logbrush.lbHatch], 8, 8 );
281 physDev->brush.fillStyle = FillStippled;
282 break;
284 return hbrush;
288 /***********************************************************************
289 * SetDCBrushColor (X11DRV.@)
291 COLORREF CDECL X11DRV_SetDCBrushColor( PHYSDEV dev, COLORREF crColor )
293 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
295 if (NtGdiGetDCObject( dev->hdc, NTGDI_OBJ_BRUSH ) == GetStockObject( DC_BRUSH ))
296 BRUSH_SelectSolidBrush( physDev, crColor );
298 return crColor;