4 * Copyright 1993, 1994 Alexandre Julliard
15 static const char HatchBrushes
[NB_HATCH_STYLES
+ 1][8] =
17 { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HS_HORIZONTAL */
18 { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HS_VERTICAL */
19 { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HS_FDIAGONAL */
20 { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HS_BDIAGONAL */
21 { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HS_CROSS */
22 { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 }, /* HS_DIAGCROSS */
23 { 0xee, 0xbb, 0xee, 0xbb, 0xee, 0xbb, 0xee, 0xbb } /* Hack for DKGRAY */
26 /* Levels of each primary for dithering */
27 #define PRIMARY_LEVELS 3
28 #define TOTAL_LEVELS (PRIMARY_LEVELS*PRIMARY_LEVELS*PRIMARY_LEVELS)
30 /* Dithering matrix size */
32 #define MATRIX_SIZE_2 (MATRIX_SIZE*MATRIX_SIZE)
34 /* Total number of possible levels for a dithered primary color */
35 #define DITHER_LEVELS (MATRIX_SIZE_2 * (PRIMARY_LEVELS-1) + 1)
37 /* Dithering matrix */
38 static const int dither_matrix
[MATRIX_SIZE_2
] =
40 0, 32, 8, 40, 2, 34, 10, 42,
41 48, 16, 56, 24, 50, 18, 58, 26,
42 12, 44, 4, 36, 14, 46, 6, 38,
43 60, 28, 52, 20, 62, 30, 54, 22,
44 3, 35, 11, 43, 1, 33, 9, 41,
45 51, 19, 59, 27, 49, 17, 57, 25,
46 15, 47, 7, 39, 13, 45, 5, 37,
47 63, 31, 55, 23, 61, 29, 53, 21
50 /* Mapping between (R,G,B) triples and EGA colors */
51 static const int EGAmapping
[TOTAL_LEVELS
] =
53 0, /* 000000 -> 000000 */
54 4, /* 00007f -> 000080 */
55 12, /* 0000ff -> 0000ff */
56 2, /* 007f00 -> 008000 */
57 6, /* 007f7f -> 008080 */
58 6, /* 007fff -> 008080 */
59 10, /* 00ff00 -> 00ff00 */
60 6, /* 00ff7f -> 008080 */
61 14, /* 00ffff -> 00ffff */
62 1, /* 7f0000 -> 800000 */
63 5, /* 7f007f -> 800080 */
64 5, /* 7f00ff -> 800080 */
65 3, /* 7f7f00 -> 808000 */
66 8, /* 7f7f7f -> 808080 */
67 7, /* 7f7fff -> c0c0c0 */
68 3, /* 7fff00 -> 808000 */
69 7, /* 7fff7f -> c0c0c0 */
70 7, /* 7fffff -> c0c0c0 */
71 9, /* ff0000 -> ff0000 */
72 5, /* ff007f -> 800080 */
73 13, /* ff00ff -> ff00ff */
74 3, /* ff7f00 -> 808000 */
75 7, /* ff7f7f -> c0c0c0 */
76 7, /* ff7fff -> c0c0c0 */
77 11, /* ffff00 -> ffff00 */
78 7, /* ffff7f -> c0c0c0 */
79 15 /* ffffff -> ffffff */
82 #define PIXEL_VALUE(r,g,b) \
83 COLOR_mapEGAPixel[EGAmapping[((r)*PRIMARY_LEVELS+(g))*PRIMARY_LEVELS+(b)]]
85 /* X image for building dithered pixmap */
86 static XImage
*ditherImage
= NULL
;
89 /***********************************************************************
92 * Create the X image used for dithering.
94 BOOL32
X11DRV_BRUSH_Init(void)
96 XCREATEIMAGE( ditherImage
, MATRIX_SIZE
, MATRIX_SIZE
, screenDepth
);
97 return (ditherImage
!= NULL
);
101 /***********************************************************************
104 static Pixmap
BRUSH_DitherColor( DC
*dc
, COLORREF color
)
106 static COLORREF prevColor
= 0xffffffff;
110 if (color
!= prevColor
)
112 int r
= GetRValue( color
) * DITHER_LEVELS
;
113 int g
= GetGValue( color
) * DITHER_LEVELS
;
114 int b
= GetBValue( color
) * DITHER_LEVELS
;
115 const int *pmatrix
= dither_matrix
;
117 for (y
= 0; y
< MATRIX_SIZE
; y
++)
119 for (x
= 0; x
< MATRIX_SIZE
; x
++)
121 int d
= *pmatrix
++ * 256;
122 int dr
= ((r
+ d
) / MATRIX_SIZE_2
) / 256;
123 int dg
= ((g
+ d
) / MATRIX_SIZE_2
) / 256;
124 int db
= ((b
+ d
) / MATRIX_SIZE_2
) / 256;
125 XPutPixel( ditherImage
, x
, y
, PIXEL_VALUE(dr
,dg
,db
) );
131 pixmap
= XCreatePixmap( display
, rootWindow
,
132 MATRIX_SIZE
, MATRIX_SIZE
, screenDepth
);
133 XPutImage( display
, pixmap
, BITMAP_colorGC
, ditherImage
, 0, 0,
134 0, 0, MATRIX_SIZE
, MATRIX_SIZE
);
139 /***********************************************************************
140 * BRUSH_SelectSolidBrush
142 static void BRUSH_SelectSolidBrush( DC
*dc
, COLORREF color
)
144 if ((dc
->w
.bitsPerPixel
> 1) && (screenDepth
<= 8) && !COLOR_IsSolid( color
))
147 dc
->u
.x
.brush
.pixmap
= BRUSH_DitherColor( dc
, color
);
148 dc
->u
.x
.brush
.fillStyle
= FillTiled
;
149 dc
->u
.x
.brush
.pixel
= 0;
154 dc
->u
.x
.brush
.pixel
= COLOR_ToPhysical( dc
, color
);
155 dc
->u
.x
.brush
.fillStyle
= FillSolid
;
160 /***********************************************************************
161 * BRUSH_SelectPatternBrush
163 static BOOL32
BRUSH_SelectPatternBrush( DC
* dc
, HBITMAP32 hbitmap
)
165 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
166 if (!bmp
) return FALSE
;
167 dc
->u
.x
.brush
.pixmap
= XCreatePixmap( display
, rootWindow
,
168 8, 8, bmp
->bitmap
.bmBitsPixel
);
169 XCopyArea( display
, bmp
->pixmap
, dc
->u
.x
.brush
.pixmap
,
170 BITMAP_GC(bmp
), 0, 0, 8, 8, 0, 0 );
172 if (bmp
->bitmap
.bmBitsPixel
> 1)
174 dc
->u
.x
.brush
.fillStyle
= FillTiled
;
175 dc
->u
.x
.brush
.pixel
= 0; /* Ignored */
179 dc
->u
.x
.brush
.fillStyle
= FillOpaqueStippled
;
180 dc
->u
.x
.brush
.pixel
= -1; /* Special case (see DC_SetupGCForBrush) */
188 /***********************************************************************
191 HBRUSH32
X11DRV_BRUSH_SelectObject( DC
* dc
, HBRUSH32 hbrush
, BRUSHOBJ
* brush
)
194 BITMAPINFO
* bmpInfo
;
195 HBRUSH16 prevHandle
= dc
->w
.hBrush
;
197 dprintf_gdi(stddeb
, "Brush_SelectObject: hdc=%04x hbrush=%04x\n",
200 if (dc
->header
.wMagic
== METAFILE_DC_MAGIC
)
202 LOGBRUSH16 logbrush
= { brush
->logbrush
.lbStyle
,
203 brush
->logbrush
.lbColor
,
204 brush
->logbrush
.lbHatch
};
205 switch (brush
->logbrush
.lbStyle
)
210 if (!MF_CreateBrushIndirect( dc
, hbrush
, &logbrush
)) return 0;
214 if (!MF_CreatePatternBrush( dc
, hbrush
, &logbrush
)) return 0;
217 return 1; /* FIXME? */
220 dc
->w
.hBrush
= hbrush
;
222 if (dc
->u
.x
.brush
.pixmap
)
224 XFreePixmap( display
, dc
->u
.x
.brush
.pixmap
);
225 dc
->u
.x
.brush
.pixmap
= 0;
227 dc
->u
.x
.brush
.style
= brush
->logbrush
.lbStyle
;
229 switch(brush
->logbrush
.lbStyle
)
232 dprintf_gdi( stddeb
,"BS_NULL\n" );
236 dprintf_gdi( stddeb
,"BS_SOLID\n" );
237 BRUSH_SelectSolidBrush( dc
, brush
->logbrush
.lbColor
);
241 dprintf_gdi( stddeb
, "BS_HATCHED\n" );
242 dc
->u
.x
.brush
.pixel
= COLOR_ToPhysical( dc
, brush
->logbrush
.lbColor
);
243 dc
->u
.x
.brush
.pixmap
= XCreateBitmapFromData( display
, rootWindow
,
244 HatchBrushes
[brush
->logbrush
.lbHatch
], 8, 8 );
245 dc
->u
.x
.brush
.fillStyle
= FillStippled
;
249 dprintf_gdi( stddeb
, "BS_PATTERN\n");
250 BRUSH_SelectPatternBrush( dc
, (HBRUSH16
)brush
->logbrush
.lbHatch
);
254 dprintf_gdi( stddeb
, "BS_DIBPATTERN\n");
255 if ((bmpInfo
= (BITMAPINFO
*) GlobalLock16( (HGLOBAL16
)brush
->logbrush
.lbHatch
)))
257 int size
= DIB_BitmapInfoSize( bmpInfo
, brush
->logbrush
.lbColor
);
258 hBitmap
= CreateDIBitmap32( dc
->hSelf
, &bmpInfo
->bmiHeader
,
259 CBM_INIT
, ((char *)bmpInfo
) + size
,
261 (WORD
)brush
->logbrush
.lbColor
);
262 BRUSH_SelectPatternBrush( dc
, hBitmap
);
263 DeleteObject16( hBitmap
);
264 GlobalUnlock16( (HGLOBAL16
)brush
->logbrush
.lbHatch
);