CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/external/source/vncdll/winvnc/tableinittctemplate.cpp
Views: 11780
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 OUTBPP.
7
* For each value of OUTBPP, 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
#if !defined(OUTBPP)
17
#error "This file shouldn't be compiled."
18
#error "It is included as part of translate.c"
19
#endif
20
21
#define OUT_T CONCAT2E(CARD,OUTBPP)
22
#define SwapOUT(x) CONCAT2E(Swap,OUTBPP) (x)
23
#define rfbInitTrueColourSingleTableOUT \
24
CONCAT2E(rfbInitTrueColourSingleTable,OUTBPP)
25
#define rfbInitTrueColourRGBTablesOUT CONCAT2E(rfbInitTrueColourRGBTables,OUTBPP)
26
#define rfbInitOneRGBTableOUT CONCAT2E(rfbInitOneRGBTable,OUTBPP)
27
28
static void
29
rfbInitOneRGBTableOUT (OUT_T *table, int inMax, int outMax, int outShift,
30
int swap);
31
32
33
/*
34
* rfbInitTrueColourSingleTable sets up a single lookup table for truecolour
35
* translation.
36
*/
37
38
static void
39
rfbInitTrueColourSingleTableOUT (char **table, rfbPixelFormat *in,
40
rfbPixelFormat *out)
41
{
42
int i;
43
int inRed, inGreen, inBlue, outRed, outGreen, outBlue;
44
OUT_T *t;
45
int nEntries = 1 << in->bitsPerPixel;
46
47
if (*table) free(*table);
48
*table = (char *)malloc(nEntries * sizeof(OUT_T));
49
if (table == NULL) return;
50
t = (OUT_T *)*table;
51
52
for (i = 0; i < nEntries; i++) {
53
inRed = (i >> in->redShift) & in->redMax;
54
inGreen = (i >> in->greenShift) & in->greenMax;
55
inBlue = (i >> in->blueShift) & in->blueMax;
56
57
outRed = (inRed * out->redMax + in->redMax / 2) / in->redMax;
58
outGreen = (inGreen * out->greenMax + in->greenMax / 2) / in->greenMax;
59
outBlue = (inBlue * out->blueMax + in->blueMax / 2) / in->blueMax;
60
61
t[i] = ((outRed << out->redShift) |
62
(outGreen << out->greenShift) |
63
(outBlue << out->blueShift));
64
#if (OUTBPP != 8)
65
if (out->bigEndian != in->bigEndian) {
66
t[i] = SwapOUT(t[i]);
67
}
68
#endif
69
}
70
}
71
72
73
/*
74
* rfbInitTrueColourRGBTables sets up three separate lookup tables for the
75
* red, green and blue values.
76
*/
77
78
static void
79
rfbInitTrueColourRGBTablesOUT (char **table, rfbPixelFormat *in,
80
rfbPixelFormat *out)
81
{
82
OUT_T *redTable;
83
OUT_T *greenTable;
84
OUT_T *blueTable;
85
86
if (*table) free(*table);
87
*table = (char *)malloc((in->redMax + in->greenMax + in->blueMax + 3)
88
* sizeof(OUT_T));
89
redTable = (OUT_T *)*table;
90
greenTable = redTable + in->redMax + 1;
91
blueTable = greenTable + in->greenMax + 1;
92
93
rfbInitOneRGBTableOUT (redTable, in->redMax, out->redMax,
94
out->redShift, (out->bigEndian != in->bigEndian));
95
rfbInitOneRGBTableOUT (greenTable, in->greenMax, out->greenMax,
96
out->greenShift, (out->bigEndian != in->bigEndian));
97
rfbInitOneRGBTableOUT (blueTable, in->blueMax, out->blueMax,
98
out->blueShift, (out->bigEndian != in->bigEndian));
99
}
100
101
static void
102
rfbInitOneRGBTableOUT (OUT_T *table, int inMax, int outMax, int outShift,
103
int swap)
104
{
105
int i;
106
int nEntries = inMax + 1;
107
108
for (i = 0; i < nEntries; i++) {
109
table[i] = ((i * outMax + inMax / 2) / inMax) << outShift;
110
#if (OUTBPP != 8)
111
if (swap) {
112
table[i] = SwapOUT(table[i]);
113
}
114
#endif
115
}
116
}
117
118
#undef OUT_T
119
#undef SwapOUT
120
#undef rfbInitTrueColourSingleTableOUT
121
#undef rfbInitTrueColourRGBTablesOUT
122
#undef rfbInitOneRGBTableOUT
123
124