version 2.8
zdither.c
1 /*
2  * Highly optimised dithering 16 bits -> 8 bits.
3  * The formulas were taken in Mesa (Bob Mercier mercier@hollywood.cinenet.net).
4  */
5 
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include "zbuffer.h"
9 #include <assert.h>
10 
11 #if defined(TGL_FEATURE_8_BITS)
12 
13 #define _R 5
14 #define _G 9
15 #define _B 5
16 #define _DX 4
17 #define _DY 4
18 #define _D (_DX*_DY)
19 #define _MIX(r,g,b) ( ((g)<<6) | ((b)<<3) | (r) )
20 
21 #define DITHER_TABLE_SIZE (1 << 15)
22 
23 #define DITHER_INDEX(r,g,b) ((b) + (g) * _B + (r) * (_B * _G))
24 
25 #define MAXC 256
26 static int kernel8[_DY*_DX] = {
27  0 * MAXC, 8 * MAXC, 2 * MAXC, 10 * MAXC,
28  12 * MAXC, 4 * MAXC, 14 * MAXC, 6 * MAXC,
29  3 * MAXC, 11 * MAXC, 1 * MAXC, 9 * MAXC,
30  15 * MAXC, 7 * MAXC, 13 * MAXC, 5 * MAXC,
31 };
32 
33 /* we build the color table and the lookup table */
34 
35 void ZB_initDither(ZBuffer *zb,int nb_colors,
36  unsigned char *color_indexes,int *color_table)
37 {
38  int c,r,g,b,i,index,r1,g1,b1;
39 
40  if (nb_colors < (_R * _G * _B)) {
41  gl_fatal_error("zdither: not enough colors\n");
42  }
43 
44  for(i=0;i<nb_colors;i++) color_table[i]=0;
45 
46  zb->nb_colors=nb_colors;
47  zb->ctable=gl_malloc(nb_colors * sizeof(int));
48 
49  for (r = 0; r < _R; r++) {
50  for (g = 0; g < _G; g++) {
51  for (b = 0; b < _B; b++) {
52  r1=(r*255) / (_R - 1);
53  g1=(g*255) / (_G - 1);
54  b1=(b*255) / (_B - 1);
55  index=DITHER_INDEX(r,g,b);
56  c=(r1 << 16) | (g1 << 8) | b1;
57  zb->ctable[index]=c;
58  color_table[index]=c;
59  }
60  }
61  }
62 
63  zb->dctable=gl_malloc( DITHER_TABLE_SIZE );
64 
65  for(i=0;i<DITHER_TABLE_SIZE;i++) {
66  r=(i >> 12) & 0x7;
67  g=(i >> 8) & 0xF;
68  b=(i >> 3) & 0x7;
69  index=DITHER_INDEX(r,g,b);
70  zb->dctable[i]=color_indexes[index];
71  }
72 }
73 
74 void ZB_closeDither(ZBuffer *zb)
75 {
76  gl_free(zb->ctable);
77  gl_free(zb->dctable);
78 }
79 
80 #if 0
81 int ZDither_lookupColor(int r,int g,int b)
82 {
83  unsigned char *ctable=zdither_color_table;
84  return ctable[_MIX(_DITH0(_R, r), _DITH0(_G, g),_DITH0(_B, b))];
85 }
86 #endif
87 
88 
89 #define DITHER_PIXEL2(a) \
90 { \
91  register int v,t,r,g,c; \
92  v=*(unsigned int *)(pp+(a)); \
93  g=(v & 0x07DF07DF) + g_d; \
94  r=(((v & 0xF800F800) >> 2) + r_d) & 0x70007000; \
95  t=r | g; \
96  c=ctable[t & 0xFFFF] | (ctable[t >> 16] << 8); \
97  *(unsigned short *)(dest+(a))=c; \
98 }
99 
100 /* NOTE: all the memory access are 16 bit aligned, so if buf or
101  linesize are not multiple of 2, it cannot work efficiently (or
102  hang!) */
103 
104 void ZB_ditherFrameBuffer(ZBuffer *zb,unsigned char *buf,
105  int linesize)
106 {
107  int xk,yk,x,y,c1,c2;
108  unsigned char *dest1;
109  unsigned short *pp1;
110  int r_d,g_d,b_d;
111  unsigned char *ctable=zb->dctable;
112  register unsigned char *dest;
113  register unsigned short *pp;
114 
115  assert( ((long)buf & 1) == 0 && (linesize & 1) == 0);
116 
117  for(yk=0;yk<4;yk++) {
118  for(xk=0;xk<4;xk+=2) {
119 #if BYTE_ORDER == BIG_ENDIAN
120  c1=kernel8[yk*4+xk+1];
121  c2=kernel8[yk*4+xk];
122 #else
123  c1=kernel8[yk*4+xk];
124  c2=kernel8[yk*4+xk+1];
125 #endif
126  r_d=((c1 << 2) & 0xF800) >> 2;
127  g_d=(c1 >> 4) & 0x07C0;
128  b_d=(c1 >> 9) & 0x001F;
129 
130  r_d|=(((c2 << 2) & 0xF800) >> 2) << 16;
131  g_d|=((c2 >> 4) & 0x07C0) << 16;
132  b_d|=((c2 >> 9) & 0x001F) << 16;
133  g_d=b_d | g_d;
134 
135  dest1=buf + (yk * linesize) + xk;
136  pp1=zb->pbuf + (yk * zb->xsize) + xk;
137 
138  for(y=yk;y<zb->ysize;y+=4) {
139  dest=dest1;
140  pp=pp1;
141  for(x=xk;x<zb->xsize;x+=16) {
142 
143  DITHER_PIXEL2(0);
144  DITHER_PIXEL2(1*4);
145  DITHER_PIXEL2(2*4);
146  DITHER_PIXEL2(3*4);
147 
148  pp+=16;
149  dest+=16;
150  }
151  dest1+=linesize*4;
152  pp1+=zb->xsize*4;
153  }
154  }
155  }
156 }
157 
158 #endif