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.

HextileDecoder.cxx 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2014-2022 Pierre Ossman for Cendio AB
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This software is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this software; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. * USA.
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #include <rdr/InStream.h>
  23. #include <rdr/MemInStream.h>
  24. #include <rdr/OutStream.h>
  25. #include <rfb/Exception.h>
  26. #include <rfb/ServerParams.h>
  27. #include <rfb/PixelBuffer.h>
  28. #include <rfb/HextileDecoder.h>
  29. #include <rfb/hextileConstants.h>
  30. using namespace rfb;
  31. HextileDecoder::HextileDecoder() : Decoder(DecoderPlain)
  32. {
  33. }
  34. HextileDecoder::~HextileDecoder()
  35. {
  36. }
  37. bool HextileDecoder::readRect(const Rect& r, rdr::InStream* is,
  38. const ServerParams& server, rdr::OutStream* os)
  39. {
  40. Rect t;
  41. size_t bytesPerPixel;
  42. is->setRestorePoint();
  43. bytesPerPixel = server.pf().bpp/8;
  44. for (t.tl.y = r.tl.y; t.tl.y < r.br.y; t.tl.y += 16) {
  45. t.br.y = __rfbmin(r.br.y, t.tl.y + 16);
  46. for (t.tl.x = r.tl.x; t.tl.x < r.br.x; t.tl.x += 16) {
  47. uint8_t tileType;
  48. t.br.x = __rfbmin(r.br.x, t.tl.x + 16);
  49. if (!is->hasDataOrRestore(1))
  50. return false;
  51. tileType = is->readU8();
  52. os->writeU8(tileType);
  53. if (tileType & hextileRaw) {
  54. if (!is->hasDataOrRestore(t.area() * bytesPerPixel))
  55. return false;
  56. os->copyBytes(is, t.area() * bytesPerPixel);
  57. continue;
  58. }
  59. if (tileType & hextileBgSpecified) {
  60. if (!is->hasDataOrRestore(bytesPerPixel))
  61. return false;
  62. os->copyBytes(is, bytesPerPixel);
  63. }
  64. if (tileType & hextileFgSpecified) {
  65. if (!is->hasDataOrRestore(bytesPerPixel))
  66. return false;
  67. os->copyBytes(is, bytesPerPixel);
  68. }
  69. if (tileType & hextileAnySubrects) {
  70. uint8_t nSubrects;
  71. if (!is->hasDataOrRestore(1))
  72. return false;
  73. nSubrects = is->readU8();
  74. os->writeU8(nSubrects);
  75. if (tileType & hextileSubrectsColoured) {
  76. if (!is->hasDataOrRestore(nSubrects * (bytesPerPixel + 2)))
  77. return false;
  78. os->copyBytes(is, nSubrects * (bytesPerPixel + 2));
  79. } else {
  80. if (!is->hasDataOrRestore(nSubrects * 2))
  81. return false;
  82. os->copyBytes(is, nSubrects * 2);
  83. }
  84. }
  85. }
  86. }
  87. is->clearRestorePoint();
  88. return true;
  89. }
  90. void HextileDecoder::decodeRect(const Rect& r, const uint8_t* buffer,
  91. size_t buflen, const ServerParams& server,
  92. ModifiablePixelBuffer* pb)
  93. {
  94. rdr::MemInStream is(buffer, buflen);
  95. const PixelFormat& pf = server.pf();
  96. switch (pf.bpp) {
  97. case 8: hextileDecode<uint8_t >(r, &is, pf, pb); break;
  98. case 16: hextileDecode<uint16_t>(r, &is, pf, pb); break;
  99. case 32: hextileDecode<uint32_t>(r, &is, pf, pb); break;
  100. }
  101. }
  102. template<class T>
  103. inline T HextileDecoder::readPixel(rdr::InStream* is)
  104. {
  105. if (sizeof(T) == 1)
  106. return is->readOpaque8();
  107. if (sizeof(T) == 2)
  108. return is->readOpaque16();
  109. if (sizeof(T) == 4)
  110. return is->readOpaque32();
  111. }
  112. template<class T>
  113. void HextileDecoder::hextileDecode(const Rect& r, rdr::InStream* is,
  114. const PixelFormat& pf,
  115. ModifiablePixelBuffer* pb)
  116. {
  117. Rect t;
  118. T bg = 0;
  119. T fg = 0;
  120. T buf[16 * 16];
  121. for (t.tl.y = r.tl.y; t.tl.y < r.br.y; t.tl.y += 16) {
  122. t.br.y = __rfbmin(r.br.y, t.tl.y + 16);
  123. for (t.tl.x = r.tl.x; t.tl.x < r.br.x; t.tl.x += 16) {
  124. t.br.x = __rfbmin(r.br.x, t.tl.x + 16);
  125. int tileType = is->readU8();
  126. if (tileType & hextileRaw) {
  127. is->readBytes((uint8_t*)buf, t.area() * sizeof(T));
  128. pb->imageRect(pf, t, buf);
  129. continue;
  130. }
  131. if (tileType & hextileBgSpecified)
  132. bg = readPixel<T>(is);
  133. int len = t.area();
  134. T* ptr = buf;
  135. while (len-- > 0) *ptr++ = bg;
  136. if (tileType & hextileFgSpecified)
  137. fg = readPixel<T>(is);
  138. if (tileType & hextileAnySubrects) {
  139. int nSubrects = is->readU8();
  140. for (int i = 0; i < nSubrects; i++) {
  141. if (tileType & hextileSubrectsColoured)
  142. fg = readPixel<T>(is);
  143. int xy = is->readU8();
  144. int wh = is->readU8();
  145. int x = ((xy >> 4) & 15);
  146. int y = (xy & 15);
  147. int w = ((wh >> 4) & 15) + 1;
  148. int h = (wh & 15) + 1;
  149. if (x + w > 16 || y + h > 16) {
  150. throw rfb::Exception("HEXTILE_DECODE: Hextile out of bounds");
  151. }
  152. T* ptr = buf + y * t.width() + x;
  153. int rowAdd = t.width() - w;
  154. while (h-- > 0) {
  155. int len = w;
  156. while (len-- > 0) *ptr++ = fg;
  157. ptr += rowAdd;
  158. }
  159. }
  160. }
  161. pb->imageRect(pf, t, buf);
  162. }
  163. }
  164. }