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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* Copyright 2009-2014 Pierre Ossman for Cendio AB
  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. namespace rfb {
  19. inline Pixel PixelFormat::pixelFromBuffer(const uint8_t* buffer) const
  20. {
  21. Pixel p;
  22. p = 0;
  23. if (bigEndian) {
  24. switch (bpp) {
  25. case 32:
  26. p |= ((Pixel)*(buffer++)) << 24;
  27. p |= ((Pixel)*(buffer++)) << 16;
  28. /* fall through */
  29. case 16:
  30. p |= ((Pixel)*(buffer++)) << 8;
  31. /* fall through */
  32. case 8:
  33. p |= *buffer;
  34. }
  35. } else {
  36. p |= buffer[0];
  37. if (bpp >= 16) {
  38. p |= ((Pixel)buffer[1]) << 8;
  39. if (bpp == 32) {
  40. p |= ((Pixel)buffer[2]) << 16;
  41. p |= ((Pixel)buffer[3]) << 24;
  42. }
  43. }
  44. }
  45. return p;
  46. }
  47. inline void PixelFormat::bufferFromPixel(uint8_t* buffer, Pixel p) const
  48. {
  49. if (bigEndian) {
  50. switch (bpp) {
  51. case 32:
  52. *(buffer++) = (p >> 24) & 0xff;
  53. *(buffer++) = (p >> 16) & 0xff;
  54. /* fall through */
  55. case 16:
  56. *(buffer++) = (p >> 8) & 0xff;
  57. /* fall through */
  58. case 8:
  59. *(buffer++) = (p >> 0) & 0xff;
  60. }
  61. } else {
  62. buffer[0] = (p >> 0) & 0xff;
  63. if (bpp >= 16) {
  64. buffer[1] = (p >> 8) & 0xff;
  65. if (bpp == 32) {
  66. buffer[2] = (p >> 16) & 0xff;
  67. buffer[3] = (p >> 24) & 0xff;
  68. }
  69. }
  70. }
  71. }
  72. inline Pixel PixelFormat::pixelFromRGB(uint16_t red, uint16_t green, uint16_t blue) const
  73. {
  74. Pixel p;
  75. p = (Pixel)downconvTable[(redBits-1)*256 + (red >> 8)] << redShift;
  76. p |= (Pixel)downconvTable[(greenBits-1)*256 + (green >> 8)] << greenShift;
  77. p |= (Pixel)downconvTable[(blueBits-1)*256 + (blue >> 8)] << blueShift;
  78. return p;
  79. }
  80. inline Pixel PixelFormat::pixelFromRGB(uint8_t red, uint8_t green, uint8_t blue) const
  81. {
  82. Pixel p;
  83. p = (Pixel)downconvTable[(redBits-1)*256 + red] << redShift;
  84. p |= (Pixel)downconvTable[(greenBits-1)*256 + green] << greenShift;
  85. p |= (Pixel)downconvTable[(blueBits-1)*256 + blue] << blueShift;
  86. return p;
  87. }
  88. inline void PixelFormat::rgbFromPixel(Pixel p, uint16_t *r, uint16_t *g, uint16_t *b) const
  89. {
  90. uint8_t _r, _g, _b;
  91. _r = p >> redShift;
  92. _g = p >> greenShift;
  93. _b = p >> blueShift;
  94. _r = upconvTable[(redBits-1)*256 + _r];
  95. _g = upconvTable[(greenBits-1)*256 + _g];
  96. _b = upconvTable[(blueBits-1)*256 + _b];
  97. *r = _r << 8 | _r;
  98. *g = _g << 8 | _g;
  99. *b = _b << 8 | _b;
  100. }
  101. inline void PixelFormat::rgbFromPixel(Pixel p, uint8_t *r, uint8_t *g, uint8_t *b) const
  102. {
  103. uint8_t _r, _g, _b;
  104. _r = p >> redShift;
  105. _g = p >> greenShift;
  106. _b = p >> blueShift;
  107. *r = upconvTable[(redBits-1)*256 + _r];
  108. *g = upconvTable[(greenBits-1)*256 + _g];
  109. *b = upconvTable[(blueBits-1)*256 + _b];
  110. }
  111. }