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