4 * Copyright 1993, 1994 Alexandre Julliard
16 #include "debugtools.h"
18 DEFAULT_DEBUG_CHANNEL(gdi);
20 static const char HatchBrushes[NB_HATCH_STYLES + 1][8] =
22 { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HS_HORIZONTAL */
23 { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HS_VERTICAL */
24 { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HS_FDIAGONAL */
25 { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HS_BDIAGONAL */
26 { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HS_CROSS */
27 { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 }, /* HS_DIAGCROSS */
28 { 0xee, 0xbb, 0xee, 0xbb, 0xee, 0xbb, 0xee, 0xbb } /* Hack for DKGRAY */
31 /* Levels of each primary for dithering */
32 #define PRIMARY_LEVELS 3
33 #define TOTAL_LEVELS (PRIMARY_LEVELS*PRIMARY_LEVELS*PRIMARY_LEVELS)
35 /* Dithering matrix size */
37 #define MATRIX_SIZE_2 (MATRIX_SIZE*MATRIX_SIZE)
39 /* Total number of possible levels for a dithered primary color */
40 #define DITHER_LEVELS (MATRIX_SIZE_2 * (PRIMARY_LEVELS-1) + 1)
42 /* Dithering matrix */
43 static const int dither_matrix[MATRIX_SIZE_2] =
45 0, 32, 8, 40, 2, 34, 10, 42,
46 48, 16, 56, 24, 50, 18, 58, 26,
47 12, 44, 4, 36, 14, 46, 6, 38,
48 60, 28, 52, 20, 62, 30, 54, 22,
49 3, 35, 11, 43, 1, 33, 9, 41,
50 51, 19, 59, 27, 49, 17, 57, 25,
51 15, 47, 7, 39, 13, 45, 5, 37,
52 63, 31, 55, 23, 61, 29, 53, 21
55 /* Mapping between (R,G,B) triples and EGA colors */
56 static const int EGAmapping[TOTAL_LEVELS] =
58 0, /* 000000 -> 000000 */
59 4, /* 00007f -> 000080 */
60 12, /* 0000ff -> 0000ff */
61 2, /* 007f00 -> 008000 */
62 6, /* 007f7f -> 008080 */
63 6, /* 007fff -> 008080 */
64 10, /* 00ff00 -> 00ff00 */
65 6, /* 00ff7f -> 008080 */
66 14, /* 00ffff -> 00ffff */
67 1, /* 7f0000 -> 800000 */
68 5, /* 7f007f -> 800080 */
69 5, /* 7f00ff -> 800080 */
70 3, /* 7f7f00 -> 808000 */
71 8, /* 7f7f7f -> 808080 */
72 7, /* 7f7fff -> c0c0c0 */
73 3, /* 7fff00 -> 808000 */
74 7, /* 7fff7f -> c0c0c0 */
75 7, /* 7fffff -> c0c0c0 */
76 9, /* ff0000 -> ff0000 */
77 5, /* ff007f -> 800080 */
78 13, /* ff00ff -> ff00ff */
79 3, /* ff7f00 -> 808000 */
80 7, /* ff7f7f -> c0c0c0 */
81 7, /* ff7fff -> c0c0c0 */
82 11, /* ffff00 -> ffff00 */
83 7, /* ffff7f -> c0c0c0 */
84 15 /* ffffff -> ffffff */
87 #define PIXEL_VALUE(r,g,b) \
88 X11DRV_PALETTE_mapEGAPixel[EGAmapping[((r)*PRIMARY_LEVELS+(g))*PRIMARY_LEVELS+(b)]]
90 /* X image for building dithered pixmap */
91 static XImage *ditherImage = NULL;
94 /***********************************************************************
97 static Pixmap BRUSH_DitherColor( DC *dc, COLORREF color )
99 static COLORREF prevColor = 0xffffffff;
105 ditherImage = X11DRV_DIB_CreateXImage( MATRIX_SIZE, MATRIX_SIZE, screen_depth );
106 if (!ditherImage) return 0;
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( gdi_display, root_window, MATRIX_SIZE, MATRIX_SIZE, screen_depth );
132 XPutImage( gdi_display, pixmap, BITMAP_colorGC, ditherImage, 0, 0,
133 0, 0, MATRIX_SIZE, MATRIX_SIZE );
139 /***********************************************************************
140 * BRUSH_SelectSolidBrush
142 static void BRUSH_SelectSolidBrush( DC *dc, COLORREF color )
144 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
146 if ((dc->bitsPerPixel > 1) && (screen_depth <= 8) && !COLOR_IsSolid( color ))
149 physDev->brush.pixmap = BRUSH_DitherColor( dc, color );
150 physDev->brush.fillStyle = FillTiled;
151 physDev->brush.pixel = 0;
156 physDev->brush.pixel = X11DRV_PALETTE_ToPhysical( dc, color );
157 physDev->brush.fillStyle = FillSolid;
162 /***********************************************************************
163 * BRUSH_SelectPatternBrush
165 static BOOL BRUSH_SelectPatternBrush( DC * dc, HBITMAP hbitmap )
168 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
169 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
170 if (!bmp) return FALSE;
173 if(!X11DRV_CreateBitmap(hbitmap))
176 if(bmp->funcs != dc->funcs) {
177 WARN("Trying to select non-X11 DDB into an X11 dc\n");
181 if ((dc->bitsPerPixel == 1) && (bmp->bitmap.bmBitsPixel != 1))
183 /* Special case: a color pattern on a monochrome DC */
184 physDev->brush.pixmap = TSXCreatePixmap( gdi_display, root_window, 8, 8, 1);
185 /* FIXME: should probably convert to monochrome instead */
186 TSXCopyPlane( gdi_display, (Pixmap)bmp->physBitmap, physDev->brush.pixmap,
187 BITMAP_monoGC, 0, 0, 8, 8, 0, 0, 1 );
191 physDev->brush.pixmap = TSXCreatePixmap( gdi_display, root_window,
192 8, 8, bmp->bitmap.bmBitsPixel );
193 TSXCopyArea( gdi_display, (Pixmap)bmp->physBitmap, physDev->brush.pixmap,
194 BITMAP_GC(bmp), 0, 0, 8, 8, 0, 0 );
197 if (bmp->bitmap.bmBitsPixel > 1)
199 physDev->brush.fillStyle = FillTiled;
200 physDev->brush.pixel = 0; /* Ignored */
204 physDev->brush.fillStyle = FillOpaqueStippled;
205 physDev->brush.pixel = -1; /* Special case (see DC_SetupGCForBrush) */
209 GDI_ReleaseObj( hbitmap );
216 /***********************************************************************
219 HBRUSH X11DRV_BRUSH_SelectObject( DC * dc, HBRUSH hbrush, BRUSHOBJ * brush )
222 BITMAPINFO * bmpInfo;
223 HBRUSH16 prevHandle = dc->hBrush;
224 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
226 TRACE("hdc=%04x hbrush=%04x\n",
231 if (physDev->brush.pixmap)
233 TSXFreePixmap( gdi_display, physDev->brush.pixmap );
234 physDev->brush.pixmap = 0;
236 physDev->brush.style = brush->logbrush.lbStyle;
238 switch(brush->logbrush.lbStyle)
245 TRACE("BS_SOLID\n" );
246 BRUSH_SelectSolidBrush( dc, brush->logbrush.lbColor );
250 TRACE("BS_HATCHED\n" );
251 physDev->brush.pixel = X11DRV_PALETTE_ToPhysical( dc, brush->logbrush.lbColor );
252 physDev->brush.pixmap = TSXCreateBitmapFromData( gdi_display, root_window,
253 HatchBrushes[brush->logbrush.lbHatch], 8, 8 );
254 physDev->brush.fillStyle = FillStippled;
258 TRACE("BS_PATTERN\n");
259 BRUSH_SelectPatternBrush( dc, (HBRUSH16)brush->logbrush.lbHatch );
263 TRACE("BS_DIBPATTERN\n");
264 if ((bmpInfo = (BITMAPINFO *) GlobalLock16( (HGLOBAL16)brush->logbrush.lbHatch )))
266 int size = DIB_BitmapInfoSize( bmpInfo, brush->logbrush.lbColor );
267 hBitmap = CreateDIBitmap( dc->hSelf, &bmpInfo->bmiHeader,
268 CBM_INIT, ((char *)bmpInfo) + size,
270 (WORD)brush->logbrush.lbColor );
271 BRUSH_SelectPatternBrush( dc, hBitmap );
272 DeleteObject16( hBitmap );
273 GlobalUnlock16( (HGLOBAL16)brush->logbrush.lbHatch );