Release 950122
[wine/multimedia.git] / objects / brush.c
blob9d185af635a48dee1385920ece1de6990dd01086
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_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: %d\n", hbitmap );
176 /* Make a copy of the bitmap */
178 if (!(bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
179 return 0;
180 logbrush.lbHatch = CreateBitmapIndirect( &bmp->bitmap );
181 newbmp = (BITMAPOBJ *) GDI_GetObjPtr( logbrush.lbHatch, BITMAP_MAGIC );
182 if (!newbmp) return 0;
183 XCopyArea( display, bmp->pixmap, newbmp->pixmap, BITMAP_GC(bmp),
184 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight, 0, 0 );
185 return CreateBrushIndirect( &logbrush );
189 /***********************************************************************
190 * CreateDIBPatternBrush (GDI.445)
192 HBRUSH CreateDIBPatternBrush( HANDLE hbitmap, WORD coloruse )
194 LOGBRUSH logbrush = { BS_DIBPATTERN, coloruse, 0 };
195 BITMAPINFO *info, *newInfo;
196 int size;
198 dprintf_gdi(stddeb, "CreateDIBPatternBrush: %d\n", hbitmap );
200 /* Make a copy of the bitmap */
202 if (!(info = (BITMAPINFO *) GlobalLock( hbitmap ))) return 0;
204 size = info->bmiHeader.biSizeImage;
205 if (!size)
206 size = (info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) / 32
207 * 8 * info->bmiHeader.biHeight;
208 size += DIB_BitmapInfoSize( info, coloruse );
210 if (!(logbrush.lbHatch = GlobalAlloc( GMEM_MOVEABLE, size )))
212 GlobalUnlock( hbitmap );
213 return 0;
215 newInfo = (BITMAPINFO *) GlobalLock( logbrush.lbHatch );
216 memcpy( newInfo, info, size );
217 GlobalUnlock( logbrush.lbHatch );
218 GlobalUnlock( hbitmap );
219 return CreateBrushIndirect( &logbrush );
223 /***********************************************************************
224 * CreateSolidBrush (GDI.66)
226 HBRUSH CreateSolidBrush( COLORREF color )
228 LOGBRUSH logbrush = { BS_SOLID, color, 0 };
229 dprintf_gdi(stddeb, "CreateSolidBrush: %06lx\n", color );
230 return CreateBrushIndirect( &logbrush );
234 /***********************************************************************
235 * SetBrushOrg (GDI.148)
237 DWORD SetBrushOrg( HDC hdc, short x, short y )
239 DWORD retval;
240 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
241 if (!dc) return FALSE;
242 retval = dc->w.brushOrgX | (dc->w.brushOrgY << 16);
243 dc->w.brushOrgX = x;
244 dc->w.brushOrgY = y;
245 return retval;
248 /***********************************************************************
249 * GetSysColorBrush (USER.281)
251 WORD GetSysColorBrush(WORD x)
253 return GetStockObject(GRAY_BRUSH);
256 /***********************************************************************
257 * BRUSH_DeleteObject
259 BOOL BRUSH_DeleteObject( HBRUSH hbrush, BRUSHOBJ * brush )
261 switch(brush->logbrush.lbStyle)
263 case BS_PATTERN:
264 DeleteObject( brush->logbrush.lbHatch );
265 break;
266 case BS_DIBPATTERN:
267 GlobalFree( brush->logbrush.lbHatch );
268 break;
270 return GDI_FreeObject( hbrush );
274 /***********************************************************************
275 * BRUSH_GetObject
277 int BRUSH_GetObject( BRUSHOBJ * brush, int count, LPSTR buffer )
279 if (count > sizeof(LOGBRUSH)) count = sizeof(LOGBRUSH);
280 memcpy( buffer, &brush->logbrush, count );
281 return count;
285 /***********************************************************************
286 * BRUSH_SelectSolidBrush
288 static void BRUSH_SelectSolidBrush( DC *dc, COLORREF color )
290 if ((dc->w.bitsPerPixel > 1) && (screenDepth <= 8) && !COLOR_IsSolid( color ))
292 /* Dithered brush */
293 dc->u.x.brush.pixmap = BRUSH_DitherColor( dc, color );
294 dc->u.x.brush.fillStyle = FillTiled;
295 dc->u.x.brush.pixel = 0;
297 else
299 /* Solid brush */
300 dc->u.x.brush.pixel = COLOR_ToPhysical( dc, color );
301 dc->u.x.brush.fillStyle = FillSolid;
306 /***********************************************************************
307 * BRUSH_SelectPatternBrush
309 static BOOL BRUSH_SelectPatternBrush( DC * dc, HBITMAP hbitmap )
311 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
312 if (!bmp) return FALSE;
313 dc->u.x.brush.pixmap = XCreatePixmap( display, rootWindow,
314 8, 8, bmp->bitmap.bmBitsPixel );
315 XCopyArea( display, bmp->pixmap, dc->u.x.brush.pixmap,
316 BITMAP_GC(bmp), 0, 0, 8, 8, 0, 0 );
318 if (bmp->bitmap.bmBitsPixel > 1)
320 dc->u.x.brush.fillStyle = FillTiled;
321 dc->u.x.brush.pixel = 0; /* Ignored */
323 else
325 dc->u.x.brush.fillStyle = FillOpaqueStippled;
326 dc->u.x.brush.pixel = -1; /* Special case (see DC_SetupGCForBrush) */
328 return TRUE;
333 /***********************************************************************
334 * BRUSH_SelectObject
336 HBRUSH BRUSH_SelectObject( HDC hdc, DC * dc, HBRUSH hbrush, BRUSHOBJ * brush )
338 HBITMAP hBitmap;
339 BITMAPINFO * bmpInfo;
340 HBRUSH prevHandle = dc->w.hBrush;
342 dprintf_gdi(stddeb, "Brush_SelectObject hdc=%04x hbrush=%04x\n",
343 hdc,hbrush);
344 if (dc->header.wMagic == METAFILE_DC_MAGIC)
346 switch (brush->logbrush.lbStyle)
348 case BS_SOLID:
349 case BS_HATCHED:
350 case BS_HOLLOW:
351 if (!MF_CreateBrushIndirect(dc, hbrush, &(brush->logbrush)))
352 return 0;
353 break;
355 case BS_PATTERN:
356 case BS_DIBPATTERN:
357 if (!MF_CreatePatternBrush(dc, hbrush, &(brush->logbrush)))
358 return 0;
359 break;
361 return 1;
364 dc->w.hBrush = hbrush;
366 if (dc->u.x.brush.pixmap)
368 XFreePixmap( display, dc->u.x.brush.pixmap );
369 dc->u.x.brush.pixmap = 0;
371 dc->u.x.brush.style = brush->logbrush.lbStyle;
373 switch(brush->logbrush.lbStyle)
375 case BS_NULL:
376 dprintf_gdi( stddeb,"BS_NULL\n" );
377 break;
379 case BS_SOLID:
380 dprintf_gdi( stddeb,"BS_SOLID\n" );
381 BRUSH_SelectSolidBrush( dc, brush->logbrush.lbColor );
382 break;
384 case BS_HATCHED:
385 dprintf_gdi( stddeb, "BS_HATCHED\n" );
386 dc->u.x.brush.pixel = COLOR_ToPhysical( dc, brush->logbrush.lbColor );
387 dc->u.x.brush.pixmap = XCreateBitmapFromData( display, rootWindow,
388 HatchBrushes[brush->logbrush.lbHatch], 8, 8 );
389 dc->u.x.brush.fillStyle = FillStippled;
390 break;
392 case BS_PATTERN:
393 dprintf_gdi( stddeb, "BS_PATTERN\n");
394 BRUSH_SelectPatternBrush( dc, brush->logbrush.lbHatch );
395 break;
397 case BS_DIBPATTERN:
398 dprintf_gdi( stddeb, "BS_DIBPATTERN\n");
399 if ((bmpInfo = (BITMAPINFO *) GlobalLock( brush->logbrush.lbHatch )))
401 int size = DIB_BitmapInfoSize( bmpInfo, brush->logbrush.lbColor );
402 hBitmap = CreateDIBitmap( hdc, &bmpInfo->bmiHeader, CBM_INIT,
403 ((char *)bmpInfo) + size, bmpInfo,
404 (WORD) brush->logbrush.lbColor );
405 BRUSH_SelectPatternBrush( dc, hBitmap );
406 DeleteObject( hBitmap );
407 GlobalUnlock( brush->logbrush.lbHatch );
410 break;
413 return prevHandle;