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.

hextileDecode.h 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // Hextile decoding function.
  20. //
  21. // This file is #included after having set the following macro:
  22. // BPP - 8, 16 or 32
  23. #include <rdr/InStream.h>
  24. #include <rfb/Exception.h>
  25. #include <rfb/hextileConstants.h>
  26. namespace rfb {
  27. // CONCAT2E concatenates its arguments, expanding them if they are macros
  28. #ifndef CONCAT2E
  29. #define CONCAT2(a,b) a##b
  30. #define CONCAT2E(a,b) CONCAT2(a,b)
  31. #endif
  32. #define PIXEL_T rdr::CONCAT2E(U,BPP)
  33. #define READ_PIXEL CONCAT2E(readOpaque,BPP)
  34. #define HEXTILE_DECODE CONCAT2E(hextileDecode,BPP)
  35. static void HEXTILE_DECODE (const Rect& r, rdr::InStream* is,
  36. const PixelFormat& pf,
  37. ModifiablePixelBuffer* pb)
  38. {
  39. Rect t;
  40. PIXEL_T bg = 0;
  41. PIXEL_T fg = 0;
  42. PIXEL_T buf[16 * 16];
  43. for (t.tl.y = r.tl.y; t.tl.y < r.br.y; t.tl.y += 16) {
  44. t.br.y = __rfbmin(r.br.y, t.tl.y + 16);
  45. for (t.tl.x = r.tl.x; t.tl.x < r.br.x; t.tl.x += 16) {
  46. t.br.x = __rfbmin(r.br.x, t.tl.x + 16);
  47. int tileType = is->readU8();
  48. if (tileType & hextileRaw) {
  49. is->readBytes(buf, t.area() * (BPP/8));
  50. pb->imageRect(pf, t, buf);
  51. continue;
  52. }
  53. if (tileType & hextileBgSpecified)
  54. bg = is->READ_PIXEL();
  55. int len = t.area();
  56. PIXEL_T* ptr = buf;
  57. while (len-- > 0) *ptr++ = bg;
  58. if (tileType & hextileFgSpecified)
  59. fg = is->READ_PIXEL();
  60. if (tileType & hextileAnySubrects) {
  61. int nSubrects = is->readU8();
  62. for (int i = 0; i < nSubrects; i++) {
  63. if (tileType & hextileSubrectsColoured)
  64. fg = is->READ_PIXEL();
  65. int xy = is->readU8();
  66. int wh = is->readU8();
  67. int x = ((xy >> 4) & 15);
  68. int y = (xy & 15);
  69. int w = ((wh >> 4) & 15) + 1;
  70. int h = (wh & 15) + 1;
  71. if (x + w > 16 || y + h > 16) {
  72. throw rfb::Exception("HEXTILE_DECODE: Hextile out of bounds");
  73. }
  74. PIXEL_T* ptr = buf + y * t.width() + x;
  75. int rowAdd = t.width() - w;
  76. while (h-- > 0) {
  77. int len = w;
  78. while (len-- > 0) *ptr++ = fg;
  79. ptr += rowAdd;
  80. }
  81. }
  82. }
  83. pb->imageRect(pf, t, buf);
  84. }
  85. }
  86. }
  87. #undef PIXEL_T
  88. #undef READ_PIXEL
  89. #undef HEXTILE_DECODE
  90. }