Release 960506
[wine.git] / objects / brush.c
blobe5ee9b0554b3ac5bea325f4de0dbfe8d34820d6a
1 /*
2 * GDI brush objects
4 * Copyright 1993, 1994 Alexandre Julliard
5 */
7 #include <stdlib.h>
8 #include "brush.h"
9 #include "bitmap.h"
10 #include "metafile.h"
11 #include "color.h"
12 #include "stddebug.h"
13 #include "debug.h"
14 #include "xmalloc.h"
16 #define NB_HATCH_STYLES 6
18 static const char HatchBrushes[NB_HATCH_STYLES][8] =
20 { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HS_HORIZONTAL */
21 { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HS_VERTICAL */
22 { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HS_FDIAGONAL */
23 { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HS_BDIAGONAL */
24 { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HS_CROSS */
25 { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 } /* HS_DIAGCROSS */
28 /* Levels of each primary for dithering */
29 #define PRIMARY_LEVELS 3
30 #define TOTAL_LEVELS (PRIMARY_LEVELS*PRIMARY_LEVELS*PRIMARY_LEVELS)
32 /* Dithering matrix size */
33 #define MATRIX_SIZE 8
34 #define MATRIX_SIZE_2 (MATRIX_SIZE*MATRIX_SIZE)
36 /* Total number of possible levels for a dithered primary color */
37 #define DITHER_LEVELS (MATRIX_SIZE_2 * (PRIMARY_LEVELS-1) + 1)
39 /* Dithering matrix */
40 static const int dither_matrix[MATRIX_SIZE_2] =
42 0, 32, 8, 40, 2, 34, 10, 42,
43 48, 16, 56, 24, 50, 18, 58, 26,
44 12, 44, 4, 36, 14, 46, 6, 38,
45 60, 28, 52, 20, 62, 30, 54, 22,
46 3, 35, 11, 43, 1, 33, 9, 41,
47 51, 19, 59, 27, 49, 17, 57, 25,
48 15, 47, 7, 39, 13, 45, 5, 37,
49 63, 31, 55, 23, 61, 29, 53, 21
52 /* Mapping between (R,G,B) triples and EGA colors */
53 static const int EGAmapping[TOTAL_LEVELS] =
55 0, /* 000000 -> 000000 */
56 4, /* 00007f -> 000080 */
57 12, /* 0000ff -> 0000ff */
58 2, /* 007f00 -> 008000 */
59 6, /* 007f7f -> 008080 */
60 6, /* 007fff -> 008080 */
61 10, /* 00ff00 -> 00ff00 */
62 6, /* 00ff7f -> 008080 */
63 14, /* 00ffff -> 00ffff */
64 1, /* 7f0000 -> 800000 */
65 5, /* 7f007f -> 800080 */
66 5, /* 7f00ff -> 800080 */
67 3, /* 7f7f00 -> 808000 */
68 8, /* 7f7f7f -> 808080 */
69 7, /* 7f7fff -> c0c0c0 */
70 3, /* 7fff00 -> 808000 */
71 7, /* 7fff7f -> c0c0c0 */
72 7, /* 7fffff -> c0c0c0 */
73 9, /* ff0000 -> ff0000 */
74 5, /* ff007f -> 800080 */
75 13, /* ff00ff -> ff00ff */
76 3, /* ff7f00 -> 808000 */
77 7, /* ff7f7f -> c0c0c0 */
78 7, /* ff7fff -> c0c0c0 */
79 11, /* ffff00 -> ffff00 */
80 7, /* ffff7f -> c0c0c0 */
81 15 /* ffffff -> ffffff */
84 #define PIXEL_VALUE(r,g,b) \
85 COLOR_mapEGAPixel[EGAmapping[((r)*PRIMARY_LEVELS+(g))*PRIMARY_LEVELS+(b)]]
87 /* X image for building dithered pixmap */
88 static XImage *ditherImage = NULL;
91 /***********************************************************************
92 * BRUSH_Init
94 * Create the X image used for dithering.
96 BOOL BRUSH_Init(void)
98 XCREATEIMAGE( ditherImage, MATRIX_SIZE, MATRIX_SIZE, screenDepth );
99 return (ditherImage != NULL);
103 /***********************************************************************
104 * BRUSH_DitherColor
106 Pixmap BRUSH_DitherColor( DC *dc, COLORREF color )
108 static COLORREF prevColor = 0xffffffff;
109 unsigned int x, y;
110 Pixmap pixmap;
112 if (color != prevColor)
114 int r = GetRValue( color ) * DITHER_LEVELS;
115 int g = GetGValue( color ) * DITHER_LEVELS;
116 int b = GetBValue( color ) * DITHER_LEVELS;
117 const int *pmatrix = dither_matrix;
119 for (y = 0; y < MATRIX_SIZE; y++)
121 for (x = 0; x < MATRIX_SIZE; x++)
123 int d = *pmatrix++ * 256;
124 int dr = ((r + d) / MATRIX_SIZE_2) / 256;
125 int dg = ((g + d) / MATRIX_SIZE_2) / 256;
126 int db = ((b + d) / MATRIX_SIZE_2) / 256;
127 XPutPixel( ditherImage, x, y, PIXEL_VALUE(dr,dg,db) );
130 prevColor = color;
133 pixmap = XCreatePixmap( display, rootWindow,
134 MATRIX_SIZE, MATRIX_SIZE, screenDepth );
135 XPutImage( display, pixmap, BITMAP_colorGC, ditherImage, 0, 0,
136 0, 0, MATRIX_SIZE, MATRIX_SIZE );
137 return pixmap;
141 /***********************************************************************
142 * CreateBrushIndirect (GDI.50)
144 HBRUSH CreateBrushIndirect( const LOGBRUSH * brush )
146 BRUSHOBJ * brushPtr;
147 HBRUSH hbrush = GDI_AllocObject( sizeof(BRUSHOBJ), BRUSH_MAGIC );
148 if (!hbrush) return 0;
149 brushPtr = (BRUSHOBJ *) GDI_HEAP_LIN_ADDR( hbrush );
150 memcpy( &brushPtr->logbrush, brush, sizeof(LOGBRUSH) );
151 return hbrush;
155 /***********************************************************************
156 * CreateHatchBrush (GDI.58)
158 HBRUSH CreateHatchBrush( INT style, COLORREF color )
160 LOGBRUSH logbrush = { BS_HATCHED, color, style };
161 dprintf_gdi(stddeb, "CreateHatchBrush: %d %06lx\n", style, color );
162 if ((style < 0) || (style >= NB_HATCH_STYLES)) return 0;
163 return CreateBrushIndirect( &logbrush );
167 /***********************************************************************
168 * CreatePatternBrush (GDI.60)
170 HBRUSH CreatePatternBrush( HBITMAP hbitmap )
172 LOGBRUSH logbrush = { BS_PATTERN, 0, 0 };
173 BITMAPOBJ *bmp, *newbmp;
175 dprintf_gdi(stddeb, "CreatePatternBrush: %04x\n", hbitmap );
177 /* Make a copy of the bitmap */
179 if (!(bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
180 return 0;
181 logbrush.lbHatch = (INT)CreateBitmapIndirect( &bmp->bitmap );
182 newbmp = (BITMAPOBJ *) GDI_GetObjPtr( (HANDLE)logbrush.lbHatch, BITMAP_MAGIC );
183 if (!newbmp) return 0;
184 XCopyArea( display, bmp->pixmap, newbmp->pixmap, BITMAP_GC(bmp),
185 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight, 0, 0 );
186 return CreateBrushIndirect( &logbrush );
190 /***********************************************************************
191 * CreateDIBPatternBrush (GDI.445)
193 HBRUSH CreateDIBPatternBrush( HGLOBAL hbitmap, UINT coloruse )
195 LOGBRUSH logbrush = { BS_DIBPATTERN, coloruse, 0 };
196 BITMAPINFO *info, *newInfo;
197 int size;
199 dprintf_gdi(stddeb, "CreateDIBPatternBrush: %04x\n", hbitmap );
201 /* Make a copy of the bitmap */
203 if (!(info = (BITMAPINFO *) GlobalLock16( hbitmap ))) return 0;
205 size = info->bmiHeader.biSizeImage;
206 if (!size)
207 size = (info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) / 32
208 * 8 * info->bmiHeader.biHeight;
209 size += DIB_BitmapInfoSize( info, coloruse );
211 if (!(logbrush.lbHatch = (INT)GlobalAlloc16( GMEM_MOVEABLE, size )))
213 GlobalUnlock16( hbitmap );
214 return 0;
216 newInfo = (BITMAPINFO *) GlobalLock16( (HANDLE)logbrush.lbHatch );
217 memcpy( newInfo, info, size );
218 GlobalUnlock16( (HANDLE)logbrush.lbHatch );
219 GlobalUnlock16( hbitmap );
220 return CreateBrushIndirect( &logbrush );
224 /***********************************************************************
225 * CreateSolidBrush (GDI.66)
227 HBRUSH CreateSolidBrush( COLORREF color )
229 LOGBRUSH logbrush = { BS_SOLID, color, 0 };
230 dprintf_gdi(stddeb, "CreateSolidBrush: %06lx\n", color );
231 return CreateBrushIndirect( &logbrush );
235 /***********************************************************************
236 * SetBrushOrg (GDI.148)
238 DWORD SetBrushOrg( HDC hdc, short x, short y )
240 DWORD retval;
241 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
242 if (!dc) return FALSE;
243 retval = dc->w.brushOrgX | (dc->w.brushOrgY << 16);
244 dc->w.brushOrgX = x;
245 dc->w.brushOrgY = y;
246 return retval;
249 /***********************************************************************
250 * GetSysColorBrush (USER.281)
252 HBRUSH GetSysColorBrush(WORD x)
254 return GetStockObject(GRAY_BRUSH);
257 /***********************************************************************
258 * BRUSH_DeleteObject
260 BOOL BRUSH_DeleteObject( HBRUSH hbrush, BRUSHOBJ * brush )
262 switch(brush->logbrush.lbStyle)
264 case BS_PATTERN:
265 DeleteObject( (HANDLE)brush->logbrush.lbHatch );
266 break;
267 case BS_DIBPATTERN:
268 GlobalFree16( (HANDLE)brush->logbrush.lbHatch );
269 break;
271 return GDI_FreeObject( hbrush );
275 /***********************************************************************
276 * BRUSH_GetObject
278 int BRUSH_GetObject( BRUSHOBJ * brush, int count, LPSTR buffer )
280 if (count > sizeof(LOGBRUSH)) count = sizeof(LOGBRUSH);
281 memcpy( buffer, &brush->logbrush, count );
282 return count;
286 /***********************************************************************
287 * BRUSH_SelectSolidBrush
289 static void BRUSH_SelectSolidBrush( DC *dc, COLORREF color )
291 if ((dc->w.bitsPerPixel > 1) && (screenDepth <= 8) && !COLOR_IsSolid( color ))
293 /* Dithered brush */
294 dc->u.x.brush.pixmap = BRUSH_DitherColor( dc, color );
295 dc->u.x.brush.fillStyle = FillTiled;
296 dc->u.x.brush.pixel = 0;
298 else
300 /* Solid brush */
301 dc->u.x.brush.pixel = COLOR_ToPhysical( dc, color );
302 dc->u.x.brush.fillStyle = FillSolid;
307 /***********************************************************************
308 * BRUSH_SelectPatternBrush
310 static BOOL BRUSH_SelectPatternBrush( DC * dc, HBITMAP hbitmap )
312 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
313 if (!bmp) return FALSE;
314 dc->u.x.brush.pixmap = XCreatePixmap( display, rootWindow,
315 8, 8, bmp->bitmap.bmBitsPixel );
316 XCopyArea( display, bmp->pixmap, dc->u.x.brush.pixmap,
317 BITMAP_GC(bmp), 0, 0, 8, 8, 0, 0 );
319 if (bmp->bitmap.bmBitsPixel > 1)
321 dc->u.x.brush.fillStyle = FillTiled;
322 dc->u.x.brush.pixel = 0; /* Ignored */
324 else
326 dc->u.x.brush.fillStyle = FillOpaqueStippled;
327 dc->u.x.brush.pixel = -1; /* Special case (see DC_SetupGCForBrush) */
329 return TRUE;
334 /***********************************************************************
335 * BRUSH_SelectObject
337 HBRUSH BRUSH_SelectObject( HDC hdc, DC * dc, HBRUSH hbrush, BRUSHOBJ * brush )
339 HBITMAP hBitmap;
340 BITMAPINFO * bmpInfo;
341 HBRUSH prevHandle = dc->w.hBrush;
343 dprintf_gdi(stddeb, "Brush_SelectObject: hdc=%04x hbrush=%04x\n",
344 hdc,hbrush);
345 if (dc->header.wMagic == METAFILE_DC_MAGIC)
347 switch (brush->logbrush.lbStyle)
349 case BS_SOLID:
350 case BS_HATCHED:
351 case BS_HOLLOW:
352 if (!MF_CreateBrushIndirect(dc, hbrush, &(brush->logbrush)))
353 return (HBRUSH)0;
354 break;
356 case BS_PATTERN:
357 case BS_DIBPATTERN:
358 if (!MF_CreatePatternBrush(dc, hbrush, &(brush->logbrush)))
359 return (HBRUSH)0;
360 break;
362 return (HBRUSH)1;
365 dc->w.hBrush = hbrush;
367 if (dc->u.x.brush.pixmap)
369 XFreePixmap( display, dc->u.x.brush.pixmap );
370 dc->u.x.brush.pixmap = 0;
372 dc->u.x.brush.style = brush->logbrush.lbStyle;
374 switch(brush->logbrush.lbStyle)
376 case BS_NULL:
377 dprintf_gdi( stddeb,"BS_NULL\n" );
378 break;
380 case BS_SOLID:
381 dprintf_gdi( stddeb,"BS_SOLID\n" );
382 BRUSH_SelectSolidBrush( dc, brush->logbrush.lbColor );
383 break;
385 case BS_HATCHED:
386 dprintf_gdi( stddeb, "BS_HATCHED\n" );
387 dc->u.x.brush.pixel = COLOR_ToPhysical( dc, brush->logbrush.lbColor );
388 dc->u.x.brush.pixmap = XCreateBitmapFromData( display, rootWindow,
389 HatchBrushes[brush->logbrush.lbHatch], 8, 8 );
390 dc->u.x.brush.fillStyle = FillStippled;
391 break;
393 case BS_PATTERN:
394 dprintf_gdi( stddeb, "BS_PATTERN\n");
395 BRUSH_SelectPatternBrush( dc, (HBRUSH)brush->logbrush.lbHatch );
396 break;
398 case BS_DIBPATTERN:
399 dprintf_gdi( stddeb, "BS_DIBPATTERN\n");
400 if ((bmpInfo = (BITMAPINFO *) GlobalLock16( (HANDLE)brush->logbrush.lbHatch )))
402 int size = DIB_BitmapInfoSize( bmpInfo, brush->logbrush.lbColor );
403 hBitmap = CreateDIBitmap( hdc, &bmpInfo->bmiHeader, CBM_INIT,
404 ((char *)bmpInfo) + size, bmpInfo,
405 (WORD) brush->logbrush.lbColor );
406 BRUSH_SelectPatternBrush( dc, hBitmap );
407 DeleteObject( hBitmap );
408 GlobalUnlock16( (HANDLE)brush->logbrush.lbHatch );
411 break;
414 return prevHandle;