Release 951003
[wine/hacks.git] / objects / brush.c
blob982920abbe10a3d1e013ad1c31ccb3f59642c089
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"
15 #define NB_HATCH_STYLES 6
17 static const char HatchBrushes[NB_HATCH_STYLES][8] =
19 { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HS_HORIZONTAL */
20 { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HS_VERTICAL */
21 { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HS_FDIAGONAL */
22 { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HS_BDIAGONAL */
23 { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HS_CROSS */
24 { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 } /* HS_DIAGCROSS */
27 /* Levels of each primary for dithering */
28 #define PRIMARY_LEVELS 3
29 #define TOTAL_LEVELS (PRIMARY_LEVELS*PRIMARY_LEVELS*PRIMARY_LEVELS)
31 /* Dithering matrix size */
32 #define MATRIX_SIZE 8
33 #define MATRIX_SIZE_2 (MATRIX_SIZE*MATRIX_SIZE)
35 /* Total number of possible levels for a dithered primary color */
36 #define DITHER_LEVELS (MATRIX_SIZE_2 * (PRIMARY_LEVELS-1) + 1)
38 /* Dithering matrix */
39 static const int dither_matrix[MATRIX_SIZE_2] =
41 0, 32, 8, 40, 2, 34, 10, 42,
42 48, 16, 56, 24, 50, 18, 58, 26,
43 12, 44, 4, 36, 14, 46, 6, 38,
44 60, 28, 52, 20, 62, 30, 54, 22,
45 3, 35, 11, 43, 1, 33, 9, 41,
46 51, 19, 59, 27, 49, 17, 57, 25,
47 15, 47, 7, 39, 13, 45, 5, 37,
48 63, 31, 55, 23, 61, 29, 53, 21
51 /* Mapping between (R,G,B) triples and EGA colors */
52 static const int EGAmapping[TOTAL_LEVELS] =
54 0, /* 000000 -> 000000 */
55 4, /* 00007f -> 000080 */
56 12, /* 0000ff -> 0000ff */
57 2, /* 007f00 -> 008000 */
58 6, /* 007f7f -> 008080 */
59 6, /* 007fff -> 008080 */
60 10, /* 00ff00 -> 00ff00 */
61 6, /* 00ff7f -> 008080 */
62 14, /* 00ffff -> 00ffff */
63 1, /* 7f0000 -> 800000 */
64 5, /* 7f007f -> 800080 */
65 5, /* 7f00ff -> 800080 */
66 3, /* 7f7f00 -> 808000 */
67 8, /* 7f7f7f -> 808080 */
68 7, /* 7f7fff -> c0c0c0 */
69 3, /* 7fff00 -> 808000 */
70 7, /* 7fff7f -> c0c0c0 */
71 7, /* 7fffff -> c0c0c0 */
72 9, /* ff0000 -> ff0000 */
73 5, /* ff007f -> 800080 */
74 13, /* ff00ff -> ff00ff */
75 3, /* ff7f00 -> 808000 */
76 7, /* ff7f7f -> c0c0c0 */
77 7, /* ff7fff -> c0c0c0 */
78 11, /* ffff00 -> ffff00 */
79 7, /* ffff7f -> c0c0c0 */
80 15 /* ffffff -> ffffff */
83 #define PIXEL_VALUE(r,g,b) \
84 COLOR_mapEGAPixel[EGAmapping[((r)*PRIMARY_LEVELS+(g))*PRIMARY_LEVELS+(b)]]
86 /* X image for building dithered pixmap */
87 static XImage *ditherImage = NULL;
90 /***********************************************************************
91 * BRUSH_Init
93 * Create the X image used for dithering.
95 BOOL BRUSH_Init(void)
97 XCREATEIMAGE( ditherImage, MATRIX_SIZE, MATRIX_SIZE, screenDepth );
98 return (ditherImage != NULL);
102 /***********************************************************************
103 * BRUSH_DitherColor
105 Pixmap BRUSH_DitherColor( DC *dc, COLORREF color )
107 static COLORREF prevColor = 0xffffffff;
108 unsigned int x, y;
109 Pixmap pixmap;
111 if (color != prevColor)
113 int r = GetRValue( color ) * DITHER_LEVELS;
114 int g = GetGValue( color ) * DITHER_LEVELS;
115 int b = GetBValue( color ) * DITHER_LEVELS;
116 const int *pmatrix = dither_matrix;
118 for (y = 0; y < MATRIX_SIZE; y++)
120 for (x = 0; x < MATRIX_SIZE; x++)
122 int d = *pmatrix++ * 256;
123 int dr = ((r + d) / MATRIX_SIZE_2) / 256;
124 int dg = ((g + d) / MATRIX_SIZE_2) / 256;
125 int db = ((b + d) / MATRIX_SIZE_2) / 256;
126 XPutPixel( ditherImage, x, y, PIXEL_VALUE(dr,dg,db) );
129 prevColor = color;
132 pixmap = XCreatePixmap( display, rootWindow,
133 MATRIX_SIZE, MATRIX_SIZE, screenDepth );
134 XPutImage( display, pixmap, BITMAP_colorGC, ditherImage, 0, 0,
135 0, 0, MATRIX_SIZE, MATRIX_SIZE );
136 return pixmap;
140 /***********************************************************************
141 * CreateBrushIndirect (GDI.50)
143 HBRUSH CreateBrushIndirect( LOGBRUSH * brush )
145 BRUSHOBJ * brushPtr;
146 HBRUSH hbrush = GDI_AllocObject( sizeof(BRUSHOBJ), BRUSH_MAGIC );
147 if (!hbrush) return 0;
148 brushPtr = (BRUSHOBJ *) GDI_HEAP_LIN_ADDR( hbrush );
149 memcpy( &brushPtr->logbrush, brush, sizeof(LOGBRUSH) );
150 return hbrush;
154 /***********************************************************************
155 * CreateHatchBrush (GDI.58)
157 HBRUSH CreateHatchBrush( short style, COLORREF color )
159 LOGBRUSH logbrush = { BS_HATCHED, color, style };
160 dprintf_gdi(stddeb, "CreateHatchBrush: %d %06lx\n", style, color );
161 if ((style < 0) || (style >= NB_HATCH_STYLES)) return 0;
162 return CreateBrushIndirect( &logbrush );
166 /***********************************************************************
167 * CreatePatternBrush (GDI.60)
169 HBRUSH CreatePatternBrush( HBITMAP hbitmap )
171 LOGBRUSH logbrush = { BS_PATTERN, 0, 0 };
172 BITMAPOBJ *bmp, *newbmp;
174 dprintf_gdi(stddeb, "CreatePatternBrush: "NPFMT"\n", hbitmap );
176 /* Make a copy of the bitmap */
178 if (!(bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
179 return 0;
180 #ifdef WINELIB32
181 logbrush.lbHatch = (LONG)CreateBitmapIndirect( &bmp->bitmap );
182 #else
183 logbrush.lbHatch = (INT)CreateBitmapIndirect( &bmp->bitmap );
184 #endif
185 newbmp = (BITMAPOBJ *) GDI_GetObjPtr( (HANDLE)logbrush.lbHatch, BITMAP_MAGIC );
186 if (!newbmp) return 0;
187 XCopyArea( display, bmp->pixmap, newbmp->pixmap, BITMAP_GC(bmp),
188 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight, 0, 0 );
189 return CreateBrushIndirect( &logbrush );
193 /***********************************************************************
194 * CreateDIBPatternBrush (GDI.445)
196 HBRUSH CreateDIBPatternBrush( HANDLE hbitmap, WORD coloruse )
198 LOGBRUSH logbrush = { BS_DIBPATTERN, coloruse, 0 };
199 BITMAPINFO *info, *newInfo;
200 int size;
202 dprintf_gdi(stddeb, "CreateDIBPatternBrush: "NPFMT"\n", hbitmap );
204 /* Make a copy of the bitmap */
206 if (!(info = (BITMAPINFO *) GlobalLock( hbitmap ))) return 0;
208 size = info->bmiHeader.biSizeImage;
209 if (!size)
210 size = (info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) / 32
211 * 8 * info->bmiHeader.biHeight;
212 size += DIB_BitmapInfoSize( info, coloruse );
214 #ifdef WINELIB32
215 if (!(logbrush.lbHatch = (LONG)GlobalAlloc( GMEM_MOVEABLE, size )))
216 #else
217 if (!(logbrush.lbHatch = (INT)GlobalAlloc( GMEM_MOVEABLE, size )))
218 #endif
220 GlobalUnlock( hbitmap );
221 return 0;
223 newInfo = (BITMAPINFO *) GlobalLock( (HANDLE)logbrush.lbHatch );
224 memcpy( newInfo, info, size );
225 GlobalUnlock( (HANDLE)logbrush.lbHatch );
226 GlobalUnlock( hbitmap );
227 return CreateBrushIndirect( &logbrush );
231 /***********************************************************************
232 * CreateSolidBrush (GDI.66)
234 HBRUSH CreateSolidBrush( COLORREF color )
236 LOGBRUSH logbrush = { BS_SOLID, color, 0 };
237 dprintf_gdi(stddeb, "CreateSolidBrush: %06lx\n", color );
238 return CreateBrushIndirect( &logbrush );
242 /***********************************************************************
243 * SetBrushOrg (GDI.148)
245 DWORD SetBrushOrg( HDC hdc, short x, short y )
247 DWORD retval;
248 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
249 if (!dc) return FALSE;
250 retval = dc->w.brushOrgX | (dc->w.brushOrgY << 16);
251 dc->w.brushOrgX = x;
252 dc->w.brushOrgY = y;
253 return retval;
256 /***********************************************************************
257 * GetSysColorBrush (USER.281)
259 HBRUSH GetSysColorBrush(WORD x)
261 return GetStockObject(GRAY_BRUSH);
264 /***********************************************************************
265 * BRUSH_DeleteObject
267 BOOL BRUSH_DeleteObject( HBRUSH hbrush, BRUSHOBJ * brush )
269 switch(brush->logbrush.lbStyle)
271 case BS_PATTERN:
272 DeleteObject( (HANDLE)brush->logbrush.lbHatch );
273 break;
274 case BS_DIBPATTERN:
275 GlobalFree( (HANDLE)brush->logbrush.lbHatch );
276 break;
278 return GDI_FreeObject( hbrush );
282 /***********************************************************************
283 * BRUSH_GetObject
285 int BRUSH_GetObject( BRUSHOBJ * brush, int count, LPSTR buffer )
287 if (count > sizeof(LOGBRUSH)) count = sizeof(LOGBRUSH);
288 memcpy( buffer, &brush->logbrush, count );
289 return count;
293 /***********************************************************************
294 * BRUSH_SelectSolidBrush
296 static void BRUSH_SelectSolidBrush( DC *dc, COLORREF color )
298 if ((dc->w.bitsPerPixel > 1) && (screenDepth <= 8) && !COLOR_IsSolid( color ))
300 /* Dithered brush */
301 dc->u.x.brush.pixmap = BRUSH_DitherColor( dc, color );
302 dc->u.x.brush.fillStyle = FillTiled;
303 dc->u.x.brush.pixel = 0;
305 else
307 /* Solid brush */
308 dc->u.x.brush.pixel = COLOR_ToPhysical( dc, color );
309 dc->u.x.brush.fillStyle = FillSolid;
314 /***********************************************************************
315 * BRUSH_SelectPatternBrush
317 static BOOL BRUSH_SelectPatternBrush( DC * dc, HBITMAP hbitmap )
319 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
320 if (!bmp) return FALSE;
321 dc->u.x.brush.pixmap = XCreatePixmap( display, rootWindow,
322 8, 8, bmp->bitmap.bmBitsPixel );
323 XCopyArea( display, bmp->pixmap, dc->u.x.brush.pixmap,
324 BITMAP_GC(bmp), 0, 0, 8, 8, 0, 0 );
326 if (bmp->bitmap.bmBitsPixel > 1)
328 dc->u.x.brush.fillStyle = FillTiled;
329 dc->u.x.brush.pixel = 0; /* Ignored */
331 else
333 dc->u.x.brush.fillStyle = FillOpaqueStippled;
334 dc->u.x.brush.pixel = -1; /* Special case (see DC_SetupGCForBrush) */
336 return TRUE;
341 /***********************************************************************
342 * BRUSH_SelectObject
344 HBRUSH BRUSH_SelectObject( HDC hdc, DC * dc, HBRUSH hbrush, BRUSHOBJ * brush )
346 HBITMAP hBitmap;
347 BITMAPINFO * bmpInfo;
348 HBRUSH prevHandle = dc->w.hBrush;
350 dprintf_gdi(stddeb, "Brush_SelectObject hdc="NPFMT" hbrush="NPFMT"\n",
351 hdc,hbrush);
352 if (dc->header.wMagic == METAFILE_DC_MAGIC)
354 switch (brush->logbrush.lbStyle)
356 case BS_SOLID:
357 case BS_HATCHED:
358 case BS_HOLLOW:
359 if (!MF_CreateBrushIndirect(dc, hbrush, &(brush->logbrush)))
360 return (HBRUSH)0;
361 break;
363 case BS_PATTERN:
364 case BS_DIBPATTERN:
365 if (!MF_CreatePatternBrush(dc, hbrush, &(brush->logbrush)))
366 return (HBRUSH)0;
367 break;
369 return (HBRUSH)1;
372 dc->w.hBrush = hbrush;
374 if (dc->u.x.brush.pixmap)
376 XFreePixmap( display, dc->u.x.brush.pixmap );
377 dc->u.x.brush.pixmap = 0;
379 dc->u.x.brush.style = brush->logbrush.lbStyle;
381 switch(brush->logbrush.lbStyle)
383 case BS_NULL:
384 dprintf_gdi( stddeb,"BS_NULL\n" );
385 break;
387 case BS_SOLID:
388 dprintf_gdi( stddeb,"BS_SOLID\n" );
389 BRUSH_SelectSolidBrush( dc, brush->logbrush.lbColor );
390 break;
392 case BS_HATCHED:
393 dprintf_gdi( stddeb, "BS_HATCHED\n" );
394 dc->u.x.brush.pixel = COLOR_ToPhysical( dc, brush->logbrush.lbColor );
395 dc->u.x.brush.pixmap = XCreateBitmapFromData( display, rootWindow,
396 HatchBrushes[brush->logbrush.lbHatch], 8, 8 );
397 dc->u.x.brush.fillStyle = FillStippled;
398 break;
400 case BS_PATTERN:
401 dprintf_gdi( stddeb, "BS_PATTERN\n");
402 BRUSH_SelectPatternBrush( dc, (HBRUSH)brush->logbrush.lbHatch );
403 break;
405 case BS_DIBPATTERN:
406 dprintf_gdi( stddeb, "BS_DIBPATTERN\n");
407 if ((bmpInfo = (BITMAPINFO *) GlobalLock( (HANDLE)brush->logbrush.lbHatch )))
409 int size = DIB_BitmapInfoSize( bmpInfo, brush->logbrush.lbColor );
410 hBitmap = CreateDIBitmap( hdc, &bmpInfo->bmiHeader, CBM_INIT,
411 ((char *)bmpInfo) + size, bmpInfo,
412 (WORD) brush->logbrush.lbColor );
413 BRUSH_SelectPatternBrush( dc, hBitmap );
414 DeleteObject( hBitmap );
415 GlobalUnlock( (HANDLE)brush->logbrush.lbHatch );
418 break;
421 return prevHandle;