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.

tightDecode.h 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* Copyright (C) 2000-2003 Constantin Kaplinsky. All Rights Reserved.
  2. * Copyright 2004-2005 Cendio AB.
  3. * Copyright (C) 2011 D. R. Commander. All Rights Reserved.
  4. *
  5. * This is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This software is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this software; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  18. * USA.
  19. */
  20. //
  21. // Tight decoding functions.
  22. //
  23. // This file is #included after having set the following macros:
  24. // BPP - 8, 16 or 32
  25. // EXTRA_ARGS - optional extra arguments
  26. // FILL_RECT - fill a rectangle with a single color
  27. // IMAGE_RECT - draw a rectangle of pixel data from a buffer
  28. #include <rdr/InStream.h>
  29. #include <rdr/ZlibInStream.h>
  30. #include <rfb/Exception.h>
  31. #include <assert.h>
  32. namespace rfb {
  33. // CONCAT2E concatenates its arguments, expanding them if they are macros
  34. #ifndef CONCAT2E
  35. #define CONCAT2(a,b) a##b
  36. #define CONCAT2E(a,b) CONCAT2(a,b)
  37. #endif
  38. #define PIXEL_T rdr::CONCAT2E(U,BPP)
  39. #define READ_PIXEL CONCAT2E(readOpaque,BPP)
  40. #define TIGHT_DECODE TightDecoder::CONCAT2E(tightDecode,BPP)
  41. #define DECOMPRESS_JPEG_RECT TightDecoder::CONCAT2E(DecompressJpegRect,BPP)
  42. #define FILTER_GRADIENT TightDecoder::CONCAT2E(FilterGradient,BPP)
  43. #define TIGHT_MIN_TO_COMPRESS 12
  44. // Main function implementing Tight decoder
  45. void TIGHT_DECODE (const Rect& r)
  46. {
  47. bool cutZeros = false;
  48. #if BPP == 32
  49. if (serverpf.is888()) {
  50. cutZeros = true;
  51. }
  52. #endif
  53. rdr::U8 comp_ctl = is->readU8();
  54. // Flush zlib streams if we are told by the server to do so.
  55. for (int i = 0; i < 4; i++) {
  56. if (comp_ctl & 1) {
  57. zis[i].reset();
  58. }
  59. comp_ctl >>= 1;
  60. }
  61. // "Fill" compression type.
  62. if (comp_ctl == rfbTightFill) {
  63. PIXEL_T pix;
  64. if (cutZeros) {
  65. rdr::U8 bytebuf[3];
  66. is->readBytes(bytebuf, 3);
  67. serverpf.bufferFromRGB((rdr::U8*)&pix, bytebuf, 1, NULL);
  68. } else {
  69. pix = is->READ_PIXEL();
  70. }
  71. FILL_RECT(r, pix);
  72. return;
  73. }
  74. // "JPEG" compression type.
  75. if (comp_ctl == rfbTightJpeg) {
  76. DECOMPRESS_JPEG_RECT(r);
  77. return;
  78. }
  79. // Quit on unsupported compression type.
  80. if (comp_ctl > rfbTightMaxSubencoding) {
  81. throw Exception("TightDecoder: bad subencoding value received");
  82. return;
  83. }
  84. // "Basic" compression type.
  85. int palSize = 0;
  86. static PIXEL_T palette[256];
  87. bool useGradient = false;
  88. if ((comp_ctl & rfbTightExplicitFilter) != 0) {
  89. rdr::U8 filterId = is->readU8();
  90. switch (filterId) {
  91. case rfbTightFilterPalette:
  92. palSize = is->readU8() + 1;
  93. if (cutZeros) {
  94. rdr::U8 tightPalette[256 * 3];
  95. is->readBytes(tightPalette, palSize * 3);
  96. serverpf.bufferFromRGB((rdr::U8*)palette, tightPalette, palSize, NULL);
  97. } else {
  98. is->readBytes(palette, palSize * sizeof(PIXEL_T));
  99. }
  100. break;
  101. case rfbTightFilterGradient:
  102. useGradient = true;
  103. break;
  104. case rfbTightFilterCopy:
  105. break;
  106. default:
  107. throw Exception("TightDecoder: unknown filter code received");
  108. return;
  109. }
  110. }
  111. int bppp = BPP;
  112. if (palSize != 0) {
  113. bppp = (palSize <= 2) ? 1 : 8;
  114. } else if (cutZeros) {
  115. bppp = 24;
  116. }
  117. // Determine if the data should be decompressed or just copied.
  118. int rowSize = (r.width() * bppp + 7) / 8;
  119. int dataSize = r.height() * rowSize;
  120. int streamId = -1;
  121. rdr::InStream *input;
  122. if (dataSize < TIGHT_MIN_TO_COMPRESS) {
  123. input = is;
  124. } else {
  125. int length = is->readCompactLength();
  126. streamId = comp_ctl & 0x03;
  127. zis[streamId].setUnderlying(is, length);
  128. input = &zis[streamId];
  129. }
  130. // Allocate netbuf and read in data
  131. rdr::U8 *netbuf = new rdr::U8[dataSize];
  132. if (!netbuf) {
  133. throw Exception("rfb::TightDecoder::tightDecode unable to allocate buffer");
  134. }
  135. input->readBytes(netbuf, dataSize);
  136. PIXEL_T *buf;
  137. int stride = r.width();
  138. if (directDecode) buf = (PIXEL_T *)handler->getRawBufferRW(r, &stride);
  139. else buf = (PIXEL_T *)reader->getImageBuf(r.area());
  140. if (palSize == 0) {
  141. // Truecolor data
  142. if (useGradient) {
  143. #if BPP == 32
  144. if (cutZeros) {
  145. FilterGradient24(netbuf, buf, stride, r);
  146. } else
  147. #endif
  148. {
  149. FILTER_GRADIENT(netbuf, buf, stride, r);
  150. }
  151. } else {
  152. // Copy
  153. int h = r.height();
  154. PIXEL_T *ptr = buf;
  155. rdr::U8 *srcPtr = netbuf;
  156. int w = r.width();
  157. if (cutZeros) {
  158. while (h > 0) {
  159. serverpf.bufferFromRGB((rdr::U8*)ptr, srcPtr, w, NULL);
  160. ptr += stride;
  161. srcPtr += w * 3;
  162. h--;
  163. }
  164. } else {
  165. while (h > 0) {
  166. memcpy(ptr, srcPtr, w * sizeof(PIXEL_T));
  167. ptr += stride;
  168. srcPtr += w * sizeof(PIXEL_T);
  169. h--;
  170. }
  171. }
  172. }
  173. } else {
  174. // Indexed color
  175. int x, h = r.height(), w = r.width(), b, pad = stride - w;
  176. PIXEL_T *ptr = buf;
  177. rdr::U8 bits, *srcPtr = netbuf;
  178. if (palSize <= 2) {
  179. // 2-color palette
  180. while (h > 0) {
  181. for (x = 0; x < w / 8; x++) {
  182. bits = *srcPtr++;
  183. for (b = 7; b >= 0; b--) {
  184. *ptr++ = palette[bits >> b & 1];
  185. }
  186. }
  187. if (w % 8 != 0) {
  188. bits = *srcPtr++;
  189. for (b = 7; b >= 8 - w % 8; b--) {
  190. *ptr++ = palette[bits >> b & 1];
  191. }
  192. }
  193. ptr += pad;
  194. h--;
  195. }
  196. } else {
  197. // 256-color palette
  198. while (h > 0) {
  199. PIXEL_T *endOfRow = ptr + w;
  200. while (ptr < endOfRow) {
  201. *ptr++ = palette[*srcPtr++];
  202. }
  203. ptr += pad;
  204. h--;
  205. }
  206. }
  207. }
  208. if (directDecode) handler->releaseRawBuffer(r);
  209. else IMAGE_RECT(r, buf);
  210. delete [] netbuf;
  211. if (streamId != -1) {
  212. zis[streamId].reset();
  213. }
  214. }
  215. void
  216. DECOMPRESS_JPEG_RECT(const Rect& r)
  217. {
  218. // Read length
  219. int compressedLen = is->readCompactLength();
  220. if (compressedLen <= 0) {
  221. throw Exception("Incorrect data received from the server.\n");
  222. }
  223. // Allocate netbuf and read in data
  224. rdr::U8* netbuf = new rdr::U8[compressedLen];
  225. if (!netbuf) {
  226. throw Exception("rfb::TightDecoder::DecompressJpegRect unable to allocate buffer");
  227. }
  228. is->readBytes(netbuf, compressedLen);
  229. // We always use direct decoding with JPEG images
  230. int stride;
  231. rdr::U8 *buf = handler->getRawBufferRW(r, &stride);
  232. jd.decompress(netbuf, compressedLen, buf, stride * clientpf.bpp / 8, r,
  233. clientpf);
  234. handler->releaseRawBuffer(r);
  235. delete [] netbuf;
  236. }
  237. #if BPP == 32
  238. void
  239. TightDecoder::FilterGradient24(rdr::U8 *netbuf, PIXEL_T* buf, int stride,
  240. const Rect& r)
  241. {
  242. int x, y, c;
  243. static rdr::U8 prevRow[TIGHT_MAX_WIDTH*3];
  244. static rdr::U8 thisRow[TIGHT_MAX_WIDTH*3];
  245. rdr::U8 pix[3];
  246. int est[3];
  247. memset(prevRow, 0, sizeof(prevRow));
  248. // Set up shortcut variables
  249. int rectHeight = r.height();
  250. int rectWidth = r.width();
  251. for (y = 0; y < rectHeight; y++) {
  252. /* First pixel in a row */
  253. for (c = 0; c < 3; c++) {
  254. pix[c] = netbuf[y*rectWidth*3+c] + prevRow[c];
  255. thisRow[c] = pix[c];
  256. }
  257. serverpf.bufferFromRGB((rdr::U8*)&buf[y*stride], pix, 1, NULL);
  258. /* Remaining pixels of a row */
  259. for (x = 1; x < rectWidth; x++) {
  260. for (c = 0; c < 3; c++) {
  261. est[c] = prevRow[x*3+c] + pix[c] - prevRow[(x-1)*3+c];
  262. if (est[c] > 0xff) {
  263. est[c] = 0xff;
  264. } else if (est[c] < 0) {
  265. est[c] = 0;
  266. }
  267. pix[c] = netbuf[(y*rectWidth+x)*3+c] + est[c];
  268. thisRow[x*3+c] = pix[c];
  269. }
  270. serverpf.bufferFromRGB((rdr::U8*)&buf[y*stride+x], pix, 1, NULL);
  271. }
  272. memcpy(prevRow, thisRow, sizeof(prevRow));
  273. }
  274. }
  275. #endif
  276. void
  277. FILTER_GRADIENT(rdr::U8 *netbuf, PIXEL_T* buf, int stride, const Rect& r)
  278. {
  279. int x, y, c;
  280. static rdr::U8 prevRow[TIGHT_MAX_WIDTH*sizeof(PIXEL_T)];
  281. static rdr::U8 thisRow[TIGHT_MAX_WIDTH*sizeof(PIXEL_T)];
  282. rdr::U8 pix[3];
  283. int est[3];
  284. memset(prevRow, 0, sizeof(prevRow));
  285. // Set up shortcut variables
  286. int rectHeight = r.height();
  287. int rectWidth = r.width();
  288. for (y = 0; y < rectHeight; y++) {
  289. /* First pixel in a row */
  290. serverpf.rgbFromBuffer(pix, (rdr::U8*)&netbuf[y*rectWidth], 1, NULL);
  291. for (c = 0; c < 3; c++)
  292. pix[c] += prevRow[c];
  293. memcpy(thisRow, pix, sizeof(pix));
  294. serverpf.bufferFromRGB((rdr::U8*)&buf[y*stride], pix, 1, NULL);
  295. /* Remaining pixels of a row */
  296. for (x = 1; x < rectWidth; x++) {
  297. for (c = 0; c < 3; c++) {
  298. est[c] = prevRow[x*3+c] + pix[c] - prevRow[(x-1)*3+c];
  299. if (est[c] > 255) {
  300. est[c] = 255;
  301. } else if (est[c] < 0) {
  302. est[c] = 0;
  303. }
  304. }
  305. serverpf.rgbFromBuffer(pix, (rdr::U8*)&netbuf[y*rectWidth+x], 1, NULL);
  306. for (c = 0; c < 3; c++)
  307. pix[c] += est[c];
  308. memcpy(&thisRow[x*3], pix, sizeof(pix));
  309. serverpf.bufferFromRGB((rdr::U8*)&buf[y*stride+x], pix, 1, NULL);
  310. }
  311. memcpy(prevRow, thisRow, sizeof(prevRow));
  312. }
  313. }
  314. #undef TIGHT_MIN_TO_COMPRESS
  315. #undef FILTER_GRADIENT
  316. #undef DECOMPRESS_JPEG_RECT
  317. #undef TIGHT_DECODE
  318. #undef READ_PIXEL
  319. #undef PIXEL_T
  320. }