Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ZRLEEncoder.cxx 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2014 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/OutStream.h>
  20. #include <rfb/Exception.h>
  21. #include <rfb/encodings.h>
  22. #include <rfb/ConnParams.h>
  23. #include <rfb/Palette.h>
  24. #include <rfb/SConnection.h>
  25. #include <rfb/ZRLEEncoder.h>
  26. #include <rfb/Configuration.h>
  27. using namespace rfb;
  28. IntParameter zlibLevel("ZlibLevel","Zlib compression level",-1);
  29. ZRLEEncoder::ZRLEEncoder(SConnection* conn)
  30. : Encoder(conn, encodingZRLE, EncoderPlain, 127),
  31. zos(0,0,zlibLevel), mos(129*1024)
  32. {
  33. zos.setUnderlying(&mos);
  34. }
  35. ZRLEEncoder::~ZRLEEncoder()
  36. {
  37. zos.setUnderlying(NULL);
  38. }
  39. bool ZRLEEncoder::isSupported()
  40. {
  41. return conn->cp.supportsEncoding(encodingZRLE);
  42. }
  43. void ZRLEEncoder::writeRect(const PixelBuffer* pb, const Palette& palette)
  44. {
  45. int x, y;
  46. Rect tile;
  47. rdr::OutStream* os;
  48. // A bit of a special case
  49. if (palette.size() == 1) {
  50. Encoder::writeSolidRect(pb, palette);
  51. return;
  52. }
  53. for (y = 0;y < pb->height();y += 64) {
  54. tile.tl.y = y;
  55. tile.br.y = y + 64;
  56. if (tile.br.y > pb->height())
  57. tile.br.y = pb->height();
  58. for (x = 0;x < pb->width();x += 64) {
  59. tile.tl.x = x;
  60. tile.br.x = x + 64;
  61. if (tile.br.x > pb->width())
  62. tile.br.x = pb->width();
  63. if (palette.size() == 0)
  64. writeRawTile(tile, pb, palette);
  65. else if (palette.size() <= 16)
  66. writePaletteTile(tile, pb, palette);
  67. else
  68. writePaletteRLETile(tile, pb, palette);
  69. }
  70. }
  71. zos.flush();
  72. os = conn->getOutStream();
  73. os->writeU32(mos.length());
  74. os->writeBytes(mos.data(), mos.length());
  75. mos.clear();
  76. }
  77. void ZRLEEncoder::writeSolidRect(int width, int height,
  78. const PixelFormat& pf,
  79. const rdr::U8* colour)
  80. {
  81. int tiles;
  82. rdr::OutStream* os;
  83. tiles = ((width + 63)/64) * ((height + 63)/64);
  84. while (tiles--) {
  85. zos.writeU8(1);
  86. writePixels(colour, pf, 1);
  87. }
  88. zos.flush();
  89. os = conn->getOutStream();
  90. os->writeU32(mos.length());
  91. os->writeBytes(mos.data(), mos.length());
  92. mos.clear();
  93. }
  94. void ZRLEEncoder::writePaletteTile(const Rect& tile, const PixelBuffer* pb,
  95. const Palette& palette)
  96. {
  97. const rdr::U8* buffer;
  98. int stride;
  99. buffer = pb->getBuffer(tile, &stride);
  100. switch (pb->getPF().bpp) {
  101. case 32:
  102. writePaletteTile(tile.width(), tile.height(),
  103. (rdr::U32*)buffer, stride,
  104. pb->getPF(), palette);
  105. break;
  106. case 16:
  107. writePaletteTile(tile.width(), tile.height(),
  108. (rdr::U16*)buffer, stride,
  109. pb->getPF(), palette);
  110. break;
  111. default:
  112. writePaletteTile(tile.width(), tile.height(),
  113. (rdr::U8*)buffer, stride,
  114. pb->getPF(), palette);
  115. }
  116. }
  117. void ZRLEEncoder::writePaletteRLETile(const Rect& tile, const PixelBuffer* pb,
  118. const Palette& palette)
  119. {
  120. const rdr::U8* buffer;
  121. int stride;
  122. buffer = pb->getBuffer(tile, &stride);
  123. switch (pb->getPF().bpp) {
  124. case 32:
  125. writePaletteRLETile(tile.width(), tile.height(),
  126. (rdr::U32*)buffer, stride,
  127. pb->getPF(), palette);
  128. break;
  129. case 16:
  130. writePaletteRLETile(tile.width(), tile.height(),
  131. (rdr::U16*)buffer, stride,
  132. pb->getPF(), palette);
  133. break;
  134. default:
  135. writePaletteRLETile(tile.width(), tile.height(),
  136. (rdr::U8*)buffer, stride,
  137. pb->getPF(), palette);
  138. }
  139. }
  140. void ZRLEEncoder::writeRawTile(const Rect& tile, const PixelBuffer* pb,
  141. const Palette& palette)
  142. {
  143. const rdr::U8* buffer;
  144. int stride;
  145. int w, h, stride_bytes;
  146. buffer = pb->getBuffer(tile, &stride);
  147. zos.writeU8(0); // Empty palette (i.e. raw pixels)
  148. w = tile.width();
  149. h = tile.height();
  150. stride_bytes = stride * pb->getPF().bpp/8;
  151. while (h--) {
  152. writePixels(buffer, pb->getPF(), w);
  153. buffer += stride_bytes;
  154. }
  155. }
  156. void ZRLEEncoder::writePalette(const PixelFormat& pf, const Palette& palette)
  157. {
  158. rdr::U8 buffer[256*4];
  159. int i;
  160. if (pf.bpp == 32) {
  161. rdr::U32* buf;
  162. buf = (rdr::U32*)buffer;
  163. for (i = 0;i < palette.size();i++)
  164. *buf++ = palette.getColour(i);
  165. } else if (pf.bpp == 16) {
  166. rdr::U16* buf;
  167. buf = (rdr::U16*)buffer;
  168. for (i = 0;i < palette.size();i++)
  169. *buf++ = palette.getColour(i);
  170. } else {
  171. rdr::U8* buf;
  172. buf = (rdr::U8*)buffer;
  173. for (i = 0;i < palette.size();i++)
  174. *buf++ = palette.getColour(i);
  175. }
  176. writePixels(buffer, pf, palette.size());
  177. }
  178. void ZRLEEncoder::writePixels(const rdr::U8* buffer, const PixelFormat& pf,
  179. unsigned int count)
  180. {
  181. Pixel maxPixel;
  182. rdr::U8 pixBuf[4];
  183. maxPixel = pf.pixelFromRGB((rdr::U16)-1, (rdr::U16)-1, (rdr::U16)-1);
  184. pf.bufferFromPixel(pixBuf, maxPixel);
  185. if ((pf.bpp != 32) || ((pixBuf[0] != 0) && (pixBuf[3] != 0))) {
  186. zos.writeBytes(buffer, count * pf.bpp/8);
  187. return;
  188. }
  189. if (pixBuf[0] == 0)
  190. buffer++;
  191. while (count--) {
  192. zos.writeBytes(buffer, 3);
  193. buffer += 4;
  194. }
  195. }
  196. //
  197. // Including BPP-dependent implementation of the encoder.
  198. //
  199. #define BPP 8
  200. #include <rfb/ZRLEEncoderBPP.cxx>
  201. #undef BPP
  202. #define BPP 16
  203. #include <rfb/ZRLEEncoderBPP.cxx>
  204. #undef BPP
  205. #define BPP 32
  206. #include <rfb/ZRLEEncoderBPP.cxx>
  207. #undef BPP