blob: b83cbba8069f6efed28d09267b4871620690375b [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18//
19// ColourCube - structure to represent a colour cube. The colour cube consists
20// of its dimensions (nRed x nGreen x nBlue) and a table mapping an (r,g,b)
21// triple to a pixel value.
22//
23// A colour cube is used in two cases. The first is internally in a viewer
24// when it cannot use a trueColour format, nor can it have exclusive access to
25// a writable colour map. This is most notably the case for an X viewer
26// wishing to use a PseudoColor X server's default colormap.
27//
28// The second use is on the server side when a client has asked for a colour
29// map and the server is trueColour. Instead of setting an uneven trueColour
30// format like bgr233, it can set the client's colour map up with a 6x6x6
31// colour cube. For this use the colour cube table has a null mapping, which
32// makes it easy to perform the reverse lookup operation from pixel value to
33// r,g,b values.
34
35#ifndef __RFB_COLOURCUBE_H__
36#define __RFB_COLOURCUBE_H__
37
38#include <rfb/Pixel.h>
39#include <rfb/ColourMap.h>
40
41namespace rfb {
42
43 class ColourCube : public ColourMap {
44 public:
45 ColourCube(int nr, int ng, int nb, Pixel* table_=0)
46 : nRed(nr), nGreen(ng), nBlue(nb), table(table_), deleteTable(false)
47 {
48 if (!table) {
49 table = new Pixel[size()];
50 deleteTable = true;
51 // set a null mapping by default
52 for (int i = 0; i < size(); i++)
53 table[i] = i;
54 }
55 }
56
57 ColourCube() : deleteTable(false) {}
58
59 virtual ~ColourCube() {
60 if (deleteTable) delete [] table;
61 }
62
63 void set(int r, int g, int b, Pixel p) {
64 table[(r * nGreen + g) * nBlue + b] = p;
65 }
66
67 Pixel lookup(int r, int g, int b) const {
68 return table[(r * nGreen + g) * nBlue + b];
69 }
70
71 int size() const { return nRed*nGreen*nBlue; }
72 int redMult() const { return nGreen*nBlue; }
73 int greenMult() const { return nBlue; }
74 int blueMult() const { return 1; }
75
76 // ColourMap lookup() method. Note that this only works when the table has
77 // the default null mapping.
78 virtual void lookup(int i, int* r, int* g, int* b) {
79 if (i >= size()) return;
80 *b = i % nBlue;
81 i /= nBlue;
82 *g = i % nGreen;
83 *r = i / nGreen;
84 *r = (*r * 65535 + (nRed-1) / 2) / (nRed-1);
85 *g = (*g * 65535 + (nGreen-1) / 2) / (nGreen-1);
86 *b = (*b * 65535 + (nBlue-1) / 2) / (nBlue-1);
87 }
88
89 int nRed;
90 int nGreen;
91 int nBlue;
92 Pixel* table;
93 bool deleteTable;
94 };
95}
96#endif