Release 941227
[wine/multimedia.git] / objects / dither.c
blob79a1dcd12ece476b5b9310964d4a1ce5b10b1ec5
1 /*
2 * Dithering functions
4 * Copyright 1994 Alexandre Julliard
6 static char Copyright[] = "Copyright Alexandre Julliard, 1994";
7 */
9 #include <stdlib.h>
10 #include <X11/Xlib.h>
11 #include <X11/Xutil.h>
12 #include "color.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 #define PIXEL_VALUE(r,g,b) \
74 COLOR_mapEGAPixel[EGAmapping[((r)*PRIMARY_LEVELS+(g))*PRIMARY_LEVELS+(b)]]
76 /* X image for building dithered pixmap */
77 static XImage *ditherImage = NULL;
78 static char *imageData = NULL;
81 /***********************************************************************
82 * DITHER_Init
84 * Create the X image used for dithering.
86 BOOL DITHER_Init(void)
88 XCREATEIMAGE( ditherImage, MATRIX_SIZE, MATRIX_SIZE, screenDepth );
89 return (ditherImage != NULL);
93 /***********************************************************************
94 * DITHER_DitherColor
96 Pixmap DITHER_DitherColor( DC *dc, COLORREF color )
98 static COLORREF prevColor = 0xffffffff;
99 unsigned int x, y;
100 Pixmap pixmap;
102 if (color != prevColor)
104 int r = GetRValue( color ) * DITHER_LEVELS;
105 int g = GetGValue( color ) * DITHER_LEVELS;
106 int b = GetBValue( color ) * DITHER_LEVELS;
107 const int *pmatrix = dither_matrix;
109 for (y = 0; y < MATRIX_SIZE; y++)
111 for (x = 0; x < MATRIX_SIZE; x++)
113 int d = *pmatrix++ * 256;
114 int dr = ((r + d) / MATRIX_SIZE_2) / 256;
115 int dg = ((g + d) / MATRIX_SIZE_2) / 256;
116 int db = ((b + d) / MATRIX_SIZE_2) / 256;
117 XPutPixel( ditherImage, x, y, PIXEL_VALUE(dr,dg,db) );
120 prevColor = color;
123 pixmap = XCreatePixmap( display, rootWindow,
124 MATRIX_SIZE, MATRIX_SIZE, screenDepth );
125 XPutImage( display, pixmap, BITMAP_colorGC, ditherImage, 0, 0,
126 0, 0, MATRIX_SIZE, MATRIX_SIZE );
127 return pixmap;