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.

tightDecode.h 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* Copyright (C) 2000-2003 Constantin Kaplinsky. All Rights Reserved.
  2. * Copyright 2004-2005 Cendio AB.
  3. * Copyright 2009-2015 Pierre Ossman for Cendio AB
  4. * Copyright (C) 2011 D. R. Commander. All Rights Reserved.
  5. *
  6. * This is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This software is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this software; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  19. * USA.
  20. */
  21. //
  22. // Tight decoding functions.
  23. //
  24. // This file is #included after having set the following macro:
  25. // BPP - 8, 16 or 32
  26. namespace rfb {
  27. // CONCAT2E concatenates its arguments, expanding them if they are macros
  28. #ifndef CONCAT2E
  29. #define CONCAT2(a,b) a##b
  30. #define CONCAT2E(a,b) CONCAT2(a,b)
  31. #endif
  32. #define PIXEL_T rdr::CONCAT2E(U,BPP)
  33. #if BPP == 32
  34. void
  35. TightDecoder::FilterGradient24(const rdr::U8 *inbuf,
  36. const PixelFormat& pf, PIXEL_T* outbuf,
  37. int stride, const Rect& r)
  38. {
  39. int x, y, c;
  40. rdr::U8 prevRow[TIGHT_MAX_WIDTH*3];
  41. rdr::U8 thisRow[TIGHT_MAX_WIDTH*3];
  42. rdr::U8 pix[3];
  43. int est[3];
  44. memset(prevRow, 0, sizeof(prevRow));
  45. // Set up shortcut variables
  46. int rectHeight = r.height();
  47. int rectWidth = r.width();
  48. for (y = 0; y < rectHeight; y++) {
  49. for (x = 0; x < rectWidth; x++) {
  50. /* First pixel in a row */
  51. if (x == 0) {
  52. for (c = 0; c < 3; c++) {
  53. pix[c] = inbuf[y*rectWidth*3+c] + prevRow[c];
  54. thisRow[c] = pix[c];
  55. }
  56. pf.bufferFromRGB((rdr::U8*)&outbuf[y*stride], pix, 1);
  57. continue;
  58. }
  59. for (c = 0; c < 3; c++) {
  60. est[c] = prevRow[x*3+c] + pix[c] - prevRow[(x-1)*3+c];
  61. if (est[c] > 0xff) {
  62. est[c] = 0xff;
  63. } else if (est[c] < 0) {
  64. est[c] = 0;
  65. }
  66. pix[c] = inbuf[(y*rectWidth+x)*3+c] + est[c];
  67. thisRow[x*3+c] = pix[c];
  68. }
  69. pf.bufferFromRGB((rdr::U8*)&outbuf[y*stride+x], pix, 1);
  70. }
  71. memcpy(prevRow, thisRow, sizeof(prevRow));
  72. }
  73. }
  74. #endif
  75. #if BPP != 8
  76. void TightDecoder::FilterGradient(const rdr::U8* inbuf,
  77. const PixelFormat& pf, PIXEL_T* outbuf,
  78. int stride, const Rect& r)
  79. {
  80. int x, y, c;
  81. static rdr::U8 prevRow[TIGHT_MAX_WIDTH*3];
  82. static rdr::U8 thisRow[TIGHT_MAX_WIDTH*3];
  83. rdr::U8 pix[3];
  84. int est[3];
  85. memset(prevRow, 0, sizeof(prevRow));
  86. // Set up shortcut variables
  87. int rectHeight = r.height();
  88. int rectWidth = r.width();
  89. for (y = 0; y < rectHeight; y++) {
  90. for (x = 0; x < rectWidth; x++) {
  91. /* First pixel in a row */
  92. if (x == 0) {
  93. pf.rgbFromBuffer(pix, &inbuf[y*rectWidth], 1);
  94. for (c = 0; c < 3; c++)
  95. pix[c] += prevRow[c];
  96. memcpy(thisRow, pix, sizeof(pix));
  97. pf.bufferFromRGB((rdr::U8*)&outbuf[y*stride], pix, 1);
  98. continue;
  99. }
  100. for (c = 0; c < 3; c++) {
  101. est[c] = prevRow[x*3+c] + pix[c] - prevRow[(x-1)*3+c];
  102. if (est[c] > 255) {
  103. est[c] = 255;
  104. } else if (est[c] < 0) {
  105. est[c] = 0;
  106. }
  107. }
  108. pf.rgbFromBuffer(pix, &inbuf[y*rectWidth+x], 1);
  109. for (c = 0; c < 3; c++)
  110. pix[c] += est[c];
  111. memcpy(&thisRow[x*3], pix, sizeof(pix));
  112. pf.bufferFromRGB((rdr::U8*)&outbuf[y*stride+x], pix, 1);
  113. }
  114. memcpy(prevRow, thisRow, sizeof(prevRow));
  115. }
  116. }
  117. #endif
  118. void TightDecoder::FilterPalette(const PIXEL_T* palette, int palSize,
  119. const rdr::U8* inbuf, PIXEL_T* outbuf,
  120. int stride, const Rect& r)
  121. {
  122. // Indexed color
  123. int x, h = r.height(), w = r.width(), b, pad = stride - w;
  124. PIXEL_T* ptr = outbuf;
  125. rdr::U8 bits;
  126. const rdr::U8* srcPtr = inbuf;
  127. if (palSize <= 2) {
  128. // 2-color palette
  129. while (h > 0) {
  130. for (x = 0; x < w / 8; x++) {
  131. bits = *srcPtr++;
  132. for (b = 7; b >= 0; b--) {
  133. *ptr++ = palette[bits >> b & 1];
  134. }
  135. }
  136. if (w % 8 != 0) {
  137. bits = *srcPtr++;
  138. for (b = 7; b >= 8 - w % 8; b--) {
  139. *ptr++ = palette[bits >> b & 1];
  140. }
  141. }
  142. ptr += pad;
  143. h--;
  144. }
  145. } else {
  146. // 256-color palette
  147. while (h > 0) {
  148. PIXEL_T *endOfRow = ptr + w;
  149. while (ptr < endOfRow) {
  150. *ptr++ = palette[*srcPtr++];
  151. }
  152. ptr += pad;
  153. h--;
  154. }
  155. }
  156. }
  157. #undef PIXEL_T
  158. }