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.

ZRLEEncoder.cxx 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  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. #include <rdr/OutStream.h>
  19. #include <rfb/Exception.h>
  20. #include <rfb/encodings.h>
  21. #include <rfb/ConnParams.h>
  22. #include <rfb/SMsgWriter.h>
  23. #include <rfb/SConnection.h>
  24. #include <rfb/ZRLEEncoder.h>
  25. #include <rfb/Configuration.h>
  26. using namespace rfb;
  27. IntParameter zlibLevel("ZlibLevel","Zlib compression level",-1);
  28. static inline void writeOpaque24A(rdr::OutStream* os, rdr::U32 u)
  29. {
  30. os->check(3);
  31. os->writeU8(((rdr::U8*)&u)[0]);
  32. os->writeU8(((rdr::U8*)&u)[1]);
  33. os->writeU8(((rdr::U8*)&u)[2]);
  34. }
  35. static inline void writeOpaque24B(rdr::OutStream* os, rdr::U32 u)
  36. {
  37. os->check(3);
  38. os->writeU8(((rdr::U8*)&u)[1]);
  39. os->writeU8(((rdr::U8*)&u)[2]);
  40. os->writeU8(((rdr::U8*)&u)[3]);
  41. }
  42. #define BPP 8
  43. #include <rfb/zrleEncode.h>
  44. #undef BPP
  45. #define BPP 16
  46. #include <rfb/zrleEncode.h>
  47. #undef BPP
  48. #define BPP 32
  49. #include <rfb/zrleEncode.h>
  50. #define CPIXEL 24A
  51. #include <rfb/zrleEncode.h>
  52. #undef CPIXEL
  53. #define CPIXEL 24B
  54. #include <rfb/zrleEncode.h>
  55. #undef CPIXEL
  56. #undef BPP
  57. ZRLEEncoder::ZRLEEncoder(SConnection* conn)
  58. : Encoder(conn), zos(0,0,zlibLevel), mos(129*1024)
  59. {
  60. }
  61. ZRLEEncoder::~ZRLEEncoder()
  62. {
  63. }
  64. void ZRLEEncoder::writeRect(const Rect& r, PixelBuffer* pb)
  65. {
  66. const PixelFormat& pf = conn->cp.pf();
  67. rdr::U8* imageBuf = conn->writer()->getImageBuf(64 * 64 * 4 + 4);
  68. mos.clear();
  69. switch (pf.bpp) {
  70. case 8:
  71. zrleEncode8(r, &mos, &zos, imageBuf, pf, pb);
  72. break;
  73. case 16:
  74. zrleEncode16(r, &mos, &zos, imageBuf, pf, pb);
  75. break;
  76. case 32:
  77. {
  78. Pixel maxPixel = pf.pixelFromRGB((rdr::U16)-1, (rdr::U16)-1, (rdr::U16)-1);
  79. bool fitsInLS3Bytes = maxPixel < (1<<24);
  80. bool fitsInMS3Bytes = (maxPixel & 0xff) == 0;
  81. if ((fitsInLS3Bytes && pf.isLittleEndian()) ||
  82. (fitsInMS3Bytes && pf.isBigEndian()))
  83. {
  84. zrleEncode24A(r, &mos, &zos, imageBuf, pf, pb);
  85. }
  86. else if ((fitsInLS3Bytes && pf.isBigEndian()) ||
  87. (fitsInMS3Bytes && pf.isLittleEndian()))
  88. {
  89. zrleEncode24B(r, &mos, &zos, imageBuf, pf, pb);
  90. }
  91. else
  92. {
  93. zrleEncode32(r, &mos, &zos, imageBuf, pf, pb);
  94. }
  95. break;
  96. }
  97. }
  98. conn->writer()->startRect(r, encodingZRLE);
  99. rdr::OutStream* os = conn->getOutStream();
  100. os->writeU32(mos.length());
  101. os->writeBytes(mos.data(), mos.length());
  102. conn->writer()->endRect();
  103. }