Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

zrleDecode.h 4.6KB

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