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.

RREDecoder.cxx 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/RREDecoder.h>
  29. using namespace rfb;
  30. RREDecoder::RREDecoder() : Decoder(DecoderPlain)
  31. {
  32. }
  33. RREDecoder::~RREDecoder()
  34. {
  35. }
  36. bool RREDecoder::readRect(const Rect& /*r*/, rdr::InStream* is,
  37. const ServerParams& server, rdr::OutStream* os)
  38. {
  39. uint32_t numRects;
  40. size_t len;
  41. if (!is->hasData(4))
  42. return false;
  43. is->setRestorePoint();
  44. numRects = is->readU32();
  45. os->writeU32(numRects);
  46. len = server.pf().bpp/8 + numRects * (server.pf().bpp/8 + 8);
  47. if (!is->hasDataOrRestore(len))
  48. return false;
  49. is->clearRestorePoint();
  50. os->copyBytes(is, len);
  51. return true;
  52. }
  53. void RREDecoder::decodeRect(const Rect& r, const uint8_t* buffer,
  54. size_t buflen, const ServerParams& server,
  55. ModifiablePixelBuffer* pb)
  56. {
  57. rdr::MemInStream is(buffer, buflen);
  58. const PixelFormat& pf = server.pf();
  59. switch (pf.bpp) {
  60. case 8: rreDecode<uint8_t >(r, &is, pf, pb); break;
  61. case 16: rreDecode<uint16_t>(r, &is, pf, pb); break;
  62. case 32: rreDecode<uint32_t>(r, &is, pf, pb); break;
  63. }
  64. }
  65. template<class T>
  66. inline T RREDecoder::readPixel(rdr::InStream* is)
  67. {
  68. if (sizeof(T) == 1)
  69. return is->readOpaque8();
  70. if (sizeof(T) == 2)
  71. return is->readOpaque16();
  72. if (sizeof(T) == 4)
  73. return is->readOpaque32();
  74. }
  75. template<class T>
  76. void RREDecoder::rreDecode(const Rect& r, rdr::InStream* is,
  77. const PixelFormat& pf,
  78. ModifiablePixelBuffer* pb)
  79. {
  80. int nSubrects = is->readU32();
  81. T bg = readPixel<T>(is);
  82. pb->fillRect(pf, r, &bg);
  83. for (int i = 0; i < nSubrects; i++) {
  84. T pix = readPixel<T>(is);
  85. int x = is->readU16();
  86. int y = is->readU16();
  87. int w = is->readU16();
  88. int h = is->readU16();
  89. if (((x+w) > r.width()) || ((y+h) > r.height()))
  90. throw Exception ("RRE decode error");
  91. pb->fillRect(pf, Rect(r.tl.x+x, r.tl.y+y, r.tl.x+x+w, r.tl.y+y+h), &pix);
  92. }
  93. }