]> git.sesse.net Git - rdpsrv/blob - Xserver/programs/Xserver/hw/vnc/tableinittctemplate.c
18ff0ef07707a073eeb86d8331615d233997ac7f
[rdpsrv] / Xserver / programs / Xserver / hw / vnc / tableinittctemplate.c
1 /*
2  * tableinittctemplate.c - template for initialising lookup tables for
3  * truecolour to truecolour translation.
4  *
5  * This file shouldn't be compiled.  It is included multiple times by
6  * translate.c, each time with a different definition of the macro OUT.
7  * For each value of OUT, this file defines two functions for initialising
8  * lookup tables.  One is for truecolour translation using a single lookup
9  * table, the other is for truecolour translation using three separate
10  * lookup tables for the red, green and blue values.
11  *
12  * I know this code isn't nice to read because of all the macros, but
13  * efficiency is important here.
14  */
15
16 /*
17  *  Copyright (C) 1999 AT&T Laboratories Cambridge.  All Rights Reserved.
18  *
19  *  This is free software; you can redistribute it and/or modify
20  *  it under the terms of the GNU General Public License as published by
21  *  the Free Software Foundation; either version 2 of the License, or
22  *  (at your option) any later version.
23  *
24  *  This software is distributed in the hope that it will be useful,
25  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
26  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  *  GNU General Public License for more details.
28  *
29  *  You should have received a copy of the GNU General Public License
30  *  along with this software; if not, write to the Free Software
31  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
32  *  USA.
33  */
34
35 #if !defined(OUT)
36 #error "This file shouldn't be compiled."
37 #error "It is included as part of translate.c"
38 #endif
39
40 #define OUT_T CONCAT2E(CARD,OUT)
41 #define SwapOUT(x) CONCAT2E(Swap,OUT(x))
42 #define rfbInitTrueColourSingleTableOUT \
43                                 CONCAT2E(rfbInitTrueColourSingleTable,OUT)
44 #define rfbInitTrueColourRGBTablesOUT CONCAT2E(rfbInitTrueColourRGBTables,OUT)
45 #define rfbInitOneRGBTableOUT CONCAT2E(rfbInitOneRGBTable,OUT)
46
47 static void
48 rfbInitOneRGBTableOUT (OUT_T *table, int inMax, int outMax, int outShift,
49                        int swap);
50
51
52 /*
53  * rfbInitTrueColourSingleTable sets up a single lookup table for truecolour
54  * translation.
55  */
56
57 static void
58 rfbInitTrueColourSingleTableOUT (char **table, rfbPixelFormat *in,
59                                  rfbPixelFormat *out)
60 {
61     int i;
62     int inRed, inGreen, inBlue, outRed, outGreen, outBlue;
63     OUT_T *t;
64     int nEntries = 1 << in->bitsPerPixel;
65
66     if (*table) free(*table);
67     *table = (char *)malloc(nEntries * sizeof(OUT_T));
68     t = (OUT_T *)*table;
69
70     for (i = 0; i < nEntries; i++) {
71         inRed   = (i >> in->redShift)   & in->redMax;
72         inGreen = (i >> in->greenShift) & in->greenMax;
73         inBlue  = (i >> in->blueShift)  & in->blueMax;
74
75         outRed   = (inRed   * out->redMax   + in->redMax / 2)   / in->redMax;
76         outGreen = (inGreen * out->greenMax + in->greenMax / 2) / in->greenMax;
77         outBlue  = (inBlue  * out->blueMax  + in->blueMax / 2)  / in->blueMax;
78
79         t[i] = ((outRed   << out->redShift)   |
80                 (outGreen << out->greenShift) |
81                 (outBlue  << out->blueShift));
82 #if (OUT != 8)
83         if (out->bigEndian != in->bigEndian) {
84             t[i] = SwapOUT(t[i]);
85         }
86 #endif
87     }
88 }
89
90
91 /*
92  * rfbInitTrueColourRGBTables sets up three separate lookup tables for the
93  * red, green and blue values.
94  */
95
96 static void
97 rfbInitTrueColourRGBTablesOUT (char **table, rfbPixelFormat *in,
98                                rfbPixelFormat *out)
99 {
100     OUT_T *redTable;
101     OUT_T *greenTable;
102     OUT_T *blueTable;
103
104     if (*table) free(*table);
105     *table = (char *)malloc((in->redMax + in->greenMax + in->blueMax + 3)
106                             * sizeof(OUT_T));
107     redTable = (OUT_T *)*table;
108     greenTable = redTable + in->redMax + 1;
109     blueTable = greenTable + in->greenMax + 1;
110
111     rfbInitOneRGBTableOUT (redTable, in->redMax, out->redMax,
112                            out->redShift, (out->bigEndian != in->bigEndian));
113     rfbInitOneRGBTableOUT (greenTable, in->greenMax, out->greenMax,
114                            out->greenShift, (out->bigEndian != in->bigEndian));
115     rfbInitOneRGBTableOUT (blueTable, in->blueMax, out->blueMax,
116                            out->blueShift, (out->bigEndian != in->bigEndian));
117 }
118
119 static void
120 rfbInitOneRGBTableOUT (OUT_T *table, int inMax, int outMax, int outShift,
121                        int swap)
122 {
123     int i;
124     int nEntries = inMax + 1;
125
126     for (i = 0; i < nEntries; i++) {
127         table[i] = ((i * outMax + inMax / 2) / inMax) << outShift;
128 #if (OUT != 8)
129         if (swap) {
130             table[i] = SwapOUT(table[i]);
131         }
132 #endif
133     }
134 }
135
136 #undef OUT_T
137 #undef SwapOUT
138 #undef rfbInitTrueColourSingleTableOUT
139 #undef rfbInitTrueColourRGBTablesOUT
140 #undef rfbInitOneRGBTableOUT