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.

rreEncode.h 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. // RRE encoding function.
  20. //
  21. // This file is #included after having set the following macro:
  22. // BPP - 8, 16 or 32
  23. //
  24. // The data argument to RRE_ENCODE contains the pixel data, and it writes the
  25. // encoded version to the given OutStream. If the encoded version exceeds w*h
  26. // it aborts and returns -1, otherwise it returns the number of subrectangles.
  27. //
  28. #include <rdr/OutStream.h>
  29. namespace rfb {
  30. // CONCAT2E concatenates its arguments, expanding them if they are macros
  31. #ifndef CONCAT2E
  32. #define CONCAT2(a,b) a##b
  33. #define CONCAT2E(a,b) CONCAT2(a,b)
  34. #endif
  35. #define PIXEL_T rdr::CONCAT2E(U,BPP)
  36. #define WRITE_PIXEL CONCAT2E(writeOpaque,BPP)
  37. #define RRE_ENCODE CONCAT2E(rreEncode,BPP)
  38. int RRE_ENCODE (PIXEL_T* data, int w, int h, rdr::OutStream* os, PIXEL_T bg)
  39. {
  40. os->WRITE_PIXEL(bg);
  41. int nSubrects = 0;
  42. for (int y = 0; y < h; y++)
  43. {
  44. int x = 0;
  45. while (x < w) {
  46. if (*data == bg) {
  47. x++;
  48. data++;
  49. continue;
  50. }
  51. // Find horizontal subrect first
  52. PIXEL_T* ptr = data+1;
  53. PIXEL_T* eol = data+w-x;
  54. while (ptr < eol && *ptr == *data) ptr++;
  55. int sw = ptr - data;
  56. ptr = data + w;
  57. int sh = 1;
  58. while (sh < h-y) {
  59. eol = ptr + sw;
  60. while (ptr < eol)
  61. if (*ptr++ != *data) goto endOfHorizSubrect;
  62. ptr += w - sw;
  63. sh++;
  64. }
  65. endOfHorizSubrect:
  66. // Find vertical subrect
  67. int vh;
  68. for (vh = sh; vh < h-y; vh++)
  69. if (data[vh*w] != *data) break;
  70. if (vh != sh) {
  71. ptr = data+1;
  72. int vw;
  73. for (vw = 1; vw < sw; vw++) {
  74. for (int i = 0; i < vh; i++)
  75. if (ptr[i*w] != *data) goto endOfVertSubrect;
  76. ptr++;
  77. }
  78. endOfVertSubrect:
  79. // If vertical subrect bigger than horizontal then use that.
  80. if (sw*sh < vw*vh) {
  81. sw = vw;
  82. sh = vh;
  83. }
  84. }
  85. nSubrects++;
  86. os->WRITE_PIXEL(*data);
  87. os->writeU16(x);
  88. os->writeU16(y);
  89. os->writeU16(sw);
  90. os->writeU16(sh);
  91. ptr = data+w;
  92. PIXEL_T* eor = data+w*sh;
  93. while (ptr < eor) {
  94. eol = ptr + sw;
  95. while (ptr < eol) *ptr++ = bg;
  96. ptr += w - sw;
  97. }
  98. x += sw;
  99. data += sw;
  100. }
  101. }
  102. return nSubrects;
  103. }
  104. #undef PIXEL_T
  105. #undef WRITE_PIXEL
  106. #undef RRE_ENCODE
  107. }