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

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