Release 950901
[wine] / objects / brush.c
1 /*
2  * GDI brush objects
3  *
4  * Copyright 1993, 1994  Alexandre Julliard
5  */
6
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
15 #define NB_HATCH_STYLES  6
16
17 static const char HatchBrushes[NB_HATCH_STYLES][8] =
18 {
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  */
25 };
26
27   /* Levels of each primary for dithering */
28 #define PRIMARY_LEVELS  3  
29 #define TOTAL_LEVELS    (PRIMARY_LEVELS*PRIMARY_LEVELS*PRIMARY_LEVELS)
30
31  /* Dithering matrix size  */
32 #define MATRIX_SIZE     8
33 #define MATRIX_SIZE_2   (MATRIX_SIZE*MATRIX_SIZE)
34
35   /* Total number of possible levels for a dithered primary color */
36 #define DITHER_LEVELS   (MATRIX_SIZE_2 * (PRIMARY_LEVELS-1) + 1)
37
38   /* Dithering matrix */
39 static const int dither_matrix[MATRIX_SIZE_2] =
40 {
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
49 };
50
51   /* Mapping between (R,G,B) triples and EGA colors */
52 static const int EGAmapping[TOTAL_LEVELS] =
53 {
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 */
81 };
82
83 #define PIXEL_VALUE(r,g,b) \
84     COLOR_mapEGAPixel[EGAmapping[((r)*PRIMARY_LEVELS+(g))*PRIMARY_LEVELS+(b)]]
85
86   /* X image for building dithered pixmap */
87 static XImage *ditherImage = NULL;
88
89
90 /***********************************************************************
91  *           BRUSH_Init
92  *
93  * Create the X image used for dithering.
94  */
95 BOOL BRUSH_Init(void)
96 {
97     XCREATEIMAGE( ditherImage, MATRIX_SIZE, MATRIX_SIZE, screenDepth );
98     return (ditherImage != NULL);
99 }
100
101
102 /***********************************************************************
103  *           BRUSH_DitherColor
104  */
105 Pixmap BRUSH_DitherColor( DC *dc, COLORREF color )
106 {
107     static COLORREF prevColor = 0xffffffff;
108     unsigned int x, y;
109     Pixmap pixmap;
110
111     if (color != prevColor)
112     {
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;
117
118         for (y = 0; y < MATRIX_SIZE; y++)
119         {
120             for (x = 0; x < MATRIX_SIZE; x++)
121             {
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) );
127             }
128         }
129         prevColor = color;
130     }
131     
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;
137 }
138
139
140 /***********************************************************************
141  *           CreateBrushIndirect    (GDI.50)
142  */
143 HBRUSH CreateBrushIndirect( LOGBRUSH * brush )
144 {
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;
151 }
152
153
154 /***********************************************************************
155  *           CreateHatchBrush    (GDI.58)
156  */
157 HBRUSH CreateHatchBrush( short style, COLORREF color )
158 {
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 );
163 }
164
165
166 /***********************************************************************
167  *           CreatePatternBrush    (GDI.60)
168  */
169 HBRUSH CreatePatternBrush( HBITMAP hbitmap )
170 {
171     LOGBRUSH logbrush = { BS_PATTERN, 0, 0 };
172     BITMAPOBJ *bmp, *newbmp;
173
174     dprintf_gdi(stddeb, "CreatePatternBrush: %d\n", hbitmap );
175
176       /* Make a copy of the bitmap */
177
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 );
186 }
187
188
189 /***********************************************************************
190  *           CreateDIBPatternBrush    (GDI.445)
191  */
192 HBRUSH CreateDIBPatternBrush( HANDLE hbitmap, WORD coloruse )
193 {
194     LOGBRUSH logbrush = { BS_DIBPATTERN, coloruse, 0 };
195     BITMAPINFO *info, *newInfo;
196     int size;
197     
198     dprintf_gdi(stddeb, "CreateDIBPatternBrush: %d\n", hbitmap );
199
200       /* Make a copy of the bitmap */
201
202     if (!(info = (BITMAPINFO *) GlobalLock( hbitmap ))) return 0;
203
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 );
209
210     if (!(logbrush.lbHatch = GlobalAlloc( GMEM_MOVEABLE, size )))
211     {
212         GlobalUnlock( hbitmap );
213         return 0;
214     }
215     newInfo = (BITMAPINFO *) GlobalLock( logbrush.lbHatch );
216     memcpy( newInfo, info, size );
217     GlobalUnlock( logbrush.lbHatch );
218     GlobalUnlock( hbitmap );
219     return CreateBrushIndirect( &logbrush );
220 }
221
222
223 /***********************************************************************
224  *           CreateSolidBrush    (GDI.66)
225  */
226 HBRUSH CreateSolidBrush( COLORREF color )
227 {
228     LOGBRUSH logbrush = { BS_SOLID, color, 0 };
229     dprintf_gdi(stddeb, "CreateSolidBrush: %06lx\n", color );
230     return CreateBrushIndirect( &logbrush );
231 }
232
233
234 /***********************************************************************
235  *           SetBrushOrg    (GDI.148)
236  */
237 DWORD SetBrushOrg( HDC hdc, short x, short y )
238 {
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;
246 }
247
248 /***********************************************************************
249  *           GetSysColorBrush    (USER.281)
250  */
251 WORD GetSysColorBrush(WORD x)
252 {
253         return GetStockObject(GRAY_BRUSH);
254 }
255
256 /***********************************************************************
257  *           BRUSH_DeleteObject
258  */
259 BOOL BRUSH_DeleteObject( HBRUSH hbrush, BRUSHOBJ * brush )
260 {
261     switch(brush->logbrush.lbStyle)
262     {
263       case BS_PATTERN:
264           DeleteObject( brush->logbrush.lbHatch );
265           break;
266       case BS_DIBPATTERN:
267           GlobalFree( brush->logbrush.lbHatch );
268           break;
269     }
270     return GDI_FreeObject( hbrush );
271 }
272
273
274 /***********************************************************************
275  *           BRUSH_GetObject
276  */
277 int BRUSH_GetObject( BRUSHOBJ * brush, int count, LPSTR buffer )
278 {
279     if (count > sizeof(LOGBRUSH)) count = sizeof(LOGBRUSH);
280     memcpy( buffer, &brush->logbrush, count );
281     return count;
282 }
283
284
285 /***********************************************************************
286  *           BRUSH_SelectSolidBrush
287  */
288 static void BRUSH_SelectSolidBrush( DC *dc, COLORREF color )
289 {
290     if ((dc->w.bitsPerPixel > 1) && (screenDepth <= 8) && !COLOR_IsSolid( color ))
291     {
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;
296     }
297     else
298     {
299           /* Solid brush */
300         dc->u.x.brush.pixel = COLOR_ToPhysical( dc, color );
301         dc->u.x.brush.fillStyle = FillSolid;
302     }
303 }
304
305
306 /***********************************************************************
307  *           BRUSH_SelectPatternBrush
308  */
309 static BOOL BRUSH_SelectPatternBrush( DC * dc, HBITMAP hbitmap )
310 {
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 );
317     
318     if (bmp->bitmap.bmBitsPixel > 1)
319     {
320         dc->u.x.brush.fillStyle = FillTiled;
321         dc->u.x.brush.pixel = 0;  /* Ignored */
322     }
323     else
324     {
325         dc->u.x.brush.fillStyle = FillOpaqueStippled;
326         dc->u.x.brush.pixel = -1;  /* Special case (see DC_SetupGCForBrush) */
327     }
328     return TRUE;
329 }
330
331
332
333 /***********************************************************************
334  *           BRUSH_SelectObject
335  */
336 HBRUSH BRUSH_SelectObject( HDC hdc, DC * dc, HBRUSH hbrush, BRUSHOBJ * brush )
337 {
338     HBITMAP hBitmap;
339     BITMAPINFO * bmpInfo;
340     HBRUSH prevHandle = dc->w.hBrush;
341
342     dprintf_gdi(stddeb, "Brush_SelectObject   hdc=%04x  hbrush=%04x\n",
343                 hdc,hbrush);
344     if (dc->header.wMagic == METAFILE_DC_MAGIC)
345     {
346         switch (brush->logbrush.lbStyle)
347         {
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;
354
355         case BS_PATTERN:
356         case BS_DIBPATTERN:
357             if (!MF_CreatePatternBrush(dc, hbrush, &(brush->logbrush)))
358                 return 0;
359             break;
360         }
361         return 1;
362     }
363     
364     dc->w.hBrush = hbrush;
365
366     if (dc->u.x.brush.pixmap)
367     {
368         XFreePixmap( display, dc->u.x.brush.pixmap );
369         dc->u.x.brush.pixmap = 0;
370     }
371     dc->u.x.brush.style = brush->logbrush.lbStyle;
372     
373     switch(brush->logbrush.lbStyle)
374     {
375       case BS_NULL:
376         dprintf_gdi( stddeb,"BS_NULL\n" );
377         break;
378
379       case BS_SOLID:
380         dprintf_gdi( stddeb,"BS_SOLID\n" );
381         BRUSH_SelectSolidBrush( dc, brush->logbrush.lbColor );
382         break;
383         
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;
391         
392       case BS_PATTERN:
393         dprintf_gdi( stddeb, "BS_PATTERN\n");
394         BRUSH_SelectPatternBrush( dc, brush->logbrush.lbHatch );
395         break;
396
397       case BS_DIBPATTERN:
398         dprintf_gdi( stddeb, "BS_DIBPATTERN\n");
399         if ((bmpInfo = (BITMAPINFO *) GlobalLock( brush->logbrush.lbHatch )))
400         {
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 );        
408         }
409         
410         break;
411     }
412     
413     return prevHandle;
414 }