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.

Palette.h 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* Copyright (C) 2000-2005 Constantin Kaplinsky. All Rights Reserved.
  2. * Copyright (C) 2011 D. R. Commander. All Rights Reserved.
  3. * Copyright 2014 Pierre Ossman for Cendio AB
  4. *
  5. * This is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This software is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this software; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  18. * USA.
  19. */
  20. #ifndef __RFB_PALETTE_H__
  21. #define __RFB_PALETTE_H__
  22. #include <assert.h>
  23. #include <string.h>
  24. #include <rdr/types.h>
  25. namespace rfb {
  26. class Palette {
  27. public:
  28. Palette() { clear(); }
  29. ~Palette() {}
  30. int size() const { return numColours; }
  31. void clear() { numColours = 0; memset(hash, 0, sizeof(hash)); }
  32. inline bool insert(rdr::U32 colour, int numPixels);
  33. inline unsigned char lookup(rdr::U32 colour) const;
  34. inline rdr::U32 getColour(unsigned char index) const;
  35. inline int getCount(unsigned char index) const;
  36. protected:
  37. inline unsigned char genHash(rdr::U32 colour) const;
  38. protected:
  39. int numColours;
  40. struct PaletteListNode {
  41. PaletteListNode *next;
  42. unsigned char idx;
  43. rdr::U32 colour;
  44. };
  45. struct PaletteEntry {
  46. PaletteListNode *listNode;
  47. int numPixels;
  48. };
  49. // This is the raw list of colours, allocated from 0 and up
  50. PaletteListNode list[256];
  51. // Hash table for quick lookup into the list above
  52. PaletteListNode *hash[256];
  53. // Occurances of each colour, where the 0:th entry is the most common.
  54. // Indices also refer to this array.
  55. PaletteEntry entry[256];
  56. };
  57. }
  58. inline bool rfb::Palette::insert(rdr::U32 colour, int numPixels)
  59. {
  60. PaletteListNode* pnode;
  61. PaletteListNode* prev_pnode;
  62. unsigned char hash_key, idx;
  63. hash_key = genHash(colour);
  64. pnode = hash[hash_key];
  65. prev_pnode = NULL;
  66. // Do we already have an entry for this colour?
  67. while (pnode != NULL) {
  68. if (pnode->colour == colour) {
  69. // Yup
  70. idx = pnode->idx;
  71. numPixels = entry[idx].numPixels + numPixels;
  72. // The extra pixels might mean we have to adjust the sort list
  73. while (idx > 0) {
  74. if (entry[idx-1].numPixels >= numPixels)
  75. break;
  76. entry[idx] = entry[idx-1];
  77. entry[idx].listNode->idx = idx;
  78. idx--;
  79. }
  80. if (idx != pnode->idx) {
  81. entry[idx].listNode = pnode;
  82. pnode->idx = idx;
  83. }
  84. entry[idx].numPixels = numPixels;
  85. return true;
  86. }
  87. prev_pnode = pnode;
  88. pnode = pnode->next;
  89. }
  90. // Check if palette is full.
  91. if (numColours == 256)
  92. return false;
  93. // Create a new colour entry
  94. pnode = &list[numColours];
  95. pnode->next = NULL;
  96. pnode->idx = 0;
  97. pnode->colour = colour;
  98. // Add it to the hash table
  99. if (prev_pnode != NULL)
  100. prev_pnode->next = pnode;
  101. else
  102. hash[hash_key] = pnode;
  103. // Move palette entries with lesser pixel counts.
  104. idx = numColours;
  105. while (idx > 0) {
  106. if (entry[idx-1].numPixels >= numPixels)
  107. break;
  108. entry[idx] = entry[idx-1];
  109. entry[idx].listNode->idx = idx;
  110. idx--;
  111. }
  112. // And add it into the freed slot.
  113. pnode->idx = idx;
  114. entry[idx].listNode = pnode;
  115. entry[idx].numPixels = numPixels;
  116. numColours++;
  117. return true;
  118. }
  119. inline unsigned char rfb::Palette::lookup(rdr::U32 colour) const
  120. {
  121. unsigned char hash_key;
  122. PaletteListNode* pnode;
  123. hash_key = genHash(colour);
  124. pnode = hash[hash_key];
  125. while (pnode != NULL) {
  126. if (pnode->colour == colour)
  127. return pnode->idx;
  128. pnode = pnode->next;
  129. }
  130. // We are being fed a bad colour
  131. assert(false);
  132. return 0;
  133. }
  134. inline rdr::U32 rfb::Palette::getColour(unsigned char index) const
  135. {
  136. return entry[index].listNode->colour;
  137. }
  138. inline int rfb::Palette::getCount(unsigned char index) const
  139. {
  140. return entry[index].numPixels;
  141. }
  142. inline unsigned char rfb::Palette::genHash(rdr::U32 colour) const
  143. {
  144. unsigned char hash_key;
  145. // djb2 hash function
  146. hash_key = 5; // 5381 & 0xff
  147. for (int i = 0; i < 32; i += 8)
  148. hash_key = ((hash_key << 5) + hash_key) ^ (colour >> i);
  149. return hash_key;
  150. }
  151. #endif