Release 960811
[wine/hacks.git] / objects / brush.c
blob27c847fa792b29854126a8053a4c24b9990e6cc3
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 LOGBRUSH16 * 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(*brush) );
151 return hbrush;
155 /***********************************************************************
156 * CreateHatchBrush (GDI.58)
158 HBRUSH CreateHatchBrush( INT style, COLORREF color )
160 LOGBRUSH16 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 LOGBRUSH16 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 = (INT16)CreateBitmapIndirect16( &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( HGLOBAL16 hbitmap, UINT coloruse )
195 LOGBRUSH16 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 if (info->bmiHeader.biCompression)
206 size = info->bmiHeader.biSizeImage;
207 else
208 size = (info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) / 32
209 * 8 * info->bmiHeader.biHeight;
210 size += DIB_BitmapInfoSize( info, coloruse );
212 if (!(logbrush.lbHatch = (INT)GlobalAlloc16( GMEM_MOVEABLE, size )))
214 GlobalUnlock16( hbitmap );
215 return 0;
217 newInfo = (BITMAPINFO *) GlobalLock16( (HANDLE)logbrush.lbHatch );
218 memcpy( newInfo, info, size );
219 GlobalUnlock16( (HANDLE)logbrush.lbHatch );
220 GlobalUnlock16( hbitmap );
221 return CreateBrushIndirect( &logbrush );
225 /***********************************************************************
226 * CreateSolidBrush (GDI.66)
228 HBRUSH CreateSolidBrush( COLORREF color )
230 LOGBRUSH16 logbrush = { BS_SOLID, color, 0 };
231 dprintf_gdi(stddeb, "CreateSolidBrush: %06lx\n", color );
232 return CreateBrushIndirect( &logbrush );
236 /***********************************************************************
237 * SetBrushOrg (GDI.148)
239 DWORD SetBrushOrg( HDC16 hdc, INT16 x, INT16 y )
241 DWORD retval;
242 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
243 if (!dc) return FALSE;
244 retval = dc->w.brushOrgX | (dc->w.brushOrgY << 16);
245 dc->w.brushOrgX = x;
246 dc->w.brushOrgY = y;
247 return retval;
251 /***********************************************************************
252 * SetBrushOrgEx (GDI32.308)
254 BOOL32 SetBrushOrgEx( HDC32 hdc, INT32 x, INT32 y, LPPOINT32 oldorg )
256 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
258 if (!dc) return FALSE;
259 if (oldorg)
261 oldorg->x = dc->w.brushOrgX;
262 oldorg->y = dc->w.brushOrgY;
264 dc->w.brushOrgX = x;
265 dc->w.brushOrgY = y;
266 return TRUE;
270 /***********************************************************************
271 * GetSysColorBrush (USER.281)
273 HBRUSH GetSysColorBrush(WORD x)
275 fprintf( stderr, "Unimplemented stub: GetSysColorBrush(%d)\n", x );
276 return GetStockObject(LTGRAY_BRUSH);
280 /***********************************************************************
281 * BRUSH_DeleteObject
283 BOOL BRUSH_DeleteObject( HBRUSH hbrush, BRUSHOBJ * brush )
285 switch(brush->logbrush.lbStyle)
287 case BS_PATTERN:
288 DeleteObject( (HANDLE)brush->logbrush.lbHatch );
289 break;
290 case BS_DIBPATTERN:
291 GlobalFree16( (HANDLE)brush->logbrush.lbHatch );
292 break;
294 return GDI_FreeObject( hbrush );
298 /***********************************************************************
299 * BRUSH_GetObject
301 int BRUSH_GetObject( BRUSHOBJ * brush, int count, LPSTR buffer )
303 if (count > sizeof(brush->logbrush)) count = sizeof(brush->logbrush);
304 memcpy( buffer, &brush->logbrush, count );
305 return count;
309 /***********************************************************************
310 * BRUSH_SelectSolidBrush
312 static void BRUSH_SelectSolidBrush( DC *dc, COLORREF color )
314 if ((dc->w.bitsPerPixel > 1) && (screenDepth <= 8) && !COLOR_IsSolid( color ))
316 /* Dithered brush */
317 dc->u.x.brush.pixmap = BRUSH_DitherColor( dc, color );
318 dc->u.x.brush.fillStyle = FillTiled;
319 dc->u.x.brush.pixel = 0;
321 else
323 /* Solid brush */
324 dc->u.x.brush.pixel = COLOR_ToPhysical( dc, color );
325 dc->u.x.brush.fillStyle = FillSolid;
330 /***********************************************************************
331 * BRUSH_SelectPatternBrush
333 static BOOL BRUSH_SelectPatternBrush( DC * dc, HBITMAP hbitmap )
335 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
336 if (!bmp) return FALSE;
337 dc->u.x.brush.pixmap = XCreatePixmap( display, rootWindow,
338 8, 8, bmp->bitmap.bmBitsPixel );
339 XCopyArea( display, bmp->pixmap, dc->u.x.brush.pixmap,
340 BITMAP_GC(bmp), 0, 0, 8, 8, 0, 0 );
342 if (bmp->bitmap.bmBitsPixel > 1)
344 dc->u.x.brush.fillStyle = FillTiled;
345 dc->u.x.brush.pixel = 0; /* Ignored */
347 else
349 dc->u.x.brush.fillStyle = FillOpaqueStippled;
350 dc->u.x.brush.pixel = -1; /* Special case (see DC_SetupGCForBrush) */
352 return TRUE;
357 /***********************************************************************
358 * BRUSH_SelectObject
360 HBRUSH BRUSH_SelectObject( DC * dc, HBRUSH hbrush, BRUSHOBJ * brush )
362 HBITMAP hBitmap;
363 BITMAPINFO * bmpInfo;
364 HBRUSH prevHandle = dc->w.hBrush;
366 dprintf_gdi(stddeb, "Brush_SelectObject: hdc=%04x hbrush=%04x\n",
367 dc->hSelf,hbrush);
368 if (dc->header.wMagic == METAFILE_DC_MAGIC)
370 switch (brush->logbrush.lbStyle)
372 case BS_SOLID:
373 case BS_HATCHED:
374 case BS_HOLLOW:
375 if (!MF_CreateBrushIndirect(dc, hbrush, &(brush->logbrush)))
376 return (HBRUSH)0;
377 break;
379 case BS_PATTERN:
380 case BS_DIBPATTERN:
381 if (!MF_CreatePatternBrush(dc, hbrush, &(brush->logbrush)))
382 return (HBRUSH)0;
383 break;
385 return (HBRUSH)1;
388 dc->w.hBrush = hbrush;
390 if (dc->u.x.brush.pixmap)
392 XFreePixmap( display, dc->u.x.brush.pixmap );
393 dc->u.x.brush.pixmap = 0;
395 dc->u.x.brush.style = brush->logbrush.lbStyle;
397 switch(brush->logbrush.lbStyle)
399 case BS_NULL:
400 dprintf_gdi( stddeb,"BS_NULL\n" );
401 break;
403 case BS_SOLID:
404 dprintf_gdi( stddeb,"BS_SOLID\n" );
405 BRUSH_SelectSolidBrush( dc, brush->logbrush.lbColor );
406 break;
408 case BS_HATCHED:
409 dprintf_gdi( stddeb, "BS_HATCHED\n" );
410 dc->u.x.brush.pixel = COLOR_ToPhysical( dc, brush->logbrush.lbColor );
411 dc->u.x.brush.pixmap = XCreateBitmapFromData( display, rootWindow,
412 HatchBrushes[brush->logbrush.lbHatch], 8, 8 );
413 dc->u.x.brush.fillStyle = FillStippled;
414 break;
416 case BS_PATTERN:
417 dprintf_gdi( stddeb, "BS_PATTERN\n");
418 BRUSH_SelectPatternBrush( dc, (HBRUSH)brush->logbrush.lbHatch );
419 break;
421 case BS_DIBPATTERN:
422 dprintf_gdi( stddeb, "BS_DIBPATTERN\n");
423 if ((bmpInfo = (BITMAPINFO *) GlobalLock16( (HANDLE)brush->logbrush.lbHatch )))
425 int size = DIB_BitmapInfoSize( bmpInfo, brush->logbrush.lbColor );
426 hBitmap = CreateDIBitmap( dc->hSelf, &bmpInfo->bmiHeader, CBM_INIT,
427 ((char *)bmpInfo) + size, bmpInfo,
428 (WORD) brush->logbrush.lbColor );
429 BRUSH_SelectPatternBrush( dc, hBitmap );
430 DeleteObject( hBitmap );
431 GlobalUnlock16( (HANDLE)brush->logbrush.lbHatch );
434 break;
437 return prevHandle;