fix disconnect bug in libvncserver
[presencevnc] / libvnc / libvncserver / tableinit24.c
1 /*
2   24 bit
3  */
4
5 /*
6  *  OSXvnc Copyright (C) 2001 Dan McGuirk <mcguirk@incompleteness.net>.
7  *  Original Xvnc code Copyright (C) 1999 AT&T Laboratories Cambridge.  
8  *  All Rights Reserved.
9  *
10  *  This is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This software is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this software; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
23  *  USA.
24  */
25
26 static void
27 rfbInitOneRGBTable24 (uint8_t *table, int inMax, int outMax, int outShift,int swap);
28
29
30 static void
31 rfbInitColourMapSingleTable24(char **table, rfbPixelFormat *in,
32                             rfbPixelFormat *out,rfbColourMap* colourMap)
33 {
34     uint32_t i, r, g, b, outValue;
35     uint8_t *t;
36     uint8_t c;
37     unsigned int nEntries = 1 << in->bitsPerPixel;
38     int shift = colourMap->is16?16:8;
39
40     if (*table) free(*table);
41     *table = (char *)malloc(nEntries * 3 + 1);
42     t = (uint8_t *)*table;
43
44     for (i = 0; i < nEntries; i++) {
45         r = g = b = 0;
46         if(i < colourMap->count) {
47           if(colourMap->is16) {
48             r = colourMap->data.shorts[3*i+0];
49             g = colourMap->data.shorts[3*i+1];
50             b = colourMap->data.shorts[3*i+2];
51           } else {
52             r = colourMap->data.bytes[3*i+0];
53             g = colourMap->data.bytes[3*i+1];
54             b = colourMap->data.bytes[3*i+2];
55           }
56         }
57         outValue = ((((r * (1 + out->redMax)) >> shift) << out->redShift) |
58                 (((g * (1 + out->greenMax)) >> shift) << out->greenShift) |
59                 (((b * (1 + out->blueMax)) >> shift) << out->blueShift));
60         *(uint32_t*)&t[3*i] = outValue;
61         if(!rfbEndianTest)
62           memmove(t+3*i,t+3*i+1,3);
63         if (out->bigEndian != in->bigEndian) {
64           c = t[3*i]; t[3*i] = t[3*i+2]; t[3*i+2] = c;
65         }
66     }
67 }
68
69 /*
70  * rfbInitTrueColourSingleTable sets up a single lookup table for truecolour
71  * translation.
72  */
73
74 static void
75 rfbInitTrueColourSingleTable24 (char **table, rfbPixelFormat *in,
76                                  rfbPixelFormat *out)
77 {
78     int i,outValue;
79     int inRed, inGreen, inBlue, outRed, outGreen, outBlue;
80     uint8_t *t;
81     uint8_t c;
82     int nEntries = 1 << in->bitsPerPixel;
83
84     if (*table) free(*table);
85     *table = (char *)malloc(nEntries * 3 + 1);
86     t = (uint8_t *)*table;
87
88     for (i = 0; i < nEntries; i++) {
89         inRed   = (i >> in->redShift)   & in->redMax;
90         inGreen = (i >> in->greenShift) & in->greenMax;
91         inBlue  = (i >> in->blueShift)  & in->blueMax;
92
93         outRed   = (inRed   * out->redMax   + in->redMax / 2)   / in->redMax;
94         outGreen = (inGreen * out->greenMax + in->greenMax / 2) / in->greenMax;
95         outBlue  = (inBlue  * out->blueMax  + in->blueMax / 2)  / in->blueMax;
96
97         outValue = ((outRed   << out->redShift)   |
98                 (outGreen << out->greenShift) |
99                 (outBlue  << out->blueShift));
100         *(uint32_t*)&t[3*i] = outValue;
101         if(!rfbEndianTest)
102           memmove(t+3*i,t+3*i+1,3);
103         if (out->bigEndian != in->bigEndian) {
104           c = t[3*i]; t[3*i] = t[3*i+2]; t[3*i+2] = c;
105         }
106     }
107 }
108
109
110 /*
111  * rfbInitTrueColourRGBTables sets up three separate lookup tables for the
112  * red, green and blue values.
113  */
114
115 static void
116 rfbInitTrueColourRGBTables24 (char **table, rfbPixelFormat *in,
117                                rfbPixelFormat *out)
118 {
119     uint8_t *redTable;
120     uint8_t *greenTable;
121     uint8_t *blueTable;
122
123     if (*table) free(*table);
124     *table = (char *)malloc((in->redMax + in->greenMax + in->blueMax + 3)
125                             * 3 + 1);
126     redTable = (uint8_t *)*table;
127     greenTable = redTable + 3*(in->redMax + 1);
128     blueTable = greenTable + 3*(in->greenMax + 1);
129
130     rfbInitOneRGBTable24 (redTable, in->redMax, out->redMax,
131                            out->redShift, (out->bigEndian != in->bigEndian));
132     rfbInitOneRGBTable24 (greenTable, in->greenMax, out->greenMax,
133                            out->greenShift, (out->bigEndian != in->bigEndian));
134     rfbInitOneRGBTable24 (blueTable, in->blueMax, out->blueMax,
135                            out->blueShift, (out->bigEndian != in->bigEndian));
136 }
137
138 static void
139 rfbInitOneRGBTable24 (uint8_t *table, int inMax, int outMax, int outShift,
140                        int swap)
141 {
142     int i;
143     int nEntries = inMax + 1;
144     uint32_t outValue;
145     uint8_t c;
146
147     for (i = 0; i < nEntries; i++) {
148       outValue = ((i * outMax + inMax / 2) / inMax) << outShift;
149       *(uint32_t *)&table[3*i] = outValue;
150       if(!rfbEndianTest)
151         memmove(table+3*i,table+3*i+1,3);
152         if (swap) {
153           c = table[3*i]; table[3*i] = table[3*i+2];
154           table[3*i+2] = c;
155         }
156     }
157 }