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.

ZRLEDecoder.cxx 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2009-2017 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. #include <rdr/InStream.h>
  20. #include <rdr/MemInStream.h>
  21. #include <rdr/OutStream.h>
  22. #include <rfb/ServerParams.h>
  23. #include <rfb/PixelBuffer.h>
  24. #include <rfb/ZRLEDecoder.h>
  25. using namespace rfb;
  26. static inline rdr::U32 readOpaque24A(rdr::InStream* is)
  27. {
  28. is->check(3);
  29. rdr::U32 r=0;
  30. ((rdr::U8*)&r)[0] = is->readU8();
  31. ((rdr::U8*)&r)[1] = is->readU8();
  32. ((rdr::U8*)&r)[2] = is->readU8();
  33. return r;
  34. }
  35. static inline rdr::U32 readOpaque24B(rdr::InStream* is)
  36. {
  37. is->check(3);
  38. rdr::U32 r=0;
  39. ((rdr::U8*)&r)[1] = is->readU8();
  40. ((rdr::U8*)&r)[2] = is->readU8();
  41. ((rdr::U8*)&r)[3] = is->readU8();
  42. return r;
  43. }
  44. #define BPP 8
  45. #include <rfb/zrleDecode.h>
  46. #undef BPP
  47. #define BPP 16
  48. #include <rfb/zrleDecode.h>
  49. #undef BPP
  50. #define BPP 32
  51. #include <rfb/zrleDecode.h>
  52. #define CPIXEL 24A
  53. #include <rfb/zrleDecode.h>
  54. #undef CPIXEL
  55. #define CPIXEL 24B
  56. #include <rfb/zrleDecode.h>
  57. #undef CPIXEL
  58. #undef BPP
  59. ZRLEDecoder::ZRLEDecoder() : Decoder(DecoderOrdered)
  60. {
  61. }
  62. ZRLEDecoder::~ZRLEDecoder()
  63. {
  64. }
  65. void ZRLEDecoder::readRect(const Rect& r, rdr::InStream* is,
  66. const ServerParams& server, rdr::OutStream* os)
  67. {
  68. rdr::U32 len;
  69. len = is->readU32();
  70. os->writeU32(len);
  71. os->copyBytes(is, len);
  72. }
  73. void ZRLEDecoder::decodeRect(const Rect& r, const void* buffer,
  74. size_t buflen, const ServerParams& server,
  75. ModifiablePixelBuffer* pb)
  76. {
  77. rdr::MemInStream is(buffer, buflen);
  78. const rfb::PixelFormat& pf = server.pf();
  79. switch (pf.bpp) {
  80. case 8: zrleDecode8 (r, &is, &zis, pf, pb); break;
  81. case 16: zrleDecode16(r, &is, &zis, pf, pb); break;
  82. case 32:
  83. {
  84. if (pf.depth <= 24) {
  85. Pixel maxPixel = pf.pixelFromRGB((rdr::U16)-1, (rdr::U16)-1, (rdr::U16)-1);
  86. bool fitsInLS3Bytes = maxPixel < (1<<24);
  87. bool fitsInMS3Bytes = (maxPixel & 0xff) == 0;
  88. if ((fitsInLS3Bytes && pf.isLittleEndian()) ||
  89. (fitsInMS3Bytes && pf.isBigEndian()))
  90. {
  91. zrleDecode24A(r, &is, &zis, pf, pb);
  92. break;
  93. }
  94. if ((fitsInLS3Bytes && pf.isBigEndian()) ||
  95. (fitsInMS3Bytes && pf.isLittleEndian()))
  96. {
  97. zrleDecode24B(r, &is, &zis, pf, pb);
  98. break;
  99. }
  100. }
  101. zrleDecode32(r, &is, &zis, pf, pb);
  102. break;
  103. }
  104. }
  105. }