Release 960611
[wine/multimedia.git] / objects / brush.c
blob6331df08d5db9c06a89328b3d9ebdb32625667c8
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( HGLOBAL 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( HDC hdc, short x, short 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;
250 /***********************************************************************
251 * GetSysColorBrush (USER.281)
253 HBRUSH GetSysColorBrush(WORD x)
255 return GetStockObject(GRAY_BRUSH);
258 /***********************************************************************
259 * BRUSH_DeleteObject
261 BOOL BRUSH_DeleteObject( HBRUSH hbrush, BRUSHOBJ * brush )
263 switch(brush->logbrush.lbStyle)
265 case BS_PATTERN:
266 DeleteObject( (HANDLE)brush->logbrush.lbHatch );
267 break;
268 case BS_DIBPATTERN:
269 GlobalFree16( (HANDLE)brush->logbrush.lbHatch );
270 break;
272 return GDI_FreeObject( hbrush );
276 /***********************************************************************
277 * BRUSH_GetObject
279 int BRUSH_GetObject( BRUSHOBJ * brush, int count, LPSTR buffer )
281 if (count > sizeof(brush->logbrush)) count = sizeof(brush->logbrush);
282 memcpy( buffer, &brush->logbrush, count );
283 return count;
287 /***********************************************************************
288 * BRUSH_SelectSolidBrush
290 static void BRUSH_SelectSolidBrush( DC *dc, COLORREF color )
292 if ((dc->w.bitsPerPixel > 1) && (screenDepth <= 8) && !COLOR_IsSolid( color ))
294 /* Dithered brush */
295 dc->u.x.brush.pixmap = BRUSH_DitherColor( dc, color );
296 dc->u.x.brush.fillStyle = FillTiled;
297 dc->u.x.brush.pixel = 0;
299 else
301 /* Solid brush */
302 dc->u.x.brush.pixel = COLOR_ToPhysical( dc, color );
303 dc->u.x.brush.fillStyle = FillSolid;
308 /***********************************************************************
309 * BRUSH_SelectPatternBrush
311 static BOOL BRUSH_SelectPatternBrush( DC * dc, HBITMAP hbitmap )
313 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
314 if (!bmp) return FALSE;
315 dc->u.x.brush.pixmap = XCreatePixmap( display, rootWindow,
316 8, 8, bmp->bitmap.bmBitsPixel );
317 XCopyArea( display, bmp->pixmap, dc->u.x.brush.pixmap,
318 BITMAP_GC(bmp), 0, 0, 8, 8, 0, 0 );
320 if (bmp->bitmap.bmBitsPixel > 1)
322 dc->u.x.brush.fillStyle = FillTiled;
323 dc->u.x.brush.pixel = 0; /* Ignored */
325 else
327 dc->u.x.brush.fillStyle = FillOpaqueStippled;
328 dc->u.x.brush.pixel = -1; /* Special case (see DC_SetupGCForBrush) */
330 return TRUE;
335 /***********************************************************************
336 * BRUSH_SelectObject
338 HBRUSH BRUSH_SelectObject( DC * dc, HBRUSH hbrush, BRUSHOBJ * brush )
340 HBITMAP hBitmap;
341 BITMAPINFO * bmpInfo;
342 HBRUSH prevHandle = dc->w.hBrush;
344 dprintf_gdi(stddeb, "Brush_SelectObject: hdc=%04x hbrush=%04x\n",
345 dc->hSelf,hbrush);
346 if (dc->header.wMagic == METAFILE_DC_MAGIC)
348 switch (brush->logbrush.lbStyle)
350 case BS_SOLID:
351 case BS_HATCHED:
352 case BS_HOLLOW:
353 if (!MF_CreateBrushIndirect(dc, hbrush, &(brush->logbrush)))
354 return (HBRUSH)0;
355 break;
357 case BS_PATTERN:
358 case BS_DIBPATTERN:
359 if (!MF_CreatePatternBrush(dc, hbrush, &(brush->logbrush)))
360 return (HBRUSH)0;
361 break;
363 return (HBRUSH)1;
366 dc->w.hBrush = hbrush;
368 if (dc->u.x.brush.pixmap)
370 XFreePixmap( display, dc->u.x.brush.pixmap );
371 dc->u.x.brush.pixmap = 0;
373 dc->u.x.brush.style = brush->logbrush.lbStyle;
375 switch(brush->logbrush.lbStyle)
377 case BS_NULL:
378 dprintf_gdi( stddeb,"BS_NULL\n" );
379 break;
381 case BS_SOLID:
382 dprintf_gdi( stddeb,"BS_SOLID\n" );
383 BRUSH_SelectSolidBrush( dc, brush->logbrush.lbColor );
384 break;
386 case BS_HATCHED:
387 dprintf_gdi( stddeb, "BS_HATCHED\n" );
388 dc->u.x.brush.pixel = COLOR_ToPhysical( dc, brush->logbrush.lbColor );
389 dc->u.x.brush.pixmap = XCreateBitmapFromData( display, rootWindow,
390 HatchBrushes[brush->logbrush.lbHatch], 8, 8 );
391 dc->u.x.brush.fillStyle = FillStippled;
392 break;
394 case BS_PATTERN:
395 dprintf_gdi( stddeb, "BS_PATTERN\n");
396 BRUSH_SelectPatternBrush( dc, (HBRUSH)brush->logbrush.lbHatch );
397 break;
399 case BS_DIBPATTERN:
400 dprintf_gdi( stddeb, "BS_DIBPATTERN\n");
401 if ((bmpInfo = (BITMAPINFO *) GlobalLock16( (HANDLE)brush->logbrush.lbHatch )))
403 int size = DIB_BitmapInfoSize( bmpInfo, brush->logbrush.lbColor );
404 hBitmap = CreateDIBitmap( dc->hSelf, &bmpInfo->bmiHeader, CBM_INIT,
405 ((char *)bmpInfo) + size, bmpInfo,
406 (WORD) brush->logbrush.lbColor );
407 BRUSH_SelectPatternBrush( dc, hBitmap );
408 DeleteObject( hBitmap );
409 GlobalUnlock16( (HANDLE)brush->logbrush.lbHatch );
412 break;
415 return prevHandle;