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.

zrleDecode.h 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. //
  19. // ZRLE decoding function.
  20. //
  21. // This file is #included after having set the following macro:
  22. // BPP - 8, 16 or 32
  23. namespace rfb {
  24. // CONCAT2E concatenates its arguments, expanding them if they are macros
  25. #ifndef CONCAT2E
  26. #define CONCAT2(a,b) a##b
  27. #define CONCAT2E(a,b) CONCAT2(a,b)
  28. #endif
  29. #ifdef CPIXEL
  30. #define PIXEL_T rdr::CONCAT2E(U,BPP)
  31. #define READ_PIXEL(is) CONCAT2E(readOpaque,CPIXEL)(is)
  32. #define ZRLE_DECODE CONCAT2E(zrleDecode,CPIXEL)
  33. #else
  34. #define PIXEL_T rdr::CONCAT2E(U,BPP)
  35. #define READ_PIXEL(is) is->CONCAT2E(readOpaque,BPP)()
  36. #define ZRLE_DECODE CONCAT2E(zrleDecode,BPP)
  37. #endif
  38. void ZRLE_DECODE (const Rect& r, rdr::InStream* is,
  39. rdr::ZlibInStream* zis,
  40. const PixelFormat& pf, ModifiablePixelBuffer* pb)
  41. {
  42. int length = is->readU32();
  43. zis->setUnderlying(is, length);
  44. Rect t;
  45. PIXEL_T buf[64 * 64];
  46. for (t.tl.y = r.tl.y; t.tl.y < r.br.y; t.tl.y += 64) {
  47. t.br.y = __rfbmin(r.br.y, t.tl.y + 64);
  48. for (t.tl.x = r.tl.x; t.tl.x < r.br.x; t.tl.x += 64) {
  49. t.br.x = __rfbmin(r.br.x, t.tl.x + 64);
  50. zlibHasData(zis, 1);
  51. int mode = zis->readU8();
  52. bool rle = mode & 128;
  53. int palSize = mode & 127;
  54. PIXEL_T palette[128];
  55. #ifdef CPIXEL
  56. zlibHasData(zis, 3 * palSize);
  57. #else
  58. zlibHasData(zis, BPP/8 * palSize);
  59. #endif
  60. for (int i = 0; i < palSize; i++) {
  61. palette[i] = READ_PIXEL(zis);
  62. }
  63. if (palSize == 1) {
  64. PIXEL_T pix = palette[0];
  65. pb->fillRect(pf, t, &pix);
  66. continue;
  67. }
  68. if (!rle) {
  69. if (palSize == 0) {
  70. // raw
  71. #ifdef CPIXEL
  72. zlibHasData(zis, 3 * t.area());
  73. for (PIXEL_T* ptr = buf; ptr < buf+t.area(); ptr++) {
  74. *ptr = READ_PIXEL(zis);
  75. }
  76. #else
  77. zlibHasData(zis, BPP/8 * t.area());
  78. zis->readBytes(buf, t.area() * (BPP / 8));
  79. #endif
  80. } else {
  81. // packed pixels
  82. int bppp = ((palSize > 16) ? 8 :
  83. ((palSize > 4) ? 4 : ((palSize > 2) ? 2 : 1)));
  84. PIXEL_T* ptr = buf;
  85. for (int i = 0; i < t.height(); i++) {
  86. PIXEL_T* eol = ptr + t.width();
  87. rdr::U8 byte = 0;
  88. rdr::U8 nbits = 0;
  89. while (ptr < eol) {
  90. if (nbits == 0) {
  91. zlibHasData(zis, 1);
  92. byte = zis->readU8();
  93. nbits = 8;
  94. }
  95. nbits -= bppp;
  96. rdr::U8 index = (byte >> nbits) & ((1 << bppp) - 1) & 127;
  97. *ptr++ = palette[index];
  98. }
  99. }
  100. }
  101. } else {
  102. if (palSize == 0) {
  103. // plain RLE
  104. PIXEL_T* ptr = buf;
  105. PIXEL_T* end = ptr + t.area();
  106. while (ptr < end) {
  107. #ifdef CPIXEL
  108. zlibHasData(zis, 3);
  109. #else
  110. zlibHasData(zis, BPP/8);
  111. #endif
  112. PIXEL_T pix = READ_PIXEL(zis);
  113. int len = 1;
  114. int b;
  115. do {
  116. zlibHasData(zis, 1);
  117. b = zis->readU8();
  118. len += b;
  119. } while (b == 255);
  120. if (end - ptr < len) {
  121. throw Exception ("ZRLE decode error");
  122. }
  123. while (len-- > 0) *ptr++ = pix;
  124. }
  125. } else {
  126. // palette RLE
  127. PIXEL_T* ptr = buf;
  128. PIXEL_T* end = ptr + t.area();
  129. while (ptr < end) {
  130. zlibHasData(zis, 1);
  131. int index = zis->readU8();
  132. int len = 1;
  133. if (index & 128) {
  134. int b;
  135. do {
  136. zlibHasData(zis, 1);
  137. b = zis->readU8();
  138. len += b;
  139. } while (b == 255);
  140. if (end - ptr < len) {
  141. throw Exception ("ZRLE decode error");
  142. }
  143. }
  144. index &= 127;
  145. PIXEL_T pix = palette[index];
  146. while (len-- > 0) *ptr++ = pix;
  147. }
  148. }
  149. }
  150. pb->imageRect(pf, t, buf);
  151. }
  152. }
  153. zis->flushUnderlying();
  154. zis->setUnderlying(NULL, 0);
  155. }
  156. #undef ZRLE_DECODE
  157. #undef READ_PIXEL
  158. #undef PIXEL_T
  159. }