Release 940405
[wine.git] / objects / dither.c
blob52101063ac6ef667fc31545e89fc81236fca0f35
1 /*
2 * Dithering functions
4 * Copyright 1994 Alexandre Julliard
5 */
7 static char Copyright[] = "Copyright Alexandre Julliard, 1994";
9 #include <stdlib.h>
10 #include <X11/Xlib.h>
11 #include <X11/Xutil.h>
13 #include "gdi.h"
14 #include "bitmap.h"
17 /* Levels of each primary */
18 #define PRIMARY_LEVELS 3
19 #define TOTAL_LEVELS (PRIMARY_LEVELS*PRIMARY_LEVELS*PRIMARY_LEVELS)
21 /* Dithering matrix size */
22 #define MATRIX_SIZE 8
23 #define MATRIX_SIZE_2 (MATRIX_SIZE*MATRIX_SIZE)
25 /* Total number of possible levels for a dithered primary color */
26 #define DITHER_LEVELS (MATRIX_SIZE_2 * (PRIMARY_LEVELS-1) + 1)
28 /* Dithering matrix */
29 static const int dither_matrix[MATRIX_SIZE_2] =
31 0, 32, 8, 40, 2, 34, 10, 42,
32 48, 16, 56, 24, 50, 18, 58, 26,
33 12, 44, 4, 36, 14, 46, 6, 38,
34 60, 28, 52, 20, 62, 30, 54, 22,
35 3, 35, 11, 43, 1, 33, 9, 41,
36 51, 19, 59, 27, 49, 17, 57, 25,
37 15, 47, 7, 39, 13, 45, 5, 37,
38 63, 31, 55, 23, 61, 29, 53, 21
41 /* Mapping between (R,G,B) triples and EGA colors */
42 static const int EGAmapping[TOTAL_LEVELS] =
44 0, /* 000000 -> 000000 */
45 4, /* 00007f -> 000080 */
46 12, /* 0000ff -> 0000ff */
47 2, /* 007f00 -> 008000 */
48 6, /* 007f7f -> 008080 */
49 6, /* 007fff -> 008080 */
50 10, /* 00ff00 -> 00ff00 */
51 6, /* 00ff7f -> 008080 */
52 14, /* 00ffff -> 00ffff */
53 1, /* 7f0000 -> 800000 */
54 5, /* 7f007f -> 800080 */
55 5, /* 7f00ff -> 800080 */
56 3, /* 7f7f00 -> 808000 */
57 8, /* 7f7f7f -> 808080 */
58 7, /* 7f7fff -> c0c0c0 */
59 3, /* 7fff00 -> 808000 */
60 7, /* 7fff7f -> c0c0c0 */
61 7, /* 7fffff -> c0c0c0 */
62 9, /* ff0000 -> ff0000 */
63 5, /* ff007f -> 800080 */
64 13, /* ff00ff -> ff00ff */
65 3, /* ff7f00 -> 808000 */
66 7, /* ff7f7f -> c0c0c0 */
67 7, /* ff7fff -> c0c0c0 */
68 11, /* ffff00 -> ffff00 */
69 7, /* ffff7f -> c0c0c0 */
70 15 /* ffffff -> ffffff */
73 /* Map an EGA index (0..15) to a pixel value */
74 extern int COLOR_mapEGAPixel[16]; /* in color.c */
76 #define PIXEL_VALUE(r,g,b) \
77 COLOR_mapEGAPixel[EGAmapping[((r)*PRIMARY_LEVELS+(g))*PRIMARY_LEVELS+(b)]]
79 /* X image for building dithered pixmap */
80 static XImage *ditherImage = NULL;
81 static char *imageData = NULL;
84 /***********************************************************************
85 * DITHER_Init
87 * Create the X image used for dithering.
89 BOOL DITHER_Init()
91 int bytes_per_line = (screenDepth * MATRIX_SIZE + 7) / 8;
92 if (!(imageData = (char *) malloc( bytes_per_line * MATRIX_SIZE )))
93 return FALSE;
94 ditherImage = XCreateImage( display, DefaultVisualOfScreen(screen),
95 screenDepth, ZPixmap, 0, imageData,
96 MATRIX_SIZE, MATRIX_SIZE, 8, bytes_per_line );
97 return (ditherImage != NULL);
101 /***********************************************************************
102 * DITHER_DitherColor
104 Pixmap DITHER_DitherColor( DC *dc, COLORREF color )
106 static COLORREF prevColor = 0xffffffff;
107 unsigned int x, y;
108 Pixmap pixmap;
110 /* printf( "Dither: %x\n", color ); */
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 WORD *mapping = (WORD *) GDI_HEAP_ADDR( dc->u.x.pal.hMapping );
121 for (y = 0; y < MATRIX_SIZE; y++)
123 for (x = 0; x < MATRIX_SIZE; x++)
125 int d = *pmatrix++ * 256;
126 int dr = ((r + d) / MATRIX_SIZE_2) / 256;
127 int dg = ((g + d) / MATRIX_SIZE_2) / 256;
128 int db = ((b + d) / MATRIX_SIZE_2) / 256;
129 XPutPixel( ditherImage, x, y, PIXEL_VALUE(dr,dg,db) );
132 prevColor = color;
135 pixmap = XCreatePixmap( display, rootWindow,
136 MATRIX_SIZE, MATRIX_SIZE, screenDepth );
137 XPutImage( display, pixmap, BITMAP_colorGC, ditherImage, 0, 0,
138 0, 0, MATRIX_SIZE, MATRIX_SIZE );
139 return pixmap;