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 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/TransImageGetter.h>
  21. #include <rfb/encodings.h>
  22. #include <rfb/ConnParams.h>
  23. #include <rfb/SMsgWriter.h>
  24. #include <rfb/ZRLEEncoder.h>
  25. #include <rfb/Configuration.h>
  26. using namespace rfb;
  27. IntParameter zlibLevel("ZlibLevel","Zlib compression level",-1);
  28. #define EXTRA_ARGS ImageGetter* ig
  29. #define GET_IMAGE_INTO_BUF(r,buf) ig->getImage(buf, r);
  30. #define BPP 8
  31. #include <rfb/zrleEncode.h>
  32. #undef BPP
  33. #define BPP 16
  34. #include <rfb/zrleEncode.h>
  35. #undef BPP
  36. #define BPP 32
  37. #include <rfb/zrleEncode.h>
  38. #define CPIXEL 24A
  39. #include <rfb/zrleEncode.h>
  40. #undef CPIXEL
  41. #define CPIXEL 24B
  42. #include <rfb/zrleEncode.h>
  43. #undef CPIXEL
  44. #undef BPP
  45. ZRLEEncoder::ZRLEEncoder(SMsgWriter* writer_)
  46. : writer(writer_), zos(0,0,zlibLevel), mos(129*1024)
  47. {
  48. }
  49. ZRLEEncoder::~ZRLEEncoder()
  50. {
  51. }
  52. bool ZRLEEncoder::writeRect(const Rect& r, TransImageGetter* ig, Rect* actual)
  53. {
  54. rdr::U8* imageBuf = writer->getImageBuf(64 * 64 * 4 + 4);
  55. mos.clear();
  56. bool wroteAll = true;
  57. *actual = r;
  58. switch (writer->bpp()) {
  59. case 8:
  60. wroteAll = zrleEncode8(r, &mos, &zos, imageBuf, actual, ig);
  61. break;
  62. case 16:
  63. wroteAll = zrleEncode16(r, &mos, &zos, imageBuf, actual, ig);
  64. break;
  65. case 32:
  66. {
  67. const PixelFormat& pf = writer->getConnParams()->pf();
  68. Pixel maxPixel = pf.pixelFromRGB((rdr::U16)-1, (rdr::U16)-1, (rdr::U16)-1);
  69. bool fitsInLS3Bytes = maxPixel < (1<<24);
  70. bool fitsInMS3Bytes = (maxPixel & 0xff) == 0;
  71. if ((fitsInLS3Bytes && pf.isLittleEndian()) ||
  72. (fitsInMS3Bytes && pf.isBigEndian()))
  73. {
  74. wroteAll = zrleEncode24A(r, &mos, &zos, imageBuf, actual, ig);
  75. }
  76. else if ((fitsInLS3Bytes && pf.isBigEndian()) ||
  77. (fitsInMS3Bytes && pf.isLittleEndian()))
  78. {
  79. wroteAll = zrleEncode24B(r, &mos, &zos, imageBuf, actual, ig);
  80. }
  81. else
  82. {
  83. wroteAll = zrleEncode32(r, &mos, &zos, imageBuf, actual, ig);
  84. }
  85. break;
  86. }
  87. }
  88. writer->startRect(*actual, encodingZRLE);
  89. rdr::OutStream* os = writer->getOutStream();
  90. os->writeU32(mos.length());
  91. os->writeBytes(mos.data(), mos.length());
  92. writer->endRect();
  93. return wroteAll;
  94. }