You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ColourCube.h 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* Copyright (C) 2002-2003 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. #ifndef __RFB_COLOURCUBE_H__
  35. #define __RFB_COLOURCUBE_H__
  36. #include <rfb/Pixel.h>
  37. #include <rfb/ColourMap.h>
  38. namespace rfb {
  39. class ColourCube : public ColourMap {
  40. public:
  41. ColourCube(int nr, int ng, int nb, Pixel* table_=0)
  42. : nRed(nr), nGreen(ng), nBlue(nb), table(table_), deleteTable(false)
  43. {
  44. if (!table) {
  45. table = new Pixel[size()];
  46. deleteTable = true;
  47. // set a null mapping by default
  48. for (int i = 0; i < size(); i++)
  49. table[i] = i;
  50. }
  51. }
  52. ColourCube() : deleteTable(false) {}
  53. virtual ~ColourCube() {
  54. if (deleteTable) delete [] table;
  55. }
  56. void set(int r, int g, int b, Pixel p) {
  57. table[(r * nGreen + g) * nBlue + b] = p;
  58. }
  59. Pixel lookup(int r, int g, int b) const {
  60. return table[(r * nGreen + g) * nBlue + b];
  61. }
  62. int size() const { return nRed*nGreen*nBlue; }
  63. int redMult() const { return nGreen*nBlue; }
  64. int greenMult() const { return nBlue; }
  65. int blueMult() const { return 1; }
  66. // ColourMap lookup() method. Note that this only works when the table has
  67. // the default null mapping.
  68. virtual void lookup(int i, int* r, int* g, int* b) {
  69. if (i >= size()) return;
  70. *b = i % nBlue;
  71. i /= nBlue;
  72. *g = i % nGreen;
  73. *r = i / nGreen;
  74. *r = (*r * 65535 + (nRed-1) / 2) / (nRed-1);
  75. *g = (*g * 65535 + (nGreen-1) / 2) / (nGreen-1);
  76. *b = (*b * 65535 + (nBlue-1) / 2) / (nBlue-1);
  77. }
  78. int nRed;
  79. int nGreen;
  80. int nBlue;
  81. Pixel* table;
  82. bool deleteTable;
  83. };
  84. }
  85. #endif