4 * Copyright 1993, 1994 Alexandre Julliard
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 */
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 /***********************************************************************
93 * Create the X image used for dithering.
97 XCREATEIMAGE( ditherImage
, MATRIX_SIZE
, MATRIX_SIZE
, screenDepth
);
98 return (ditherImage
!= NULL
);
102 /***********************************************************************
105 Pixmap
BRUSH_DitherColor( DC
*dc
, COLORREF color
)
107 static COLORREF prevColor
= 0xffffffff;
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
) );
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
);
140 /***********************************************************************
141 * CreateBrushIndirect (GDI.50)
143 HBRUSH
CreateBrushIndirect( LOGBRUSH
* brush
)
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
) );
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
)))
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
;
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
;
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
);
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
)
240 DC
* dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
241 if (!dc
) return FALSE
;
242 retval
= dc
->w
.brushOrgX
| (dc
->w
.brushOrgY
<< 16);
248 /***********************************************************************
249 * GetSysColorBrush (USER.281)
251 WORD
GetSysColorBrush(WORD x
)
253 return GetStockObject(GRAY_BRUSH
);
256 /***********************************************************************
259 BOOL
BRUSH_DeleteObject( HBRUSH hbrush
, BRUSHOBJ
* brush
)
261 switch(brush
->logbrush
.lbStyle
)
264 DeleteObject( brush
->logbrush
.lbHatch
);
267 GlobalFree( brush
->logbrush
.lbHatch
);
270 return GDI_FreeObject( hbrush
);
274 /***********************************************************************
277 int BRUSH_GetObject( BRUSHOBJ
* brush
, int count
, LPSTR buffer
)
279 if (count
> sizeof(LOGBRUSH
)) count
= sizeof(LOGBRUSH
);
280 memcpy( buffer
, &brush
->logbrush
, 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
))
293 dc
->u
.x
.brush
.pixmap
= BRUSH_DitherColor( dc
, color
);
294 dc
->u
.x
.brush
.fillStyle
= FillTiled
;
295 dc
->u
.x
.brush
.pixel
= 0;
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 */
325 dc
->u
.x
.brush
.fillStyle
= FillOpaqueStippled
;
326 dc
->u
.x
.brush
.pixel
= -1; /* Special case (see DC_SetupGCForBrush) */
333 /***********************************************************************
336 HBRUSH
BRUSH_SelectObject( HDC hdc
, DC
* dc
, HBRUSH hbrush
, BRUSHOBJ
* brush
)
339 BITMAPINFO
* bmpInfo
;
340 HBRUSH prevHandle
= dc
->w
.hBrush
;
342 dprintf_gdi(stddeb
, "Brush_SelectObject hdc=%04x hbrush=%04x\n",
344 if (dc
->header
.wMagic
== METAFILE_DC_MAGIC
)
346 switch (brush
->logbrush
.lbStyle
)
351 if (!MF_CreateBrushIndirect(dc
, hbrush
, &(brush
->logbrush
)))
357 if (!MF_CreatePatternBrush(dc
, hbrush
, &(brush
->logbrush
)))
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
)
376 dprintf_gdi( stddeb
,"BS_NULL\n" );
380 dprintf_gdi( stddeb
,"BS_SOLID\n" );
381 BRUSH_SelectSolidBrush( dc
, brush
->logbrush
.lbColor
);
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
;
393 dprintf_gdi( stddeb
, "BS_PATTERN\n");
394 BRUSH_SelectPatternBrush( dc
, brush
->logbrush
.lbHatch
);
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
);